2022-07-12 12:25 PM
// 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.
2022-07-12 12:43 PM
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.
2022-07-13 12:32 AM
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.