电子说
移植需要更改sendrcv函数里面内容,宏定义内容
[cpp] view plain copy《code class=“language-cpp”》#define DRV_SPI_FLASH_WRITE_ENABLE (0x06) //Write Enable
#define DRV_SPI_FLASH_WRITE_DISABLE (0x04) //Write Disable
#define DRV_SPI_FLASH_READ_STATUS_REG (0x05) //Read Status Register
#define DRV_SPI_FLASH_WRITE_STATUS_REG (0x01) //Write Status Register
#define DRV_SPI_FLASH_READ_DATA (0x03) //Read Data
#define DRV_SPI_FLASH_FAST_READ (0x0B) //Fast Read
#define DRV_SPI_FLASH_FAST_READ_DUAL_OUTPUT (0x3B) //Fast Read Dual Output
#define DRV_SPI_FLASH_PAGEPROGRAM (0x02) //Page Program
#define DRV_SPI_FLASH_BLOCK_ERASE (0xD8) //Block Erase(64KB)
#define DRV_SPI_FLASH_HALF_BLOCK_ERASE (0x52) //Half Block Erase(32KB)
#define DRV_SPI_FLASH_SECTOR_ERASE (0x20) //Sector Erase(4KB)
#define DRV_SPI_FLASH_CHIP_ERASE (0xC7) //Chip Erase
#define DRV_SPI_FLASH_JEDEC_ID (0x9F) //JEDEC ID
#define APP_SPI3_CS_LOW SCU_SetSpiCsn(SPI3_CSN,LOW)
#define APP_SPI3_CS_HIGH SCU_SetSpiCsn(SPI3_CSN,HIGH)
uint8_t DrvFlashSendRcvByte(uint8_t ucData)
{
uint8_t ucTemp;
while(SPI_GetFlagStatus(SGCC_SPI3_P,SPI_FLAG_RFNE))
ucTemp = SPI_ReceiveData(SGCC_SPI3_P);
SPI_SendData(SGCC_SPI3_P, ucData);
while(0 == SPI_GetFlagStatus(SGCC_SPI3_P,SPI_FLAG_RFNE));
ucTemp = SPI_ReceiveData(SGCC_SPI3_P);
return ucTemp;
}
uint32_t DrvFlashReadID(void)
{
uint32_t ulDevID = 0;
APP_SPI3_CS_LOW;
DrvFlashSendRcvByte(DRV_SPI_FLASH_JEDEC_ID);
ulDevID |= DrvFlashSendRcvByte(0xFF) 《《 16;
ulDevID |= DrvFlashSendRcvByte(0xFF) 《《 8;
ulDevID |= DrvFlashSendRcvByte(0xFF);
APP_SPI3_CS_HIGH;
return ulDevID;
}
uint8_t DrvFlashReadStausReg(void)
{
uint8_t ucReg;
APP_SPI3_CS_LOW;
DrvFlashSendRcvByte(DRV_SPI_FLASH_READ_STATUS_REG);
ucReg = DrvFlashSendRcvByte(0xFF);
APP_SPI3_CS_HIGH;
return ucReg;
}
void DrvFlashWaitBusy(void)
{
while((DrvFlashReadStausReg() & 0x01) == 0x01);
}
void DrvFlashWriteEnable(void)
{
APP_SPI3_CS_LOW;
DrvFlashSendRcvByte(DRV_SPI_FLASH_WRITE_ENABLE);
APP_SPI3_CS_HIGH;
}
void DrvFlashWriteDisable(void)
{
APP_SPI3_CS_LOW;
DrvFlashSendRcvByte(DRV_SPI_FLASH_WRITE_DISABLE);
APP_SPI3_CS_HIGH;
}
void DrvFlashReadData(uint8_t* pRcvBuf, uint32_t ulReAddr, uint16_t usReNum)
{
uint16_t i;
APP_SPI3_CS_LOW;
DrvFlashSendRcvByte(DRV_SPI_FLASH_READ_DATA);
DrvFlashSendRcvByte((uint8_t)((ulReAddr)》》16));
DrvFlashSendRcvByte((uint8_t)((ulReAddr)》》8));
DrvFlashSendRcvByte((uint8_t)ulReAddr);
for(i=0; i 《 usReNum; i++)
{
pRcvBuf[i] = DrvFlashSendRcvByte(0XFF);
}
APP_SPI3_CS_HIGH;
}
void DrvFlashPageWrite(uint8_t* pWrBuf, uint32_t ulWrAddr, uint16_t usWrNum)
{
uint16_t i;
DrvFlashWriteEnable();
APP_SPI3_CS_LOW;
DrvFlashSendRcvByte(DRV_SPI_FLASH_PAGEPROGRAM);
DrvFlashSendRcvByte((uint8_t)((ulWrAddr)》》16));
DrvFlashSendRcvByte((uint8_t)((ulWrAddr)》》8));
DrvFlashSendRcvByte((uint8_t)ulWrAddr);
for(i = 0; i 《 usWrNum; i++)
DrvFlashSendRcvByte(pWrBuf[i]);
APP_SPI3_CS_HIGH;
DrvFlashWaitBusy();
}
void DrvFlashWriteNoCheck(uint8_t* pBuffer, uint32_t addr, uint16_t num)
{
uint16_t pageremain;
pageremain = 256 - addr % 256;//鍗曢〉鍓╀綑瀛楄妭鏁�
if(num 《= pageremain)//涓嶅ぇ浜�256
pageremain = num;
while(1)
{
DrvFlashPageWrite(pBuffer, addr, pageremain);
if(num == pageremain)
break;//鍐欑粨鏉熶簡
else
{
pBuffer += pageremain;
addr += pageremain;
num -= pageremain;//鍑忓幓宸茬粡鍐欏叆鐨勫瓧鑺傛暟
if(num 》 256)
pageremain = 256;//鏈鍙互鍐欏叆涓�椤垫暟鎹�
else
pageremain = num;//鏈�鍚庝竴娆″啓
}
};
}
void DrvFlashWriteData(uint8_t* pBuffer,uint32_t addr,uint16_t num)
{
uint32_t secpos;
uint16_t secoff;
uint16_t secremain;
uint16_t i;
uint8_t * W25QXX_BUF;
uint8_t W25QXX_BUFFER[4096];
W25QXX_BUF = W25QXX_BUFFER;
secpos = addr / 4096;//鎵囧尯鍦板潃
secoff = addr % 4096;//鍦ㄦ墖鍖哄唴鐨勫亸绉�
secremain = 4096 - secoff;//鎵囧尯鍓╀綑绌洪棿澶у皬
// printf
if(num 《= secremain)//涓嶅ぇ浜庡綋鍓嶆墖鍖哄墿浣欏瓧鑺�
{
secremain = num;
}
while(1)
{
DrvFlashReadData(W25QXX_BUF, secpos * 4096, 4096);//璇诲彇鏁翠釜鎵囧尯
for(i = 0; i 《 secremain; i++)//鏍¢獙鏁版嵁
{
if(W25QXX_BUF[secoff + i] != 0XFF)//闇�瑕佹摝鍑�
break;
}
if(i 《 secremain)//闇�瑕佹摝鍑�
{
DrvFlashSectorErase(secpos * 4096);//鎿﹂櫎鏁翠釜鎵囧尯
for(i = 0; i 《 secremain; i++)
{
W25QXX_BUF[i + secoff] = pBuffer[i];//澶嶅埗
}
DrvFlashWriteNoCheck(W25QXX_BUF, secpos * 4096, 4096);//鍐欏叆鏁翠釜鎵囧尯
}
else
DrvFlashWriteNoCheck(pBuffer, addr, secremain);//鍐欏凡缁忔摝闄や簡鐨勶紝鐩存帴鍐欏叆鎵囧尯鍓╀綑绌洪棿
if(num == secremain)
break;//鍐欑粨鏉熶簡
else//
{
secpos++;//鎵囧尯鍦板潃澧炲姞1
secoff = 0;//鍋忕Щ浣嶇疆涓�0
pBuffer += secremain;//鎸囬拡鍋忕Щ
addr += secremain;//鍐欏湴鍧�鍋忕Щ
num -= secremain;//瀛楄妭鏁伴�掑噺
if(num 》 4096)
secremain = 4096;//涓嬩竴涓墖鍖鸿繕鏄啓涓嶅畬
else
secremain = num;//涓嬩竴涓墖鍖哄彲浠ュ啓瀹屼簡
}
};
}
void DrvFlashChipErase(void)
{
DrvFlashWriteEnable();
DrvFlashWaitBusy();
APP_SPI3_CS_LOW;
DrvFlashSendRcvByte(DRV_SPI_FLASH_CHIP_ERASE);
APP_SPI3_CS_HIGH;
DrvFlashWaitBusy();
}
void DrvFlashSectorErase(uint32_t ulErAddr)
{
DrvFlashWriteEnable();
DrvFlashWaitBusy();
APP_SPI3_CS_LOW;
DrvFlashSendRcvByte(DRV_SPI_FLASH_SECTOR_ERASE);
DrvFlashSendRcvByte((uint8_t)((ulErAddr)》》16));
DrvFlashSendRcvByte((uint8_t)((ulErAddr)》》8));
DrvFlashSendRcvByte((uint8_t)ulErAddr);
APP_SPI3_CS_HIGH;
DrvFlashWaitBusy();
}
《/code》
全部0条评论
快来发表一下你的评论吧 !