最近在做F0项目要用到多个IIC地址,使用Cube生成的LL库,第二地址进不了中断
F0版本:1.11.0
STM32CubeX生成的LL库代码为:
1 static void MX_I2C1_Init(void) 2 { 3 4 /* USER CODE BEGIN I2C1_Init 0 */ 5 6 /* USER CODE END I2C1_Init 0 */ 7 8 LL_I2C_InitTypeDef I2C_InitStruct = {0}; 9 10 LL_GPIO_InitTypeDef GPIO_InitStruct = {0}; 11 12 LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB); 13 /**I2C1 GPIO Configuration 14 PB6 ------> I2C1_SCL 15 PB7 ------> I2C1_SDA 16 */ 17 GPIO_InitStruct.Pin = LL_GPIO_PIN_6; 18 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE; 19 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH; 20 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN; 21 GPIO_InitStruct.Pull = LL_GPIO_PULL_UP; 22 GPIO_InitStruct.Alternate = LL_GPIO_AF_1; 23 LL_GPIO_Init(GPIOB, &GPIO_InitStruct); 24 25 GPIO_InitStruct.Pin = LL_GPIO_PIN_7; 26 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE; 27 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH; 28 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN; 29 GPIO_InitStruct.Pull = LL_GPIO_PULL_UP; 30 GPIO_InitStruct.Alternate = LL_GPIO_AF_1; 31 LL_GPIO_Init(GPIOB, &GPIO_InitStruct); 32 33 /* Peripheral clock enable */ 34 LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1); 35 36 /* I2C1 interrupt Init */ 37 NVIC_SetPriority(I2C1_IRQn, 3); 38 NVIC_EnableIRQ(I2C1_IRQn); 39 40 /* USER CODE BEGIN I2C1_Init 1 */ 41 42 /* USER CODE END I2C1_Init 1 */ 43 /** I2C Initialization 44 */ 45 LL_I2C_EnableOwnAddress2(I2C1); 46 LL_I2C_DisableGeneralCall(I2C1); 47 LL_I2C_EnableClockStretching(I2C1); 48 I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C; 49 I2C_InitStruct.Timing = 0x2000090E; 50 I2C_InitStruct.AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE; 51 I2C_InitStruct.DigitalFilter = 0; 52 I2C_InitStruct.OwnAddress1 = 214; 53 I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK; 54 I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT; 55 LL_I2C_Init(I2C1, &I2C_InitStruct); 56 LL_I2C_EnableAutoEndMode(I2C1); 57 LL_I2C_SetOwnAddress2(I2C1, 182, LL_I2C_OWNADDRESS2_NOMASK); 58 /* USER CODE BEGIN I2C1_Init 2 */ 59 60 /* USER CODE END I2C1_Init 2 */ 61 62 }
对比HAL库,发现LL库在配置第二地址时,先使能了第二地址:
LL_I2C_EnableOwnAddress2(I2C1);
导致第二地址配置失败。
更改代码如下:
1 static void MX_I2C1_Init(void) 2 { 3 4 /* USER CODE BEGIN I2C1_Init 0 */ 5 6 /* USER CODE END I2C1_Init 0 */ 7 8 LL_I2C_InitTypeDef I2C_InitStruct = {0}; 9 10 LL_GPIO_InitTypeDef GPIO_InitStruct = {0}; 11 12 LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB); 13 /**I2C1 GPIO Configuration 14 PB6 ------> I2C1_SCL 15 PB7 ------> I2C1_SDA 16 */ 17 GPIO_InitStruct.Pin = LL_GPIO_PIN_6; 18 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE; 19 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH; 20 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN; 21 GPIO_InitStruct.Pull = LL_GPIO_PULL_UP; 22 GPIO_InitStruct.Alternate = LL_GPIO_AF_1; 23 LL_GPIO_Init(GPIOB, &GPIO_InitStruct); 24 25 GPIO_InitStruct.Pin = LL_GPIO_PIN_7; 26 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE; 27 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH; 28 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN; 29 GPIO_InitStruct.Pull = LL_GPIO_PULL_UP; 30 GPIO_InitStruct.Alternate = LL_GPIO_AF_1; 31 LL_GPIO_Init(GPIOB, &GPIO_InitStruct); 32 33 /* Peripheral clock enable */ 34 LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1); 35 36 /* I2C1 interrupt Init */ 37 NVIC_SetPriority(I2C1_IRQn, 3); 38 NVIC_EnableIRQ(I2C1_IRQn); 39 40 /* USER CODE BEGIN I2C1_Init 1 */ 41 42 /* USER CODE END I2C1_Init 1 */ 43 /** I2C Initialization 44 */ 45 LL_I2C_DableOwnAddress2(I2C1); 46 LL_I2C_SetOwnAddress2(I2C1, 182, LL_I2C_OWNADDRESS2_NOMASK); 47 LL_I2C_EnableOwnAddress2(I2C1); 48 LL_I2C_DisableGeneralCall(I2C1); 49 LL_I2C_EnableClockStretching(I2C1); 50 I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C; 51 I2C_InitStruct.Timing = 0x2000090E; 52 I2C_InitStruct.AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE; 53 I2C_InitStruct.DigitalFilter = 0; 54 I2C_InitStruct.OwnAddress1 = 214; 55 I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK; 56 I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT; 57 LL_I2C_Init(I2C1, &I2C_InitStruct); 58 LL_I2C_EnableAutoEndMode(I2C1); 59 60 /* USER CODE BEGIN I2C1_Init 2 */ 61 62 /* USER CODE END I2C1_Init 2 */ 63 64 }