Introduction
In this engaging tutorial, we continue to delve into how to store data using an I2C 32KB FRAM MB85RC256V with STM32 – Part 2, a powerful tool for the microcontroller community that can seamlessly store 32KB x 8 bits of data.
Our journey began with a straightforward example where we will guided you through the setup process, demonstrating how to write and read from this innovative memory device.
In this tutorial we delve into an innovative and practical technique for efficiently storing and retrieving blocks of data, utilizing structures and other C type-related methods. We will address an intriguing topic that has sparked discussion in various forums: the use of external storage while maintaining the ability of the compiler to generate addresses for global variables, with the actual data being stored externally.
This critical area of exploration will be thoroughly covered in our this post, empowering users to confidently navigate advanced data storage solutions in their projects. As we embark on this journey together, we encourage our community to engage, share insights, and collaboratively enhance our understanding of these essential concepts.
This tutorial is essentially the same as part 1 of the 2 part series, with once exception, how we address the memory in the I2C FRAM will be a little different, in that we are allowing the compiler to map out the memory address of the storage content for us.
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
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.

When working with I2C Devices it is import to ensure that the SCL and SDA lines are setup with physical pull-up resistors. Otherwise unexpected results and strange behavior could occur. Here is an article which pertains to I2C and Pull-Ups. Note, that on some STM32 MCU’s this can be done by setting the GPIO pins for the SCL/SDA lines to include pull-up resistors as parameters. Therefore, external resistors are not needed.
Schematic
Schematic to create the I2C 32KB FRAM MB85RC256V project

Schematic
Pinouts & Configurations






