Mark L
Mark L

Reputation: 1

Data Flash storage issue with ATSAML21E17

I have been unable to store into the Data Flash of an ATSAML21E17. (It did work once) The fuses are all set to default. Stepping through the function shows no errors in the status or interrupt registers. NVMCTL status shows ready after each step. Flash content does not change. Naturally, this doesn't work even when the debugger probe isn't used.

Perhaps the processor manual fails to name all of the conditions for writing that flash. Or, after all of these years, I have finally gone insane.

I'd appreciate any help.

I tried storing into flash. Watched with a debugger.

int SaveNvmData (void *uBufr, uint16_t bytCnt)
    {
    uint16_t *src = uBufr, *dst = (uint16_t *)DATAFLASH_ADDR;
    uint32_t ctlB = NVMCTRL->CTRLB.reg;
    int rtnVal = ERR_OK;

    /* Use manual page write */
    NVMCTRL->CTRLB.bit.MANW = 1;
    
    /* Word alignment is needed */
    if ((uint32_t)uBufr & 3)
        rtnVal = ERR_Align;

    /* Size must be less than 2048 */
    else if (bytCnt > 2048)
        rtnVal = ERR_NvmSize;

    /* Size must be multiple of four */
    else if (bytCnt & 3)
        rtnVal = ERR_NvmMod4;

    /* OK, copy to Data Flash */
    else
        {
        /* Clear the page buffer */
        NVMCTRL->CTRLA.reg = 0xA544;
        while ( ! (NVMCTRL->INTFLAG.reg & 1));

        /* Write to FLASH one page at a time */
        do 
            {
            /* Erase a row every 256 bytes */
            if (((uint32_t)dst & 0xFF) == 0)
                {
                NVMCTRL->ADDR.reg = (uint32_t)dst;
                NVMCTRL->CTRLA.reg = 0xA51A;
                while ( ! (NVMCTRL->INTFLAG.reg & 1));
                }

            /* Write up to 64 bytes into page buffer */
            do
                {
                *dst++ = *src++;
                bytCnt -= 2;
                }
            while (bytCnt && ((uint32_t)dst & 0x3F) != 0);

            /* Perform a page write */
            NVMCTRL->CTRLA.reg = 0xA51C;
            while ( ! (NVMCTRL->INTFLAG.reg & 1));
            }
        while (bytCnt);
        }

    /* Restore the original write control */
    NVMCTRL->CTRLB.reg = ctlB;

    /* Non-zero indicates an error */
    return (rtnVal);
    }

Upvotes: 0

Views: 47

Answers (1)

Mark L
Mark L

Reputation: 1

It seems that the AVR ICE debugger (SWDIO/SWCLK connection) prevents writing to the Data Flash. Values are stored when that dongle is unplugged. It is not enough to be cruising without debugging. I will check to see if there is a hardware setting for this. Such controls exist on other peripherals.

Upvotes: 0

Related Questions