当前位置:网站首页>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);
}
边栏推荐
- Day30-t540-2022-02-14-don't answer by yourself
- Comparable interface and comparator interface
- Fluent learning (4) listview
- Hcip day 14 notes
- 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?
- D25:sequence search (sequence search, translation + problem solving)
- 2022.02.13
- Weekly leetcode - nc9/nc56/nc89/nc126/nc69/nc120
- Powerful blog summary
- Recursion and recursion
猜你喜欢
Gorilla/mux framework (RK boot): add tracing Middleware
Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
How to solve the "safe startup function prevents the operating system from starting" prompt when installing windows10 on parallel desktop?
2022 free examination questions for hoisting machinery command and hoisting machinery command theory examination
SDMU OJ#P19. Stock trading
Shell script three swordsman awk
Ppt image processing
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
How to make icons easily
Esp-idf turns off serial port log output.
随机推荐
股票开户佣金最低的券商有哪些大家推荐一下,手机上开户安全吗
Fudan 961 review
[network security] what is emergency response? What indicators should you pay attention to in emergency response?
Maxwell equation and Euler formula - link
Investment demand and income forecast report of China's building ceramics industry, 2022-2028
D23:multiple of 3 or 5 (multiple of 3 or 5, translation + solution)
[note] IPC traditional interprocess communication and binder interprocess communication principle
"Learning notes" recursive & recursive
Learning notes of raspberry pie 4B - IO communication (SPI)
ThreadLocal function, scene and principle
Blue Bridge Cup -- guess age
D30:color tunnels (color tunnels, translation)
Actual combat | use composite material 3 in application
QT creator source code learning note 05, how does the menu bar realize plug-in?
Get current JVM data
How to prevent malicious crawling of information by one-to-one live broadcast source server
Hcip day 14 notes
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
Common mode interference of EMC
Pointer concept & character pointer & pointer array yyds dry inventory