STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA

这篇具有很好参考价值的文章主要介绍了STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA

在较低容量存储领域,EEPROM是常用的存储介质,可以通过直接或者文件操作方式进行读写。不同容量的EEPROM的地址对应位数不同,在发送字节的格式上有所区别。EEPROM是非快速访问存储,因为EEPROM按页进行组织,在连续操作模式,当跨页时访问地址不是跳到下一页到开始,而是跳到当前页的首地址,因此跨页时要重新指定起始地址。而在控制端发送写操作I2C数据后还需要有等待EEPROM内部操作完成的时间才能进行下一次操作。ZD24C1MA是1M bit / 128K Byte容量的EEPROM,ZD24C1MA的管脚定义为:
STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C
这里介绍STM32 通过文件系统FATS访问EEPROM ZD24C1MA的例程。采用STM32CUBEIDE开发平台,以STM32F401CCU6芯片为例,通过STM32 I2C硬件电路实现读写操作,通过UART串口进行控制。

STM32工程配置

首先建立基本工程并设置时钟:
STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C
STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C
STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C
配置硬件I2C接口,STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C
STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C
配置UART1作为通讯串口:
STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C
STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C
对FATS文件系统进行配置:
STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C
STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C
STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C
STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C
保存并生成初始工程代码:
STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C

STM32工程代码

代码里用到的微秒延时函数参考: STM32 HAL us delay(微秒延时)的指令延时实现方式及优化

ZD24C1MA的设备默认访问地址为0xA0, ZD24C1MA的存储单元地址访问略为特殊,17位地址分为两部分,最高位的1位放置于I2C设备默认访问地址的第1位,I2C设备默认访问地址第0位仍然为读写控制位,由于采用硬件I2C控制,库函数自行通过识别调用的是发送还是接收函数对第0位进行发送前设置,因此,不管是调用库函数的I2C写操作还是读操作,提供的地址相同。17位地址的低16位通过在发送设备地址后的作为跟随的第一,二个字节发送。

建立ZD24C1MA.h库头文件

#ifndef INC_ZD24C1MA_H_
#define INC_ZD24C1MA_H_

#include "main.h"

void PY_Delay_us_t(uint32_t Delay);
void ZD24C1MA_Read(uint32_t addr, uint8_t * data, uint32_t len);
void ZD24C1MA_Write(uint32_t addr, uint8_t * data, uint32_t len);

#endif

建立ZD24C1MA.c库源文件:


#include <string.h>
#include <ZD24C1MA.h>

#define Page_Size 256
#define Delay_Param 5
extern I2C_HandleTypeDef hi2c1;
extern uint8_t ZD24C1MA_Default_I2C_Addr ;


void ZD24C1MA_Read(uint32_t addr, uint8_t * data, uint32_t len)
{
	uint8_t ZD24C1MA_I2C_Addr;

	ZD24C1MA_I2C_Addr = ZD24C1MA_Default_I2C_Addr | ((addr>>16)<<1); //highest 1-bit access address placed into I2C address

	uint8_t RA[2];
	RA[0] = (addr & 0xFF00)>>8; //high 8-bit access address placed into I2C first data
	RA[1] =addr & 0x00FF; //low 8-bit access address placed into I2C first data

	HAL_I2C_Master_Transmit(&hi2c1, ZD24C1MA_I2C_Addr, &RA[0], 2, 2700); //Write address for read
	HAL_I2C_Master_Receive(&hi2c1, ZD24C1MA_I2C_Addr, data, len, 2700); //Read data

}

