当前位置:网站首页>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);
}
边栏推荐
- Hcip day 16 notes
- Flutter internationalized Intl
- FPGA tutorial and Allegro tutorial - link
- . Net ADO splicing SQL statement with parameters
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- [source code] VB6 chat robot
- "Learning notes" recursive & recursive
- 2022.02.13
- Fluent learning (5) GridView
- Interesting 10 CMD commands
猜你喜欢

Shiftvit uses the precision of swing transformer to outperform the speed of RESNET, and discusses that the success of Vit does not lie in attention!

Deep learning ----- using NN, CNN, RNN neural network to realize MNIST data set processing

Qtoolbutton available signal

Unity shader visualizer shader graph

SDMU OJ#P19. Stock trading

In VS_ In 2019, scanf and other functions are used to prompt the error of unsafe functions

QT creator source code learning note 05, how does the menu bar realize plug-in?

"Learning notes" recursive & recursive
![[source code] VB6 chat robot](/img/89/46b67f627c8257eaddc70a247c9ba5.jpg)
[source code] VB6 chat robot

C deep anatomy - the concept of keywords and variables # dry inventory #
随机推荐
Mongoose the table associated with the primary key, and automatically bring out the data of another table
Get current JVM data
D30:color tunnels (color tunnels, translation)
Learning methods of zynq
Fluent learning (5) GridView
Day30-t540-2022-02-14-don't answer by yourself
2022 examination of safety production management personnel of hazardous chemical production units and examination skills of safety production management personnel of hazardous chemical production unit
Enter MySQL in docker container by command under Linux
Hcip 13th day notes
Gossip about redis source code 82
Fashion cloud interview questions series - JS high-frequency handwritten code questions
Hcip day 15 notes
D24:divisor and multiple (divisor and multiple, translation + solution)
finalize finalization finally final
How to write a good title of 10w+?
After the Lunar New Year and a half
D27:mode of sequence (maximum, translation)
[MySQL] sql99 syntax to realize multi table query
[Happy Valentine's day] "I still like you very much, like sin ² a+cos ² A consistent "(white code in the attached table)
Cgb2201 preparatory class evening self-study and lecture content