当前位置:网站首页>[Zhongyi Xinsheng] 5 SPI interface test TF Card
[Zhongyi Xinsheng] 5 SPI interface test TF Card
2022-06-28 14:19:00 【Birds hate fish】
List of articles
1 Preface
Tests in this chapter spi tf Card example , Realization txt Reading and writing of documents .
2 Source code
Source path :
https://gitee.com/CMIOT-XinShengTech/CMIOT.CM32M4xxR_Library/tree/main/Projects/CM32M4xxR_LQFP128_STB/Examples/SPI/SPI_SD_CARD
Pay attention to the , It's used here CM32M4xxR_LQFP128_STB, The development board is CM32M433R-START, Compare the two codes , Little difference , among uart Serial printing log Use different ports ,START It's using uart4, and LQFP128_STB yes uart5.
If you are creating a project using CM32M433R-START, This serial port difference can be ignored . If you use CM32M4xxR_LQFP128_STB, It needs to use uart5, The corresponding mouth is pin8、9.
See https://gitee.com/CMIOT-XinShengTech/CMIOT.CM32M4xxR_Library/blob/main/Projects/CM32M4xxR_LQFP128_STB/BSP/Include/log.h.
Use CM32M4xxR_LQFP128_STB You can select when creating spi Related examples , And then sdcard Of main Copy the contents of the file .
In addition, we need to copy FatFs_User and FatFs Folder to Application Under the same category .
3 Add header file