void ZD24C1MA_Write(uint32_t addr, uint8_t * data, uint32_t len)
{

	uint8_t ZD24C1MA_I2C_Addr;

	uint32_t addr_page = addr/Page_Size;
	uint32_t addr_index = addr%Page_Size;
	uint32_t TLEN;
    uint8_t TAD[Page_Size+2];
    uint32_t i=0;

    if(len<=(Page_Size-addr_index))
    {
    	TAD[0] = (addr & 0xFF00) >> 8;
    	TAD[1] = addr & 0x00FF ;
    	memcpy(TAD+2, data, len);

    	ZD24C1MA_I2C_Addr = ZD24C1MA_Default_I2C_Addr | ((addr>>16)<<1); //highest 1-bit access address placed into I2C address
    	HAL_I2C_Master_Transmit(&hi2c1, ZD24C1MA_I2C_Addr, TAD, len+2, 2700);  //Write data
    	PY_Delay_us_t(Delay_Param*1000);
    }
    else
    {
    	TAD[0] = (addr & 0xFF00) >> 8;
    	TAD[1] = addr & 0x00FF ;
    	memcpy(TAD+2, data, (Page_Size-addr_index));

    	ZD24C1MA_I2C_Addr = ZD24C1MA_Default_I2C_Addr | ((addr>>16)<<1); //highest 1-bit access address placed into I2C address
    	HAL_I2C_Master_Transmit(&hi2c1, ZD24C1MA_I2C_Addr, TAD, (Page_Size-addr_index)+2, 2700);  //Write data
    	PY_Delay_us_t(Delay_Param*1000);

    	TLEN = (len-(Page_Size-addr_index));
    	while( TLEN >= Page_Size )
    	{
    		addr_page += 1;

        	TAD[0] = ((addr_page*Page_Size) & 0xFF00 ) >> 8;
        	TAD[1] = (addr_page*Page_Size) & 0x00FF ;
        	memcpy(TAD+2, data + (Page_Size-addr_index) + i*Page_Size, Page_Size);

        	ZD24C1MA_I2C_Addr = ZD24C1MA_Default_I2C_Addr | (((addr_page*Page_Size)>>16)<<1); //highest 1-bit access address placed into I2C address
        	HAL_I2C_Master_Transmit(&hi2c1, ZD24C1MA_I2C_Addr, TAD, Page_Size+2, 2700);  //Write data
        	HAL_Delay(Delay_Param);

        	i++;
        	TLEN -= Page_Size;
        	PY_Delay_us_t(Delay_Param*1000);
    	}

    	if(TLEN>0)
    	{
    		addr_page += 1;

        	TAD[0] = ((addr_page*Page_Size) & 0xFF00 ) >> 8;
        	TAD[1] = (addr_page*Page_Size) & 0x00FF ;
        	memcpy(TAD+2, data + (Page_Size-addr_index) + i*Page_Size, TLEN);

        	ZD24C1MA_I2C_Addr = ZD24C1MA_Default_I2C_Addr | (((addr_page*Page_Size)>>16)<<1); //highest 1-bit access address placed into I2C address
        	HAL_I2C_Master_Transmit(&hi2c1, ZD24C1MA_I2C_Addr, TAD, TLEN+2, 2700);  //Write data
        	PY_Delay_us_t(Delay_Param*1000);
    	}


    }

}


对ffconf.h添加包含信息:
STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C

#include "main.h"
#include "stm32f4xx_hal.h"
#include "ZD24C1MA.h"

修改user_diskio.c,对文件操作函数与底层I2C读写提供连接:

/* USER CODE BEGIN Header */
/**
 ******************************************************************************
  * @file    user_diskio.c
  * @brief   This file includes a diskio driver skeleton to be completed by the user.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 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 */

#ifdef USE_OBSOLETE_USER_CODE_SECTION_0
/*
 * Warning: the user section 0 is no more in use (starting from CubeMx version 4.16.0)
 * To be suppressed in the future.
 * Kept to ensure backward compatibility with previous CubeMx versions when
 * migrating projects.
 * User code previously added there should be copied in the new user sections before
 * the section contents can be deleted.
 */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
#endif

/* USER CODE BEGIN DECL */

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "ff_gen_drv.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/
/* Disk status */
static volatile DSTATUS Stat = STA_NOINIT;

/* USER CODE END DECL */

/* Private function prototypes -----------------------------------------------*/
DSTATUS USER_initialize (BYTE pdrv);
DSTATUS USER_status (BYTE pdrv);
DRESULT USER_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count);
#if _USE_WRITE == 1
  DRESULT USER_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count);
#endif /* _USE_WRITE == 1 */
#if _USE_IOCTL == 1
  DRESULT USER_ioctl (BYTE pdrv, BYTE cmd, void *buff);
#endif /* _USE_IOCTL == 1 */

Diskio_drvTypeDef  USER_Driver =
{
  USER_initialize,
  USER_status,
  USER_read,
#if  _USE_WRITE
  USER_write,
#endif  /* _USE_WRITE == 1 */
#if  _USE_IOCTL == 1
  USER_ioctl,
#endif /* _USE_IOCTL == 1 */
};

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Initializes a Drive
  * @param  pdrv: Physical drive number (0..)
  * @retval DSTATUS: Operation status
  */
