void SpiFlash_ChipErase(void) { // /CS: active SPI_SET_SS0_LOW(SPI_FLASH_PORT); // send Command: 0x06, Write enable SPI_WRITE_TX(SPI_FLASH_PORT, 0x06); // wait tx finish while(SPI_IS_BUSY(SPI_FLASH_PORT)); // /CS: de-active SPI_SET_SS0_HIGH(SPI_FLASH_PORT); ////////////////////////////////////////// // /CS: active SPI_SET_SS0_LOW(SPI_FLASH_PORT); // send Command: 0xC7, Chip Erase SPI_WRITE_TX(SPI_FLASH_PORT, 0xC7); // wait tx finish while(SPI_IS_BUSY(SPI_FLASH_PORT)); // /CS: de-active SPI_SET_SS0_HIGH(SPI_FLASH_PORT); SPI_ClearRxFIFO(SPI0); }
void SpiFlash_WriteStatusReg(uint8_t u8Value) { // /CS: active SPI_SET_SS0_LOW(SPI_FLASH_PORT); // send Command: 0x06, Write enable SPI_WRITE_TX(SPI_FLASH_PORT, 0x06); // wait tx finish while(SPI_IS_BUSY(SPI_FLASH_PORT)); // /CS: de-active SPI_SET_SS0_HIGH(SPI_FLASH_PORT); /////////////////////////////////////// // /CS: active SPI_SET_SS0_LOW(SPI_FLASH_PORT); // send Command: 0x01, Write status register SPI_WRITE_TX(SPI_FLASH_PORT, 0x01); // write status SPI_WRITE_TX(SPI_FLASH_PORT, u8Value); // wait tx finish while(SPI_IS_BUSY(SPI_FLASH_PORT)); // /CS: de-active SPI_SET_SS0_HIGH(SPI_FLASH_PORT); }
uint16_t SpiFlash_ReadMidDid(void) { uint8_t u8RxData[6], u8IDCnt = 0; // /CS: active SPI_SET_SS0_LOW(SPI_FLASH_PORT); // send Command: 0x90, Read Manufacturer/Device ID SPI_WRITE_TX(SPI_FLASH_PORT, 0x90); // send 24-bit '0', dummy SPI_WRITE_TX(SPI_FLASH_PORT, 0x00); SPI_WRITE_TX(SPI_FLASH_PORT, 0x00); SPI_WRITE_TX(SPI_FLASH_PORT, 0x00); // receive 16-bit SPI_WRITE_TX(SPI_FLASH_PORT, 0x00); SPI_WRITE_TX(SPI_FLASH_PORT, 0x00); // wait tx finish while(SPI_IS_BUSY(SPI_FLASH_PORT)); // /CS: de-active SPI_SET_SS0_HIGH(SPI_FLASH_PORT); while(!SPI_GET_RX_FIFO_EMPTY_FLAG(SPI_FLASH_PORT)) u8RxData[u8IDCnt ++] = SPI_READ_RX(SPI_FLASH_PORT); return ( (u8RxData[4]<<8) | u8RxData[5] ); }
void SpiFlash_NormalPageProgram(uint32_t StartAddress, uint8_t *u8DataBuffer) { uint32_t i = 0; // /CS: active SPI_SET_SS0_LOW(SPI_FLASH_PORT); // send Command: 0x06, Write enable SPI_WRITE_TX(SPI_FLASH_PORT, 0x06); // wait tx finish while(SPI_IS_BUSY(SPI_FLASH_PORT)); // /CS: de-active SPI_SET_SS0_HIGH(SPI_FLASH_PORT); // /CS: active SPI_SET_SS0_LOW(SPI_FLASH_PORT); // send Command: 0x02, Page program SPI_WRITE_TX(SPI_FLASH_PORT, 0x02); // send 24-bit start address SPI_WRITE_TX(SPI_FLASH_PORT, (StartAddress>>16) & 0xFF); SPI_WRITE_TX(SPI_FLASH_PORT, (StartAddress>>8) & 0xFF); SPI_WRITE_TX(SPI_FLASH_PORT, StartAddress & 0xFF); // write data while(1) { if(!SPI_GET_TX_FIFO_FULL_FLAG(SPI_FLASH_PORT)) { SPI_WRITE_TX(SPI_FLASH_PORT, u8DataBuffer[i++]); if(i >= 255) break; } } // wait tx finish while(SPI_IS_BUSY(SPI_FLASH_PORT)); // /CS: de-active SPI_SET_SS0_HIGH(SPI_FLASH_PORT); SPI_ClearRxFIFO(SPI_FLASH_PORT); }
void SpiFlash_DualFastRead(uint32_t StartAddress, uint8_t *u8DataBuffer) { uint32_t i; // /CS: active SPI_SET_SS0_LOW(SPI_FLASH_PORT); // Command: 0x3B, Fast Read dual data SPI_WRITE_TX(SPI_FLASH_PORT, 0x3B); // send 24-bit start address SPI_WRITE_TX(SPI_FLASH_PORT, (StartAddress>>16) & 0xFF); SPI_WRITE_TX(SPI_FLASH_PORT, (StartAddress>>8) & 0xFF); SPI_WRITE_TX(SPI_FLASH_PORT, StartAddress & 0xFF); // dummy byte SPI_WRITE_TX(SPI_FLASH_PORT, 0x00); while(SPI_IS_BUSY(SPI_FLASH_PORT)); // clear RX buffer SPI_ClearRxFIFO(SPI_FLASH_PORT); // enable SPI dual IO mode and set direction to input SPI_ENABLE_DUAL_MODE(SPI_FLASH_PORT); SPI_ENABLE_DUAL_INPUT_MODE(SPI_FLASH_PORT); // read data for(i=0; i<256; i++) { SPI_WRITE_TX(SPI_FLASH_PORT, 0x00); while(SPI_IS_BUSY(SPI_FLASH_PORT)); u8DataBuffer[i] = SPI_READ_RX(SPI_FLASH_PORT); } // wait tx finish while(SPI_IS_BUSY(SPI_FLASH_PORT)); // /CS: de-active SPI_SET_SS0_HIGH(SPI_FLASH_PORT); SPI_DISABLE_DUAL_MODE(SPI_FLASH_PORT); }
uint8_t SpiFlash_ReadStatusReg(void) { // /CS: active SPI_SET_SS0_LOW(SPI_FLASH_PORT); // send Command: 0x05, Read status register SPI_WRITE_TX(SPI_FLASH_PORT, 0x05); // read status SPI_WRITE_TX(SPI_FLASH_PORT, 0x00); // wait tx finish while(SPI_IS_BUSY(SPI_FLASH_PORT)); // /CS: de-active SPI_SET_SS0_HIGH(SPI_FLASH_PORT); // skip first rx data SPI_READ_RX(SPI_FLASH_PORT); return (SPI_READ_RX(SPI_FLASH_PORT) & 0xff); }
void spi_deselect_device(SPI_T *spi, int ss) { SPI_SET_SS0_HIGH(spi); }