STM32存储左右互搏 I2C总线FATS读写FRAM MB85RC1M

news2024/9/25 19:19:51

STM32存储左右互搏 I2C总线FATS读写FRAM MB85RC1M

在较低容量存储领域,除了EEPROM的使用,还有铁电存储器FRAM的使用,相对于EEPROM, 同样是非易失性存储单元,FRAM支持更高的访问速度, 其主要优点为没有EEPROM持续写操作跨页地址需要变换的要求,没有写之后的延时等待要求。MB85RC1M是128K Byte(1M bit)的FRAM,能够按字节进行写入且没有写入等待时间。其管脚功能兼容相应容量的EEPOM:
在这里插入图片描述
这里介绍STM32 通过文件系统FATS访问FRAM MB85RC1M的例程。采用STM32CUBEIDE开发平台,以STM32F401CCU6芯片为例,通过STM32 I2C硬件电路实现读写操作,通过USB虚拟串口进行控制。

STM32工程配置

首先建立基本工程并设置时钟:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
配置硬件I2C接口,STM32F401CCU6的I2C快速模式只支持400KHz速率:
在这里插入图片描述
在这里插入图片描述
然后配置USB虚拟串口:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
对FATS文件系统进行配置:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
保存并生成初始工程代码:
在这里插入图片描述

STM32工程代码

USB虚拟串口的使用参考:STM32 USB VCOM和HID的区别,配置及Echo功能实现(HAL)

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

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

建立MB85RC1M.h库头文件

#ifndef INC_MB85RC1M_H_
#define INC_MB85RC1M_H_

#include "main.h"

void MB85RC1M_Write(uint32_t addr, uint8_t * data, uint32_t len);
void MB85RC1M_Read(uint32_t addr, uint8_t * data, uint32_t len);

#endif

建立MB85RC1M.c库源文件:


#include "MB85RC1M.h"
#include <string.h>

extern I2C_HandleTypeDef hi2c1;
extern uint8_t MB85RC1M_Default_I2C_Addr ;
void MB85RC1M_Write(uint32_t addr, uint8_t * data, uint32_t len)
{
	uint8_t MB85RC1M_I2C_Addr;

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

	uint8_t TD[len+2];
	TD[0] = (addr & 0xFF00)>>8; //high 8-bit access address placed into I2C first data
	TD[1] =addr & 0x00FF; //low 8-bit access address placed into I2C first data
	memcpy(TD+2, data, len);
	HAL_I2C_Master_Transmit(&hi2c1, MB85RC1M_I2C_Addr, TD, len+2, 2700);  //Write data
}


void MB85RC1M_Read(uint32_t addr, uint8_t * data, uint32_t len)
{
	uint8_t MB85RC1M_I2C_Addr;

	MB85RC1M_I2C_Addr = MB85RC1M_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, MB85RC1M_I2C_Addr, &RA[0], 2, 2700); //Write address for read
	HAL_I2C_Master_Receive(&hi2c1, MB85RC1M_I2C_Addr, data, len, 2700); //Read data

}

USB接收命令的代码:
在这里插入图片描述

static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
{
  /* USER CODE BEGIN 6 */
	extern uint8_t cmd;
	cmd = Buf[0];

  USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
  USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  return (USBD_OK);
  /* USER CODE END 6 */
}

对ffconf.h添加包含信息:
在这里插入图片描述

