Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. /* (++) Blocking mode : The communication is performed in the polling mode.
  2. The status of all data processing is returned by the same function
  3. after finishing transfer.
  4. * (#) Blocking mode functions are :
  5. (++) HAL_I2C_Master_Transmit()
  6. (++) HAL_I2C_Master_Receive()
  7. (++) HAL_I2C_Slave_Transmit()
  8. (++) HAL_I2C_Slave_Receive()
  9. (++) HAL_I2C_Mem_Write()
  10. (++) HAL_I2C_Mem_Read()
  11. (++) HAL_I2C_IsDeviceReady() */
  12.  
  13. void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c3)
  14. {
  15. /* Enable the I2C3 interface clock 启用I2C3接口时钟 / Peripheral Clock Enable 启用外围时钟 */
  16. __HAL_RCC_I2C3_CLK_ENABLE();
  17.  
  18. /* I2C pins configuration */
  19.  
  20. GPIO_InitTypeDef GPIO_InitStruct = {0};
  21.  
  22. /* Enable the clock for the I2C GPIOs / GPIO Ports Clock Enable 启用GPIO端口时钟 */
  23. /*I2C3 GPIO Configuration
  24. * PA8 ----------> I2C3_SCL
  25. * PC9 ----------> I2C3_SDA
  26. */
  27. __HAL_RCC_GPIOA_CLK_ENABLE();
  28. __HAL_RCC_GPIOC_CLK_ENABLE();
  29.  
  30. /* Configure I2C pins as alternate function open-drain */
  31. /*Configure GPIO pins : GPIO_PIN_8 */
  32. GPIO_InitStruct.Pin = GPIO_PIN_8; //Specifies the GPIO pins to be configured 指定要配置的引脚,
  33. //Possible values: GPIO_PIN_x or GPIO_PIN_All, where x[0..15]
  34. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; //GPIO_MODE_AF_OD : Alternate function open drain 开漏输出(i2C需要这种模式)
  35. //GPIO_InitStruct.Pull = GPIO_NOPULL; //Specifies the Pull-up or Pull-down activation for the selected pins 指定所选引脚的上拉或下拉激活
  36. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; //Specifies the speed for the selected pins 指定所选引脚的速度
  37. GPIO_InitStruct.Alternate = GPIO_AF4_I2C3; // Peripheral to be connected to the selected pins 外设要连接到所选的引脚上
  38. // Possible values: GPIO_AFx_PPP, where: 可能的值:GPIO_AFx_PPP,其中:
  39. // AFx: is the alternate function index AFx是备用函数索引
  40. // PPP: is the peripheral instance PPP:是外围设备实例 */
  41. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); //GPIOA
  42.  
  43. /*Configure GPIO pins : GPIO_PIN_9 */
  44. GPIO_InitStruct.Pin = GPIO_PIN_9;
  45. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  46. //GPIO_InitStruct.Pull = GPIO_NOPULL;
  47. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  48. GPIO_InitStruct.Alternate = GPIO_AF4_I2C3; //????????不确定是不是AF4
  49. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); //GPIOC
  50.  
  51. /* NVIC configuration to use interrupt process */
  52. /* -@- IRQ priority order (sorted by highest to lowest priority):
  53. *(+@) Lowest preemption priority 最低抢占优先级
  54. *(+@) Lowest sub priority 最低子优先级
  55. *(+@) Lowest hardware priority (IRQ number) 最低硬件优先级(IRQ号)*/
  56. /* Configure the I2Cx interrupt priority */
  57. HAL_NVIC_SetPriority(I2C3_EV_IRQn, 0, 0); /* Configure the priority of the selected IRQ Channels using : 配置IRQ通道的优先级
  58. * HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority) */
  59. /* Enable the NVIC I2C IRQ Channel */
  60. HAL_NVIC_EnableIRQ(I2C3_EV_IRQn); /* Enable the selected IRQ Channels using : 启用选定的IRQ通道
  61. * HAL_NVIC_EnableIRQ(IRQn_Type IRQn) */
  62. HAL_NVIC_SetPriority(I2C3_ER_IRQn, 0, 0);
  63. HAL_NVIC_EnableIRQ(I2C3_ER_IRQn);
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement