IIC初始化 首先定义iic初始化结构体,如下:
///i2c定义 typedef struct { I2C_TypeDef* i2cPort; ///i2cx uint32_t i2cPerif; ///i2c时钟 uint32_t i2cEVIRQn; ///i2c事件中断 uint32_t i2cERIRQn; ///i2c错误处理中断 uint32_t i2cClockSpeed; ///通信速率 uint32_t gpioSCLPerif; ///scl时钟 GPIO_TypeDef* gpioSCLPort; ///scl端口 uint32_t gpioSCLPin; ///scl引脚 uint32_t gpioSCLPinSource; ///scl引脚source uint32_t gpioSDAPerif; ///sda时钟 GPIO_TypeDef* gpioSDAPort; ///sda端口 uint32_t gpioSDAPin; ///sda引脚 uint32_t gpioSDAPinSource; ///sda端口source
uint32_t gpioAF; ///复用pack
uint32_t dmaPerif; ///dma时钟 uint32_t dmaChannel; ///dma通道 DMA_Stream_TypeDef* dmaRxStream; ///dma数据流 uint32_t dmaRxIRQ; ///dma中断 uint32_t dmaRxTCFlag; ///dma接收完成中断 uint32_t dmaRxTEFlag; ///dma接收错误中断
} I2cDef;
这里包含了stm32 的iic初始化需要的所有信息,这个结构体最终包含在I2cDrv上,如下:
///i2c驱动结构体 typedef struct { const I2cDef *def; //< i2c定义 I2cMessage txMessage; //< i2c通信message uint32_t messageIndex; //< 发送或者接收字节的索引 SemaphoreHandle_t isBusFreeSemaphore; //< 信号量用来同步传输 SemaphoreHandle_t isBusFreeMutex; //< 互斥信号量来保护传输数据 DMA_InitTypeDef DMAStruct; //< DMA 配置 } I2cDrv; 注意const I2cDef *def; 这表明I2cDrv包含了I2cDef的指针。
|