/****************************************************************************************** *函数名:SPI_FLASH_PageWrite() * 参数:uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite 数据指针,写入地址,写入的个数 * 返回值:void * 功能:SPIFLASH页写入数据函数,外部调用 *********************************************************************************************/ void SPI_FLASH_PageWrite(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) { /*使能写入*/ SPI_FLASH_WriteEnable(); /*使能片选*/ SPI_FLASH_CS_LOW(); /* 发送页写入指令*/ SSP0_SendByte(W25X_PageProgram); /*发送高8位数据地址*/ SSP0_SendByte((WriteAddr & 0xFF0000) >> 16); /*发送中8位数据地址*/ SSP0_SendByte((WriteAddr & 0xFF00) >> 8); /*发送低8位数据地址*/ SSP0_SendByte(WriteAddr & 0xFF); /*检测写入的数据是否超出页的容量大小*/ if (NumByteToWrite > SPI_FLASH_PerWritePageSize) { NumByteToWrite = SPI_FLASH_PerWritePageSize; } /*循环写入数据*/ while (NumByteToWrite--) { /*发送数据*/ SSP0_SendByte(*pBuffer); /* 指针移到下一个写入数据 */ pBuffer++; } /*失能片选*/ SPI_FLASH_CS_HIGH(); /* 等待写完成*/ SPI_FLASH_WaitForWriteEnd(); }
/** * @brief Writes more than one byte to the FLASH with a single WRITE * cycle(Page WRITE sequence). The number of byte can't exceed * the FLASH page size. * @param pBuffer : pointer to the buffer containing the data to be * written to the FLASH. * @param WriteAddr : FLASH's internal address to write to. * @param NumByteToWrite : number of bytes to write to the FLASH, * must be equal or less than "SPI_FLASH_PageSize" value. * @retval : None */ void SPI_FLASH_PageWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) { /* Enable the write access to the FLASH */ SPI_FLASH_WriteEnable(); /* Select the FLASH: Chip Select low */ SPI_FLASH_CS_LOW(); /* Send "Write to Memory " instruction */ SPI_FLASH_SendByte(WRITE); /* Send WriteAddr high nibble address byte to write to */ SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16); /* Send WriteAddr medium nibble address byte to write to */ SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8); /* Send WriteAddr low nibble address byte to write to */ SPI_FLASH_SendByte(WriteAddr & 0xFF); /* while there is data to be written on the FLASH */ while (NumByteToWrite--) { /* Send the current byte */ SPI_FLASH_SendByte(*pBuffer); /* Point on the next byte to be written */ pBuffer++; } /* Deselect the FLASH: Chip Select high */ SPI_FLASH_CS_HIGH(); /* Wait the end of Flash writing */ SPI_FLASH_WaitForWriteEnd(); }
uint8_t SPI_FLASH_ReadStatus(uint8_t Status) { uint8_t FLASH_Status; SPI_FLASH_WriteEnable(); SPI_FLASH_CS_LOW(); SPI_FLASH_SendByte(Status); FLASH_Status = SPI_FLASH_GetByte(); SPI_FLASH_CS_HIGH(); //SPI_FLASH_WriteDisable(); return FLASH_Status; }
/******************************************************************************* SPI_FLASH_PageErase : Erases the specified FLASH Page. *******************************************************************************/ void SPI_FLASH_PageErase(u32 PageAddr) { SPI_FLASH_WriteEnable(); SPI_FLASH_CS_LOW(); SPI_FLASH_SendByte(PE); SPI_FLASH_SendByte((PageAddr & 0xFF0000) >> 16); //Send high address byte SPI_FLASH_SendByte((PageAddr & 0xFF00) >> 8); //Send medium address byte SPI_FLASH_SendByte(PageAddr & 0xFF); //Send low address byte SPI_FLASH_CS_HIGH(); SPI_FLASH_WaitForWriteEnd(); // Wait the end of Flash writing }
/****************************************************************************************** *函数名:SPI_FLASH_BulkErase() * 参数:void * 返回值:void * 功能:SPIFLASH整片擦除函数,外部调用 *********************************************************************************************/ void SPI_FLASH_BulkErase(void) { /*使能写入*/ SPI_FLASH_WriteEnable(); /* 使能片选 */ SPI_FLASH_CS_LOW(); /*发送整片擦除指令*/ SSP0_SendByte(W25X_ChipErase); /*失能片选*/ SPI_FLASH_CS_HIGH(); /* 等待写完成*/ SPI_FLASH_WaitForWriteEnd(); }
//SPI在一页(0~65535)内写入少于256个字节的数据 //在指定地址开始写入最大256字节的数据 //pBuffer:数据存储区 //WriteAddr:开始写入的地址(24bit) //NumByteToWrite:要写入的字节数(最大256),该数不应该超过该页的剩余字节数!!! void SPI_Flash_Write_Page(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite) { u16 i; SPI_FLASH_WriteEnable(); //SET WEL SPI_FLASH_CS_LOW(); //使能器件 SPI_FLASH_SendByte(W25X_PageProgram); //发送写页命令 SPI_FLASH_SendByte((u8)((WriteAddr)>>16)); //发送24bit地址 SPI_FLASH_SendByte((u8)((WriteAddr)>>8)); SPI_FLASH_SendByte((u8)WriteAddr); for(i=0;i<NumByteToWrite;i++)SPI_FLASH_SendByte(pBuffer[i]);//循环写数 SPI_FLASH_CS_HIGH(); //取消片选 SPI_FLASH_WaitForWriteEnd(); //等待写入结束 }
void SPI_FLASH_WriteStatus(uint16_t wStatus) { SPI_FLASH_WriteEnable(); SPI_FLASH_CS_LOW(); //SPI_FLASH_WriteEnable(); //Spi_Soft_Delay(5); SPI_FLASH_SendByte(WRSR); SPI_FLASH_SendByte(wStatus & 0xff); SPI_FLASH_SendByte(wStatus>>8); SPI_FLASH_CS_HIGH(); SPI_FLASH_WaitForWriteEnd(); }
/** * @brief Erases the entire FLASH. * @param None * @retval : None */ void SPI_FLASH_BulkErase(void) { /* Send write enable instruction */ SPI_FLASH_WriteEnable(); /* Bulk Erase */ /* Select the FLASH: Chip Select low */ SPI_FLASH_CS_LOW(); /* Send Bulk Erase instruction */ SPI_FLASH_SendByte(BE); /* Deselect the FLASH: Chip Select high */ SPI_FLASH_CS_HIGH(); /* Wait the end of Flash writing */ SPI_FLASH_WaitForWriteEnd(); }
void SPI_FLASH_PageWrite(u8* pBuffer, u32 WriteAddr) { u16 Lenght = 0x100; SPI_FLASH_WriteEnable(); SPI_FLASH_CS_LOW(); SPI_FLASH_SendByte(PW); SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16); SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8); SPI_FLASH_SendByte(WriteAddr & 0xFF); while(Lenght--) { // while there is data to be written on the FLASH SPI_FLASH_SendByte(*pBuffer); pBuffer++; } SPI_FLASH_CS_HIGH(); SPI_FLASH_WaitForWriteEnd(); }
/****************************************************************************************** *函数名:SPI_FLASH_SectorErase() * 参数:uint32_t SectorAddr 块地址 * 返回值:void * 功能:SPIFLASH扇区擦除函数,外部调用 *********************************************************************************************/ void SPI_FLASH_SectorErase(uint32_t SectorAddr) { /*发送写数据使能指令*/ SPI_FLASH_WriteEnable(); /*等待数据写完,保证写操作是空闲的*/ SPI_FLASH_WaitForWriteEnd(); /* 使能片选 */ SPI_FLASH_CS_LOW(); /*发送扇区擦除指令*/ SSP0_SendByte(W25X_SectorErase); /*发送块地址高8位*/ SSP0_SendByte((SectorAddr & 0xFF0000) >> 16); /*发送块地址中8位*/ SSP0_SendByte((SectorAddr & 0xFF00) >> 8); /*发送块地址低8位*/ SSP0_SendByte(SectorAddr & 0xFF); /*失能片选*/ SPI_FLASH_CS_HIGH(); /* 等待写完毕*/ SPI_FLASH_WaitForWriteEnd(); }
/******************************************************************************* * Function Name : SPI_FLASH_SectorErase * Description : Erases the specified FLASH sector. * Input : SectorAddr: address of the sector to erase. * Output : None * Return : None *******************************************************************************/ void SPI_FLASH_SectorErase(u32 SectorAddr) { /* Send write enable instruction */ SPI_FLASH_WriteEnable(); SPI_FLASH_WaitForWriteEnd(); /* Sector Erase */ /* Select the FLASH: Chip Select low */ SPI_FLASH_CS_LOW(); /* Send Sector Erase instruction */ SPI_FLASH_SendByte(W25X_SectorErase); /* Send SectorAddr high nibble address byte */ SPI_FLASH_SendByte((SectorAddr & 0xFF0000) >> 16); /* Send SectorAddr medium nibble address byte */ SPI_FLASH_SendByte((SectorAddr & 0xFF00) >> 8); /* Send SectorAddr low nibble address byte */ SPI_FLASH_SendByte(SectorAddr & 0xFF); /* Deselect the FLASH: Chip Select high */ SPI_FLASH_CS_HIGH(); /* Wait the end of Flash writing */ SPI_FLASH_WaitForWriteEnd(); }
/******************************************************************************* SPI_FLASH_PageWrite : SPI FLASH 的2~16页被定义成参数区 *******************************************************************************/ void Param_PageWrite(u8* pBuffer, u8 PageNum) { u16 Lenght; u32 WriteAddr; Lenght = 0x100; WriteAddr = 0x200 * (1 + PageNum); // SPI_FLASH_PageErase(WriteAddr); SPI_FLASH_WriteEnable(); SPI_FLASH_CS_LOW(); SPI_FLASH_SendByte(PW); SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16); SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8); SPI_FLASH_SendByte(WriteAddr & 0xFF); while(Lenght--) { // while there is data to be written on the FLASH SPI_FLASH_SendByte(*pBuffer); pBuffer++; } SPI_FLASH_CS_HIGH(); SPI_FLASH_WaitForWriteEnd(); }
/******************************************************************************* * Function Name : SPI_FLASH_PageWrite * Description : Writes more than one byte to the FLASH with a single WRITE * cycle(Page WRITE sequence). The number of byte can't exceed * the FLASH page size. * Input : - pBuffer : pointer to the buffer containing the data to be * written to the FLASH. * - WriteAddr : FLASH's internal address to write to. * - NumByteToWrite : number of bytes to write to the FLASH, * must be equal or less than "SPI_FLASH_PageSize" value. * Output : None * Return : None *******************************************************************************/ void SPI_FLASH_PageWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite) { /* Enable the write access to the FLASH */ SPI_FLASH_WriteEnable(); /* Select the FLASH: Chip Select low */ SPI_FLASH_CS_LOW(); /* Send "Write to Memory " instruction */ SPI_FLASH_SendByte(W25X_PageProgram); /* Send WriteAddr high nibble address byte to write to */ SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16); /* Send WriteAddr medium nibble address byte to write to */ SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8); /* Send WriteAddr low nibble address byte to write to */ SPI_FLASH_SendByte(WriteAddr & 0xFF); if(NumByteToWrite > SPI_FLASH_PerWritePageSize) { NumByteToWrite = SPI_FLASH_PerWritePageSize; //printf("\n\r Err: SPI_FLASH_PageWrite too large!"); } /* while there is data to be written on the FLASH */ while (NumByteToWrite--) { /* Send the current byte */ SPI_FLASH_SendByte(*pBuffer); /* Point on the next byte to be written */ pBuffer++; } /* Deselect the FLASH: Chip Select high */ SPI_FLASH_CS_HIGH(); /* Wait the end of Flash writing */ SPI_FLASH_WaitForWriteEnd(); }