当前位置:网站首页>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);
}
边栏推荐
- Minimum commission for stock account opening. Stock account opening is free. Is online account opening safe
- Enter MySQL in docker container by command under Linux
- Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
- ADB related commands
- ThreadLocal function, scene and principle
- 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
- How to quickly build high availability of service discovery
- D30:color tunnels (color tunnels, translation)
- D27:mode of sequence (maximum, translation)
- 33 restrict the input of qlineedit control (verifier)
猜你喜欢

I wrote a chat software with timeout connect function

How to make recv have a little temper?

Learning notes of raspberry pie 4B - IO communication (SPI)

Common mode interference of EMC

How to quickly build high availability of service discovery

Design of logic level conversion in high speed circuit

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
![Yyds dry goods inventory [practical] simply encapsulate JS cycle with FP idea~](/img/af/1975b37d81bbdb9709ff181b9a72f9.jpg)
Yyds dry goods inventory [practical] simply encapsulate JS cycle with FP idea~

Current detection circuit - including op amp current scheme

Alibaba cloud container service differentiation SLO hybrid technology practice
随机推荐
Get current JVM data
Enter MySQL in docker container by command under Linux
URLEncoder. Encode and urldecoder Decode processing URL
Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
Amway by head has this project management tool to improve productivity in a straight line
EPF: a fuzzy testing framework for network protocols based on evolution, protocol awareness and coverage guidance
Day30-t540-2022-02-14-don't answer by yourself
2022 a special equipment related management (elevator) examination questions and a special equipment related management (elevator) examination contents
D27:mode of sequence (maximum, translation)
股票開戶傭金最低的券商有哪些大家推薦一下,手機上開戶安全嗎
Op amp related - link
Tencent interview: can you pour water?
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
finalize finalization finally final
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
C # basic knowledge (2)
Creation of the template of the password management software keepassdx
Deep learning ----- using NN, CNN, RNN neural network to realize MNIST data set processing
Hcip day 14 notes
Errors taken 1 Position1 argument but 2 were given in Mockingbird