Bootloader Issue

541 views Asked by At

I am having a strange problem with my serial bootloader on PIC18F87J11, the issue is primarily related with the GOTO instruction. I will explain further, first off this is my HEX File.

:040C0000E2EFFFF030
:10FFC4000001E5EFFFF0000E956E00D08C8CA30EBF
:10FFD400016E550EE82EFED7012EFCD78C9CA30E85
:10FFE400016E550EE82EFED7012EFCD7EFD7EED7C3
:04FFF40000EF06F024
:04FFF800A0F4C0FFB2
:00000001FF

As you can see the first address is 0C00 (line 1) then it writes the following E2EF, FFF0, and the rest FFFF until it reaches 64 byes. In another way this is what I am writing to memory.

Programming Flash Memory Addresses 0C00 - 0C3F  with the following 64 bytes of machine code

 EFE2  F0FF  FFFF  FFFF  FFFF  FFFF  FFFF  FFFF
 FFFF  FFFF  FFFF  FFFF  FFFF  FFFF  FFFF  FFFF
 FFFF  FFFF  FFFF  FFFF  FFFF  FFFF  FFFF  FFFF
 FFFF  FFFF  FFFF  FFFF  FFFF  FFFF  FFFF  FFFF

Now when I read the Program Memory from the PIC after writing to it, the GOTO instruction is not the same as the second line of the HEX file which has address FFC4

1537           00C00          EFE2                          GOTO 0x1FFC4 (should be 0x0FFC4)
1538           00C02          F0FF                          NOP   

Now the next few lines of my HEX file goes to the following address and look like this.

Programming Flash Memory Addresses FFC4 - FFFF  with the following 64 bytes of machine code

 FFFF  FFFF  FFFF  FFFF  FFFF  FFFF  FFFF  FFFF
 FFFF  FFFF  6E01  0E55  2EE8  D7FE  2E01  D7FC
 9C8C  0EA3  6E01  0E55  2EE8  D7FE  2E01  D7FC
 D7EF  D7EE  EF00  F006  F4A0  FFC0  FFFF  FFFF

And this is what I read from program memory after I write to it the above data.

           32739          0FFC4          FFFF                          NOP           
           32740          0FFC6          FFFF                          NOP           
           32741          0FFC8          FFFF                          NOP           
           32742          0FFCA          FFFF                          NOP           
           32743          0FFCC          FFFF                          NOP           
           32744          0FFCE          FFFF                          NOP           
           32745          0FFD0          FFFF                          NOP           
           32746          0FFD2          FFFF                          NOP           
           32747          0FFD4          FFFF                          NOP           
           32748          0FFD6          FFFF                          NOP           
           32749          0FFD8          6E01                          MOVWF config, ACCESS
           32750          0FFDA          0E55                          MOVLW 0x55    
           32751          0FFDC          2EE8                          DECFSZ WREG, F, ACCESS
           32752          0FFDE          D7FE                          BRA 0xFFDC    
           32753          0FFE0          2E01                          DECFSZ config, F, ACCESS
           32754          0FFE2          D7FC                          BRA 0xFFDC    
           32755          0FFE4          9C8C                          BCF LATD, 6, ACCESS
           32756          0FFE6          0EA3                          MOVLW 0xA3    
           32757          0FFE8          6E01                          MOVWF config, ACCESS
           32758          0FFEA          0E55                          MOVLW 0x55    
           32759          0FFEC          2EE8                          DECFSZ WREG, F, ACCESS
           32760          0FFEE          D7FE                          BRA 0xFFEC    
           32761          0FFF0          2E01                          DECFSZ config, F, ACCESS
           32762          0FFF2          D7FC                          BRA 0xFFEC    
           32763          0FFF4          D7EF                          BRA 0xFFD4    
           32764          0FFF6          D7EE                          BRA 0xFFD4    
           32765          0FFF8          EF00                          GOTO 0x1E0C00 
           32766          0FFFA          FF06                          NOP           
           32767          0FFFC          A0F0                          BTFSS INTCON3, 0, ACCESS
           32768          0FFFE          C0F4                          MOVFF 0xF4, TOSU

Now why does address 00C00 point to 1FFC4 (GOTO 0x1FFC4) instead of 0FFC4 where the rest of HEX file is actually written to?

I really hope I can get some help here because I have been trying it to figure it out for the last week. Thanks!

UPDATE

I am starting to think that the problem is related to the hex file of the application code. The hex file I was using was produced from the batch file mentioned on the tutorial, however the hex file produced by mplab xc8 is somewhat different. I will include more information and hopefully something can catch someone's eyes.

This is what's written in the batch file

@echo off
set cfile=%1
set hexfile=%cfile:.c=.hex%

rem Check if the c file has been updated and needs to be compiled
FOR /F %%i IN ('DIR /B /O:D %cfile% %hexfile%') DO SET NEWEST=%%i
IF "%NEWEST%"=="%cfile%" (ECHO xc8 --chip=18F87J11 --CODEOFFSET=00c00h %cfile%
xc8 --chip=18F87J11 --CODEOFFSET=00c00h %cfile%

) ELSE ( echo No changes to %cfile% since last compiled. Skipping to download...
)

rem Only program the pic if the c file compiled successfully
FOR /F %%i IN ('DIR /B /O:D %cfile% %hexfile%') DO SET NEWEST=%%i
IF "%NEWEST%"=="%hexfile%" (echo serialprog %hexfile%
serialprog %hexfile%
)

This is the hex file produced by the batch file

:040C0000E2EFFFF030
:020000040001F9   <---- I deleted this line because it didn't seem needed
:10FFC4000001E5EFFFF0000E956E00D08C8CA30EBF
:10FFD400016E550EE82EFED7012EFCD78C9CA30E85
:10FFE400016E550EE82EFED7012EFCD7EFD7EED7C3
:04FFF40000EF06F024
:04FFF800A0F4C0FFB2
:00000001FF

This is the hex file produced by mplab xc8

:100C000002EF06F0E6EFFFF0FF00FF00FF00FF003D
:020000040001F9
:10FFC000FF00FF00FF00FF00FF00FF000001E9EF5E
:10FFD000FFF0000E956E8C8CA30E016E550EE82E70
:10FFE000FED7012EFCD78C9CA30E016E550EE82E79
:0CFFF000FED7012EFCD7EFD7A0F4C0FF15
:00000001FF
1

There are 1 answers

0
DrRobotNinja On BEST ANSWER

Actually, you do need this line.

:020000040001F9   <---- I deleted this line because it didn't seem needed

I believe this might be the source of your problem. Here's the Wikipedia page on Intel HEX

Using it as a guide, we can parse this record.

: 02 00 00 04 00 01 F9

Byte count: 02 (Two bytes)
Address: 00 00 (Not used with this record type)
Record Type: 04 (Extended Linear Address Record: Allows for 32-bit addressing)
Data payload: 00 01 (For this record type: this is the high word in the address)
Checksum: F9 (0x02 + 0x04 + 0x01 + 0xF9 = 256: Valid record)

The 04 Extended Linear Address Record modifies all subsequent records until another 04 record is encountered.

The data payload of the 04 record is 0001 which means all subsequent addresses should be in the 00010000 - 0001FFFF block. The next line in the HEX file :10FFC4... doesn't point to 0000FFC4 but instead to 0001FFC4.

So, the GOTO is correctly formatted and properly points to 0001FFC4, it's the bootloader not recognizing the 04 record that is the problem.