SaintStefan
10/2/2019 - 1:44 PM

HAL_I2C_MspInit

/* (++) Blocking mode : The communication is performed in the polling mode.
        The status of all data processing is returned by the same function
        after finishing transfer.
 * (#) Blocking mode functions are :
      (++) HAL_I2C_Master_Transmit()
      (++) HAL_I2C_Master_Receive()
      (++) HAL_I2C_Slave_Transmit()
      (++) HAL_I2C_Slave_Receive()
      (++) HAL_I2C_Mem_Write()
      (++) HAL_I2C_Mem_Read()
      (++) HAL_I2C_IsDeviceReady() */

void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c3)
{
	  /* Enable the I2C3 interface clock 启用I2C3接口时钟 / Peripheral Clock Enable 启用外围时钟 */
	  __HAL_RCC_I2C3_CLK_ENABLE();

	  /* I2C pins configuration */

	  GPIO_InitTypeDef GPIO_InitStruct = {0};

	  /* Enable the clock for the I2C GPIOs / GPIO Ports Clock Enable 启用GPIO端口时钟 */
	  /*I2C3 GPIO Configuration
	   * PA8  ---------->  I2C3_SCL
	   * PC9  ---------->  I2C3_SDA
	   */
	  __HAL_RCC_GPIOA_CLK_ENABLE();
	  __HAL_RCC_GPIOC_CLK_ENABLE();

	  /* Configure I2C pins as alternate function open-drain */
	  /*Configure GPIO pins : GPIO_PIN_8 */
	  GPIO_InitStruct.Pin = GPIO_PIN_8;	//Specifies the GPIO pins to be configured 指定要配置的引脚,
	  	  	  	  	  	  	  	  	  	//Possible values: GPIO_PIN_x or GPIO_PIN_All, where x[0..15]
	  GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;	//GPIO_MODE_AF_OD : Alternate function open drain 开漏输出(i2C需要这种模式)
	  //GPIO_InitStruct.Pull = GPIO_NOPULL;	//Specifies the Pull-up or Pull-down activation for the selected pins 指定所选引脚的上拉或下拉激活
	  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;	//Specifies the speed for the selected pins 指定所选引脚的速度
	  GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;	// Peripheral to be connected to the selected pins 外设要连接到所选的引脚上
	   	   	   	   	   	   	   	   	   	   	   	   	// Possible values: GPIO_AFx_PPP, where: 可能的值:GPIO_AFx_PPP,其中:
	   	   	   	   	   	   	   	   	   	   	   	   	// AFx: is the alternate function index AFx是备用函数索引
	   	   	   	   	   	   	   	   	   	   	   	   	// PPP: is the peripheral instance PPP:是外围设备实例 */
	  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);	//GPIOA

	  /*Configure GPIO pins : GPIO_PIN_9 */
	  GPIO_InitStruct.Pin = GPIO_PIN_9;
	  GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
	  //GPIO_InitStruct.Pull = GPIO_NOPULL;
	  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	  GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;	//????????不确定是不是AF4
	  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);	//GPIOC

	  /* NVIC configuration to use interrupt process */
	  /* -@- IRQ priority order (sorted by highest to lowest priority):
	      *(+@) Lowest preemption priority 最低抢占优先级
	      *(+@) Lowest sub priority 最低子优先级
	      *(+@) Lowest hardware priority (IRQ number) 最低硬件优先级(IRQ号)*/
	  /* Configure the I2Cx interrupt priority */
	  HAL_NVIC_SetPriority(I2C3_EV_IRQn, 0, 0);	/* Configure the priority of the selected IRQ Channels using : 配置IRQ通道的优先级
	   	   	   	   	   	   	     * HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority) */
	  /* Enable the NVIC I2C IRQ Channel */
	  HAL_NVIC_EnableIRQ(I2C3_EV_IRQn);	/* Enable the selected IRQ Channels using : 启用选定的IRQ通道
	   	   	   	   	   	     * HAL_NVIC_EnableIRQ(IRQn_Type IRQn) */
	  HAL_NVIC_SetPriority(I2C3_ER_IRQn, 0, 0);
	  HAL_NVIC_EnableIRQ(I2C3_ER_IRQn);
}