DSTATUS USER_initialize (
	BYTE pdrv           /* Physical drive nmuber to identify the drive */
)
{
  /* USER CODE BEGIN INIT */
	/**************************SELF DEFINITION PART************/
	 extern uint8_t ZD24C1MA_Default_I2C_Addr ;
	 ZD24C1MA_Default_I2C_Addr =  0xA0; //Pin A2=A1=0
     return RES_OK;
	/**********************************************************/
	/*
    Stat = STA_NOINIT;
    return Stat;
    */
  /* USER CODE END INIT */
}

/**
  * @brief  Gets Disk Status
  * @param  pdrv: Physical drive number (0..)
  * @retval DSTATUS: Operation status
  */
DSTATUS USER_status (
	BYTE pdrv       /* Physical drive number to identify the drive */
)
{
  /* USER CODE BEGIN STATUS */
	/**************************SELF DEFINITION PART************/
		switch (pdrv)
			{
				case 0 :
					return RES_OK;
				case 1 :
					return RES_OK;
				case 2 :
					return RES_OK;
				default:
					return STA_NOINIT;
			}
	/**********************************************************/
    /*
    Stat = STA_NOINIT;
    return Stat;
    */
  /* USER CODE END STATUS */
}

/**
  * @brief  Reads Sector(s)
  * @param  pdrv: Physical drive number (0..)
  * @param  *buff: Data buffer to store read data
  * @param  sector: Sector address (LBA)
  * @param  count: Number of sectors to read (1..128)
  * @retval DRESULT: Operation result
  */
DRESULT USER_read (
	BYTE pdrv,      /* Physical drive nmuber to identify the drive */
	BYTE *buff,     /* Data buffer to store read data */
	DWORD sector,   /* Sector address in LBA */
	UINT count      /* Number of sectors to read */
)
{
  /* USER CODE BEGIN READ */
	/**************************SELF DEFINITION PART************/
		    uint16_t len;
			if( !count )
			{
				return RES_PARERR;  /*count status*/
			}
			switch (pdrv)
			{
				case 0:
					sector <<= 9; //Convert sector number to byte address
				    len = count*512;
				    ZD24C1MA_Read(sector, buff, len);
				    return RES_OK;
				default:
					return RES_ERROR;
			}
	/**********************************************************/
	/*
    return RES_OK;
    */
  /* USER CODE END READ */
}

/**
  * @brief  Writes Sector(s)
  * @param  pdrv: Physical drive number (0..)
  * @param  *buff: Data to be written
  * @param  sector: Sector address (LBA)
  * @param  count: Number of sectors to write (1..128)
  * @retval DRESULT: Operation result
  */
#if _USE_WRITE == 1
DRESULT USER_write (
	BYTE pdrv,          /* Physical drive nmuber to identify the drive */
	const BYTE *buff,   /* Data to be written */
	DWORD sector,       /* Sector address in LBA */
	UINT count          /* Number of sectors to write */
)
{
  /* USER CODE BEGIN WRITE */
  /* USER CODE HERE */
	/**************************SELF DEFINITION PART************/
		    uint16_t len;
			if( !count )
			{
				return RES_PARERR;  /*count status*/
			}
			switch (pdrv)
			{
				case 0:
					sector <<= 9; //Convert sector number to byte address
				    len = count*512;
				    ZD24C1MA_Write(sector, (uint8_t *)buff,len);
				    return RES_OK;
				default:
					return RES_ERROR;
			}
	/*********************************************************/

	/*
    return RES_OK;
    */
  /* USER CODE END WRITE */
}
#endif /* _USE_WRITE == 1 */

/**
  * @brief  I/O control operation
  * @param  pdrv: Physical drive number (0..)
  * @param  cmd: Control code
  * @param  *buff: Buffer to send/receive control data
  * @retval DRESULT: Operation result
  */
