当前位置:网站首页>SPI based on firmware library
SPI based on firmware library
2022-07-03 23:31:00 【Clear glass, brilliant orange】
SPI Structure
typedef struct
{
uint16_t SPI_Direction; // Direction
uint16_t SPI_Mode; // Pattern
uint16_t SPI_DataSize; // data size
uint16_t SPI_CPOL; // Clock polarity
uint16_t SPI_CPHA; // Clock phase
uint16_t SPI_NSS; //NSS position
uint16_t SPI_BaudRatePrescaler; // Baud rate
uint16_t SPI_FirstBit; // Select whether the data transmission direction starts high or low
uint16_t SPI_CRCPolynomial; //CRC Check bit
}SPI_InitTypeDef;
SPI The configuration process
① Configure pins , Can make the clock
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
② initialization SPI, Set the working mode
void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct);
③ Can make SPIx
void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);
④SPI To transmit data
void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data); uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx);
⑤ see SPI Transmission status
FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);
initialization SPI
void SPI2_Init(void)//SPI2 initialization Main mode
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable SPI1 and GPIOA clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
/* Configure SPI1 pins: NSS, SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;//SCK MOSI
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//PB13/14/15 Multiplexing push pull output
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);//PB13/14/15 Pull up
/* SPI2 configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //SPI2 Set to two-wire full duplex
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; // Set up SPI2 Main mode
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //SPI Send receive 8 Bit frame structure
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; // The serial clock does not operate in the state ( Free ) when , The clock is high
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; // The second clock edge starts sampling data ( Here is the rising edge acquisition data )
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS The signal is controlled by software ( Use SSI position ) management
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; // Define the baud rate prescaler value : Baud rate prescaler value is 8, After frequency division, it is 9MHZ
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; // Data transfer from MSB Bit start high
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC The polynomial of the value calculation
SPI_Init(SPI2, &SPI_InitStructure);
/* Enable SPI2 */
SPI_Cmd(SPI2, ENABLE); // Can make SPI1 peripherals
}
Read and write bytes 

u8 SPI2_ReadWriteByte(u8 Data)
{
unsigned char t = 0;
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET) /* Constantly check whether the flag is 0 Send empty */
{
t++;
if(t>=200) /* Overtime */
{
return 0;
}
}
SPI_I2S_SendData(SPI2, Data); /* send data */
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET) /* be equal to 0 That is, the reception is empty */
{
t++;
if(t>=200)
{
return 0;
}
}
return SPI_I2S_ReceiveData(SPI2); /* Return the received data */
}
Set up SPI Baud rate
stay STM32 Firmware library and provided routines , You can see it everywhere assert_param() Use . If you open any of the routines stm32f10x_conf.h file , You can see that actually assert_param Is a macro definition ;
In the firmware library , Its function is to detect whether the parameter passed to the function is a valid parameter .
Illustrate with examples :
assert_param(IS_USART_ALL_PERIPH(USARTx));
This code is used to check parameters USARTx Whether it works , among IS_USART_ALL_PERIPH(USARTx) Is a macro definition , as follows :
#define IS_USART_ALL_PERIPH(PERIPH) (((PERIPH) == USART1) || \ ((PERIPH) == USART2) || \ ((PERIPH) == USART3) || \ ((PERIPH) == USART4) || \ ((PERIPH) == USART5) || \ ((PERIPH) == USART6) || \ ((PERIPH) == USART7) || \ ((PERIPH) == USART8))
The function of macro definition is parameter USARTx yes USART1~USART8 One of them , Said parameters USARTx It works , return true, Otherwise return to false.
void SPI2_SetSpeed(u8 SPI_BaudRatePrescaler)
{
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler)); /* The parameter is frequency division */
SPI2->CR1 &= 0XFFC7; //&1111 1111 1100 0111 /* hold D3~D4 Zero clearing
SPI2->CR1 |= SPI_BaudRatePrescaler; /* Here is the handle. SPI_BaudRatePrescaler The value of is equal to SPI2->CR1 Press the value in 2 Add in hexadecimal */
/* If SPI_BaudRatePrescaler The value of is 0x038, Binary numbers are 00111000 And 1111 1111 1100 0111 The result is 1111 1111 1111 1111->0XFFFF;*/
SPI_Cmd(SPI2, ENABLE);
}
边栏推荐
- Gossip about redis source code 78
- After the Lunar New Year and a half
- Enter MySQL in docker container by command under Linux
- [MySQL] sql99 syntax to realize multi table query
- QT creator source code learning note 05, how does the menu bar realize plug-in?
- Comment obtenir une commission préférentielle pour l'ouverture d'un compte en bourse? Est - ce que l'ouverture d'un compte en ligne est sécurisée?
- Pandaoxi's video
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- Recursion and recursion
- Qtoolbutton - menu and popup mode
猜你喜欢

Enter MySQL in docker container by command under Linux

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

How to write a good title of 10w+?

SDMU OJ#P19. Stock trading
![[network security] what is emergency response? What indicators should you pay attention to in emergency response?](/img/ff/c733ffbb922760910ab09af3ae2886.jpg)
[network security] what is emergency response? What indicators should you pay attention to in emergency response?

Hcip day 15 notes

JDBC Technology

Interesting 10 CMD commands

leetcode-43. String multiplication

Idea integrates Microsoft TFs plug-in
随机推荐
2022 a special equipment related management (elevator) examination questions and a special equipment related management (elevator) examination contents
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
In VS_ In 2019, scanf and other functions are used to prompt the error of unsafe functions
[MySQL] sql99 syntax to realize multi table query
Ramble 72 of redis source code
How about opening an account at Hengtai securities? Is it safe?
How to make recv have a little temper?
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Pyqt5 sensitive word detection tool production, operator's Gospel
Simple solution of m3u8 file format
Gossip about redis source code 75
Fashion cloud interview questions series - JS high-frequency handwritten code questions
ThreadLocal function, scene and principle
File copy method
[Happy Valentine's day] "I still like you very much, like sin ² a+cos ² A consistent "(white code in the attached table)
Unity shader visualizer shader graph
Scratch uses runner Py run or debug crawler
Idea set class header comments
Get current JVM data
How to write a good title of 10w+?