小学生
最后登录1970-1-1
在线时间 小时
注册时间2021-9-20
|
#include "stm32f10x.h" // 相当于51单片机中的 #include <reg51.h>
#include "led.h"
#include "beep.h"
#include "delay.h"
#include "usart.h"
#include "i2c_ee.h"
#include <string.h>
#include <stdio.h>
#include "cJSON.h"
#include "lock.h"
#include "spi_flash.h"
#include "ff.h"
typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
/* 获取缓冲区的长度 */
#define TxBufferSize1 (countof(TxBuffer1) - 1)
#define RxBufferSize1 (countof(TxBuffer1) - 1)
#define countof(a) (sizeof(a) / sizeof(*(a)))
#define BufferSize (countof(Tx_Buffer)-1)
#define FLASH_WriteAddress 0x00000
#define FLASH_ReadAddress FLASH_WriteAddress
#define FLASH_SectorToErase FLASH_WriteAddress
#define EEP_Firstpage 0 //标识位
/* 发送缓冲区初始化 */
uint8_t Tx_Buffer[] = "{\"tp\":0,\"uid\":\"asdfgh\",\"t1\":\"08:00-12:00\",\"t2\":\"13:00-18:00\",\"key\":\"asdfgh\",\"lk\":\"asdfgh,asdfgh,asdfgh,asdfgh,asdfgh\"}";
uint8_t Rx_Buffer[BufferSize];
__IO uint32_t DeviceID = 0;
__IO uint32_t FlashID = 0;
__IO TestStatus TransferStatus1 = FAILED;
FATFS fs; /* FatFs文件系统对象 */
FIL fnew; /* 文件对象 */
FRESULT res_flash; /* 文件操作结果 */
UINT fnum; /* 文件成功读写数量 */
BYTE ReadBuffer[1024]={0}; /* 读缓冲区 */
BYTE WriteBuffer[] = /* 写缓冲区*/
"感豆腐干豆腐干梵蒂冈地方官地方官地方官豆腐干豆腐干豆腐干豆腐干豆腐干\r\n";
// 函数原型声明
void Delay(__IO uint32_t nCount);
TestStatus Buffercmp(uint8_t* pBuffer1,uint8_t* pBuffer2, uint16_t BufferLength);
int main(void)
{
LED_Init(); //LED初始化
BEEP_Init();
delay_init(); //延时函数初始化
LOCK_Init();
Usart1_Init(115200);//115200波特率 debug串口
Usart2_Init(115200);//115200波特率
Usart3_Init(115200);//115200波特率
UsartPrintf(USART_DEBUG,"****** 这是一个SPI FLASH 文件系统实验 ******\r\n");
//在外部SPI Flash挂载文件系统,文件系统挂载时会对SPI设备初始化
//初始化函数调用流程如下
//f_mount()->find_volume()->disk_initialize->SPI_FLASH_Init()
res_flash = f_mount(&fs,"1:",1);
/*----------------------- 格式化测试 -----------------*/
/* 如果没有文件系统就格式化创建创建文件系统 */
if(res_flash == FR_NO_FILESYSTEM)
{
UsartPrintf(USART_DEBUG,"》FLASH还没有文件系统,即将进行格式化...\r\n");
/* 格式化 */
res_flash=f_mkfs("1:",0,0);
if(res_flash == FR_OK)
{
UsartPrintf(USART_DEBUG,"》FLASH已成功格式化文件系统。\r\n");
/* 格式化后,先取消挂载 */
res_flash = f_mount(NULL,"1:",1);
/* 重新挂载 */
res_flash = f_mount(&fs,"1:",1);
}
else
{
LED1 = 0;
UsartPrintf(USART_DEBUG,"《《格式化失败。》》\r\n");
while(1);
}
}
else if(res_flash!=FR_OK)
{
UsartPrintf(USART_DEBUG,"!!外部Flash挂载文件系统失败。(%d)\r\n",res_flash);
UsartPrintf(USART_DEBUG,"!!可能原因:SPI Flash初始化不成功。\r\n");
while(1);
}
else
{
UsartPrintf(USART_DEBUG,"》文件系统挂载成功,可以进行读写测试\r\n");
}
/*------------------- 防止文件累积过大 --------------------------*/
res_flash=f_mkfs("1:",0,0);
if(res_flash == FR_OK)
{
UsartPrintf(USART_DEBUG,"格式化成功\r\n");
}
/*------------------- 防止文件累积过大 --------------------------*/
/*----------------------- 文件系统测试:写测试 -------------------*/
/* 打开文件,每次都以新建的形式打开,属性为可写 */
UsartPrintf(USART_DEBUG,"\r\n****** 即将进行文件写入测试... ******\r\n");
//res_flash = f_open(&fnew, "1:FatFs读写测试文件.txt",FA_CREATE_ALWAYS | FA_WRITE );
res_flash = f_open(&fnew, "1:FatFs.txt", FA_OPEN_ALWAYS|FA_WRITE );
UsartPrintf(USART_DEBUG,"》第一次写时原文件大小:%d\r\n",f_size(&fnew));
if ( res_flash == FR_OK )
{
UsartPrintf(USART_DEBUG,"》打开/创建FatFs读写测试文件.txt文件成功,向文件写入数据。\r\n");
/* 将指定存储区内容写入到文件内 */
res_flash=f_write(&fnew,WriteBuffer,sizeof(WriteBuffer),&fnum);
if(res_flash==FR_OK)
{
UsartPrintf(USART_DEBUG,"》文件写入成功,写入字节数据:%d\n",fnum);
UsartPrintf(USART_DEBUG,"》向文件写入的数据为:\r\n%s\r\n",WriteBuffer);
}
else
{
UsartPrintf(USART_DEBUG,"!!文件写入失败:(%d)\n",res_flash);
}
/* 不再读写,关闭文件 */
f_close(&fnew);
}
else
{
LED1 = 0;
UsartPrintf(USART_DEBUG,"!!打开/创建文件失败。\r\n");
}
/*------------------- 再次写数据测试START --------------------------*/
UsartPrintf(USART_DEBUG,"\r\n****** 即将进行文件再次写入测试... ******\r\n");
res_flash = f_open(&fnew, "1:FatFs.txt", FA_OPEN_ALWAYS|FA_WRITE );
UsartPrintf(USART_DEBUG,"》再次写时原文件大小:%d\r\n",f_size(&fnew));
if ( res_flash == FR_OK )
{
UsartPrintf(USART_DEBUG,"》打开/创建FatFs读写测试文件.txt文件成功,向文件再次写入数据。\r\n");
/* 文件定位 */
res_flash = f_lseek(&fnew,f_size(&fnew));
if (res_flash == FR_OK)
{
/* 将指定存储区内容写入到文件内 */
res_flash=f_write(&fnew,Tx_Buffer,sizeof(Tx_Buffer),&fnum);
if(res_flash==FR_OK)
{
UsartPrintf(USART_DEBUG,"》文件再次写入成功,再次写入字节数据:%d\n",fnum);
UsartPrintf(USART_DEBUG,"》向文件再次写入的数据为:\r\n%s\r\n",Tx_Buffer);
}
else
{
UsartPrintf(USART_DEBUG,"!!文件再次写入失败:(%d)\n",res_flash);
}
}
/* 不再读写,关闭文件 */
f_close(&fnew);
}
else
{
LED1 = 0;
UsartPrintf(USART_DEBUG,"!!再次打开/创建文件失败。\r\n");
}
/*------------------- 再次写数据测试END --------------------------*/
/*------------------- 文件系统测试:读测试 --------------------------*/
UsartPrintf(USART_DEBUG,"****** 即将进行文件读取测试... ******\r\n");
//res_flash = f_open(&fnew, "1:FatFs读写测试文件.txt",FA_OPEN_EXISTING | FA_READ);
res_flash = f_open(&fnew, "1:FatFs.txt",FA_OPEN_ALWAYS|FA_READ);
UsartPrintf(USART_DEBUG,"》读取时文件大小:%d\r\n",f_size(&fnew));
if(res_flash == FR_OK)
{
LED0= 0;
UsartPrintf(USART_DEBUG,"》打开文件成功。\r\n");
res_flash = f_read(&fnew,ReadBuffer,f_size(&fnew),&fnum);
if(res_flash==FR_OK)
{
UsartPrintf(USART_DEBUG,"》文件读取成功,读到字节数据:%d\r\n",fnum);
UsartPrintf(USART_DEBUG,"》读取得的文件数据为:\r\n%s \r\n", ReadBuffer);
}
else
{
UsartPrintf(USART_DEBUG,"!!文件读取失败:(%d)\n",res_flash);
}
}
else
{
LED1 = 0;
UsartPrintf(USART_DEBUG,"!!打开文件失败。\r\n");
}
/* 不再读写,关闭文件 */
f_close(&fnew);
/* 不再使用文件系统,取消挂载文件系统 */
//f_mount(NULL,"1:",1);
/* 操作完成,停机 */
while(1)
{
}
}
/*
* 函数名:Buffercmp
* 描述 :比较两个缓冲区中的数据是否相等
* 输入 :-pBuffer1 src缓冲区指针
* -pBuffer2 dst缓冲区指针
* -BufferLength 缓冲区长度
* 输出 :无
* 返回 :-PASSED pBuffer1 等于 pBuffer2
* -FAILED pBuffer1 不同于 pBuffer2
*/
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
{
while(BufferLength--)
{
if(*pBuffer1 != *pBuffer2)
{
return FAILED;
}
pBuffer1++;
pBuffer2++;
}
return PASSED;
}
void Delay(__IO uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
|
|