Hot!dsPIC33/24 flash rtsp code example bug

Author
jeff_d
Starting Member
  • Total Posts : 36
  • Reward points : 0
  • Joined: 2017/12/11 08:49:30
  • Location: 0
  • Status: offline
2018/11/07 08:05:34 (permalink)
0

dsPIC33/24 flash rtsp code example bug

I beleive this is a bug, Microchip provides two code examples for real-time serial-programming of the flash (ce478_rtsp_word_write and ce409_flash_rtsp) which I found on the dsPIC33EP32MC202 product page. 
 
The rtsp_api.s file has assembly routines to do the flash page write:
_FlashPageWrite:
        push TBLPAG
        MOV W0, W9 ; Add loc to be written
        MOV w1, w8
        mov #0xFA, w0
        mov w0, TBLPAG ;TBLPAG has to point to the SRAM latch section
…more write work…
        pop TBLPAG
        return

 
In the event this gets called from a non-interrupt context we could have a problem with the calling function's local variables. (don't get me started on erase/write flash from inside an interrupt handler...)
 
XC16 expects W8-15 to be preserved when called outside of an interrupt.
Pg 227 XC16 user guide:
Functions not called during interrupt handling must preserve registers W8-W15.
That is, the values in these registers must be saved before they are modified and
restored before returning to the calling function. Registers W0-W7 may be used
without restoring their values.

 
This RTSP write will trash W8 and W9, which can distrub local variables in the calling function. In my case it was a retry counter which got reset every time this function was called.
 
push/pop of W8 and W9 will preserve these registers
_FlashPageWrite:
        push TBLPAG
        push W8
        push W9
        MOV W0, W9 ; Add loc to be written
        MOV w1, w8
        mov #0xFA, w0
        mov w0, TBLPAG ;TBLPAG has to point to the SRAM latch section
…do the work…
        pop W9
        pop W8
        pop TBLPAG
        return

_FlashWordModify should also have the same change to preserve W8 and W9


#1

0 Replies Related Threads

    Jump to:
    © 2019 APG vNext Commercial Version 4.5