drv_i2c.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-09-03 AisinoChip first implementation.
  9. */
  10. #include "board.h"
  11. #if defined(RT_USING_I2C)
  12. #if defined(BSP_USING_I2C1) || defined(BSP_USING_I2C2)
  13. #include <rtdevice.h>
  14. #include "i2c_config.h"
  15. enum
  16. {
  17. #ifdef BSP_USING_I2C1
  18. I2C1_INDEX,
  19. #endif
  20. #ifdef BSP_USING_I2C2
  21. I2C2_INDEX,
  22. #endif
  23. I2C_MAX_INDEX
  24. };
  25. struct acm32_i2c_config
  26. {
  27. I2C_TypeDef *Instance;
  28. char *name;
  29. IRQn_Type irq_type;
  30. enum_Enable_ID_t enable_id;
  31. uint32_t clock_speed;
  32. enum_GPIOx_t scl_port;
  33. rt_uint32_t scl_pin;
  34. rt_uint32_t scl_alternate;
  35. enum_GPIOx_t sda_port;
  36. rt_uint32_t sda_pin;
  37. rt_uint32_t sda_alternate;
  38. };
  39. struct acm32_i2c
  40. {
  41. I2C_HandleTypeDef handle;
  42. struct acm32_i2c_config *config;
  43. struct rt_i2c_bus_device i2c_bus;
  44. };
  45. static struct acm32_i2c_config i2c_config[] =
  46. {
  47. #ifdef BSP_USING_I2C1
  48. I2C1_CONFIG,
  49. #endif
  50. #ifdef BSP_USING_I2C2
  51. I2C2_CONFIG,
  52. #endif
  53. };
  54. static struct acm32_i2c i2c_objs[sizeof(i2c_config) / sizeof(i2c_config[0])] = {0};
  55. static int acm32_i2c_read(struct acm32_i2c *hi2c, rt_uint16_t slave_address, rt_uint8_t *p_buffer, rt_uint16_t data_byte)
  56. {
  57. if (HAL_I2C_Master_Receive(&hi2c->handle, slave_address, p_buffer, data_byte, 1000) != HAL_OK)
  58. {
  59. return -1;
  60. }
  61. return 0;
  62. }
  63. static int acm32_i2c_write(struct acm32_i2c *hi2c, uint16_t slave_address, uint8_t *p_buffer, uint16_t data_byte)
  64. {
  65. if (HAL_I2C_Master_Transmit(&hi2c->handle, slave_address, p_buffer, data_byte, 1000) != HAL_OK)
  66. {
  67. return -1;
  68. }
  69. return 0;
  70. }
  71. static rt_ssize_t _i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
  72. {
  73. struct rt_i2c_msg *msg;
  74. rt_uint32_t i;
  75. struct acm32_i2c *i2c_obj;
  76. RT_ASSERT(bus != RT_NULL);
  77. RT_ASSERT(msgs != RT_NULL);
  78. i2c_obj = rt_container_of(bus, struct acm32_i2c, i2c_bus);
  79. for (i = 0; i < num; i++)
  80. {
  81. msg = &msgs[i];
  82. if (msg->flags & RT_I2C_RD)
  83. {
  84. if (acm32_i2c_read(i2c_obj, msg->addr, msg->buf, msg->len) != 0)
  85. {
  86. goto out;
  87. }
  88. }
  89. else
  90. {
  91. if (acm32_i2c_write(i2c_obj, msg->addr, msg->buf, msg->len) != 0)
  92. {
  93. goto out;
  94. }
  95. }
  96. }
  97. out:
  98. return i;
  99. }
  100. static const struct rt_i2c_bus_device_ops i2c_ops =
  101. {
  102. _i2c_xfer,
  103. RT_NULL,
  104. RT_NULL
  105. };
  106. int rt_hw_i2c_init(void)
  107. {
  108. rt_err_t result;
  109. for (int i = 0; i < sizeof(i2c_config) / sizeof(i2c_config[0]); i++)
  110. {
  111. i2c_objs[i].config = &i2c_config[i];
  112. i2c_objs[i].i2c_bus.parent.user_data = &i2c_config[i];
  113. i2c_objs[i].handle.Instance = i2c_config[i].Instance;
  114. i2c_objs[i].i2c_bus.ops = &i2c_ops;
  115. /* hardware initial */
  116. i2c_objs[i].handle.Init.Clock_Speed = i2c_config[i].clock_speed ;
  117. i2c_objs[i].handle.Init.Tx_Auto_En = TX_AUTO_EN_ENABLE;
  118. i2c_objs[i].handle.Init.I2C_Mode = I2C_MODE_MASTER;
  119. HAL_I2C_Init(&i2c_objs[i].handle);
  120. result = rt_i2c_bus_device_register(&i2c_objs[i].i2c_bus, i2c_config[i].name);
  121. RT_ASSERT(result == RT_EOK);
  122. }
  123. return 0;
  124. }
  125. INIT_DEVICE_EXPORT(rt_hw_i2c_init);
  126. /************************************************************************
  127. * function : HAL_I2C_MspInit
  128. * Description:
  129. * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
  130. * the configuration information for I2C module
  131. ************************************************************************/
  132. void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
  133. {
  134. GPIO_InitTypeDef GPIO_Handle;
  135. struct acm32_i2c *i2c_obj;
  136. struct acm32_i2c_config *i2c_config;
  137. RT_ASSERT(hi2c != RT_NULL);
  138. i2c_obj = rt_container_of(hi2c, struct acm32_i2c, handle);
  139. RT_ASSERT(i2c_obj->i2c_bus.parent.user_data != RT_NULL);
  140. i2c_config = (struct acm32_i2c_config *)i2c_obj->i2c_bus.parent.user_data;
  141. /* Enable Clock */
  142. System_Module_Enable(i2c_config->enable_id);
  143. /* I2C SDA */
  144. GPIO_Handle.Pin = i2c_config->sda_pin;
  145. GPIO_Handle.Mode = GPIO_MODE_AF_PP;
  146. GPIO_Handle.Pull = GPIO_PULLUP;
  147. GPIO_Handle.Alternate = i2c_config->sda_alternate;
  148. HAL_GPIO_Init(i2c_config->sda_port, &GPIO_Handle);
  149. /* I2C SCL */
  150. GPIO_Handle.Pin = i2c_config->scl_pin;
  151. GPIO_Handle.Mode = GPIO_MODE_AF_PP;
  152. GPIO_Handle.Pull = GPIO_PULLUP;
  153. GPIO_Handle.Alternate = i2c_config->scl_alternate;
  154. HAL_GPIO_Init(i2c_config->scl_port, &GPIO_Handle);
  155. /* Clear Pending Interrupt */
  156. NVIC_ClearPendingIRQ(i2c_config->irq_type);
  157. /* Enable External Interrupt */
  158. NVIC_EnableIRQ(i2c_config->irq_type);
  159. }
  160. #endif /* defined(BSP_USING_I2C1) || defined(BSP_USING_I2C2) */
  161. #endif /* RT_USING_I2C */