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

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

在中低容量存储领域,除了FLASH的使用,,还有铁电存储器FRAM的使用,相对于FLASH,FRAM写操作时不需要预擦除,所以执行写操作时可以达到更高的速度,其主要优点为没有FLASH持续写操作跨页地址需要变换的要求。相比于SRAM则具有非易失性, 因此价格方面会高一些。MB85RS2M是256K Byte(2M bit)的FRAM,能够按字节进行写入且没有写入等待时间。其管脚功能兼容FLASH:在这里插入图片描述
这里介绍STM32 FATS文件操作方式访问FRAM MB85RS2M的例程。采用STM32CUBEIDE开发平台,以STM32F401CCU6芯片为例,通过STM32 SPI硬件电路实现读写操作,通过USB虚拟串口进行控制。

STM32工程配置

首先建立基本工程并设置时钟:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
配置硬件SPI接口:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
增加配置PA4作为SPI软件代码控制输出的片选管脚
并增加PA2和PA3连接到/WP和/HOLD管脚,并保持输出高电平:
在这里插入图片描述
配置USB作为通讯口:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
对FATS文件系统进行配置:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
保存并生成初始工程代码:
在这里插入图片描述

STM32工程代码

USB虚拟串口的使用参考:STM32 USB VCOM和HID的区别,配置及Echo功能实现(HAL)
代码里用到的微秒延时函数参考: STM32 HAL us delay(微秒延时)的指令延时实现方式及优化

建立MB85RS2M.h头文件:

#ifndef INC_MB85RS2M_H_
#define INC_MB85RS2M_H_
#include "main.h"

/*To define operation code*/
#define WREN 0x06    //Set Write Enable Latch
#define WRDI 0x04    //Reset Write Enable Latch
#define RDSR 0x05    //Read Status Register
#define WRSR 0x01    //Write Status Register
#define READ 0x03    //Read Memory Code
#define WRITE 0x02   //Write Memory Code
#define RDID 0x9F    //Read Device ID

#define MB85RS2M_ID 0x03487F04

uint32_t MB85RS2M_ReadID(void);
uint8_t MB85RS2M_Init(void);
void MB85RS2M_Set_Write_Enable_Latch(void);
void MB85RS2M_Reset_Write_Enable_Latch(void);
void MB85RS2M_Write_Status_Register(uint8_t SRV);
uint8_t MB85RS2M_Read_Status_Register(void);
void MB85RS2M_Write_Memory(uint8_t * wd, uint32_t addr, uint32_t len);
void MB85RS2M_Read_Memory(uint8_t * rd, uint32_t addr, uint32_t len);

#endif /* INC_MB85RS2M_H_ */

建立MB85RS2M.c源文件:

//Written by Pegasus Yu in 2023

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

#define SPI1_CS_L HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET)
#define SPI1_CS_H HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET)
extern SPI_HandleTypeDef hspi1;
extern void PY_Delay_us_t(uint32_t Delay);


uint32_t MB85RS2M_ReadID(void)
{
	uint8_t ftd[5];
	uint8_t frd[5];
	uint8_t Manufacturer_ID;
	uint8_t Continuation_Code;
	uint8_t Product_ID_L;
	uint8_t Product_ID_H;

	ftd[0]=RDID;


	SPI1_CS_L;

	HAL_SPI_TransmitReceive(&hspi1, ftd, frd, 5, 0xFFFFFFFF);

	SPI1_CS_H;

	Manufacturer_ID = frd[1];
	Continuation_Code = frd[2];
	Product_ID_L = frd[3];
	Product_ID_H = frd[4];

	return ((Product_ID_H<<24)|(Product_ID_L<<16)|(Continuation_Code<<8)|(Manufacturer_ID));
}

uint8_t MB85RS2M_Init(void)
{
	uint8_t st = 0;

	for(uint8_t i=0; i<4; i++)
	{
		if(MB85RS2M_ReadID()==MB85RS2M_ID)
		{
			st = 1;
			break;
		}

	}

	return st;

}