#if _USE_IOCTL == 1
DRESULT USER_ioctl (
	BYTE pdrv,      /* Physical drive nmuber (0..) */
	BYTE cmd,       /* Control code */
	void *buff      /* Buffer to send/receive control data */
)
{
  /* USER CODE BEGIN IOCTL */
	/**************************SELF DEFINITION PART************/
             #define user_sector_byte_size 512
		     DRESULT res;
			 switch(cmd)
			    {
				    case CTRL_SYNC:
								res=RES_OK;
				        break;
				    case GET_SECTOR_SIZE:
				        *(WORD*)buff = user_sector_byte_size;
				        res = RES_OK;
				        break;
				    case GET_BLOCK_SIZE:
				        *(WORD*)buff = 4096/user_sector_byte_size;
				        res = RES_OK;
				        break;
				    case GET_SECTOR_COUNT:
				    	*(DWORD*)buff = (128*1024/512);
				        res = RES_OK;
				        break;
				    default:
				        res = RES_PARERR;
				        break;
			    }
				return res;
	/**********************************************************/
	/*
    DRESULT res = RES_ERROR;
    return res;
    */
  /* USER CODE END IOCTL */
}
#endif /* _USE_IOCTL == 1 */

然后在main.c里根据串口输入命令(16进制单字节)实现如下功能:
0x01. 读取EEPROM ID
0x02. 装载FATS文件系统
0x03: 创建/打开文件并从头位置写入数据
0x04: 打开文件并从头位置读入数据
0x05: 创建/打开文件并从特定位置写入数据
0x06: 打开文件并从特定位置读入数据
完整的代码实现如下:

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 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.
  *
  ******************************************************************************
  */
//Written by Pegasus Yu in 2023
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "fatfs.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "usart.h"
#include "string.h"
#include "ZD24C1MA.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
__IO float usDelayBase;
void PY_usDelayTest(void)
{
  __IO uint32_t firstms, secondms;
  __IO uint32_t counter = 0;

  firstms = HAL_GetTick()+1;
  secondms = firstms+1;

  while(uwTick!=firstms) ;

  while(uwTick!=secondms) counter++;

  usDelayBase = ((float)counter)/1000;
}

void PY_Delay_us_t(uint32_t Delay)
{
  __IO uint32_t delayReg;
  __IO uint32_t usNum = (uint32_t)(Delay*usDelayBase);

  delayReg = 0;
  while(delayReg!=usNum) delayReg++;
}

void PY_usDelayOptimize(void)
{
  __IO uint32_t firstms, secondms;
  __IO float coe = 1.0;

  firstms = HAL_GetTick();
  PY_Delay_us_t(1000000) ;
  secondms = HAL_GetTick();

  coe = ((float)1000)/(secondms-firstms);
  usDelayBase = coe*usDelayBase;
}

void PY_Delay_us(uint32_t Delay)
{
  __IO uint32_t delayReg;

  __IO uint32_t msNum = Delay/1000;
  __IO uint32_t usNum = (uint32_t)((Delay%1000)*usDelayBase);

  if(msNum>0) HAL_Delay(msNum);

  delayReg = 0;
  while(delayReg!=usNum) delayReg++;
}
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
DMA_HandleTypeDef hdma_i2c1_tx;

UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_I2C1_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t cmd=0;          //for status control
uint8_t URX;

uint8_t ZD24C1MA_Default_I2C_Addr =  0xA0; //Pin A2=A1=0
uint32_t ZD24C1MA_Access_Addr = 0;   //EEPROM ZD24C1MA access address (17-bit)

uint8_t EEPROM_mount_status = 0; //EEPROM fats mount status indication (0: unmount; 1: mount)
uint8_t FATS_Buff[_MAX_SS]; //Buffer for f_mkfs() operation

FRESULT retEEPROM;
FIL file;
FATFS *fs;

UINT bytesread;
UINT byteswritten;
uint8_t rBuffer[20];      //Buffer for read
uint8_t WBuffer[20] ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; //Buffer for write

#define user_sector_byte_size 512
uint8_t eeprombuffer[user_sector_byte_size];

extern char USERPath[4];