Add the header file according to the above figure , You can compile , Otherwise it will be reported h The file could not be found .
4 Hardware
spi sdcard The example USES spi1.
Be careful not to connect the wires incorrectly , If the connection is wrong f_open Will return an error status , Such as return 3 It is The physical drive cannot work.
See ff.h Medium FRESULT.
5 Code
The code is spi sdcard Example , Only two return values are added for error analysis .
main.c
#include <main.h>
#include <stdio.h>
#include "log.h"
#include "fatfs.h"
#include "user_diskio.h" /* defines USER_Driver as external */
/** @addtogroup CM32M4xxR_StdPeriph_Examples * @{ */
/** @addtogroup SPI_CRC * @{ */
#define BufferSize (32)
#define SPICRC
volatile SPI_InitType SPI_InitStructure;
void RCC_Configuration(void);
void GPIO_Configuration(void);
volatile FATFS fs;
volatile FATFS *pfs;
volatile FIL fil;
volatile FRESULT fres;
volatile DWORD fre_clust;
volatile uint32_t totalSpace, freeSpace;
volatile char buffer[100];
volatile char USERPath[4]; /* USER logical drive path */
/** * @brief Main function. */
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_cm32m4xxr.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_cm32m4xxr.c file */
log_init();
log_info("This is a SPI_SD_CARD demo-----------------------------------\r\n");
SysTick_Config(144000000/100);
/* System clocks configuration ---------------------------------------------*/
RCC_Configuration();
/* GPIO configuration ------------------------------------------------------*/
GPIO_Configuration();
/* SPI1 configuration ------------------------------------------------------*/
SPI_InitStructure.DataDirection = SPI_DIR_DOUBLELINE_FULLDUPLEX;
SPI_InitStructure.SpiMode = SPI_MODE_MASTER;
SPI_InitStructure.DataLen = SPI_DATA_SIZE_8BITS;
SPI_InitStructure.CLKPOL = SPI_CLKPOL_LOW;
SPI_InitStructure.CLKPHA = SPI_CLKPHA_FIRST_EDGE;
SPI_InitStructure.NSS = SPI_NSS_SOFT;
SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_16;
SPI_InitStructure.FirstBit = SPI_FB_MSB;
SPI_InitStructure.CRCPoly = 0x0007;
SPI_Init(SPI1, (SPI_InitType*)&SPI_InitStructure);
/* Enable SPI1 */
SPI_Enable(SPI1, ENABLE);
uint8_t ret = 1;
ret = FATFS_LinkDriver((Diskio_drvTypeDef*)&USER_Driver, (char*)USERPath);
if(ret == 0)
printf("fatfs init ok\r\n");
else
printf("fatfs init error\r\n");
delay_ms(500);
ret = f_mount((FATFS*)&fs, "", 0);
printf("mount = %d\r\n",ret);
if( ret != FR_OK)
{
printf("error %s,line %d\r\n",__FILE__, __LINE__);
}
/* Open file to write */
ret = f_open((FIL*)&fil, "first.txt", FA_OPEN_APPEND | FA_READ | FA_WRITE);
printf("open = %d\r\n",ret);
if(ret != FR_OK)
{
printf("error %s,line %d\r\n",__FILE__, __LINE__);
}
/* Check freeSpace space */
if(f_getfree("", (DWORD*)&fre_clust, (FATFS **)&pfs) != FR_OK)
{
printf("error %s,line %d\r\n",__FILE__, __LINE__);
}
totalSpace = (uint32_t)((pfs->n_fatent - 2) * pfs->csize * 0.5);
freeSpace = (uint32_t)(fre_clust * pfs->csize * 0.5);
/* free space is less than 1kb */
if(freeSpace < 1)
{
printf("error %s,line %d\r\n",__FILE__, __LINE__);
}
/* Writing text */
f_puts("XinSheng Tech SD Card I/O Example via SPI\n", (FIL*)&fil);
f_puts("test!!!\n", (FIL*)&fil);
/* Close file */
if(f_close((FIL*)&fil) != FR_OK)
{
printf("error %s,line %d\r\n",__FILE__, __LINE__);
}
/* Open file to read */
if(f_open((FIL*)&fil, "first.txt", FA_READ) != FR_OK)
{
printf("error %s,line %d\r\n",__FILE__, __LINE__);
}
while(f_gets((char*)buffer, sizeof(buffer), (FIL*)&fil))
{
/* SWV output */
printf("%s", buffer);
fflush(stdout);
}
/* Close file */
if(f_close((FIL*)&fil) != FR_OK)
{
printf("error %s,line %d\r\n",__FILE__, __LINE__);
}
/* Unmount SDCARD */
if(f_mount(NULL, "", 1) != FR_OK)
{
printf("error %s,line %d\r\n",__FILE__, __LINE__);
}
while (1)
{
}
}
/** * @brief Configures the different system clocks. */
void RCC_Configuration(void)
{
/* Enable peripheral clocks --------------------------------------------------*/
/* GPIOA, GPIOB and SPI1 clock enable */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_SPI1, ENABLE);
/* SPI2 Periph clock enable */
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_SPI2, ENABLE);
}
/** * @brief Configures the different GPIO ports. */
void GPIO_Configuration(void)
{
GPIO_InitType GPIO_InitStructure;
/* Configure SPI1 pins: SCK, MISO and MOSI ---------------------------------*/
/* Confugure SCK and MOSI pins as Alternate Function Push Pull */
GPIO_InitStructure.Pin = GPIO_PIN_5 | GPIO_PIN_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Confugure MISO pin as Input Floating */
GPIO_InitStructure.Pin = GPIO_PIN_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Confugure NSS pin as Output Push Pull */
GPIO_InitStructure.Pin = GPIO_PIN_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
6 result
from log Let's see if the file is read successfully , from sd Read from the card and confirm that the writing is successful .

边栏推荐
- 2022中式烹調師(高級)試題及在線模擬考試
- Opening and closing principle
- BERT为何无法彻底干掉BM25??
- Quantum frontier hero spectrum - "light quantum Explorer" McMahon: turning any physical system into a neural network
- 你的代码会说话吗?(上)
- 物联网低代码平台常用《组件介绍》
- Analysis and processing of GPS data format [easy to understand]
- i++ , ++i
- flutter 系列之:flutter 中的 offstage
- Ionq and Ge research confirmed that quantum computing has great potential in risk aggregation
猜你喜欢

sort

openGauss内核:SQL解析过程分析

Talking from the little nematode -- tracing the evolution of nervous system and starting life simulation

Flutter series: offstage in flutter

Dry goods | how to calculate the KPI of scientific researchers, and what are the h index and G index

Recommended practice sharing of Zhilian recruitment based on Nebula graph

Opengauss kernel: analysis of SQL parsing process

Euler equation: a truly perfect formula in the history of mathematics!

IonQ联合GE Research证实:量子计算在风险聚合上有巨大潜力

Ionq and Ge research confirmed that quantum computing has great potential in risk aggregation
随机推荐
Template_ Large integer multiplication
华泰证券开户有什么后果 怎么办理开户最安全
What is generics and how to use generics analysis
RSLO:自监督激光雷达里程计(实时+高精度,ICRA2022)
BERT为何无法彻底干掉BM25??
2022 Chinese cook (Advanced) test questions and online simulation test
药物发现新方法,阿斯利康团队通过课程学习改进从头分子设计
How to count dimensions of foreign trade E-mail Promotion
如何备份mysql_史上最全的MYSQL备份方法
一个bug肝一周...忍不住提了issue
基于 Nebula Graph 构建百亿关系知识图谱实践
有效提高绩效面谈的10个关键点
IonQ联合GE Research证实:量子计算在风险聚合上有巨大潜力
Native JS implements drag and drop of page elements
线程终止的 4 种方式
量子前沿英雄谱|“光量子探险家”McMahon:将任何物理系统变成神经网络
Reverse a stack with recursive functions and stack operations only
叮!Techo Day 腾讯技术开放日如约而至!
2022 welder (technician) examination question bank simulated examination platform operation
2022金属非金属矿山安全检查(地下矿山)复训题库及在线模拟考试