#include "main.h"
#include "stm32f4xx_hal.h"
#include "MB85RC1M.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 MB85RC1M_Default_I2C_Addr ;
	 MB85RC1M_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不能等于0,否则返回参数错�?*/
			}
			switch (pdrv)
			{
				case 0:
					sector <<= 9; //Convert sector number to byte address
				    len = count*512;
				    MB85RC1M_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不能等于0,否则返回参数错�?*/
			}
			switch (pdrv)
			{
				case 0:
					sector <<= 9; //Convert sector number to byte address
				    len = count*512;
				    MB85RC1M_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. 读取FRAM 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"
#include "usb_device.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "MB85RC1M.h"
#include <string.h>
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len);
/* 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;

/* 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);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

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

uint8_t MB85RC1M_Default_I2C_Addr =  0xA0; //Pin A2=A1=0
uint32_t MB85RC1M_Access_Addr = 0;   //FRAM MB85RC1M access address (17-bit)

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

FRESULT retFRAM;
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 frambuffer[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 */
	FRAM_mount_status = 0;
	uint32_t FRAM_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_I2C1_Init();
  MX_USB_DEVICE_Init();
  MX_FATFS_Init();
  /* USER CODE BEGIN 2 */
  PY_usDelayTest();
  PY_usDelayOptimize();


  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	     if(cmd==1) //Read ID
	     {
	    	 cmd = 0;
	    	 sprintf(console, "FRAM ID=MB85RC1MT\r\n\r\n");
	    	 while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);

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

	    	 retFRAM=f_mount(&USERFatFS, (TCHAR const*)USERPath, 1);
	    	    		 if (retFRAM != FR_OK)
	    	    		 {
	    	  	    	   sprintf(console, "File system mount failure: %d\r\n", retFRAM);
	    	  	    	   while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);

	    	    		   if(retFRAM==FR_NO_FILESYSTEM)
	    	    		   {
	    	    		       sprintf(console, "No file system. Now to format......\r\n");
	    	    		       while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);

	    	    			   retFRAM = f_mkfs((TCHAR const*)USERPath, FM_FAT, 1024, FATS_Buff, sizeof(FATS_Buff)); //FRAM formatting
	    	    			   if(retFRAM == FR_OK)
	    	    			   {
	    	         	    	  sprintf(console, "FRAM formatting success!\r\n");
	    	         	    	  while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
	    	    			   }
	    	    				else
	    	    			   {
	    	    			      sprintf(console, "FRAM formatting failure!\r\n");
	    	    			      while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
	    	    			   }

	    	    		   }
	    	    		 }
	    	    		 else
	    	    		 {
	    	    			 FRAM_mount_status = 1;
	    	    	    	 sprintf(console, "File system mount success\r\n");
	    	    	    	 while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
	    	    		 }
	     }

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

				  if(FRAM_mount_status==0)
				  {
				    	 sprintf(console, "\r\nFRAM File system not mounted: %d\r\n",retFRAM);
				    	 while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
				  }
				  else
				  {
						retFRAM = f_open( &file, filepath, FA_CREATE_ALWAYS | FA_WRITE );  //Open or create file
						if(retFRAM == FR_OK)
						{
					    	sprintf(console, "\r\nFile open or creation successful\r\n");
					    	while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);

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

							if(retFRAM == FR_OK)
							{
						    	 sprintf(console, "\r\nFile write successful\r\n");
						    	 while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
							}
							else
							{
						    	 sprintf(console, "\r\nFile write error: %d\r\n",retFRAM);
						    	 while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
							}

							f_close(&file);   //Close file
						}
						else
						{
					    	 sprintf(console, "\r\nFile open or creation error %d\r\n",retFRAM);
					    	 while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
						}
				   }

	    }

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

				  if(FRAM_mount_status==0)
				  {
				    	 sprintf(console, "\r\nFRAM File system not mounted: %d\r\n",retFRAM);
				    	 while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
				  }
				  else
				  {
						retFRAM = f_open( &file, filepath, FA_OPEN_EXISTING | FA_READ); //Open file
						if(retFRAM == FR_OK)
						{
					    	 sprintf(console, "\r\nFile open successful\r\n");
					    	 while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);

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

							if(retFRAM == FR_OK)
							{
						    	sprintf(console, "\r\nFile read successful\r\n");
						    	while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);

								PY_Delay_us_t(200000);

								FRAM_Read_Size = sizeof(rBuffer);
								for(uint16_t i = 0;i < FRAM_Read_Size;i++)
								{
							    	sprintf(console, "%d ", rBuffer[i]);
							    	while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
								}
						    	sprintf(console, "\r\n");
						    	while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
							}
							else
							{
						    	sprintf(console, "\r\nFile read error: %d\r\n", retFRAM);
						    	while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
							}
							f_close(&file); //Close file
						}
						else
						{
					    	sprintf(console, "\r\nFile open error: %d\r\n", retFRAM);
					    	while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
						}
				  }

		}

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

				  if(FRAM_mount_status==0)
				  {
				    	 sprintf(console, "\r\nFRAM File system not mounted: %d\r\n",retFRAM);
				    	 while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
				  }
				  else
				  {
						retFRAM = f_open( &file, filepath, FA_CREATE_ALWAYS | FA_WRITE);  //Open or create file
						if(retFRAM == FR_OK)
						{
					    	sprintf(console, "\r\nFile open or creation successful\r\n");
					    	while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);

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

							if(retFRAM == FR_OK)
							{

								retFRAM = f_write( &file, (const void *)WBuffer, sizeof(WBuffer), &byteswritten);
								if(retFRAM == FR_OK)
								{
							    	sprintf(console, "\r\nFile locating write successful\r\n");
							    	while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
								}
								else
								{
							    	sprintf(console, "\r\nFile locating write error: %d\r\n", retFRAM);
							    	while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
								}

							}
							else
							{
						    	sprintf(console, "\r\nFile pointer error: %d\r\n",retFRAM);
						    	while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
							}

							f_close(&file);   //Close file
						}
						else
						{
					    	sprintf(console, "\r\nFile open or creation error %d\r\n",retFRAM);
					    	while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
						}
				  }
		}

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

				  if(FRAM_mount_status==0)
				  {
				    	sprintf(console, "\r\nFRAM File system not mounted: %d\r\n",retFRAM);
				    	while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);

				  }
				  else
				  {
						retFRAM = f_open(&file, filepath, FA_OPEN_EXISTING | FA_READ); //Open file
						if(retFRAM == FR_OK)
						{

					    	sprintf(console, "\r\nFile open successful\r\n");
					    	while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);

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

							if(retFRAM == FR_OK)
							{
								retFRAM = f_read( &file, (void *)rBuffer, sizeof(rBuffer), &bytesread);
								if(retFRAM == FR_OK)
								{
							    	sprintf(console, "\r\nFile locating read successful\r\n");
							    	while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
									PY_Delay_us_t(200000);

									FRAM_Read_Size = sizeof(rBuffer);
									for(uint16_t i = 0;i < FRAM_Read_Size;i++)
									{
								    	sprintf(console, "%d ",rBuffer[i]);
								    	while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
									}

							    	sprintf(console, "\r\n");
							    	while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
								}
								else
								{
							    	sprintf(console, "\r\nFile locating read error: %d\r\n",retFRAM);
							    	while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
								}
							}
							else
							{
						    	sprintf(console, "\r\nFile pointer error: %d\r\n",retFRAM);
						    	while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
							}
							f_close(&file);
						}
						else
						{
					    	sprintf(console, "\r\nFile open error: %d\r\n",retFRAM);
					    	while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
						}
				  }
	     }

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

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

