Overview
In this tutorial, you will learn how to use an STM32 I2C FRAM device (MB85RC256V 32KB) to store and retrieve data reliably. This guide walks through the hardware setup, STM32CubeMX configuration, and the code required to interface with the device over I2C.
You will also learn the fundamentals of working with external FRAM, including how to safely perform read and write operations, how memory addressing works, and how to ensure data integrity during communication. These concepts provide a solid foundation for integrating external memory devices into STM32-based embedded systems, such as storing sensor data, configuration parameters, or small application assets.
What You Will Learn
- How to connect an MB85RC256V 32KB I2C FRAM device to an STM32 NUCLEO-F439ZI.
- How to configure the STM32 I2C peripheral and verify that the FRAM responds on the bus.
- How to write a simple string to FRAM and read it back using a small device driver.
- How to validate the setup with serial terminal output and logic-analyzer captures.
Prerequisites
This tutorial assumes basic familiarity with STM32CubeIDE, embedded C, UART terminal output, and the fundamentals of I2C communication.
Materials List
- FTDI to USB
- NUCLEO-F439ZI
- MB85RC256V Memory IC 32KB Development Tools FRAM Breakout Board I2C Non-Volatile
- Breadboards Kit Include 2PCS 830 Point 2PCS 400 Point Solderless Breadboards
- Logic 8 – Saleae 8-Channel Logic Analyzer
Project Structure
This example is centered around the application entry files main.c and main.h, plus the dedicated FRAM driver files mb85rc256v.c and mb85rc256v.h. The application performs a simple write/read verification cycle while printing results over UART for easy confirmation.
Hardware Configuration / Pinouts
FTDI Pinouts
FTDI to USB Pinout from right to left
- Pin 1 – GND
- Pin 2 – CTS
- Pin 3 – VCC
- Pin 4 – TX
- Pin 5 – RX
- Pin 6 – DTR
- USB Mini – Connect to PC via USB cable
Make sure the jumper is set for 5v or 3v which ever is being used to power the FTDI device. If we look below at the schematic, it is showing in this case as 5v.


MB85RC256V Pinouts
MB85RC256V Memory IC 32KB FRAM
The Adafruit breakout board is based upon the MB85RC256V chip.

Schematic

