How to use EEPROM M24256 with STM32 microcontroller

H

In this post we will see how we can use the M24256 EEPROM to read and write data with an STM32 microcontroller. The complexity of those operations are really minimal as long as the provided HAL libraries generated by STM32CubeIDE basically does most of this job.

Our main idea is to safely read and write data to M23256 by considering the delays that this chip may have and ensuring our operations. For that reason each time we will try to use the EEPROM, our simple algorithm will check the operational status of the chip.

Below is the source code:

/*!
	@file   drv_eeprom.h
	@brief  <brief description here>
	@t.odo	-
	---------------------------------------------------------------------------

	MIT License
	Copyright (c) 2020 Io. D (Devcoons.com)

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
*/
/******************************************************************************
* Preprocessor Definitions & Macros
******************************************************************************/

#ifndef DRIVERS_INC_DRV_EEPROM_H_
	#define DRIVERS_INC_DRV_EEPROM_H_

/******************************************************************************
* Includes
******************************************************************************/

	#include "definitions.h"
	#include "i2c.h"

/******************************************************************************
* Enumerations, structures & Variables
******************************************************************************/

	typedef enum
	{
		EEPROM0 = 0xA0U,
		EEPROM1 = 0xA2U
	}i_eeprom_address;

	typedef enum
	{
		I_EEPROM_OK = 0x00,
		I_EEPROM_BUSY = 0x10,
		I_EEPROM_BUSY_RX = 0x11,
		I_EEPROM_BUSY_TX = 0x12,
		I_EEPROM_ERROR_RST = 0xFB,
		I_EEPROM_INVALID = 0xFC,
		I_EEPROM_TIMEOUT = 0xFD,
		I_EEPROM_ERROR = 0xFE,
		I_EEPROM_UNKNOWN = 0xFF
	}i_status_eeprom;

/******************************************************************************
* Declaration | Public Functions
******************************************************************************/

	i_status_eeprom eeprom_read(i_eeprom_address eeprom,  uint32_t address, uint8_t* data, uint32_t data_size);
	i_status_eeprom eeprom_write(i_eeprom_address eeprom, uint32_t address, uint8_t* data, uint32_t data_size);
	i_status_eeprom eeprom_status();

/******************************************************************************
* EOF - NO CODE AFTER THIS LINE
******************************************************************************/
#endif
/*!
	@file   drv_eeprom.c
	@brief  <brief description here>
	@t.odo	-
	---------------------------------------------------------------------------

	MIT License
	Copyright (c) 2020 Io. D (Devcoons.com)

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
*/
/******************************************************************************
* Preprocessor Definitions & Macros
******************************************************************************/

/******************************************************************************
* Includes
******************************************************************************/

#include "drv_eeprom.h"

/******************************************************************************
* Enumerations, structures & Variables
******************************************************************************/

	static volatile i_status_eeprom status;

/******************************************************************************
* Declaration | Static Functions
******************************************************************************/

/******************************************************************************
* Definition  | Static Functions
******************************************************************************/

/******************************************************************************
* Definition  | Public Functions
******************************************************************************/

	i_status_eeprom eeprom_read(i_eeprom_address eeprom,  uint32_t address, uint8_t* data, uint32_t data_size)
	{
		if(status != I_EEPROM_OK )
			return I_EEPROM_ERROR;

		status = I_EEPROM_BUSY;
		if(HAL_I2C_Mem_Read_IT(&hi2c1, eeprom, address, 2, data, data_size) != HAL_OK)
			status = I_EEPROM_ERROR;

		return status;
	}

	i_status_eeprom eeprom_write(i_eeprom_address eeprom, uint32_t address, uint8_t* data, uint32_t data_size)
	{
		if(status != I_EEPROM_OK)
			return I_EEPROM_ERROR;

		status = I_EEPROM_BUSY;
		if(HAL_I2C_Mem_Write_IT(&hi2c1, eeprom , address, 2, data, data_size) != HAL_OK)
			status = I_EEPROM_ERROR;

		return status;
	}

	i_status_eeprom eeprom_status()
	{
		if(HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)
			return I_EEPROM_BUSY;

		if(status == I_EEPROM_BUSY_TX)
		{
			if(HAL_I2C_IsDeviceReady(&hi2c1, EEPROM0, 3, 25) !=HAL_OK)
				return I_EEPROM_BUSY;
			if(HAL_I2C_IsDeviceReady(&hi2c1, EEPROM1 , 3, 25)!=HAL_OK)
				return I_EEPROM_BUSY;
			status = I_EEPROM_OK;
		}

		return status;
	}


	void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
	{
		status = I_EEPROM_BUSY_TX;
	}

	void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
	{
		status = I_EEPROM_OK;
	}

	void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
	{
		status =I_EEPROM_ERROR_RST;
	}

/******************************************************************************
* EOF - NO CODE AFTER THIS LINE
******************************************************************************/
/*
 * TESTED WITH THE FOLLOWING EXAMPLE
 */
	uint8_t write_data1[] = {0x56,0x02,0x73,0x14};
	uint8_t write_data2[] = {0x29,0x02,0x73,0x14};
	uint8_t read_data1[4];
	uint8_t read_data2[4];
	uint8_t read_data3[4];
	eeprom_read(EEPROM1, 0, read_data1, 4);
	while(eeprom_status()!= I_EEPROM_OK)
		HAL_Delay(1);
	eeprom_write(EEPROM1, 0, write_data1, 4);
	while(eeprom_status()!= I_EEPROM_OK)
		HAL_Delay(1);
	eeprom_read(EEPROM1, 0, read_data2, 4);
	while(eeprom_status()!= I_EEPROM_OK)
		HAL_Delay(1);
	eeprom_write(EEPROM1, 0, write_data2, 4);
	while(eeprom_status()!= I_EEPROM_OK)
		HAL_Delay(1);
	eeprom_read(EEPROM1, 0, read_data3, 4);
	while(eeprom_status()!= I_EEPROM_OK)
		HAL_Delay(1);

Disclaimer: The present content may not be used for training artificial intelligence or machine learning algorithms. All other uses, including search, entertainment, and commercial use, are permitted.

Categories

Tags