Previously, I used the RT1052 development board, but now STM32 is obviously more popular among everyone. Therefore, I am providing this C file for everyone to benefit from the driver I wrote earlier.
#include "bsp_lpspi.h"
SPI_HandleTypeDef SpiHandle;
void Spi_delay(uint32_t count){ volatile uint32_t i = 0; for (i = 0; i < count; ++i) { __asm("NOP"); /* Call nop empty instruction */ __asm("NOP"); /* Call nop empty instruction */ __asm("NOP"); /* Call nop empty instruction */ __asm("NOP"); /* Call nop empty instruction */ __asm("NOP"); /* Call nop empty instruction */ }}
void LPSPI_Init(void){ GPIO_InitTypeDef GPIO_InitStruct; /* Enable SPI1 clock */ SPIx_CLK_ENABLE(); SPIx_SCK_GPIO_CLK_ENABLE(); SPIx_MISO_GPIO_CLK_ENABLE(); SPIx_MOSI_GPIO_CLK_ENABLE(); SPIx_CS_GPIO_CLK_ENABLE(); /* Initialize GPIO configuration*/ /*##-1- Configure peripheral GPIO ##########################################*/ /* SPI SCK GPIO pin configuration */ GPIO_InitStruct.Pin = SPIx_SCK_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLDOWN; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Alternate = SPIx_SCK_AF; HAL_GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStruct);
/* SPI MISO GPIO pin configuration */ GPIO_InitStruct.Pin = SPIx_MISO_PIN; GPIO_InitStruct.Alternate = SPIx_MISO_AF; HAL_GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStruct);
/* SPI MOSI GPIO pin configuration */ GPIO_InitStruct.Pin = SPIx_MOSI_PIN; GPIO_InitStruct.Alternate = SPIx_MOSI_AF; HAL_GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStruct); /* SPI CS GPIO pin configuration */ GPIO_InitStruct.Pin = SPIx_CS_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLDOWN; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(SPIx_CS_GPIO_PORT, &GPIO_InitStruct); /* Initialize SPI configuration*/ /*##-2- Configure the SPI peripheral #######################################*/ /* Set the SPI parameters */ SpiHandle.Instance = SPIx; SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256; SpiHandle.Init.Direction = SPI_DIRECTION_2LINES; SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE; SpiHandle.Init.CLKPolarity = SPI_POLARITY_HIGH; SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT; SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB; SpiHandle.Init.TIMode = SPI_TIMODE_DISABLE; SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; SpiHandle.Init.CRCPolynomial = 7; SpiHandle.Init.NSS = SPI_NSS_SOFT; SpiHandle.Init.Mode = SPI_MODE_MASTER; // Initialize SPI HAL_SPI_Init(&SpiHandle); // Set CS pin high by default HAL_GPIO_WritePin(SPIx_CS_GPIO_PORT,SPIx_CS_PIN,GPIO_PIN_SET);
}
void Lpspi_Write(uint8_t datasize,uint8_t *data){ // Write data HAL_GPIO_WritePin(SPIx_CS_GPIO_PORT,SPIx_CS_PIN,GPIO_PIN_RESET); HAL_SPI_Transmit(&SpiHandle,data, datasize, 5000); Spi_delay(30); HAL_GPIO_WritePin(SPIx_CS_GPIO_PORT,SPIx_CS_PIN,GPIO_PIN_SET);}
void Lpspi_Read(uint8_t datasize,uint8_t *data,uint8_t *Rxdata){// Read and write data HAL_GPIO_WritePin(SPIx_CS_GPIO_PORT,SPIx_CS_PIN,GPIO_PIN_RESET); HAL_SPI_TransmitReceive(&SpiHandle,data,Rxdata, datasize, 5000); Spi_delay(30); HAL_GPIO_WritePin(SPIx_CS_GPIO_PORT,SPIx_CS_PIN,GPIO_PIN_SET);}
/* analogysemi xutong 2023/10/19*/
#ifndef __BSP_LPSPI_H
#define __BSP_LPSPI_H
#include "stm32h7xx_hal.h"
#define SPIx SPI1
#define SPIx_CLK_ENABLE() __HAL_RCC_SPI1_CLK_ENABLE()
#define SPIx_SCK_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define SPIx_MISO_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define SPIx_MOSI_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define SPIx_CS_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
/* Definition for SPIx Pins */
#define SPIx_SCK_PIN GPIO_PIN_3
#define SPIx_SCK_GPIO_PORT GPIOB
#define SPIx_SCK_AF GPIO_AF5_SPI1
#define SPIx_MISO_PIN GPIO_PIN_4
#define SPIx_MISO_GPIO_PORT GPIOB
#define SPIx_MISO_AF GPIO_AF5_SPI1
#define SPIx_MOSI_PIN GPIO_PIN_5
#define SPIx_MOSI_GPIO_PORT GPIOB
#define SPIx_MOSI_AF GPIO_AF5_SPI1
#define SPIx_CS_PIN GPIO_PIN_6
#define SPIx_CS_GPIO_PORT GPIOB
void LPSPI_Init(void);
void Lpspi_Write(uint8_t datasize,uint8_t *data);
void Lpspi_Read(uint8_t datasize,uint8_t *data,uint8_t *Rxdata);
#endif /* __BSP_LPSPI_H */
Drivers available for use:
Analogous to motor driver DR7808 driver rev1
ADX516 driver
A Tesla owner in Wenzhou, Leqing, with license plate ZheCDR1371 suddenly stopped on the highway, causing me to be rear-ended!