With an STM32 bit microcontroller, I am working on an EEPROM interface. I'm using STMCUBEIDE and proteus software to simulate hardware for the same purpose. The following is my code:
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
uart_buf_len = sprintf(uart_buf, "SPI Test Begin\r\n");
HAL_UART_Transmit(&huart1, (uint8_t *)uart_buf, uart_buf_len, 100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi1, (uint8_t *)&EEPROM_WREN, 1, 100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
spi_buf[0] = 0x01;
spi_buf[1] = 0x02;
spi_buf[2] = 0x03;
addr = 0x05;
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi1, (uint8_t *)&EEPROM_WRITE, 1, 100);
HAL_SPI_Transmit(&hspi1, (uint8_t *)&addr, 1, 100);
HAL_SPI_Transmit(&hspi1, (uint8_t *)spi_buf, 3, 100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
spi_buf[0] = 0;
spi_buf[1] = 0;
spi_buf[2] = 0;
wip = 1;
while (wip)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, 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(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
wip = spi_buf[0] & 0b00000001;
}
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi1, (uint8_t *)&EEPROM_READ, 1, 100);
HAL_SPI_Transmit(&hspi1, (uint8_t *)&addr, 1, 100);
HAL_SPI_Receive(&hspi1, (uint8_t *)spi_buf, 3, 100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
uart_buf_len = sprintf(uart_buf,
"0x%02x 0x%02x 0x%02x\r\n",
(unsigned int)spi_buf[0],
(unsigned int)spi_buf[1],
(unsigned int)spi_buf[2]);
HAL_UART_Transmit(&huart1, (uint8_t *)uart_buf, uart_buf_len, 100);
I am reusing code given in https://www.digikey.in/en/maker/projects/getting-started-with-stm32-how-to-use-spi/09eab3dfe74c4d0391aaaa99b0a8ee17
I am facing following issue:
If I start at address 0x05 and write three bytes with the data sequence 01, 02, 03, and then retrieve the data from that address, I get 02, 03, and ff. However, if I read data from address 0x04, I can read the exact sequence that I just wrote in the writing portion.