cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f303re to write data in the st M95160w EEPROM and reading back the written data. but cannot write accurately. how to do that?

MAlam.2
Associate II

using stm32f303re to write data in the st M95160w EEPROM and reading back the written data. the problem is i am not getting back the accurate output.

the output should be like this:

status 0x02

11 22 33

status 0x00

but getting

status 0x02

00 ff ff

status 0x00

Here is the Code:

#include "main.h"

#include <stdio.h>

SPI_HandleTypeDef hspi1;

UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */

const uint8_t EEPROM_READ = 0b00000011;  //Read from Memory Array

const uint8_t EEPROM_WRITE = 0b00000010;  //Write to Memory Array

const uint8_t EEPROM_WRDI = 0b00000100;  //Write Disable

const uint8_t EEPROM_WREN = 0b00000110;  //Write Enable

const uint8_t EEPROM_RDSR = 0b00000101;  //Read Status Register

const uint8_t EEPROM_WRSR = 0b00000001;  //Write Status Register

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART2_UART_Init(void);

static void MX_SPI1_Init(void);

int main(void)

{

 char uart_buf[50];

 int uart_buf_len;

 char spi_buf[20];

 uint8_t addr;

 uint8_t wip;

 SystemClock_Config();

 MX_GPIO_Init();

 MX_USART2_UART_Init();

 MX_SPI1_Init();

  

// CS pin should default high

  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);

// Say something

  uart_buf_len = sprintf(uart_buf, "SPI Test\r\n");

  HAL_UART_Transmit(&huart2, (uint8_t *)uart_buf, uart_buf_len, 100);

// Enable write enable latch (allow write operations)

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

// Read status register

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

// Print out status register

  uart_buf_len = sprintf(uart_buf,"Status: 0x%02x\r\n",(unsigned int)spi_buf[0]);

  HAL_UART_Transmit(&huart2, (uint8_t *)uart_buf, uart_buf_len, 100);

// Test bytes to write to EEPROM

  spi_buf[0] = 0x11;

  spi_buf[1] = 0x22;

  spi_buf[2] = 0x33;

// Set starting address

  addr = 0x01;

// Write 3 bytes starting at a given address

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

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

   // Mask out WIP bit

   wip = spi_buf[0] & 0b00000001;

  }

// Read the 3 bytes back

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

// Print out bytes read

  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(&huart2, (uint8_t *)uart_buf, uart_buf_len, 100);

// Read status register

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

// Print out status register

  uart_buf_len = sprintf(uart_buf,"Status: 0x%02x\r\n",(unsigned int)spi_buf[0]);

  HAL_UART_Transmit(&huart2, (uint8_t *)uart_buf, uart_buf_len, 100);

 while (1)

 {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 }

  

}

static void MX_SPI1_Init(void)

{

/* USER CODE END SPI1_Init 1 */

 /* SPI1 parameter configuration*/

 hspi1.Instance = SPI1;

 hspi1.Init.Mode = SPI_MODE_MASTER;

 hspi1.Init.Direction = SPI_DIRECTION_2LINES;

 hspi1.Init.DataSize = SPI_DATASIZE_8BIT;

 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;

 hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;

 hspi1.Init.NSS = SPI_NSS_SOFT;

 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;

 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;

 hspi1.Init.TIMode = SPI_TIMODE_DISABLE;

 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

 hspi1.Init.CRCPolynomial = 10;

 hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;

 hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;

 if (HAL_SPI_Init(&hspi1) != HAL_OK)

 {

  Error_Handler();

 }

}

0 REPLIES 0