当前位置:网站首页>[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 .

边栏推荐
- Clipping of raster vector data
- Reading notes of Mr. toad going to see a psychologist
- A queue of two stacks
- 由两个栈组成的队列
- 验证回文串
- How to design data visualization platform
- 2022 questions d'examen pour les cuisiniers chinois (Senior) et l'examen de simulation en ligne
- Connected to rainwater series problems
- CVPR disputes again: IBM's Chinese draft papers were accused of copying idea, who won the second place in the competition
- 木兰开放作品许可证1.0面向社会公开征求意见
猜你喜欢

BERT为何无法彻底干掉BM25??

线程的生命周期以及其中的方法

Nature | mapping the interaction map of plant foliar flora to establish genotype phenotype relationship

How to count dimensions of foreign trade E-mail Promotion

3. Overall UI architecture of the project

How to design data visualization platform

Tencent cloud international ECS has no network after logging in. How to troubleshoot?

Adding virtual environments to the Jupiter notebook

The time schedule for the soft test in the second half of 2022 has been determined!

中国内地仅四家突围 联想智慧颐和园荣获 “2022年IDC亚太区智慧城市大奖”
随机推荐
(original) [Maui] realize "floating action button" step by step
量子前沿英雄谱|“光量子探险家”McMahon:将任何物理系统变成神经网络
Gas station (greedy)
2022金属非金属矿山(地下矿山)主要负责人考试模拟100题模拟考试平台操作
Template_ Large integer multiplication
Luogu_ P1303 A*B Problem_ High precision calculation
你的代碼會說話嗎?(上)
The time schedule for the soft test in the second half of 2022 has been determined!
New drug discovery methods, AstraZeneca team improves ab initio molecular design through course learning
单一职责原则
Black apple installation tutorial OC boot "suggestions collection"
Talking from the little nematode -- tracing the evolution of nervous system and starting life simulation
DevEco Studio 3.0编辑器配置技巧篇
3. Overall UI architecture of the project
【中移芯昇】5. spi接口测试tf卡
@ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常
Reading notes of Mr. toad going to see a psychologist
RSLO:自监督激光雷达里程计(实时+高精度,ICRA2022)
2022中式烹調師(高級)試題及在線模擬考試
[codec] write H264 decoder (1) from scratch