STM32例程测试

串口指令0x01测试效果如下:在这里插入图片描述

串口指令0x02测试效果如下:

在这里插入图片描述
串口指令0x03测试效果如下:
在这里插入图片描述
串口指令0x04测试效果如下:
在这里插入图片描述

串口指令0x05测试效果如下:
在这里插入图片描述
串口指令0x06测试效果如下:
在这里插入图片描述

STM32例程下载

STM32F401CCU6 I2C总线FATS读写FRAM MB85RC1M例程下载

–End–

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1074335.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基于Spring Boot的网上租贸系统设计与实现(源码+lw+部署文档+讲解等)

文章目录 前言具体实现截图论文参考详细视频演示代码参考源码获取 前言 &#x1f497;博主介绍&#xff1a;✌全网粉丝10W,CSDN特邀作者、博客专家、CSDN新星计划导师、全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技…

台灯什么材质的比较好?专家推荐的护眼台灯分享

台灯是现在普遍使用的家用照明灯具&#xff0c;不仅可以很好地装饰卧室房间&#xff0c;最主要的作用是可以给我我们补充室内灯源不足的部分&#xff0c;让我们工作、学习等保持极高的专注度&#xff0c;也可以在晚上娱乐休闲时&#xff0c;身心更加愉悦。更重要的是&#xff0…

git操作流程与清单

在团队中的git使用流程 git add git stash push -- src/index.js git pull origin master commit push git stash pop git commit和pull的先后顺序 前辈的原文链接 git stash 优质的原文链接 git stash 将本地部分文件暂存&#xff0c;而不提交到远程仓库 使用 git add …

Java——Math类

Java——Math类 Math类是数学操作类&#xff0c;提供了一系列用于数学运算的静态方法。 package com.yushifu.javaAPI; //Math类是数学操作类&#xff0c;提供了一系列用于数学运算的静态方法。 public class MathDemo01 {public static void main(String[] args) {//计算绝对…

淘宝网址链接采集(用 Python 实现淘宝商品信息抓取)

在网页抓取方面&#xff0c;可以使用 Python、Java 等编程语言编写程序&#xff0c;通过模拟 HTTP 请求&#xff0c;获取淘宝网站上的商品页面。在数据提取方面&#xff0c;可以使用正则表达式、XPath 等方式从 HTML 代码中提取出有用的信息。值得注意的是&#xff0c;淘宝网站…

crontab 定时任务详解

使用这个命令自动定时编译 crontab -e 然后输入ctrx 然后输入ctry保存 至此自动编译脚本就做好了。 设置完了之后可以使用crontab -l查看相关内容是不是做好了

基于若依ruoyi-nbcio支持flowable流程增加自定义业务表单(一)

因为需要支持自定义业务表单的相关流程&#xff0c;所以需要建立相应的关联表 1、首先先建表wf_custom_form -- ---------------------------- -- Table structure for wf_custom_form -- ---------------------------- DROP TABLE IF EXISTS wf_custom_form; CREATE TABLE wf…

[每日算法 - 阿里机试] leetcode19. 删除链表的倒数第 N 个结点 「 详细图释一看就懂!」

入口 力扣&#xff08;LeetCode&#xff09;官网 - 全球极客挚爱的技术成长平台备战技术面试&#xff1f;力扣提供海量技术面试资源&#xff0c;帮助你高效提升编程技能&#xff0c;轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/remove-nth-node-from-end…

