Tiny412 - Boot problem writing app 2

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

I have a strange problem...

When running in BootFlash I want to reprogram my App

WhenI run the following code:

 

// Note:

//    Fuse bit BootEnd == 6
//    Fuse bit AppEnd  == 0

 

#define ProtectedWriteSpm(RegU8, ValueU8) { _PROTECTED_WRITE_SPM ((RegU8), (ValueU8)); }

#define NvmCtrl_ControlA_CmdExec(CmdU8) { ProtectedWriteSpm(NVMCTRL.CTRLA, (((U8)(CmdU8) << NVMCTRL_CMD_gp)  & NVMCTRL_CMD_gm) ); }

 

void Swdl_NewDataBOOL(void)
{
    U16 Cnt_U16 = 0;
    U8* Flash_U8Ptr;

 

    NvmCtrl_ControlA_CmdExec(NVMCTRL_CMD_PAGEBUFCLR_gc);
    
    Flash_U8Ptr = (U8*)0x8600;
    while (Cnt_U16 < 256)
    {
        Flash_U8Ptr[Cnt_U16] = Cnt_U16;
        Cnt_U16++;
    }

    NvmCtrl_ControlA_CmdExec(NVMCTRL_CMD_PAGEERASEWRITE_gc); // Erase + Program page
    
    while (0 < (NVMCTRL.STATUS & NVMCTRL_FBUSY_bm)) {}       // Wait until ready
}
 

The flash ends up like this:

prog 0x0600  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
prog 0x0610  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
prog 0x0620  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
prog 0x0630  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
prog 0x0640  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
prog 0x0650  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
prog 0x0660  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
prog 0x0670  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
prog 0x0680  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
prog 0x0690  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
prog 0x06A0  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
prog 0x06B0  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
prog 0x06C0  00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f  ................
prog 0x06D0  10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f  ................
prog 0x06E0  20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f   !"#$%&'()*+,-./
prog 0x06F0  30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f  0123456789:;<=>?

 

What have a done to make the code end up in the last part of the page?

 

/Conny

This topic has a solution.

Can

Last Edited: Mon. Dec 2, 2019 - 08:06 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

while (Cnt_U16 < 256)

 

I would start with only writing a page, which is 64 bytes for a tiny412. You are writing only the last 64 bytes of your 256 bytes because the last address written was in that page using the data in the temp buffer. The temp buffer one would think would then contain 0xC0-0xFF and show up as such, but you are only clearing bits in the temp buffer after it is erased so the 0xC0 write is trying to rewrite over the 0x00 (done earlier from 0-63), which it cannot do (cannot set bits) so you get 0x00 (essentially the upper 2 bits have been masked off on the first 0-63 counts through the loop).

Last Edited: Fri. Nov 29, 2019 - 08:02 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's exactly as curtvm explained. You need to write each page once you reach the page boundary.

For example:

 

void Swdl_NewDataBOOL(void)
{
    U16 Cnt_U16 = 0;
    U8* Flash_U8Ptr;

    NvmCtrl_ControlA_CmdExec(NVMCTRL_CMD_PAGEBUFCLR_gc);

    Flash_U8Ptr = (U8*)0x8600;
    while (Cnt_U16 < 256)
    {
        Flash_U8Ptr[Cnt_U16] = Cnt_U16;
        Cnt_U16++;

        if ((Cnt_U16 & (PROGMEM_PAGE_SIZE-1)) == 0) // Reached page boundary, time to write
        {
            while (0 < (NVMCTRL.STATUS & NVMCTRL_FBUSY_bm)) {}       // Wait until ready
            NvmCtrl_ControlA_CmdExec(NVMCTRL_CMD_PAGEERASEWRITE_gc); // Erase + Program page
        }
    }

}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Omg... For some reason I was thinking a page was 256b... This of course explains the problem. Thanks a lot!

Can