Project Setup
Code Walkthrough
main.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include "mb85rc256v.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
#ifdef REDIRECT_PRINTF
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#endif
#ifdef REDIRECT_PRINTF
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART2 and Loop until the end of transmission */
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
#endif
void simpleTest() {
HAL_StatusTypeDef halStatus = HAL_OK;
int len = 0;
char rcvBuf[128];
char buffer[128];
printf("Starting simpleTest\r\n");
memset(rcvBuf, 0x00, sizeof(rcvBuf));
memset(buffer, 0x00, sizeof(buffer));
halStatus = HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t) MB85rc_ADDRESS, 5, 10);
if (halStatus == HAL_OK) {
sprintf(buffer, "Now is the time for all good men to come to the aid of their country!!!!\r\n");
len = strlen(buffer) + 1;
halStatus = MB85rc_WriteByte(0x00, (uint8_t *) &buffer, len);
if (halStatus != HAL_OK) {
Error_Handler();
}
} else {
Error_Handler();
}
if (halStatus == HAL_OK) {
halStatus = MB85rc_ReadByte(0x00, (uint8_t *) &rcvBuf, len);
if (halStatus != HAL_OK) {
Error_Handler();
}
}
printf(rcvBuf);
printf("Ending simpleTest\r\n");
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
#ifdef REDIRECT_PRINTF
printf("\x1b[2J\x1b[H"); // Clear the dumb terminal screen
#endif
MB85rc_Init(&hi2c1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
simpleTest();
printf("End of Tests\r\n");
fflush(stdout);
HAL_Delay(2000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 4;
RCC_OscInitStruct.PLL.PLLN = 180;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Activate the Over-Drive mode
*/
if (HAL_PWREx_EnableOverDrive() != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief I2C1 Initialization Function
* @param None
* @retval None
*/
static void MX_I2C1_Init(void)
{
/* USER CODE BEGIN I2C1_Init 0 */
/* USER CODE END I2C1_Init 0 */
/* USER CODE BEGIN I2C1_Init 1 */
/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 400000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
/** Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
Error_Handler();
}
/** Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C1_Init 2 */
/* USER CODE END I2C1_Init 2 */
}
/**
* @brief USART2 Initialization Function
* @param None
* @retval None
*/
static void MX_USART2_UART_Init(void)
{
/* USER CODE BEGIN USART2_Init 0 */
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 19200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
/* USER CODE END USART2_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
/**
* @brief This function is executed in case of error occurrence.
* @param file: The file name as string.
* @param line: The line in file as a number.
* @retval None
*/
void _Error_Handler(const char *file, int line)
{
/* USER CODE BEGIN Error_Handler_Debug */
__disable_irq();
#ifdef REDIRECT_PRINTF
char buf[80];
sprintf(buf, "Trapped in Error_Handler(). Called from: %s, line: %d\r\n", file, line);
printf(buf);
#endif
/* User can add his own implementation to report the HAL error return state */
while(1)
{
}
}
/* USER CODE END 4 */
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
main.h
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.h
* @brief : Header for main.c file.
* This file contains the common defines of the application.
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
/* USER CODE END ET */
/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
/* USER CODE END EC */
/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
/* USER CODE END EM */
/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);
/* USER CODE BEGIN EFP */
/* USER CODE END EFP */
/* Private defines -----------------------------------------------------------*/
/* USER CODE BEGIN Private defines */
void _Error_Handler(const char *, int);
#define Error_Handler() _Error_Handler((const char *)__FILE__, __LINE__)
#define REDIRECT_PRINTF
/* USER CODE END Private defines */
#ifdef __cplusplus
}
#endif
#endif /* __MAIN_H */
Device Driver
mb85rc256v.h
/*
* mb85rc256v.h
*
* Created on: Jul 29, 2024
* Author: jpgroulx
*/
#include <string.h>
#include "main.h"
#ifndef MB85RC256V_H_
#define MB85RC256V_H_
/* MB85rc Register ----------------------------------------------------------*/
#define MB85rc_ADDRESS (0x50<<1) // mb85rc256v default address
#define MB85rc_EEPROM_SIZE 0x8000 // EEPROM Size 32768 bytes
/* MB85rc External Function -------------------------------------------------*/
void MB85rc_Init(I2C_HandleTypeDef *hi2c);
HAL_StatusTypeDef MB85rc_Bus_Write(uint16_t DevAddr, uint16_t MemAddr, uint8_t *pData, uint16_t Len);
HAL_StatusTypeDef MB85rc_Bus_Read(uint16_t DevAddr, uint16_t MemAddr, uint8_t *pData, uint16_t Len);
HAL_StatusTypeDef MB85rc_ReadByte(uint16_t MemAddr, uint8_t *pData, uint16_t Len);
HAL_StatusTypeDef MB85rc_WriteByte(uint16_t MemAddr, uint8_t *value, uint16_t Len);
#ifdef __cplusplus
}
#endif
#endif /* MB85RC256V_H_ */
mb85rc256v.c
/*
* mb85rc256v.c
*
* Created on: Jul 29, 2024
* Author: johng
*/
#include "mb85rc256v.h"
#include "stdio.h"
I2C_HandleTypeDef *i2c;
/**
* @brief Initialize local I2C handle
*/
void MB85rc_Init(I2C_HandleTypeDef *hi2c) {
i2c = hi2c;
}
/**
* @brief I2C Bus Write 16bit
* @retval HAL_StatusTypeDef
* @param DevAddr Target device address
* @param memAddr Internal memory address
* @param pData Pointer to data buffer
* @param Len Amount of data to be Write
*/
HAL_StatusTypeDef MB85rc_Bus_Write(uint16_t DevAddr, uint16_t memAddr, uint8_t *pData, uint16_t Len)
{
HAL_StatusTypeDef halStatus = HAL_OK;
halStatus = HAL_I2C_Mem_Write(i2c, DevAddr, memAddr, I2C_MEMADD_SIZE_16BIT, pData, Len, HAL_MAX_DELAY);
return halStatus;
}
/**
* @brief I2C Bus Read 16bits
* @retval HAL_StatusTypeDef
* @param DevAddr Target device address
* @param memAddr Internal memory address
* @param pData Pointer to data buffer
* @param Len Amount of data to be read
*/
HAL_StatusTypeDef MB85rc_Bus_Read(uint16_t DevAddr, uint16_t memAddr, uint8_t *pData, uint16_t Len)
{
HAL_StatusTypeDef halStatus = HAL_OK;
halStatus = HAL_I2C_Mem_Read(i2c, DevAddr, memAddr, I2C_MEMADD_SIZE_16BIT, pData, Len, HAL_MAX_DELAY);
return halStatus;
}
/**
* @brief Sequential read from selected memory address with number of byte
* @retval HAL_StatusTypeDef
* @param memAddr Memory address to start read from
* @param pData Pointer to data
* @param Len Number of byte to read
*/
uint8_t MB85rc_ReadByte(uint16_t memAddr, uint8_t *pData, uint16_t Len)
{
HAL_StatusTypeDef halStatus = HAL_OK;
halStatus = MB85rc_Bus_Read(MB85rc_ADDRESS, memAddr, pData, Len);
return halStatus;
}
/**
* @brief Write data into EEPROM
* @retval HAL_StatusTypeDef
* @param memAddr Memory address to start write from
* @param pData Pointer to data
* @param Len Number of byte to write
*/
uint8_t MB85rc_WriteByte(uint16_t memAddr, uint8_t *value, uint16_t Len)
{
HAL_StatusTypeDef halStatus = HAL_OK;
halStatus = MB85rc_Bus_Write(MB85rc_ADDRESS, memAddr, value, Len);
return halStatus;
}
Results / Verification
USART Output
Terminal emulator output window

Logic Analyzer Output