孙哥Spring源码第28集

第28集 事务基础概念 【视频来源于&#xff1a;B站up主孙帅suns Spring源码视频】【微信号&#xff1a;suns45】 1、什么是事务 保证业务操作完整性的一种数据库机制 &#xff08;driver 驱动&#xff09; 2、事务特点 ACID ​ A 原子性 &#xff08;多次操作 要不一起成功…

【狐妖小红娘】真人剧,王权篇主演定下,二搭情侣,网友:别魔改

Hello,小伙伴们&#xff0c;我是小郑继续为大家深度解析国漫资讯。 近几年【改编】基本成为国内影视剧创作的主流风向&#xff0c;有不少电视剧都是由网文改编而成&#xff0c;尽管国内网文市场庞大&#xff0c;也有不少优质的网文小说&#xff0c;但也耐不住大批量地改编&…

基于Dockerfile搭建LNMP环境

准备工作 #关闭防火墙和防护机制 systemctl stop firewalld systemctl disable firewalld setenforce 0 docker network create --subnet172.18.0.0/16 --opt "com.docker.network.bridge.name""docker1" mynetwork#设置自定义网络模式&#xff0c;模…

海外ASO优化之提高应用下载量的策略有哪些

ASO也被称为应用商店优化&#xff0c;是优化软件使其排名变得更高一个的过程&#xff0c;这提高了应用的可见度&#xff0c;并增加了下载量。ASO并不像看上去那么容易&#xff0c;它需要多次A/B测试和观察&#xff0c;才能确定哪种策略更有效。 1、应用的界面和用户体验。 如果…

泛微OA与ERP集成的关键要点

泛微OA办公系统与ERP系统的集成是为了实现企业内部各个系统之间的数据共享和协同工作&#xff0c;提高工作效率和管理水平。下面将详细介绍泛微OA办公系统如何与ERP系统集成以及轻易云数据集成平台在该过程中发挥的重要作用。 集成方式 泛微OA办公系统与ERP系统的集成可以采用…

Stable Diffusion 最新Ebsynth Utility脚本生成AI动画视频

早期的EbSynth制作的AI视频闪烁能闪瞎人的双眼,可以通过【temporalkit+ebsynth+controlnet】让视频变得丝滑不闪烁,现在又多了一个新的方法,在最新版本的Ebsynth Utility中可以通过脚本进行操作设置,更加简单方便。 插件安装 在你已经安装好Ebsynth之后,如果还没有安装好…

iPad平板哪种电容笔更好用?apple pencil的平替

Ipad平板是很有必要买一支电容笔的。我们究竟要不要购买苹果的原装电容笔呢&#xff1f;事实上&#xff0c;如果只是单纯的记录和记录&#xff0c;并不需要花费太多的金钱选择原装电容笔。平替电容笔不单品质优良&#xff0c;用于书写上笔迹流畅&#xff0c;是极佳的学习与工作…

uni-app:实现view元素强制换行(解决长字符和英文字符不换行问题)

效果 换行前 换行后 核心代码 word-wrap: break-word; 或 word-break: break-all; 完整代码demo <template><view><view class"all_style"><view class"line1">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</view>…

浅谈制造业数仓建设

制造业一直是国民经济的支柱产业&#xff0c;而随着全球市场竞争的加剧和客户需求的不断变化&#xff0c;制造业企业面临着诸多挑战。为了应对这些挑战&#xff0c;制造业企业需要借助先进的信息技术手段&#xff0c;提高生产效率、降低成本、提供个性化的产品和服务。在这个背…

开源联合、聚力共赢丨2023 CCF中国开源大会会议通知(第二轮)

会议简介 2023 CCF中国开源大会&#xff08;CCF ChinaOSC&#xff09;拟于2023年10月21日至22日在湖南省长沙市北辰国际会议中心召开。大会由中国计算机学会&#xff08;CCF&#xff09;与开放原子开源基金会主办&#xff0c;CCF开源发展委员会、湖南先进技术研究院承办&#…

靶场上新:PigCMS任意文件上传漏洞

本文由掌控安全学院-江月投稿 封神台新上线漏洞复现靶场&#xff1a;PigCMS action_flashUpload 任意文件上传漏洞。 漏洞详情&#xff1a; PigCms&#xff08;又称小猪CMS&#xff09;是一个基于phpmysql的多用户微信营销系统&#xff0c;是国内使用较多、功能强大、性能稳定…

【20】c++设计模式——>组合模式

组合模式定义 C组合模式&#xff08;Composite Pattern&#xff09;是一种结构型设计模式&#xff0c;他允许将对象组合成树形结构来表示“部分-整体”的层次结构&#xff1b;在组合模式中有两种基本类型的对象&#xff1a;叶子对象和组合对象&#xff0c;叶子对象时没有子对象…