Code Listings for the I2C 32KB FRAM MB85RC256V project
Memory Definitions for STM32F439ZITX_FLASH.ld
On line 51 of this linker script we declare that there is an SRAM starting at address 0x60000000, and that it’s size if 32K Bytes. Although this is not really true, we are using this to allow the compiler to manage the address pointers of each variable location for us.
/*
******************************************************************************
**
** @file : LinkerScript.ld
**
** @author : Auto-generated by STM32CubeIDE
**
** Abstract : Linker script for NUCLEO-F439ZI Board embedding STM32F439ZITx Device from stm32f4 series
** 2048KBytes FLASH
** 64KBytes CCMRAM
** 192KBytes RAM
**
** Set heap size, stack size and stack location according
** to application requirements.
**
** Set memory bank area and size if external memory is used
**
** Target : STMicroelectronics STM32
**
** Distribution: The file is distributed as is, without any warranty
** of any kind.
**
******************************************************************************
** @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.
**
******************************************************************************
*/
/* Entry Point */
ENTRY(Reset_Handler)
/* Highest address of the user mode stack */
_estack = ORIGIN(RAM) + LENGTH(RAM); /* end of "RAM" Ram type memory */
_Min_Heap_Size = 0x200; /* required amount of heap */
_Min_Stack_Size = 0x400; /* required amount of stack */
/* Memories definition */
MEMORY
{
CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 64K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 2048K
SRAM (rw) : ORIGIN = 0x60000000, LENGTH = 32K
}
SRAM Section of STM32F439ZITX_FLASH.ld
In the following section of the linker script, we are telling the linker where to assign variables to memory locations base on the following line of code in main.cpp
#define EEPROM attribute((section(“.eeprom”)))
Any variable prefixed with EEPROM will request that the linker manage the location of these variables based up the directive on line 51 above.
On line 211, we indicate that this area of memory called .eeprom be located and managed at the starting address define on line 51 above call SRAM
_eeprom = LOADADDR(.eeprom);
/* external SRAM */
.eeprom :
{
. = ALIGN(4);
_seeprom = .; /* create a global symbol at data start */
*(.eeprom) /* .data sections */
*(.eeprom*) /* .data* sections */
. = ALIGN(4);
_eeeprom = .; /* define a global symbol at data end */
} > SRAM AT> FLASH
Full Listing of STM32F439ZITX_FLASH.ld
/*
******************************************************************************
**
** @file : LinkerScript.ld
**
** @author : Auto-generated by STM32CubeIDE
**
** Abstract : Linker script for NUCLEO-F439ZI Board embedding STM32F439ZITx Device from stm32f4 series
** 2048KBytes FLASH
** 64KBytes CCMRAM
** 192KBytes RAM
**
** Set heap size, stack size and stack location according
** to application requirements.
**
** Set memory bank area and size if external memory is used
**
** Target : STMicroelectronics STM32
**
** Distribution: The file is distributed as is, without any warranty
** of any kind.
**
******************************************************************************
** @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.
**
******************************************************************************
*/
/* Entry Point */
ENTRY(Reset_Handler)
/* Highest address of the user mode stack */
_estack = ORIGIN(RAM) + LENGTH(RAM); /* end of "RAM" Ram type memory */
_Min_Heap_Size = 0x200; /* required amount of heap */
_Min_Stack_Size = 0x400; /* required amount of stack */
/* Memories definition */
MEMORY
{
CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 64K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 2048K
SRAM (rw) : ORIGIN = 0x60000000, LENGTH = 32K
}
/* Sections */
SECTIONS
{
/* The startup code into "FLASH" Rom type memory */
.isr_vector :
{
. = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
} >FLASH
/* The program code and other data into "FLASH" Rom type memory */
.text :
{
. = ALIGN(4);
*(.text) /* .text sections (code) */
*(.text*) /* .text* sections (code) */
*(.glue_7) /* glue arm to thumb code */
*(.glue_7t) /* glue thumb to arm code */
*(.eh_frame)
KEEP (*(.init))
KEEP (*(.fini))
. = ALIGN(4);
_etext = .; /* define a global symbols at end of code */
} >FLASH
/* Constant data into "FLASH" Rom type memory */
.rodata :
{
. = ALIGN(4);
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
. = ALIGN(4);
} >FLASH
.ARM.extab (READONLY) : /* The "READONLY" keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */
{
. = ALIGN(4);
*(.ARM.extab* .gnu.linkonce.armextab.*)
. = ALIGN(4);
} >FLASH
.ARM (READONLY) : /* The "READONLY" keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */
{
. = ALIGN(4);
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
. = ALIGN(4);
} >FLASH
.preinit_array (READONLY) : /* The "READONLY" keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */
{
. = ALIGN(4);
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP (*(.preinit_array*))
PROVIDE_HIDDEN (__preinit_array_end = .);
. = ALIGN(4);
} >FLASH
.init_array (READONLY) : /* The "READONLY" keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */
{
. = ALIGN(4);
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array*))
PROVIDE_HIDDEN (__init_array_end = .);
. = ALIGN(4);
} >FLASH
.fini_array (READONLY) : /* The "READONLY" keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */
{
. = ALIGN(4);
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(SORT(.fini_array.*)))
KEEP (*(.fini_array*))
PROVIDE_HIDDEN (__fini_array_end = .);
. = ALIGN(4);
} >FLASH
/* Used by the startup to initialize data */
_sidata = LOADADDR(.data);
/* Initialized data sections into "RAM" Ram type memory */
.data :
{
. = ALIGN(4);
_sdata = .; /* create a global symbol at data start */
*(.data) /* .data sections */
*(.data*) /* .data* sections */
*(.RamFunc) /* .RamFunc sections */
*(.RamFunc*) /* .RamFunc* sections */
. = ALIGN(4);
_edata = .; /* define a global symbol at data end */
} >RAM AT> FLASH
_siccmram = LOADADDR(.ccmram);
/* CCM-RAM section
*
* IMPORTANT NOTE!
* If initialized variables will be placed in this section,
* the startup code needs to be modified to copy the init-values.
*/
.ccmram :
{
. = ALIGN(4);
_sccmram = .; /* create a global symbol at ccmram start */
*(.ccmram)
*(.ccmram*)
. = ALIGN(4);
_eccmram = .; /* create a global symbol at ccmram end */
} >CCMRAM AT> FLASH
/* Uninitialized data section into "RAM" Ram type memory */
. = ALIGN(4);
.bss :
{
/* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
*(.bss*)
*(COMMON)
. = ALIGN(4);
_ebss = .; /* define a global symbol at bss end */
__bss_end__ = _ebss;
} >RAM
/* User_heap_stack section, used to check that there is enough "RAM" Ram type memory left */
._user_heap_stack :
{
. = ALIGN(8);
PROVIDE ( end = . );
PROVIDE ( _end = . );
. = . + _Min_Heap_Size;
. = . + _Min_Stack_Size;
. = ALIGN(8);
} >RAM
_eeprom = LOADADDR(.eeprom);
/* external SRAM */
.eeprom :
{
. = ALIGN(4);
_seeprom = .; /* create a global symbol at data start */
*(.eeprom) /* .data sections */
*(.eeprom*) /* .data* sections */
. = ALIGN(4);
_eeeprom = .; /* define a global symbol at data end */
} > SRAM AT> FLASH
/* Remove information from the compiler libraries */
/DISCARD/ :
{
libc.a ( * )
libm.a ( * )
libgcc.a ( * )
}
.ARM.attributes 0 : { *(.ARM.attributes) }
}
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 "stdbool.h"
#include "mb85rc256v.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
typedef struct data {
bool flag;
char string[5];
int value;
} DATA_t;
typedef struct arraryTest {
int table[5];
} arrayTest_t;
typedef struct memory {
int test1;
char buf[256];
float myFloat;
arrayTest_t myArray;
int test2;
} FRAM_t;
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
// Any global variable prefixed with EEPROM will be compiled and linked to have a start address as denoted in the FLASH.id file
#define EEPROM __attribute__((section(".eeprom")))
/* 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 */
// The following variables have memory addresses tracked and generated by the compiler and have sudo-real memory addresses,
// but they are not actually real variables that can be access programmatically.
// They are used as a map to be written to and read from the MB85RC256 SPI FRAM memory.
// These variables that are mapped to the MB85RC256 SPI FRAM memory must be grouped together in this code for clarity.
EEPROM FRAM_t eepromFramStorageArea;
EEPROM DATA_t eepromDataStructStorageArea;
EEPROM char eepromBufferStorageArea[256];
// These are the real storage areas that the entire code can access as global variables.
FRAM_t realFramStorageArea;
DATA_t realDataStructStorageArea;
char realEEpromBufferStorageArea;
/* 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 variableTest() {
HAL_StatusTypeDef halStatus = HAL_OK;
char buf[128];
printf("Starting variableTest\r\n");
realFramStorageArea.test1 = 12;
realFramStorageArea.test2 = 200;
sprintf(realFramStorageArea.buf, "This is a test of the emergency broadcasting system. " \
"This is only a test.\r\nThe quick brown fox jumped easily over the fence to avoid the old gray hound.\r\n");
realFramStorageArea.myFloat = 3.1415926535;
realFramStorageArea.myArray.table[2] = 1222;
halStatus = MB85rc_WriteByte((uint32_t) &eepromFramStorageArea, (uint8_t *) &realFramStorageArea, sizeof(FRAM_t));
sprintf(buf, "Now is the time for all good men to come to the aid of their country!!!!\r\n");
realFramStorageArea.test1 = 0;
realFramStorageArea.test2 = 101;
realFramStorageArea.myFloat = 0.0;
strcpy(realFramStorageArea.buf, buf);
realFramStorageArea.myArray.table[2] = 0;
if (halStatus == HAL_OK) {
halStatus = MB85rc_ReadByte((uint32_t) &eepromFramStorageArea, (uint8_t *) &realFramStorageArea, sizeof(FRAM_t));
}
printf(realFramStorageArea.buf);
memset(&realDataStructStorageArea, 0x00, sizeof(realDataStructStorageArea));
realDataStructStorageArea.flag = true;
strcpy(realDataStructStorageArea.string, "6543");
realDataStructStorageArea.value = -215;
halStatus = MB85rc_WriteByte((uint32_t) &eepromDataStructStorageArea, (uint8_t *) &realDataStructStorageArea, sizeof(DATA_t));
memset(&realDataStructStorageArea, 0x00, sizeof(realDataStructStorageArea));
if (halStatus == HAL_OK) {
halStatus = MB85rc_ReadByte((uint32_t) &eepromDataStructStorageArea, (uint8_t *) &realDataStructStorageArea, sizeof(DATA_t));
}
printf("realDataStructStorageArea.flag = %s\r\n", realDataStructStorageArea.flag ? "true" : "false");
printf("realDataStructStorageArea.string = %s\r\n", realDataStructStorageArea.string);
printf("realDataStructStorageArea.value = %d\r\n", realDataStructStorageArea.value);
printf("Ending variableTest\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
printf("This is a test, a very big test!!!!!\r\n");
#endif
MB85rc_Init(&hi2c1, (uint32_t) &eepromFramStorageArea);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
variableTest();
printf("End of All 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 ---------------------------------------------*/
/* 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 for I2C 32KB FRAM MB85RC256V project
mb85rc256v.h
/*
* mb85rc256v.h
*
* Created on: Jul 29, 2024
* Author: johng
*/
#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
#define MB85rc_PAGE_SIZE 0x20 // Page size 32 bytes
#define MB85rc_PAGE_NUM 0x400 // Number of pages 1024
/* MB85rc External Function -------------------------------------------------*/
void MB85rc_Init(I2C_HandleTypeDef *hi2c, uint32_t);
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);
uint8_t MB85rc_EraseChip(void);
uint8_t MB85rc_FillPage(uint16_t Page, uint8_t Val);
HAL_StatusTypeDef MB85rc_ReadByte(uint32_t MemAddr, uint8_t *pData, uint16_t Len);
HAL_StatusTypeDef MB85rc_WriteByte(uint32_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"
#include "math.h"
I2C_HandleTypeDef *i2c;
uint32_t baseAddress;
void MB85rc_Init(I2C_HandleTypeDef *hi2c, uint32_t address) {
i2c = hi2c;
baseAddress = address;
}
/**
* @brief I2C Bus Write 16bit
* @retval Success = 0, Failed = 1
* @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;
do {
halStatus = HAL_I2C_IsDeviceReady(i2c, DevAddr, 5, 10);
if (halStatus != HAL_OK) {
HAL_Delay(10);
}
} while (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 Success = 0, Failed = 1
* @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)
{
if (HAL_I2C_Mem_Read(i2c, DevAddr, MemAddr, I2C_MEMADD_SIZE_16BIT, pData, Len, HAL_MAX_DELAY) != 0)
{
return 1;
}
return 0;
}
/**
* @brief Fill EEPROM page with selected value
* @retval Success = 0, Failed = 1
* @param Page Any page up to MB85rc_PAGE_NUM
* @param Value Value to filled
*/
uint8_t MB85rc_FillPage(uint16_t Page, uint8_t Value)
{
uint8_t Buffer[MB85rc_PAGE_SIZE];
memset(Buffer, Value, MB85rc_PAGE_SIZE);
uint16_t MemAddr = Page << (int)(log(MB85rc_PAGE_SIZE) / log(2));
MB85rc_Bus_Write(MB85rc_ADDRESS, MemAddr, Buffer, MB85rc_PAGE_SIZE);
HAL_Delay(5); // 5ms Write cycle delay
return 0;
}
/**
* @brief Erase EEPROM with value 0xFF
* @retval Success = 0, Failed = 1
*/
uint8_t MB85rc_EraseChip(void)
{
for (uint16_t i = 0; i < MB85rc_PAGE_NUM; i++)
{
if (MB85rc_FillPage(i, 0xFF) != 0)
{
return 1;
}
}
return 0;
}
/**
* @brief Sequential read from selected memory address with number of byte
* @retval Success = 0, Failed = 1
* @param MemAddr Memory address to start read from
* @param pData Pointer to data
* @param Len Number of byte to read
*/
uint8_t MB85rc_ReadByte(uint32_t MemAddr, uint8_t *pData, uint16_t Len)
{
HAL_StatusTypeDef halStatus = HAL_OK;
uint16_t memAddress = MemAddr - baseAddress;
halStatus = MB85rc_Bus_Read(MB85rc_ADDRESS, memAddress, pData, Len);
return halStatus;
}
/**
* @brief Write data into EEPROM
* @retval Success = 0, Failed = 1
* @param MemAddr Memory address to start write from
* @param pData Pointer to data
* @param Len Number of byte to write
*/
uint8_t MB85rc_WriteByte(uint32_t MemAddr, uint8_t *value, uint16_t Len)
{
HAL_StatusTypeDef halStatus = HAL_OK;
uint16_t memAddress = MemAddr - baseAddress;
if (halStatus == HAL_OK) {
/* Write the remaining data */
halStatus = MB85rc_Bus_Write(MB85rc_ADDRESS, memAddress, value, Len);
HAL_Delay(5); // 5ms Write cycle delay
}
return halStatus;
}
USART Output
Terminal emulator output window

Logic Analyzer Output


