; Example code to automatically start a SDCC built application ; on the PJRC 8051 development board, rev 5 ; http://www.pjrc.com/tech/8051/board5/autostart.html ; This code contains two parts, a "fixed baud rate" initialization ; that PAULMON2 executes before initializing most hardware, which ; sets the baud rate to avoid PAULMON2 from doing automatic baud ; rate detection (waiting for Enter on the serial port). ; The "automatic startup" section is run by PAULMON2 after ; initialization and it causes the application code to run instead ; of launching into PAULMON2's normal menus. Seven LJMP instructions ; are written into the RAM-based vectors before the app is run, ; so that interrupts will jump to the correct place in the flash. ; set this to the location where your application code begins. ; With SDCC, this is the value you used for --code-loc ; With AS31, this is the .org locations where your code was built .equ application, 0x8000 ; set this to where you'd like these two auto-startup routines ; to be placed in the flash. Normally 0xF600 is a good choice, ; to place this at the end of the flash (where it is least likely ; to conflict with the space for your application). .equ location, 0xF600 ; choose one of these to configure the baud rate. If you do not ; know what value to use, you can find it with "my_th1.asm", at: ; http://www.pjrc.com/tech/8051/pm2_docs/headers.html .equ baud_const, 255 ;115200 baud w/ 22.1182 MHz ;.equ baud_const, 254 ;57600 baud w/ 22.1182 MHz ;.equ baud_const, 253 ;38400 baud w/ 22.1182 MHz ;.equ baud_const, 250 ;19200 baud w/ 22.1182 MHz ;.equ baud_const, 244 ;9600 baud w/ 22.1182 MHz ; This is the place where PAULMON2's interrupts LJMP. The ; default version shipped on all PJRC boards is 0x2000. ; DO NOT CHANGE THIS unless you have programmed a different ; set of interrupt LJMPs into PAULMON2. .equ ram_vectors, 0x2000 ; The Automatic Startup code. This causes the application to ; run instead of PAULMON2's menus. .org location .db 0xA5,0xE5,0xE0,0xA5 ;signiture bytes .db 253,255,0,0 ;id (253=startup) .db 0,0,0,0 ;prompt code vector .db 0,0,0,0 ;reserved .db 0,0,0,0 ;reserved .db 0,0,0,0 ;reserved .db 0,0,0,0 ;user defined .db 255,255,255,255 ;length and checksum (255=unused) .db "Automatic Startup",0 ;max 31 characters, plus the zero .org location+64 ;executable code begins here ;First, write LJMP instructions into the RAM where PAULMON2 ;will jump when an interrupt occurs, so that interrupts ;will correctly jump to the SDCC-build interrupt code. mov dptr, #ram_vectors ;start point mov r0, #application & 255 mov r1, #application >> 8 acall write_vector mov dptr, #(ram_vectors + 3) ;int0 vector mov r0, #(application + 3) & 255 mov r1, #(application + 3) >> 8 acall write_vector mov dptr, #(ram_vectors + 0x0B) ;timer0 vector mov r0, #(application + 0x0B) & 255 mov r1, #(application + 0x0B) >> 8 acall write_vector mov dptr, #(ram_vectors + 0x13) ;int1 vector mov r0, #(application + 0x13) & 255 mov r1, #(application + 0x13) >> 8 acall write_vector mov dptr, #(ram_vectors + 0x1B) ;timer1 vector mov r0, #(application + 0x1B) & 255 mov r1, #(application + 0x1B) >> 8 acall write_vector mov dptr, #(ram_vectors + 0x23) ;uart vector mov r0, #(application + 0x23) & 255 mov r1, #(application + 0x23) >> 8 acall write_vector mov dptr, #(ram_vectors + 0x2B) ;timer2 vector mov r0, #(application + 0x2B) & 255 mov r1, #(application + 0x2B) >> 8 acall write_vector ;And finally jump to the application code.... hopefully there ;is something useful located there in memory! ljmp application ;Subroutine to write an LJMP into RAM. write_vector: mov a, #2 ; LJMP opcode movx @dptr, a inc dptr mov a, r1 ; LJMP MSB movx @dptr, a inc dptr mov a, r0 ; LJMP LSB movx @dptr, a ret ;The Fixed Baud Rate section... this magical code writes ;the baud rate into memory so that PAULMON2 will believe ;this is a warm boot with a previously-detected baud rate ;and it will skip the automatic detection (that waits for ;the user to press Enter). .org location+256 .db 0xA5,0xE5,0xE0,0xA5 ;signiture bytes .db 249,255,0,0 ;id (249=init) .db 0,0,0,0 ;prompt code vector .db 0,0,0,0 ;reserved .db 0,0,0,0 ;reserved .db 0,0,0,0 ;reserved .db 0,0,0,0 ;user defined .db 255,255,255,255 ;length and checksum (255=unused) .db "Fixed Baud Rate",0 .org location+256+64 ;executable code begins here mov a, #baud_const mov 0x7B, a mov 0x7A, a ;store the baud rate for next warm boot. mov 0x79, a mov 0x78, a xrl 0x7A, #01010101b xrl 0x79, #11001100b xrl 0x78, #00011101b ret