on 2022-06-30 12:35 AM
#define flashSPIh hspi2 #define CMD_RDID 0x90 static uint16_t readDeviceId () { FLASH_CS_LOW(); uint8_t bufferOut[] = {CMD_RDID, 0, 0, 0}; uint8_t bufferIn[] = {0, 0}; if (HAL_SPI_Transmit(&flashSPIh, bufferOut, sizeof(bufferOut), 1000) !=HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } if (HAL_SPI_Receive(&flashSPIh, bufferIn, sizeof(bufferIn), 1000) != HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } FLASH_CS_HIGH(); //combine bytes return bufferIn[0]<<8 | bufferIn[1]; }readStatusRegister () :
#define CMD_RDSR 0x05 static uint8_t readStatusRegister() { FLASH_CS_LOW(); uint8_t bufferOut[] = {CMD_RDSR}; uint8_t bufferIn[] = {0, 0}; if (HAL_SPI_Transmit(&flashSPIh, bufferOut, sizeof(bufferOut), 1000) !=HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } if (HAL_SPI_Receive(&flashSPIh, bufferIn, sizeof(bufferIn), 1000) != HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } FLASH_CS_HIGH(); return bufferIn[0]; }sendWriteEnable ():
#define CMD_WREN 0x06 void sendWriteEnable() { FLASH_CS_LOW(); uint8_t bufferOut[] = {CMD_WREN}; if (HAL_SPI_Transmit(&flashSPIh, bufferOut, sizeof(bufferOut), 1000) !=HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } FLASH_CS_HIGH(); }writeStatusRegister (uint8_t command, uint8_t value) :
static void writeStatusRegister(uint8_t command, uint8_t value) { FLASH_CS_LOW(); uint8_t bufferOut[2]; bufferOut[0] = command; bufferOut[1] = value; if (HAL_SPI_Transmit(&flashSPIh, bufferOut, sizeof(bufferOut), 1000) !=HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } FLASH_CS_HIGH(); }sendSectorErase (uint32_t address24):
#define CMD_SE 0x20 void sendSectorErase(uint32_t address24) { FLASH_CS_LOW(); uint8_t bufferOut[4]; bufferOut[0] = CMD_SE; bufferOut[1] = (address24 >> 16) & 0xFF; bufferOut[2] = (address24 >> 8) & 0xFF; bufferOut[3] = address24 & 0xFF; if (HAL_SPI_Transmit(&flashSPIh, bufferOut, sizeof(bufferOut), 1000) !=HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } FLASH_CS_HIGH(); }sendPageProgram (uint32_t address24, const uint8_t* data, uint32_t length):
#define CMD_PP 0x02 void sendPageProgram(uint32_t address24, constuint8_t* data, uint32_t length) { FLASH_CS_LOW(); uint8_t bufferOut[4]; bufferOut[0] = CMD_PP; bufferOut[1] = (address24 >> 16) & 0xFF; bufferOut[2] = (address24 >> 8) & 0xFF; bufferOut[3] = address24 & 0xFF; if (HAL_SPI_Transmit(&flashSPIh, bufferOut, sizeof(bufferOut), 1000) !=HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } if (HAL_SPI_Transmit(&flashSPIh, (uint8_t *)data, length, 1000) !=HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } FLASH_CS_HIGH(); }