从ATtiny1616 EEPROM读/写

时间:2018-11-12 19:05:36

标签: avr avr-gcc eeprom non-volatile

在avr-gcc中使用ATting1616,我试图读写EEPROM。

ATtiny1616使用NVMCTRL-非易失性存储器控制器进行字节级读/写。我正在使用NVMCTRL来从EEPROM读取/写入块,但无法正常工作。

这里是一个例子,用以说明我正在尝试的做法。

让我们说我要在EEPROM中保存两个不同的值,然后读回每个值。

uint16_t eeprom_address1 = 0x01;//!< Address one for first saved value
uint16_t eeprom_address2 = 0x32;//!< Address two for second saved value

char save_one = "12345"; //!< Test value to save, one
char save_two = "testing";//!< Test value to save, two


FLASH_0_write_eeprom_block(eeprom_address1,save_one,7); //!< Save first value to address 1
FLASH_0_write_eeprom_block(eeprom_address2,save_two,7); //!< Save second value to address 2


char test_data[7] = {0}; //!< Just some empty array to put chars into

FLASH_0_read_eeprom_block(eeprom_address1,test_data,7); //!< Read eeprom from address, to address+ 7, and store back into test_data

以下是读/写功能:

#  define EEPROM_START     (0x1400)//!< is located in header file
/**
 * \brief Read a block from eeprom
 *
 * \param[in] eeprom_adr The byte-address in eeprom to read from
 * \param[in] data Buffer to place read data into
 *
 * \return Nothing
 */
void FLASH_0_read_eeprom_block(eeprom_adr_t eeprom_adr, uint8_t *data, size_t size)
{

    // Read operation will be stalled by hardware if any write is in progress

    memcpy(data, (uint8_t *)(EEPROM_START + eeprom_adr), size);
}

/**
 * \brief Write a block to eeprom
 *
 * \param[in] eeprom_adr The byte-address in eeprom to write to
 * \param[in] data The buffer to write
 *
 * \return Status of write operation
 */
nvmctrl_status_t FLASH_0_write_eeprom_block(eeprom_adr_t eeprom_adr, uint8_t *data, size_t size)
{

    uint8_t *write = (uint8_t *)(EEPROM_START + eeprom_adr);

    /* Wait for completion of previous write */
    while (NVMCTRL.STATUS & NVMCTRL_EEBUSY_bm)
        ;

    /* Clear page buffer */
    ccp_write_spm((void *)&NVMCTRL.CTRLA, NVMCTRL_CMD_PAGEBUFCLR_gc);

    do {
        /* Write byte to page buffer */
        *write++ = *data++;
        size--;
        // If we have filled an entire page or written last byte to a partially filled page
        if ((((uintptr_t)write % EEPROM_PAGE_SIZE) == 0) || (size == 0)) {
            /* Erase written part of page and program with desired value(s) */
            ccp_write_spm((void *)&NVMCTRL.CTRLA, NVMCTRL_CMD_PAGEERASEWRITE_gc);
        }
    } while (size != 0);

    return NVM_OK;
}

如果打印了test_data [7],则该值将为“测试”。 在调试模式下查看存储器时,我可以看到该值始终被写入数据EEPROM中的第一个存储器位置。[0x1400] 在这种情况下,从内存x1400开始,“ testing”的值开始。

似乎有些基本的知识我对EEPROM的读写不了解。任何指导将不胜感激。

0 个答案:

没有答案