Skip to main content
Donkanaille
Associate
July 12, 2022
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.

  • July 12, 2022
  • 2 replies
  • 1409 views

// 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);

0693W00000QKzvlQAD.png0693W00000QKzwAQAT.png0693W00000QKzw5QAD.png0693W00000QKzsUQAT.png0693W00000QKzm6QAD.pnghttps://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.

https://www.digikey.com/en/maker/projects/getting-started-with-stm32-how-to-use-spi/09eab3dfe74c4d0391aaaa99b0a8ee17

This topic has been closed for replies.

2 replies

Tesla DeLorean
Guru
July 12, 2022

Probably need to make sure the transmit actually completes before releasing CS

Use the TransmitReceive form, push out dummy bytes where you need clocks, and discarding returned data where it's purely an output.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Donkanaille
Associate
July 13, 2022

i inserted some HAL_Delay to wait after transmitting but this doesn´t helped.

Where is the difference between TransmitReceive and single use of Transmit and Receive?

The weird thing is it worked a few month ago. But the chip should be okay, because i can read the manufactor Id. Perhaps i accidentally changed some settings or its a timing problem.

The 24 bit used for the adress should be alright too. I first used the example vom digikey, but he used 8 bit for the adress what didnt work.