/** * @brief Eval I2Cx MSP Initialization * @param hi2c: I2C handle * @retval None */ static void I2Cx_MspInit(I2C_HandleTypeDef *hi2c) { GPIO_InitTypeDef GPIO_InitStructure; RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct; if (hi2c->Instance == EVAL_I2Cx) { /*##-1- Configure the Eval I2C clock source. The clock is derived from the SYSCLK #*/ RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C2; RCC_PeriphCLKInitStruct.I2c2ClockSelection = RCC_I2C2CLKSOURCE_SYSCLK; HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct); /*##-2- Configure the GPIOs ################################################*/ /* Enable GPIO clock */ EVAL_I2Cx_SDA_GPIO_CLK_ENABLE(); EVAL_I2Cx_SCL_GPIO_CLK_ENABLE(); /* Configure I2C Tx as alternate function */ GPIO_InitStructure.Pin = EVAL_I2Cx_SCL_PIN; GPIO_InitStructure.Mode = GPIO_MODE_AF_OD; GPIO_InitStructure.Pull = GPIO_NOPULL; GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; GPIO_InitStructure.Alternate = EVAL_I2Cx_SCL_SDA_AF; HAL_GPIO_Init(EVAL_I2Cx_SCL_GPIO_PORT, &GPIO_InitStructure); /* Configure I2C Rx as alternate function */ GPIO_InitStructure.Pin = EVAL_I2Cx_SDA_PIN; HAL_GPIO_Init(EVAL_I2Cx_SDA_GPIO_PORT, &GPIO_InitStructure); /*##-3- Configure the Eval I2Cx peripheral #######################################*/ /* Enable Eval_I2Cx clock */ EVAL_I2Cx_CLK_ENABLE(); /* Force the I2C Periheral Clock Reset */ EVAL_I2Cx_FORCE_RESET(); /* Release the I2C Periheral Clock Reset */ EVAL_I2Cx_RELEASE_RESET(); /* Enable and set Eval I2Cx Interrupt to the highest priority */ HAL_NVIC_SetPriority(EVAL_I2Cx_EV_IRQn, 0x00, 0); HAL_NVIC_EnableIRQ(EVAL_I2Cx_EV_IRQn); /* Enable and set Eval I2Cx Interrupt to the highest priority */ HAL_NVIC_SetPriority(EVAL_I2Cx_ER_IRQn, 0x00, 0); HAL_NVIC_EnableIRQ(EVAL_I2Cx_ER_IRQn); } }
/** * @brief Initializes I2C MSP. * @param None * @retval None */ static void I2Cx_MspInit(void) { GPIO_InitTypeDef GPIO_InitStruct; /*** Configure the GPIOs ***/ /* Enable GPIO clock */ EVAL_I2Cx_SCL_SDA_GPIO_CLK_ENABLE(); /* Configure I2C Tx as alternate function */ GPIO_InitStruct.Pin = EVAL_I2Cx_SCL_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; GPIO_InitStruct.Alternate = EVAL_I2Cx_SCL_SDA_AF; HAL_GPIO_Init(EVAL_I2Cx_SCL_SDA_GPIO_PORT, &GPIO_InitStruct); /* Configure I2C Rx as alternate function */ GPIO_InitStruct.Pin = EVAL_I2Cx_SDA_PIN; HAL_GPIO_Init(EVAL_I2Cx_SCL_SDA_GPIO_PORT, &GPIO_InitStruct); /*** Configure the I2C peripheral ***/ /* Enable I2C clock */ EVAL_I2Cx_CLK_ENABLE(); /* Force the I2C peripheral clock reset */ EVAL_I2Cx_FORCE_RESET(); /* Release the I2C peripheral clock reset */ EVAL_I2Cx_RELEASE_RESET(); /* Set priority and enable I2Cx event Interrupt */ HAL_NVIC_SetPriority(EVAL_I2Cx_EV_IRQn, 5, 0); HAL_NVIC_EnableIRQ(EVAL_I2Cx_EV_IRQn); /* Set priority and enable I2Cx error Interrupt */ HAL_NVIC_SetPriority(EVAL_I2Cx_ER_IRQn, 5, 0); HAL_NVIC_EnableIRQ(EVAL_I2Cx_ER_IRQn); }