'* PIC18F1320's Oshonsoft Bootloader*'
'Title: Oshonsoft Bootloader
'Description: This program uses MCU to perform bootloader function
''Author: languer (2011)
'Pin Allocation:
'PIN# Main_Fn Secondary_Fn
'RA0 -> AN0
'RA1 -> AN1
'RA2 -> AN2
'RA3 -> not used
'RA4 -> not used
'RA5 -> not used
'RA6 -> not used
'RA7 -> not used
'RB0 -> not used
'RB1 -> UART-TX
'RB2 -> not used
'RB3 -> not used
'RB4 -> UART-RX
'RB5 -> not used
'RB6 -> PGC - Programming clock
'RB7 -> PGD - Programming data
'Version Info: v1
'->basic interface
'General Configuration
Define CONFIG1L = 0x00
Define CONFIG1H = 0x09
Define CONFIG2L = 0x1a
Define CONFIG2H = 0x00
Define CONFIG3L = 0x00
Define CONFIG3H = 0x03
Define CONFIG4L = 0x80
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40
'Oscillator/Clock Configuration
Define CLOCK_FREQUENCY = 8
'Define SIMULATION_WAITMS_VALUE = 1
'Bootloader Code Start
StartFromZero
Dim i As Byte
Dim j As Byte
Dim total As Word
Dim eraseblock As Byte
Dim programblock As Byte
Dim address As Word
Dim data(64) As Byte
Dim din As Byte
ASM: BSF RCON,IPEN
Goto bootloader_start
'beginning of user program
ASM: ORG 0x0008
user_program:
Goto user_program
'beginning of bootloader
ASM: ORG 0x1C00
'changed from 0x7c00 For 18f452 To 0x1c00 For 18f1320
execute_user_program:
ASM: BSF RCON,IPEN
ASM: GOTO 0x0008
ASM: NOP
ASM: ORG 0x1d00
'changed from 0x7d00 for 18F452 to 0x1d00 for 18F1320
bootloader_start:
'added oscillator configuration
OSCCON.IRCF2 = 1
OSCCON.IRCF1 = 1
OSCCON.IRCF0 = 1
'added port configuration statement
AllDigital
TRISB = %11111101
Hseropen 19200
WaitMs 100
Hserout 0x55
For i = 1 To 100
WaitMs 1
Hserget j
If j = 0x55 Then
Goto bootloader_main
Endif
Next i
Gosub reset_state
Goto execute_user_program
bootloader_main:
Gosub get_byte
total.LB = din
Gosub get_byte
total.HB = din
'total is the last address to be programmed
Gosub get_byte
eraseblock = din
Gosub get_byte
programblock = din
Gosub erase_flash
For address = 0 To total Step programblock
j = programblock - 1
Gosub get_data
Gosub write_flash_memory
Next address
address = 0x1c00 'changed from 0x7c00 for 18F452 to 0x1c00 for 18F1320
Gosub do_erase
j = 7
Gosub get_data
Gosub write_flash_memory
WaitMs 100
Gosub reset_state
Goto execute_user_program
End
get_data:
For i = 0 To j
Gosub get_byte
data(i) = din
Next i
Return
get_byte:
Hserin din
Hserout din
Return
write_flash_memory:
TBLPTRU = 0
TBLPTRH = address.HB
TBLPTRL = address.LB
For i = 0 To j
TABLAT = data(i)
ASM: TBLWT*+
Next i
TBLPTRU = 0
TBLPTRH = address.HB
TBLPTRL = address.LB
ASM: BSF EECON1,EEPGD
ASM: BCF EECON1,CFGS
ASM: BSF EECON1,WREN
ASM: MOVLW 0x55
ASM: MOVWF EECON2
ASM: MOVLW 0xAA
ASM: MOVWF EECON2
ASM: BSF EECON1,WR
ASM: NOP
ASM: NOP
ASM: BCF EECON1,WREN
ASM: BCF EECON1,EEPGD
Return
erase_flash:
For address = 0 To total Step eraseblock
Gosub do_erase
Next address
Return
do_erase:
TBLPTRU = 0
TBLPTRH = address.HB
TBLPTRL = address.LB
ASM: BSF EECON1,EEPGD
ASM: BCF EECON1,CFGS
ASM: BSF EECON1,WREN
ASM: BSF EECON1,FREE
ASM: MOVLW 0x55
ASM: MOVWF EECON2
ASM: MOVLW 0xAA
ASM: MOVWF EECON2
ASM: BSF EECON1,WR
ASM: NOP
ASM: NOP
ASM: BCF EECON1,WREN
ASM: BCF EECON1,EEPGD
ASM: BCF EECON1,FREE
Return
reset_state:
RCSTA = 0
TXSTA = 0
EECON1 = 0
PIR1 = 0
PIR2 = 0
STATUS = 0
AllDigital 'added command to reset all ports
'trisc = 0xff 'commented out for 18F1320
Return