/*
 * WEL is reset after the following operations which means every write operation must follow once WREN operation MB85RS2M_Set_Write_Enable_Latch().
 * After power ON.
 * After WRDI command recognition.
 * At the rising edge of CS after WRSR command recognition.
 * At the rising edge of CS after WRITE command recognition.
 */
void MB85RS2M_Set_Write_Enable_Latch(void)
{
    uint8_t cmd = WREN;
	SPI1_CS_L;

	HAL_SPI_Transmit(&hspi1, &cmd, 1, 0xFFFFFFFF);

	SPI1_CS_H;
}


void MB85RS2M_Reset_Write_Enable_Latch(void)
{
    uint8_t cmd = WRDI;
	SPI1_CS_L;

	HAL_SPI_Transmit(&hspi1, &cmd, 1, 0xFFFFFFFF);

	SPI1_CS_H;
}


void MB85RS2M_Write_Status_Register(uint8_t SRV)
{
    uint8_t data[2];
    data[0] = WRSR;
    data[1] = SRV;

    MB85RS2M_Set_Write_Enable_Latch();

    PY_Delay_us_t(2);

	SPI1_CS_L;

	HAL_SPI_Transmit(&hspi1, data, 2, 0xFFFFFFFF);

	SPI1_CS_H;
}

uint8_t MB85RS2M_Read_Status_Register(void)
{
    uint8_t cmd[2];
    uint8_t data[2];
    uint8_t SRV;

    cmd[0] = RDSR;

	SPI1_CS_L;

	HAL_SPI_TransmitReceive(&hspi1, cmd, data, 2, 0xFFFFFFFF);

	SPI1_CS_H;

	SRV = data[1];
	return SRV;

}

/*
 * wd: data buffer pointer
 * addr: address to operate for MB85RS2M
 * len: data length to be written
 */

void MB85RS2M_Write_Memory(uint8_t * wd, uint32_t addr, uint32_t len)
{
    uint8_t data[len+4];
    data[0] = WRITE;
    data[1] = (uint8_t)(addr>>16);
    data[2] = (uint8_t)(addr>>8);
    data[3] = (uint8_t)addr;

    memcpy(data+4, wd, len);

    MB85RS2M_Set_Write_Enable_Latch();

    PY_Delay_us_t(2);

	SPI1_CS_L;

	HAL_SPI_Transmit(&hspi1, data, len+4, 0xFFFFFFFF);

	SPI1_CS_H;
}


/*
 * rd: data buffer pointer
 * addr: address to operate for MB85RS2M
 * len: data length to be written
 */

void MB85RS2M_Read_Memory(uint8_t * rd, uint32_t addr, uint32_t len)
{
    uint8_t cmd[len+4];
    uint8_t data[len+4];
    cmd[0] = READ;
    cmd[1] = (uint8_t)(addr>>16);
    cmd[2] = (uint8_t)(addr>>8);
    cmd[3] = (uint8_t)addr;

	SPI1_CS_L;

	HAL_SPI_TransmitReceive(&hspi1, cmd, data , len+4, 0xFFFFFFFF);

	SPI1_CS_H;

	memcpy(rd, data+4, len);
}

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 "MB85RS2M.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************/
		  uint8_t res;
		  res = MB85RS2M_Init();

		  if(res) return RES_OK;
		  else return  STA_NOINIT;
	/**********************************************************/
/*
    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;
					MB85RS2M_Read_Memory((uint8_t *)buff, sector, 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;
					    MB85RS2M_Write_Memory((uint8_t *)buff, sector, 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 = (256*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 <string.h>
#include "MB85RS2M.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 ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_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 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_USB_DEVICE_Init();
  MX_SPI1_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=MB85RS2MT\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 SPI1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_SPI1_Init(void)
{

  /* USER CODE BEGIN SPI1_Init 0 */

  /* USER CODE END SPI1_Init 0 */

  /* USER CODE BEGIN SPI1_Init 1 */

  /* USER CODE END SPI1_Init 1 */
  /* SPI1 parameter configuration*/
  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 10;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI1_Init 2 */

  /* USER CODE END SPI1_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
/* 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();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4, GPIO_PIN_SET);

  /*Configure GPIO pins : PA2 PA3 PA4 */
  GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* 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 SPI总线FATS读写FRAM MB85RS2M例程下载

