FORTH for Arduino?

Download Report

Transcript FORTH for Arduino?

328eForth for Arduino Uno
C. H. Ting
February 16, 2012
SVFIG
Summary









Optimized FORTH for Arduino Uno
ATmega328P
Assembly Code of 328eForth
AVR Studio 4 Tool Suite
Assembling 328eForth
HyperTerminal
Demo of 328eForth
Optimized Flash Programming
Conclusion
Optimized Forth for Arduino







Subroutine Thread Model
Fully optimized
Uniform byte addressing
Case insensitive
Interpreter is in Bootloader section
Optimized flash programming
Visible words are reduced to 140 for ease of
learning
ATmega328P








8 Bit microcontroller, 131 instructions
20 MIPS at 20 MHz
32KB Flash, 2 KB RAM, 1 KB EEPROM
3 Counter/Timers
6 Channels 10-bit ADC
USART, SPI, I2C, ISP
23 Programmable I/O lines
1.8-5.5 V
Assembly Code of 328eForth




Reset and interrupt vectors
Assembly macros
Kernel of primitive commands
Compound commands





Common utilities
Interpreter
Compiler
Programming tools
RAM memory



CPU and IO registers
System Variable
Buffers and stacks
AVR Studio 4 Tool Suite




AVR Assembler 2
AVR Simulator 2
In System Programming with AVRISP
mkll
Configuration of Atmega328 chip
Assembling 328eForth




Assembling by AVR Assembler 2
Simulating by AVR Simulator 2
In System Programming with AVRISP
mkll
Running 328eForth from HyperTerminal
HyperTerminal




Arduino 0022 supplies USB to UART
driver
19,200 Baud, 8 data bits, 1 stop bit, no
parity, no flow control
900 ms delay after sending each line of
text
Send Text File option to download file to
Uno
Demo of 328eForth
Turn
HEX
20 24
20 23
 Turn
40 2A
42 44
FF 47
3 45
0 45

LED on and off
C! ;set Pin 13 as output
C! ;toggle Pin 13
beeper on and off
C! ;set Pin 6 as output
C! ;set Timer0 CTC mode
C! ;maximum count for Timer0
C! ;prescaler=3, start beeper
C! ;prescaler=0, stop beeper
Demo of 328eForth
HEX
: TEST1
: TEST2
: TEST3
: TEST4
: TEST5
1 2 3 4 5 ;
IF 1 ELSE 2 THEN . ;
10 FOR R@ . NEXT ;
CR .” HELLO, WORLD!” ;
FOR FFFF FOR NEXT NEXT
;
Optimized Flash Programming




Flash memory in ATmega328 can
endure 10,000 programming cycles.
Flash programming must be optimized.
Two 128 byte buffers are allocated to
store data before writing into flash
memory.
Ping-Pong algorithm minimizes flash
programming.
Ping-Pong Algorithm for Read
1. If data is in New Buffer, go to 6.
2. If data is in Old Buffer, go to 5.
3. If Old Buffer was modified, write data to
flash
4. Fill Old Buffer with new data from flash
5. Switch New and Old Buffers.
6. Read data from New Buffer.
Ping-Pong Algorithm for Write
1. If address is in New Buffer, go to 6.
2. If address is in Old Buffer, go to 5.
3. If Old Buffer was modified, write data to
flash
4. Fill Old Buffer with new data from flash
5. Switch New and Old Buffers.
6. Write data to New Buffer and mark it as
modified.
Conclusion




ISP is very pleasing.
Ping-pong flash buffers optimize flash
programming.
2.1 us per empty FOR-NEXT loop at 16
MHz.
ATmega328P is forced to be compatible
with eForth.