当前位置:网站首页>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);
}
边栏推荐
- Investment demand and income forecast report of China's building ceramics industry, 2022-2028
- In VS_ In 2019, scanf and other functions are used to prompt the error of unsafe functions
- EPF: a fuzzy testing framework for network protocols based on evolution, protocol awareness and coverage guidance
- Hcip day 15 notes
- Hcip day 12 notes
- Selenium check box
- Gossip about redis source code 82
- Ramble 72 of redis source code
- finalize finalization finally final
- How to prevent malicious crawling of information by one-to-one live broadcast source server
猜你喜欢
![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~

I wrote a chat software with timeout connect function

Investment demand and income forecast report of China's building ceramics industry, 2022-2028

2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) simulated examination and Guangdong Provincial Safety Officer a certificate third batch (main person in charg

Flutter internationalized Intl
![[note] IPC traditional interprocess communication and binder interprocess communication principle](/img/f6/36c28df02198539e27352e3cdf4ba6.jpg)
[note] IPC traditional interprocess communication and binder interprocess communication principle

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

How to quickly build high availability of service discovery

Fluent learning (5) GridView

Hcip day 15 notes
随机推荐
Generic tips
URLEncoder. Encode and urldecoder Decode processing URL
Format cluster and start cluster
ThreadLocal function, scene and principle
FPGA tutorial and Allegro tutorial - link
In VS_ In 2019, scanf and other functions are used to prompt the error of unsafe functions
How to solve the "safe startup function prevents the operating system from starting" prompt when installing windows10 on parallel desktop?
[note] IPC traditional interprocess communication and binder interprocess communication principle
Summary of fluent systemchrome
Gossip about redis source code 80
Gossip about redis source code 82
Deep learning ----- using NN, CNN, RNN neural network to realize MNIST data set processing
D23:multiple of 3 or 5 (multiple of 3 or 5, translation + solution)
Qtoolbutton - menu and popup mode
Gossip about redis source code 73
Op amp related - link
Blue Bridge Cup -- guess age
JarPath
Errors taken 1 Position1 argument but 2 were given in Mockingbird
Weekly leetcode - nc9/nc56/nc89/nc126/nc69/nc120