–End–

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Jenkins-自动化

定时构建 使用Cron表达式指定执行时间。 # 格式 # ┌──分&#xff08;0 - 59&#xff09; # │ ┌──时&#xff08;0 - 23&#xff09; # │ │ ┌──日&#xff08;1 - 31&#xff09; # │ │ │ ┌─月&#xff08;1 - 12&#xff09; # │ │ │ │ ┌─星期&#…

构建搜索引擎,而不是向量数据库

英文原文地址&#xff1a;Build a search engine, not a vector DB 构建搜索引擎&#xff0c;而不是矢量数据库 2023 年 12 月 19 日 在过去12个月里&#xff0c;向量数据库初创公司数量激增。我并不是来讨论其中任何一个的具体设计权衡。相反&#xff0c;我想回顾一下向量数…

【Java 设计模式】创建型之单例模式

文章目录 1. 定义2. 应用场景3. 代码实现1&#xff09;懒汉式2&#xff09;饿汉式 4. 应用示例结语 在软件开发中&#xff0c;单例模式是一种常见的设计模式&#xff0c;它确保一个类只有一个实例&#xff0c;并提供一个全局访问点。单例模式在需要控制某些资源&#xff0c;如数…

正则表达式中的“回引用(回溯)”——别名引用与序号引用的差异及正则表达式中的“P”关键字

读到一段巧妙的正则表达式&#xff0c;勾起我对正则表达式欠缺知识点的探寻&#xff1a; P y t h o n Python Python正则表达式中的“回引用(回溯)”——分组别名引用与序号引用的差异及正则表达式中的“P”关键字详情。 (笔记模板由python脚本于2024年01月14日 07:49:35创建&a…

Makefile编译原理 变量的使用

一.变量和不同的赋值方法 ->makefile中支持程序设计语言中变量的概念 ->makefile中的变量只代表文本数据&#xff08;字符串&#xff09; ->makefile中的变量名规则 - 变量名可以包含字符&#xff0c;数字&#xff0c;下划线。 - 不能包含 ":" "#&…

动态规划篇-04:完全平方数

279、完全平方数 状态转移方程 base case 当n 0 时&#xff0c;和为n的完全平方数的最少数量为0. 明确状态 “原问题或子问题中变化的变量” 在本题中&#xff0c;状态是 “完全平方数的最少数量”。因为当我们选择不同的完全平方数的时候&#xff0c;所需完全平方数的数量…

个人网站制作 Part 5 优化网站性能(图片压缩、代码优化) | Web开发项目

文章目录 &#x1f469;‍&#x1f4bb; 基础Web开发练手项目系列&#xff1a;个人网站制作&#x1f680; 优化网站性能&#x1f528;图片优化&#x1f527;步骤 1: 使用压缩工具 &#x1f528;代码优化&#x1f527;步骤 2: 压缩CSS和JavaScript&#x1f527;步骤 3: 合并文件…

PCL ISS关键点提取(C++详细过程版)

边界提取 一、概述二、代码实现三、结果展示PCL ISS关键点提取(C++详细过程版)由CSDN点云侠原创,爬虫自重。如果你不是在点云侠的博客中看到该文章,那么此处便是不要脸的爬虫。 一、概述 ISS关键点提取在PCL里有现成的调用函数,具体算法原理和实现代码见:PCL ISS关键点提…

性能优化2.0,新增缓存后,程序的秒开率不升反降

目录 一、前情提要经过4次优化&#xff0c;将页面的加载时间控制在了1秒以内&#xff0c;实打实的提升了程序的秒开率。 二、先了解一下&#xff0c;什么是缓存1、缓存有哪些分类2、本地缓存与分布式缓存 三、Guava Cache本地缓存1、Google Guava2、Loadingcache数据结构3、Loa…

上海亚商投顾:创业板指冲高回落 光伏、航运股逆势走强

上海亚商投顾前言&#xff1a;无惧大盘涨跌&#xff0c;解密龙虎榜资金&#xff0c;跟踪一线游资和机构资金动向&#xff0c;识别短期热点和强势个股。 一.市场情绪 沪指1月12日冲高回落&#xff0c;创业板指午后跌近1%。北证50指数跌超6%&#xff0c;倍益康、华信永道、众诚科…