char * console;
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
	EEPROM_mount_status = 0;
	uint32_t EEPROM_Read_Size;

    extern char USERPath[4];

    char * dpath = "0:"; //Disk Path
	for(uint8_t i=0; i<4; i++)
	{
		USERPath[i] = *(dpath+i);
	}

	const TCHAR* filepath = "0:test.txt";

	char cchar[256];
	console = cchar;

  /* 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_DMA_Init();
  MX_I2C1_Init();
  MX_USART1_UART_Init();
  MX_FATFS_Init();
  /* USER CODE BEGIN 2 */
  PY_usDelayTest();
  PY_usDelayOptimize();

  HAL_UART_Receive_IT(&huart1, &URX, 1);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	     if(cmd==1) //Read ID
	     {
	    	 cmd = 0;
	    	 printf("EEPROM ID=ZD24C1MAT\r\n\r\n");

	     }
	     else if(cmd==2) //EEPROM File System Mount
	     {
	    	 cmd = 0;

	    	 retEEPROM=f_mount(&USERFatFS, (TCHAR const*)USERPath, 1);
	    	    		 if (retEEPROM != FR_OK)
	    	    		 {
	    	  	    	   printf("File system mount failure: %d\r\n", retEEPROM);

	    	    		   if(retEEPROM==FR_NO_FILESYSTEM)
	    	    		   {
	    	    		       printf("No file system. Now to format......\r\n");

	    	    			   retEEPROM = f_mkfs((TCHAR const*)USERPath, FM_FAT, 1024, FATS_Buff, sizeof(FATS_Buff)); //EEPROM formatting
	    	    			   if(retEEPROM == FR_OK)
	    	    			   {
	    	         	    	  printf("EEPROM formatting success!\r\n");
	    	    			   }
	    	    				else
	    	    			   {
	    	    			      printf("EEPROM formatting failure!\r\n");
	    	    			   }

	    	    		   }
	    	    		 }
	    	    		 else
	    	    		 {
	    	    			 EEPROM_mount_status = 1;
	    	    	    	 printf("File system mount success\r\n");
	    	    		 }
	     }

		 else if(cmd==3) //File creation and write
		 {
				  cmd = 0;

				  if(EEPROM_mount_status==0)
				  {
				    	 printf( "\r\nEEPROM File system not mounted: %d\r\n",retEEPROM);
				  }
				  else
				  {
						retEEPROM = f_open( &file, filepath, FA_CREATE_ALWAYS | FA_WRITE );  //Open or create file
						if(retEEPROM == FR_OK)
						{
					    	printf( "\r\nFile open or creation successful\r\n");

							retEEPROM = f_write( &file, (const void *)WBuffer, sizeof(WBuffer), &byteswritten); //Write data

							if(retEEPROM == FR_OK)
							{
						    	 printf("\r\nFile write successful\r\n");
							}
							else
							{
						    	 printf("\r\nFile write error: %d\r\n",retEEPROM);
							}

							f_close(&file);   //Close file
						}
						else
						{
					    	 printf("\r\nFile open or creation error %d\r\n",retEEPROM);
						}
				   }

	    }

	    else if(cmd==4) //File read
	    {
				  cmd = 0;

				  if(EEPROM_mount_status==0)
				  {
				    	 printf("\r\nEEPROM File system not mounted: %d\r\n",retEEPROM);
				  }
				  else
				  {
						retEEPROM = f_open( &file, filepath, FA_OPEN_EXISTING | FA_READ); //Open file
						if(retEEPROM == FR_OK)
						{
					    	printf("\r\nFile open successful\r\n");

							retEEPROM = f_read( &file, (void *)rBuffer, sizeof(rBuffer), &bytesread); //Read data

							if(retEEPROM == FR_OK)
							{
						    	printf("\r\nFile read successful\r\n");

								PY_Delay_us_t(200000);

								EEPROM_Read_Size = sizeof(rBuffer);
								for(uint16_t i = 0;i < EEPROM_Read_Size;i++)
								{
							    	printf("%d ", rBuffer[i]);

								}
						    	printf("\r\n");
							}
							else
							{
						    	printf("\r\nFile read error: %d\r\n", retEEPROM);
							}
							f_close(&file); //Close file
						}
						else
						{
					    	printf("\r\nFile open error: %d\r\n", retEEPROM);
						}
				  }

		}

		else if(cmd==5) //File locating write
	    {
				  cmd = 0;

				  if(EEPROM_mount_status==0)
				  {
				    	 printf("\r\nEEPROM File system not mounted: %d\r\n",retEEPROM);
				  }
				  else
				  {
						retEEPROM = f_open( &file, filepath, FA_CREATE_ALWAYS | FA_WRITE);  //Open or create file
						if(retEEPROM == FR_OK)
						{
					    	printf("\r\nFile open or creation successful\r\n");
							retEEPROM=f_lseek( &file, f_tell(&file) + sizeof(WBuffer) ); //move file operation pointer, f_tell(&file) gets file head locating

							if(retEEPROM == FR_OK)
							{

								retEEPROM = f_write( &file, (const void *)WBuffer, sizeof(WBuffer), &byteswritten);
								if(retEEPROM == FR_OK)
								{
							    	printf("\r\nFile locating write successful\r\n");
								}
								else
								{
							    	printf("\r\nFile locating write error: %d\r\n", retEEPROM);
								}

							}
							else
							{
						    	printf("\r\nFile pointer error: %d\r\n",retEEPROM);
							}

							f_close(&file);   //Close file
						}
						else
						{
					    	printf("\r\nFile open or creation error %d\r\n",retEEPROM);
						}
				  }
		}

	    else if(cmd==6) //File locating read
		{
				  cmd = 0;

				  if(EEPROM_mount_status==0)
				  {
				    	printf("\r\nEEPROM File system not mounted: %d\r\n",retEEPROM);

				  }
				  else
				  {
						retEEPROM = f_open(&file, filepath, FA_OPEN_EXISTING | FA_READ); //Open file
						if(retEEPROM == FR_OK)
						{
    				    	printf("\r\nFile open successful\r\n");

							retEEPROM =  f_lseek(&file,f_tell(&file)+ sizeof(WBuffer)/2); //move file operation pointer, f_tell(&file) gets file head locating

							if(retEEPROM == FR_OK)
							{
								retEEPROM = f_read( &file, (void *)rBuffer, sizeof(rBuffer), &bytesread);
								if(retEEPROM == FR_OK)
								{
							    	printf("\r\nFile locating read successful\r\n");
									PY_Delay_us_t(200000);

									EEPROM_Read_Size = sizeof(rBuffer);
									for(uint16_t i = 0;i < EEPROM_Read_Size;i++)
									{
								    	printf("%d ",rBuffer[i]);
									}

							    	printf("\r\n");
								}
								else
								{
							    	printf("\r\nFile locating read error: %d\r\n",retEEPROM);
								}
							}
							else
							{
						    	printf("\r\nFile pointer error: %d\r\n",retEEPROM);
							}
							f_close(&file);
						}
						else
						{
					    	printf("\r\nFile open error: %d\r\n",retEEPROM);
						}
				  }
	     }

	     PY_Delay_us_t(100);

    /* 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_SCALE2);

  /** 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 = 25;
  RCC_OscInitStruct.PLL.PLLN = 336;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != 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_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != 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();
  }
  /* USER CODE BEGIN I2C1_Init 2 */

  /* USER CODE END I2C1_Init 2 */

}

