CC2530 片内FLASH读写问题
你好,现在我有需求将一些参数保存在片内FLASH中,主要是保存通信的一些参数,可以配置,然后储存在FLASH内。我使用下面的代码进行FLASH操作,读取和擦出没有问题,现在写数据写不进去,请问下面的代码中有什么问题吗?
void FlashErase(unsigned char page)
{
FADDRH = page << 1;
FCTL |= 0x01; //启动页面擦除
while (FCTL & 0x80); //等待擦除完成
}
unsigned char DmaDesc[8]={0,0,0x62,0x73,0,0,0x12,0x42}; //定义DMA描述符
void FlashWrite(unsigned char page, unsigned int offset, unsigned char *buf, unsigned int length)
{
FADDRH = (page << 1)|((offset >> 10) & 1); //计算FLASH地址
FADDRL = (offset >> 2) & 0xFF;
DmaDesc[0] = (unsigned int)buf >> 8; //设置数据地址
DmaDesc[1] = (unsigned int)buf & 0xFF;
DmaDesc[4] = (length >> 8) & 0x1F; //设置字节数
DmaDesc[5] = length & 0xFC;
DMA0CFGH = (unsigned int)DmaDesc >> 8; //设置DMA通道0描述符地址
DMA0CFGL = (unsigned int)DmaDesc & 0xFF;
DMAIRQ = 0x01; //激活DMA通道0
DMAARM = 0x01; //使DMA通道0进入工作状态 FCTL |= 0x02; //开始DMA写入操作
while (FCTL & 0x80); //等待写入完成
}
void FlashRead(unsigned char page, unsigned int offset, unsigned char *buf, unsigned int length)
{
unsigned char *pData = (unsigned char *)(offset + (page & 0xF) * 2048 + 0x8000);
unsigned char memctr = MEMCTR; //暂存当前存储器映射状态
MEMCTR = (MEMCTR & 0xF8)|((page >> 4) & 7); //将被读FLASH映射到XDATA
while (length--)
*buf++ = *pData++; //读取数据
MEMCTR = memctr; //回复存储器映射状态
}
如果要对flash中同一个位置进行第二次写入的时候,必须要把整个page擦除了,才能写的。
你好,这个顺序我知道的,现在是数据无法写入 flash 之前已经擦除整页。
有没有参考下协议栈里面对Flash的操作
void HalFlashWrite(uint16 addr, uint8 *buf, uint16 cnt)
{
#if (defined HAL_DMA) && (HAL_DMA == TRUE)
halDMADesc_t *ch = HAL_NV_DMA_GET_DESC();
HAL_DMA_SET_SOURCE(ch, buf);
HAL_DMA_SET_DEST(ch, &FWDATA);
HAL_DMA_SET_VLEN(ch, HAL_DMA_VLEN_USE_LEN);
HAL_DMA_SET_LEN(ch, (cnt * HAL_FLASH_WORD_SIZE));
HAL_DMA_SET_WORD_SIZE(ch, HAL_DMA_WORDSIZE_BYTE);
HAL_DMA_SET_TRIG_MODE(ch, HAL_DMA_TMODE_SINGLE);
HAL_DMA_SET_TRIG_SRC(ch, HAL_DMA_TRIG_FLASH);
HAL_DMA_SET_SRC_INC(ch, HAL_DMA_SRCINC_1);
HAL_DMA_SET_DST_INC(ch, HAL_DMA_DSTINC_0);
// The DMA is to be polled and shall not issue an IRQ upon completion.
HAL_DMA_SET_IRQ(ch, HAL_DMA_IRQMASK_DISABLE);
HAL_DMA_SET_M8( ch, HAL_DMA_M8_USE_8_BITS);
HAL_DMA_SET_PRIORITY(ch, HAL_DMA_PRI_HIGH);
HAL_DMA_CLEAR_IRQ(HAL_NV_DMA_CH);
HAL_DMA_ARM_CH(HAL_NV_DMA_CH);
FADDRL = (uint8)addr;
FADDRH = (uint8)(addr >> 8);
FCTL |= 0x02; // Trigger the DMA writes.
while (FCTL & 0x80); // Wait until writing is done.
#endif
}