SpringBoot 入门教程

1.复习SSM项目中&#xff0c;用spring&#xff0c;mybatis,springmvc这三个框架整合的项目。 SSM项目的所有类&#xff0c;这是用SSM整合一个搜索书籍种类和呈现的前端和后端的ssm的小项目。 2.springboot如何去开发这个页面&#xff1a; 新建springboot项目&#xff0c;勾选对…

【Nuxt3】Nuxt3脚手架nuxi安装项目和项目目录介绍

简言 最近学了Nuxt3,并使用它创建了自己的小网站。记录下学习到的nuxt3内容。 Nuxt3官网 Nuxt 是一个免费的开源框架&#xff0c;可通过直观、可扩展的方式使用 Vue.js 创建类型安全、高性能、生产级的全栈 Web 应用程序和网站。 支持SSR、SPA、建立静态网站&#xff0c;也可以…

分布式限流的主流方案

本文已收录至我的个人网站&#xff1a;程序员波特&#xff0c;主要记录Java相关技术系列教程&#xff0c;共享电子书、Java学习路线、视频教程、简历模板和面试题等学习资源&#xff0c;让想要学习的你&#xff0c;不再迷茫。 常见的分布式限流方案 前面我们了解了什么是分布式…

【算法实验】实验1

实验1-1 斐波那契数 【问题描述】斐波那契数 &#xff08;通常用 F(n) 表示&#xff09;形成的序列称为 斐波那契数列 。该数列由 0 和 1 开始&#xff0c;后面的每一项数字都是前面两项数字的和。 定义&#xff1a;F(0) 0, F(1) 1, F(n) F(n-1) F(n-2) 其中n>1 要求计…

11.云原生存储之TIDB

云原生专栏大纲 文章目录 为什么使用TIDB后端视角运维视角基础架构视角 TiDB Operator 简介软件版本要求部署tidbTIDB工具helm常用命令TIDB学习推荐资料 为什么使用TIDB 从后端视角、运维视角和基础架构视角来看&#xff0c;使用 TiDB 作为数据库系统可以获得分布式架构、高可…

Unity 踩坑记录 项目启动时获取目标子UI的位置相同

检查是否使用了 LayoutGroup ui控件控制位置 因为项目刚启动的时候 控件还没有工作所以他们都挤在一个位置 延迟两秒钟获取 就可以获取到 子UI 的正确坐标位置

【HarmonyOS4.0】第七篇-ArkUI系统组件(二)

鸿蒙开发系统组件详细剖析 五、进度条组件 进度条也是UI开发最常用的组件之一&#xff0c;ArkUI开发框架提供了两种类型的进度条&#xff1a; Progress 和LoadingProgress &#xff0c;前者可以精准指定进度&#xff0c;后者表示正在加载的状态&#xff0c;我们接下来对它们分…

对Transformer的理解。

要理解Transformer&#xff0c;需要先理解注意力机制&#xff0c;下面大部分内容来自台大教授李宏毅老师讲课资料。 注意力机制 之前使用的MLP&#xff0c;CNN&#xff0c;RNN模型可以解决一些简单序列问题&#xff0c;但当序列长度太长容易失去效果&#xff0c;原因是看了新…

python 列表的高级应用

当前版本&#xff1a; Python 3.8.4 简介 列表&#xff08;list&#xff09;是Python编程语言中的基本数据类型之一&#xff0c;也是一个非常重要的通用序列。在其它编程语言中&#xff0c;它们通常被称为“数组”。可以存储多个元素&#xff0c;包括数字、字符串、甚至其他列…

python 字符串的详细处理方法

当前版本&#xff1a; Python 3.8.4 简介 字符串是由字符组成的序列&#xff0c;可以用单引号、双引号或三引号&#xff08;单引号或双引号的连续使用&#xff09;括起来。一般用来表示和处理文本信息&#xff0c;可以是字母、数字、标点符号以及其他特殊字符&#xff0c;用于…