当前位置:网站首页>STM32LL library use - SPI communication
STM32LL library use - SPI communication
2022-08-02 15:28:00 【Tianshan no longevity tea】
STM32使用前的准备
对于要使用的每个STM32芯片,首先我们手上必备的两本手册(ST官网有pdf版):
- 参考手册(Reference manual)
- 数据手册(Datasheet)
其中参考手册包括各个功能模块的具体信息、原理、各种工作模式介绍、配置方法以及寄存器相关信息;数据手册包括芯片的基本参数、引脚数量与各自功能、电气特性、封装信息等内容.一般在选型与硬件设计阶段,参考数据手册多一些,而到了程序设计阶段,参考手册就是必须的了.
以下是STM32G4系列的参考手册的“自我介绍”:
This reference manual targets application developers. It provides complete information on how to use the STM32G4 Series microcontroller memory and peripherals.
本参考手册的目标是应用程序开发人员. 它提供了关于的完整信息如何使用STM32G4系列单片机的内存和外设.
SPI相关设置
我们以STM32G系列为例,直接翻到SPI章节,SPI结构示意图如下:
一共4pins can be connected to peripherals:
- MISO(Master In / Slave Out data):该引脚在从模式下发送数据,在主模式下接收数据
- MOSI(Master Out / Slave In data):该引脚在主模式下发送数据,在从模式下接收数据
- SCK(Serial Clock):The clock signal transmitted from the master device to the slave device
- NSS(Slave select):Used by the master to select the slave
The schematic diagram of the full-duplex communication mode between a single master device and a single slave device is as follows:
我们采用STM32CubeMXBasic configuration can be easily completed,Actually, you only need to write the code required for actual communication as follows
Basic communication code
Tx:发送缓冲区;Rx:接收缓冲区;DR:数据寄存器
Status indicator:
- Tx buffer empty flag (TXE):发送缓冲区为空
- Rx buffer not empty (RXNE):接收缓冲区非空
- Busy flag (BSY):SPIData is being transferred
基本工作原理:The master sends a value to the slave(指令),The slave then returns a value according to the received command
We use it in common casesSTM32作为主机
- 等待TXE标志置1(Tx空),Indicates that the buffer is sent at this timeTxThere are no values to send in
- 将数据写入SPIx_DR寄存器,对DRThe write operation will write the dataTx末尾
- 等待BSY标志置0(即busy,置1表明TxThe data in is being transferred).During the data passMOSI发送给从机,The information returned by the slave is passedMISOBack to the host receive bufferRx
- 等待RXNE标志置1(Rx非空),表明此时RxThe received value exists
- 读取SPIx_DR寄存器,对DR的读操作将返回RxThe earliest value in
// data_in:待发送的值
// data_out:接收到的值
static int spi_transmit_receive(uint16_t data_in, uint16_t *data_out){
int state = 0;
*data_out = 0;
uint32_t timeout_cnt;
static const uint32_t timeout_cnt_num = 10000;
// Wait until TXE flag is set to send data
timeout_cnt = 0;
while(!LL_SPI_IsActiveFlag_TXE(SPI1)){
timeout_cnt ++;
if(timeout_cnt > timeout_cnt_num){
state = -1;
break;
}
}
// Transmit data in 16 Bit mode
LL_SPI_TransmitData16(SPI1, data_in);
// Check BSY flag
timeout_cnt = 0;
while(LL_SPI_IsActiveFlag_BSY(SPI1)){
timeout_cnt ++;
if(timeout_cnt > timeout_cnt_num){
state = -1;
break;
}
}
// Check RXNE flag
timeout_cnt = 0;
while(!LL_SPI_IsActiveFlag_RXNE(SPI1)){
timeout_cnt ++;
if(timeout_cnt > timeout_cnt_num){
state = -1;
break;
}
}
// Read 16-Bits in the data register
*data_out = LL_SPI_ReceiveData16(SPI1);
return state;
}
所涉及的LL库相关函数:
/** * @brief Write 16-Bits in the data register * @rmtoll DR DR LL_SPI_TransmitData16 * @param SPIx SPI Instance * @param TxData Value between Min_Data=0x00 and Max_Data=0xFFFF * @retval None */ __STATIC_INLINE void LL_SPI_TransmitData16(SPI_TypeDef *SPIx, uint16_t TxData) { #if defined (__GNUC__) __IO uint16_t *spidr = ((__IO uint16_t *)&SPIx->DR); *spidr = TxData; #else SPIx->DR = TxData; #endif /* __GNUC__ */ } /** * @brief Read 16-Bits in the data register * @rmtoll DR DR LL_SPI_ReceiveData16 * @param SPIx SPI Instance * @retval RxData Value between Min_Data=0x00 and Max_Data=0xFFFF */ __STATIC_INLINE uint16_t LL_SPI_ReceiveData16(SPI_TypeDef *SPIx) { return (uint16_t)(READ_REG(SPIx->DR)); } /** * @brief Check if Tx buffer is empty * @rmtoll SR TXE LL_SPI_IsActiveFlag_TXE * @param SPIx SPI Instance * @retval State of bit (1 or 0). */ __STATIC_INLINE uint32_t LL_SPI_IsActiveFlag_TXE(SPI_TypeDef *SPIx) { return ((READ_BIT(SPIx->SR, SPI_SR_TXE) == (SPI_SR_TXE)) ? 1UL : 0UL); } /** * @brief Get busy flag * @note The BSY flag is cleared under any one of the following conditions: * -When the SPI is correctly disabled * -When a fault is detected in Master mode (MODF bit set to 1) * -In Master mode, when it finishes a data transmission and no new data is ready to be * sent * -In Slave mode, when the BSY flag is set to '0' for at least one SPI clock cycle between * each data transfer. * @rmtoll SR BSY LL_SPI_IsActiveFlag_BSY * @param SPIx SPI Instance * @retval State of bit (1 or 0). */ __STATIC_INLINE uint32_t LL_SPI_IsActiveFlag_BSY(SPI_TypeDef *SPIx) { return ((READ_BIT(SPIx->SR, SPI_SR_BSY) == (SPI_SR_BSY)) ? 1UL : 0UL); } /** * @brief Check if Rx buffer is not empty * @rmtoll SR RXNE LL_SPI_IsActiveFlag_RXNE * @param SPIx SPI Instance * @retval State of bit (1 or 0). */ __STATIC_INLINE uint32_t LL_SPI_IsActiveFlag_RXNE(SPI_TypeDef *SPIx) { return ((READ_BIT(SPIx->SR, SPI_SR_RXNE) == (SPI_SR_RXNE)) ? 1UL : 0UL); }
实际使用spi_transmit_receive函数时,Generally, the following chip selects need to be set before and after the functionNSS输出,Select the current slave before communication,Communication and then close,Prevent conflicts in multi-master or multi-slave mode.
// NSSChip select pin setting0,Open the current master-slave relationship
LL_GPIO_ResetOutputPin(GPIOx, LL_GPIO_PIN_x);
spi_transmit_receive(controlword, &recbuff);
// NSS置1,Close the current master-slave relationship
LL_GPIO_SetOutputPin(GPIOx, LL_GPIO_PIN_x);
边栏推荐
- 使用npx -p @storybook/cli sb init安装失败,手把手搭建专属的storybook
- 基于矩阵计算的线性回归分析方程中系数的估计
- Win11没有本地用户和组怎么解决
- What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
- Win7怎么干净启动?如何只加载基本服务启动Win7系统
- Win11系统找不到dll文件怎么修复
- Mysql connection error solution
- pygame绘制弧线
- 开心一下,9/28名场面合集
- 将SSE指令转换为ARM NEON指令
猜你喜欢
Win10 computer can't read U disk?Don't recognize U disk how to solve?
Spark及相关生态组件安装配置——快速回忆
A clean start Windows 7?How to load only the basic service start Windows 7 system
GICv3/v4-软件概述
CI24R1小模块2.4G收发模块无线通信低成本兼容si24r1/XN297超低功耗
FP7195转模拟调光技术解决智能家居调光频闪和电感噪音的原理
win11一直弹出用户账户控制怎么解决
刷卡芯片CI520可直接PIN对PIN替换CV520支持SPI通讯接口
为android系统添加产品的过程
Win11声卡驱动如何更新?Win11声卡驱动更新方法
随机推荐
Win11 system cannot find dll file how to fix
jest测试,组件测试
SQL的通用语法和使用说明(图文)
ASR6601牛羊定位器芯片GPS国内首颗支持LoRa的LPWAN SoC
设备驱动框架简介
TypeScript 快速进阶
推开机电的大门《电路》(一):电压,电流,参考方向
What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
MATLAB绘图函数plot详解
A clean start Windows 7?How to load only the basic service start Windows 7 system
Golang 垃圾回收机制详解
DP1101兼容CC1101是SUB1GHz无线收发芯片应用于智能家居
FP7122降压恒流内置MOS耐压100V共正极阳极PWM调光方案原理图
arm push/pop/b/bl汇编指令
小T成长记-网络篇-1-什么是网络?
win10系统更新错误代码0x80244022怎么办
为vscode配置clangd
Win11 computer off for a period of time without operating network how to solve
How to reinstall Win7 system with U disk?How to reinstall win7 using u disk?
MATLAB制作简易小动画入门详解