当前位置:网站首页>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);
边栏推荐
- FP7122降压恒流内置MOS耐压100V共正极阳极PWM调光方案原理图
- 编译error D8021 :无效的数值参数“/Wextra” cl command line error d8021 invalid numeric argument ‘/wextra‘
- Win11系统找不到dll文件怎么修复
- FP6293电池升压5V-12V大电流2APWM模式升压方案
- 2020-02-06-快速搭建个人博客
- Win7遇到错误无法正常开机进桌面怎么解决?
- STM32LL库——USART中断接收不定长信息
- 一篇文章彻底理解Redis的持久化:RDB、AOF
- Daily - Notes
- 7. How to add the Click to RecyclerView and LongClick events
猜你喜欢
推开机电的大门《电路》(二):功率计算与判断
Open the door of power and electricity "Circuit" (2): Power Calculation and Judgment
flink+sklearn——使用jpmml实现flink上的机器学习模型部署
Win10无法连接打印机怎么办?不能使用打印机的解决方法
将SSE指令转换为ARM NEON指令
How to solve Win11 without local users and groups
MATLAB绘图命令fimplicit绘制隐函数图形入门详解
[STM32 Learning 1] Basic knowledge and concepts are clear
Use tencent cloud builds a personal blog
Yolov5 official code reading - prior to transmission
随机推荐
pygame拖动条的实现方法
MATLAB绘图函数plot详解
使用 腾讯云搭建一个个人博客
CI24R1小模块2.4G收发模块无线通信低成本兼容si24r1/XN297超低功耗
Yolov5 official code reading - prior to transmission
FP7126降压恒流65536级高辉无频闪调光共阳极舞台灯RGB驱动方案
FP7195芯片PWM转模拟调光至0.1%低亮度时恒流一致性的控制原理
小T成长记-网络篇-1-什么是网络?
CMAKE
关于c语言的调试技巧
网络安全抓包
What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
cmake配置libtorch报错Failed to compute shorthash for libnvrtc.so
编译error D8021 :无效的数值参数“/Wextra” cl command line error d8021 invalid numeric argument ‘/wextra‘
Redis的线程模型
How to set the win10 taskbar does not merge icons
Makefile容易犯错的语法
pygame图像连续旋转
FP7195转模拟恒流调光芯片在机器视觉光源的应用优势
Win11没有本地用户和组怎么解决