前言
引用参考:
头文件
/*******************************************************************************
Copyright (c) [scl]。保留所有权利。
******************************************************************************/
#ifndef STM32_F1XX_TEMPLATE_BSP_I2C_H
#define STM32_F1XX_TEMPLATE_BSP_I2C_H
#include "bsp_include.h"
#define I2C_DEFAULT_CNF {\
.ClockSpeed = 100000,\
.DutyCycle = I2C_DUTYCYCLE_2,\
.OwnAddress1 = 0,\
.AddressingMode = I2C_ADDRESSINGMODE_7BIT,\
.DualAddressMode = I2C_DUALADDRESS_DISABLE,\
.OwnAddress2 = 0,\
.GeneralCallMode = I2C_GENERALCALL_DISABLE,\
.NoStretchMode = I2C_NOSTRETCH_DISABLE,\
}
// PB6------> I2C1_SCL PB7------> I2C1_SDA
#define I2C1_SCL PB6
#define I2C1_SDA PB7
void i2c1_gpio_msp_init();
void i2c1_gpio_msp_de_init();
// PB10------> I2C2_SCL PB11------> I2C2_SDA
#define I2C2_SCL PB10
#define I2C2_SDA PB11
void i2c2_gpio_msp_init();
void i2c2_gpio_msp_de_init();
void bsp_I2cSimpleInit(I2C_HandleTypeDef *handle);
sys_force_static_inline void bsp_I2cInit(I2C_HandleTypeDef *handle, uint32_t own_addr) {
handle->Init.OwnAddress1 = own_addr;
bsp_I2cSimpleInit(handle);
}
#endif //STM32_F1XX_TEMPLATE_BSP_I2C_H
源文件
/*******************************************************************************
Copyright (c) [scl]。保留所有权利。
******************************************************************************/
#include "bsp_i2c.h"
void HAL_I2C_MspInit(I2C_HandleTypeDef *i2cHandle) {
if (i2cHandle->Instance == I2C1) {
__HAL_RCC_I2C1_CLK_ENABLE();
i2c1_gpio_msp_init();
} else if (i2cHandle->Instance == I2C2) {
__HAL_RCC_I2C2_CLK_ENABLE();
i2c2_gpio_msp_init();
}
}
void HAL_I2C_MspDeInit(I2C_HandleTypeDef *i2cHandle) {
if (i2cHandle->Instance == I2C1) {
__HAL_RCC_I2C1_CLK_DISABLE();
i2c1_gpio_msp_de_init();
} else if (i2cHandle->Instance == I2C2) {
__HAL_RCC_I2C2_CLK_DISABLE();
i2c2_gpio_msp_de_init();
}
}
void bsp_I2cSimpleInit(I2C_HandleTypeDef *handle) {
if (HAL_I2C_Init(handle) != HAL_OK) {
error_handle();
}
}
__weak void i2c1_gpio_msp_init() {
stm32_pin_define_mode_set(stm_get_pin(I2C1_SCL), pin_mode_af_od);
stm32_pin_define_mode_set(stm_get_pin(I2C1_SDA), pin_mode_af_od);
}
__weak void i2c1_gpio_msp_de_init() {
stm32_pin_define_reset(stm_get_pin(I2C1_SCL));
stm32_pin_define_reset(stm_get_pin(I2C1_SDA));
}
__weak void i2c2_gpio_msp_init() {
stm32_pin_define_mode_set(stm_get_pin(I2C2_SCL), pin_mode_af_od);
stm32_pin_define_mode_set(stm_get_pin(I2C2_SDA), pin_mode_af_od);
}
__weak void i2c2_gpio_msp_de_init() {
stm32_pin_define_reset(stm_get_pin(I2C2_SCL));
stm32_pin_define_reset(stm_get_pin(I2C2_SDA));
}