/**
  * @brief USART1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_USART1_UART_Init(void)
{

  /* USER CODE BEGIN USART1_Init 0 */

  /* USER CODE END USART1_Init 0 */

  /* USER CODE BEGIN USART1_Init 1 */

  /* USER CODE END USART1_Init 1 */
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */

  /* USER CODE END USART1_Init 2 */

}

/**
  * Enable DMA controller clock
  */
static void MX_DMA_Init(void)
{

  /* DMA controller clock enable */
  __HAL_RCC_DMA1_CLK_ENABLE();

  /* DMA interrupt init */
  /* DMA1_Stream6_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Stream6_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Stream6_IRQn);

}

/**
  * @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_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_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 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart==&huart1)
	{
      cmd = URX;
      HAL_UART_Receive_IT(&huart1, &URX, 1);


	}

}
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#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 */

#endif /* USE_FULL_ASSERT */

STM32例程测试

串口指令0x01测试效果如下:
STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C
串口指令0x02测试效果如下:
STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C
串口指令0x03测试效果如下:
STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C
串口指令0x04测试效果如下:
STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C
串口指令0x05测试效果如下:
STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C
串口指令0x06测试效果如下:
STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA,STM32,stm32,FATS,EEPROM,1MBIT,ZD24C1MA,128KBYTE,I2C

STM32例程下载

STM32F401CCU6 I2C总线FATS读写EEPROM ZD24C1MA例程下载

–End–文章来源地址https://www.toymoban.com/news/detail-659632.html

到了这里,关于STM32存储左右互搏 I2C总线FATS读写EEPROM ZD24C1MA的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • STM32存储左右互搏 SPI总线读写FRAM MB85RS2M

    在中低容量存储领域,除了FLASH的使用,,还有铁电存储器FRAM的使用,相对于FLASH,FRAM写操作时不需要预擦除,所以执行写操作时可以达到更高的速度,其主要优点为没有FLASH持续写操作跨页地址需要变换的要求。相比于SRAM则具有非易失性, 因此价格方面会高一些。MB85RS2M是

    2024年01月22日
    浏览(43)
  • day9 STM32 I2C总线通信

            I2C(Inter-Integrated Circuit)总线(也称IIC或I2C)是由PHILIPS公司开发的两线式串行总线,用于连接微控制器及其外围设备,是微电子通信控制领域广泛采用的一种总线标准。         它是同步通信的一种特殊形式,具有接口线少,控制方式简单,期间封装形式少,通

    2024年02月12日
    浏览(51)
  • STM32之I2C总线知识和HAL库函数

    一、 I2C总线知识 I2C总线物理拓扑结构 I2C 总线在物理连接上非常简单,分别由SDA(串行数据线)和SCL(串行时钟线)及上拉电阻组成。通信原理是通过对SCL和SDA线高低电平时序的控制,来 产生I2C总线协议所需要的信号进行数据的传递。在总线空闲状态时,这两根线一般被上面所接

    2024年02月21日
    浏览(44)
  • 【stm32】软件I2C读写MPU6050

    概况 首先建立通信层的.c和.h模块 在通信层里写好I2C底层的GPIO初始化 以及6个时序基本单元 起始、终值、发送一个字节、接收一个字节、发送应答、接收应答 写好I2C通信层之后,再建立MPU6050的.c和.h模块 基于I2C通信的模块,来实现指定地址读、指定地址写 再实现写寄存器对

    2024年04月26日
    浏览(50)
  • 【STM32】STM32学习笔记-硬件I2C读写MPU6050(35)

    I2C(Inter-Integrated Circuit)总线是一种由NXP(原PHILIPS)公司开发的两线式串行总线,用于连接微控制器及其外围设备。多用于主控制器和从器件间的主从通信,在小数据量场合使用,传输距离短,任意时刻只能有一个主机等特性。 串行的 8 位双向数据传输位速率在标准模式下可

    2024年01月25日
    浏览(57)
  • 【STM32】STM32学习笔记-软件I2C读写MPU6050(33)

    I2C(Inter-Integrated Circuit)总线 是一种由NXP(原PHILIPS)公司开发的两线式串行总线,用于连接微控制器及其外围设备。多用于主控制器和从器件间的主从通信,在小数据量场合使用,传输距离短,任意时刻只能有一个主机等特性。 串行的 8 位双向数据传输位速率在标准模式下可

    2024年01月21日
    浏览(62)
  • 【STM32学习】——I2C通信协议&MPU6050姿态传感器&软件I2C读写MPU6050

    ​   目录 前言 一、I2C通信协议 1.简介 2.硬件电路设计 3.I2C时序(软件)

    2024年02月16日
    浏览(52)
  • 【STM32学习】——STM32-I2C外设&硬件读写MPU6050&软硬件读写波形对比

    目录 前言 一、I2C外设 二、硬件I2C操作流程 1.主机发送时序 3.其他时序

    2024年02月10日
    浏览(45)
  • 【STM32CubeMX+HAL库】I2C详解+读写EEPROM

    在之前的标准库中,STM32的硬件IIC非常复杂,更重要的是它并不稳定,所以都不推荐使用。但是在我们的HAL库中,对硬件IIC做了全新的优化,使得之前软件IIC几百行代码,在HAL库中,只需要寥寥几行就可以完成 那么这篇文章将带你去感受下它的优异之处。 通过本篇博客您将

    2024年02月03日
    浏览(53)
  • STM32 i2c读写寄存器地址8位,16位的方法

    一般常用的寄存器地址是8位的,遇到一个寄存器地址为16为的器件。总结一下代码编写。 寄存器地址16位的为SY103,寄存器地址为8位的为LT7911. 测试代码 lt7911该位置寄存器参考手册 sy103该位置寄存器参考手册 仿真测试 可以看到把值写入到相对应的寄存器内,并且读出。

    2024年01月19日
    浏览(60)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包