写块设备编程范式

在对SPI Flash,EEPROM等设备进行写入数据时,通常此类设备内部具有Page Buffer,写数据长度不能超过Page Buffer的长度,否则会出现问题。在对此类设备进行写入,提出相应的编程范式。

本文以SPI Flash为例,其他诸如EEPROM设备也可参考。

#define SPI_FLASH_PAGE_SIZE	(0x100)

uint32_t SPIFlash_WriteBuffer(uint32_t Address, uint8_t *pBuffer, uint32_t nLength)
{	
	uint32_t curLength = 0;
	uint32_t curAddress = 0;
	uint32_t nRemain = 0;
    uint32_t nPageRemain = 0;
  
    if ((pBuffer == NULL) || (nLength == 0))
    {
        return 0;
    }

    curAddress = Address;

    while (curLength < nLength)
    {
        nPageRemain = SPI_FLASH_PAGE_SIZE - (curAddress & (SPI_FLASH_PAGE_SIZE - 1));   /* adjust max possible size to page boundary. */
        nRemain = nLength - curLength;
        if (nRemain < nPageRemain)
        {
            nPageRemain = nRemain;
        }

		SPIFlash_WritePage(curAddress, pBuffer + curLength, nPageRemain);

        curAddress += nPageRemain;
        curLength += nPageRemain;
    }

    SPIFlash_WriteDisable();

    return nLength;
}


void SPIFlash_WritePage(uint32_t Address, uint8_t *pBuffer, uint32_t nLength)
{

}

其中,Write Page函数可根据实际情况进行编写。

猜你喜欢

转载自blog.csdn.net/propor/article/details/131221925