0

在 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 开始,“测试”的值开始。

在读写 EEPROM 时,我似乎无法理解一些基本的东西。任何指导将不胜感激。

4

0 回答 0