i2c.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "drivers/i2c/i2c.h"
  2. #include "stm32l1xx_hal.h"
  3. #include "stm32l1xx_hal_i2c.h"
  4. #include "core/gpio.h"
  5. #include "core/main.h"
  6. #include "core/csect.h"
  7. #include "core/config_pins.h"
  8. I2C_HandleTypeDef i2c_handle = {
  9. #if CONFIG_PERIF_TSENSI2C == CONFIG_PERIF_I2C1
  10. .Instance = I2C1
  11. #elif CONFIG_PERIF_TSENSI2C == CONFIG_PERIF_I2C2
  12. .Instance = I2C2
  13. #else
  14. #error Invalid I2C specified in 'config_pins.h': see 'CONFIG_PERIF_TSENSI2C'
  15. #endif
  16. };
  17. static bool I2C_Init( eI2CPinMode_t );
  18. static HAL_StatusTypeDef I2C_Master_Transmit( uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  19. static HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  20. static void I2C_DeInit( eI2CPinMode_t );
  21. const I2C_Handle_t I2C_Handle = {
  22. .Init = I2C_Init,
  23. .Transmit = I2C_Master_Transmit,
  24. .Receive = I2C_Master_Receive,
  25. .DeInit = I2C_DeInit,
  26. };
  27. // HAL Callback: initialize I2C GPIO, CLOCK, NVIC...etc
  28. void HAL_I2C_MspInit( I2C_HandleTypeDef * hi2c )
  29. {
  30. GPIO_InitTypeDef GPIO_InitStruct = {0};
  31. if( hi2c->Instance == I2C1 )
  32. {
  33. __HAL_RCC_I2C1_CLK_ENABLE();
  34. }
  35. if( hi2c->Instance == I2C2 )
  36. {
  37. __HAL_RCC_I2C2_CLK_ENABLE();
  38. }
  39. // Perform periferal reset
  40. MODIFY_REG(hi2c->Instance->CR1, (0), (I2C_CR1_SWRST));
  41. MODIFY_REG(hi2c->Instance->CR1, (I2C_CR1_SWRST), (0));
  42. if( hi2c->Instance == I2C1 )
  43. {
  44. __HAL_RCC_I2C1_FORCE_RESET();
  45. __HAL_RCC_I2C1_RELEASE_RESET();
  46. HAL_NVIC_SetPriority( I2C1_EV_IRQn, I2C_INT_PRIORITY, 0);
  47. HAL_NVIC_EnableIRQ( I2C1_EV_IRQn );
  48. }
  49. if( hi2c->Instance == I2C2 )
  50. {
  51. __HAL_RCC_I2C2_FORCE_RESET();
  52. __HAL_RCC_I2C2_RELEASE_RESET();
  53. HAL_NVIC_SetPriority( I2C2_EV_IRQn, I2C_INT_PRIORITY, 0);
  54. HAL_NVIC_EnableIRQ( I2C2_EV_IRQn );
  55. }
  56. // Configure the pin muxing - setup I2C pins
  57. GPIO_InitStruct.Pin = CONFIG_PIN__TSENSI2C__DA | CONFIG_PIN__TSENSI2C__CL;
  58. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  59. GPIO_InitStruct.Pull = GPIO_PULLUP;
  60. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  61. if( hi2c->Instance == I2C1 )
  62. GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
  63. if( hi2c->Instance == I2C2 )
  64. GPIO_InitStruct.Alternate = GPIO_AF4_I2C2;
  65. HAL_GPIO_Init(CONFIG_PORT__TSENSI2C__DA_CL, &GPIO_InitStruct);
  66. }
  67. // HAL Callback: deinitialize I2C GPIO, CLOCK, NVIC...etc
  68. void HAL_I2C_MspDeInit( I2C_HandleTypeDef * hi2c )
  69. {
  70. if( hi2c->Instance == I2C1 )
  71. {
  72. HAL_NVIC_DisableIRQ( I2C1_EV_IRQn );
  73. __HAL_RCC_I2C1_CLK_DISABLE();
  74. }
  75. if( hi2c->Instance == I2C2 )
  76. {
  77. HAL_NVIC_DisableIRQ( I2C2_EV_IRQn );
  78. __HAL_RCC_I2C2_CLK_DISABLE();
  79. }
  80. }
  81. static bool I2C_Init( eI2CPinMode_t pinmode )
  82. {
  83. bool ret = false;
  84. i2c_handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  85. i2c_handle.Init.ClockSpeed = 100000;
  86. i2c_handle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  87. i2c_handle.Init.DutyCycle = I2C_DUTYCYCLE_2;
  88. i2c_handle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  89. i2c_handle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  90. i2c_handle.Init.OwnAddress1 = 0;
  91. i2c_handle.Init.OwnAddress2 = 0;
  92. DI();
  93. // Initialize I2C-bus
  94. if( HAL_OK == HAL_I2C_Init( &i2c_handle ) )
  95. {
  96. ret = true;
  97. }
  98. EI();
  99. if( !ret )
  100. {
  101. // ERROR, deinitialize
  102. I2C_DeInit( pinmode );
  103. }
  104. return ret;
  105. }
  106. static void I2C_DeInit( eI2CPinMode_t pinmode )
  107. {
  108. { // I2C Pin Muxing
  109. GPIO_InitTypeDef GPIO_InitStruct = {0};
  110. // Deinitialize I2C pins
  111. GPIO_InitStruct.Pin = CONFIG_PIN__TSENSI2C__DA | CONFIG_PIN__TSENSI2C__CL;
  112. switch( pinmode )
  113. {
  114. case eI2CPinMode_KeepPullUp:
  115. GPIO_InitStruct.Mode = GPIO_MODE_INPUT; //
  116. GPIO_InitStruct.Pull = GPIO_PULLUP; // required to guaranee the correct level
  117. break;
  118. case eI2CPinMode_KeepPullDown:
  119. GPIO_InitStruct.Mode = GPIO_MODE_INPUT; //
  120. GPIO_InitStruct.Pull = GPIO_PULLDOWN; // required to guaranee the correct level
  121. break;
  122. default:
  123. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  124. GPIO_InitStruct.Pull = GPIO_NOPULL;
  125. }
  126. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  127. HAL_GPIO_Init(CONFIG_PORT__TSENSI2C__DA_CL, &GPIO_InitStruct);
  128. }
  129. DI();
  130. // Deinitialize I2C-bus
  131. HAL_I2C_DeInit( &i2c_handle );
  132. EI();
  133. }
  134. static HAL_StatusTypeDef I2C_Master_Transmit( uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  135. {
  136. return HAL_I2C_Master_Transmit( &i2c_handle, DevAddress, pData, Size, Timeout );
  137. }
  138. static HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  139. {
  140. return HAL_I2C_Master_Receive( &i2c_handle, DevAddress, pData, Size, Timeout );
  141. }