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 LED on and off HEX 20 24 C! ;set Pin 13 as output 20 23 C! ;toggle Pin 13 Turn beeper on and off 40 2A C! ;set Pin 6 as output 42 44 C! ;set Timer0 CTC mode FF 47 C! ;maximum count for Timer0 3 45 C! ;prescaler=3, start beeper 0 45 C! ;prescaler=0, stop beeper
Demo of 328eForth HEX : TEST1 1 2 3 4 5 ; : TEST2 IF 1 ELSE 2 THEN . ; : TEST3 10 FOR R@ . NEXT ; : TEST4 CR .” HELLO, WORLD!” ; : TEST5 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 If data is in New Buffer, go to 6. If data is in Old Buffer, go to 5. If Old Buffer was modified, write data to flash Fill Old Buffer with new data from flash Switch New and Old Buffers. Read data from New Buffer.
Ping-Pong Algorithm for Write If address is in New Buffer, go to 6. If address is in Old Buffer, go to 5. If Old Buffer was modified, write data to flash Fill Old Buffer with new data from flash Switch New and Old Buffers. 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.