The code can be flashed to the chip just fine. However, when using the Mikrobootloader to load new code, the code does not write the new code correctly. after flashing new code, it will either not go through the bootloader sequence (when Const _Flash_Erase = 64) or end up in a continuous loop (when Const _Flash_Erase = 128).
If I download the code from the chip after flashing the new code (when Const _Flash_Erase = 64) the code does not contain the intended flash code. However, when Const _Flash_Erase = 128, it looks good but does not work or jump to where the code is.
Code: Select all
program Boot_Test_25k83
Const _Flash_Erase = 64
dim block as byte[_FLASH_ERASE]
'-------------- Empty sub function, this is where the start address of "real"
' program will be placed (by Write_Begin()).
sub procedure Start_Program org 0x7FC0
' FLASH_ERASE is 64
' 32 nops are stored as last 64 bytes of program memory
nop nop nop nop nop nop nop nop
nop nop nop nop nop nop nop nop
nop nop nop nop nop nop nop nop
nop nop nop nop nop nop
' 31 is for return ' 32 is unused
end sub
sub procedure Flash_Write_Sector(dim address as longword, dim sdata as ^byte)
dim saveintcon, i, j as byte
saveintcon = INTCON
'--- erase FLASH memory
TBLPTRU = Higher(address)
TBLPTRH = Hi(address)
TBLPTRL = Lo(address)
' Write Address High
'--- write memory
' Read Block
NVMCON1.REG0 = 0 ' Clear the register
NVMCON1.REG1 = 1 ' Set the Register
NVMCON1.WREN = 1
NVMCON1.FREE = 1
INTCON0.GIE = 0
NVMCON2 = $55
NVMCON2 = $AA
NVMCON1.WR = 1
INTCON0.GIE = 1
asm
TBLRD*- ; read into TABLAT, and inc
end asm
'--- write memory
FSR0L = Lo(sdata)
FSR0H = Hi(sdata)
j = 0
while (j < (_FLASH_ERASE div _FLASH_WRITE_LATCH))
i = 0
while (i < _FLASH_WRITE_LATCH)
TABLAT = POSTINC0
asm
TBLWT+*
end asm
Inc(i)
wend
NVMCON1.REG0 = 0
NVMCON1.REG1 = 1
NVMCON1.WREN = 1
NVMCON1.FREE = 0
INTCON0.GIE = 0
NVMCON2 = $55
NVMCON2 = $AA
NVMCON1.WR = 1
Inc(j)
wend
TABLAT = POSTINC0
INTCON.GIE = 1
'--- restore interrupt
INTCON = saveintcon
end sub
'-------------- This sub procedure will recover the bootlocation 0x0000, 0x0001 and
' 0x0002 to point to the bootloaer"s main. It will also move
' the reset vector of the program that is uploaded to a new
' location.
sub procedure Write_Begin
Flash_Write_Sector(0x7FA0, @block)' address is org of Start_Program
'--- goto main bootloader (see GOTO _main opcode at the beginning of asm listing)
' 0xF0 3A EF 80
block[0] = 0x80
block[1] = 0xEF
block[2] = 0x3A
block[3] = 0xF0
end sub
sub function Susart_Write_Loop(dim send, receive as byte) as byte
result = 0
while(true)
Delay_8ms
UART1_Write(send)
Delay_8ms
Inc(result)
if (result = 255) then
result = 0
exit
end if
if (UART1_Read = receive) then
result = 1
exit
end if
wend
end sub
sub procedure Start_Bootload
dim i, xx, yy as byte
j as longword
i = 0
j = 0
'--- If 32 words (64 bytes) recieved then write to flash
while true
if (i = _FLASH_ERASE) then
if (j = 0) then
Write_Begin
end if
Flash_Write_Sector(j, @block)
i = 0
j = j + _FLASH_ERASE
end if
'--- Ask for yy
UART1_Write("y")
while UART1_Data_Ready = 0
nop
wend
'--- Read yy
yy = UART1_Read()
'--- Ask for xx
UART1_Write("x")
while UART1_Data_Ready = 0
nop
wend
'--- Read xx
xx = UART1_Read
'--- Save xxyy in block[i]
block[i] = yy
Inc(i)
block[i] = xx
Inc(i)
wend
end sub
main: org 0x7500 ' org main
orgall(0x7500) ' store bootloader code
ANSELC = 0x00'
RC6PPS = 0x13' //RC6->UART1:TX1;
U1RXPPS = 0x17' //RC7->UART1:RX1;
UART1_Init(9600) ' Init USART1 at 9600
if (Susart_Write_Loop("g","r")) then ' Send "g" for ~5 sec, if "r"
Start_Bootload() ' received start bootload
else
Start_Program() ' else start user"s program
end if
end.