Question
Hello everyone, I have the flash memory AT25SF321B and I want to write and read it. As a µC I use an STM32WB55. I write some values but if i read it back i get only zeros. But i can read the manufacture id 1F 87 01. Hope you can help me.
// 25AA040A instructions
const uint8_t EEPROM_READ = 0x03; // 03h Normal Read Data
const uint8_t EEPROM_WRITE = 0x02; // 02h Page Program
const uint8_t EEPROM_WRDI = 0x04; // 04h Write Disable
const uint8_t EEPROM_WREN = 0x06; // 06h Write Enable
const uint8_t EEPROM_RDSR = 0x05; // 05h Read Status Register
const uint8_t EEPROM_WRSR = 0x01; // 01h Write Status Register
const uint8_t EEPROM_RDSR2 = 0x35; // Read Status Register 2
const uint8_t EEPROM_RDSR3 = 0x15; // Read Status Register 3
const uint8_t RESETENABELE = 0x66;
const uint8_t RESETDIVICE = 0x99;
const uint8_t CHIPERASE = 0x60;
const uint8_t ID = 0x9F;
const uint8_t ERASE = 0x44;
const uint8_t BLOCKERASE = 0x20;
// Variablen
char spi_buf[20];
uint8_t addr[3];
uint8_t wip;
// CS pin should default high
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
// Enable write enable latch (allow write operations)
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
memStat = HAL_SPI_Transmit(&hspi1, (uint8_t *)&EEPROM_WREN, 1, 100);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
// Read status register
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
memStat = HAL_SPI_Transmit(&hspi1, (uint8_t *)&EEPROM_RDSR, 1, 100);
memStat = HAL_SPI_Receive(&hspi1, (uint8_t *)spi_buf, 1, 100);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
// Test bytes to write to EEPROM
spi_buf[0] = 0xBB;
spi_buf[1] = 0xCD;
spi_buf[2] = 0xEF;
// Set starting address
addr[0] = 0x05;
// Write 3 bytes starting at given address
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi1, (uint8_t *)&EEPROM_WRITE, 1, 100);
HAL_SPI_Transmit(&hspi1, (uint8_t *)&addr, 3, 100);
HAL_SPI_Transmit(&hspi1, (uint8_t *)spi_buf, 3, 100);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
// Clear buffer
spi_buf[0] = 0;
spi_buf[1] = 0;
spi_buf[2] = 0;
// Wait until WIP bit is cleared
wip = 1;
while (wip)
{
// Read status register
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi1, (uint8_t *)&EEPROM_RDSR, 1, 100);
HAL_SPI_Receive(&hspi1, (uint8_t *)spi_buf, 1, 100);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
// Mask out WIP bit
wip = spi_buf[0] & 0b00000001;
}
// Read the 3 bytes back
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi1, (uint8_t *)&EEPROM_READ, 1, 100);
HAL_SPI_Transmit(&hspi1, (uint8_t *)&addr, 3, 100);
HAL_SPI_Receive(&hspi1, (uint8_t *)spi_buf, 3, 100);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
// Read status register
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi1, (uint8_t *)&EEPROM_RDSR, 1, 100);
HAL_SPI_Receive(&hspi1, (uint8_t *)spi_buf, 1, 100);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);



https://www.mouser.de/datasheet/2/590/AT25SF321B-1635166.pdf
Here is an tutorial i used. The difference to my memory is the 24bit adress length.