drv_i2c.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-07-08 Rbb666 first implementation.
  9. */
  10. #include "board.h"
  11. #if defined(RT_USING_I2C)
  12. #if defined(BSP_USING_HW_I2C2) || defined(BSP_USING_HW_I2C3) || defined(BSP_USING_HW_I2C4)|| defined(BSP_USING_HW_I2C6)
  13. #include <rtdevice.h>
  14. #ifndef I2C2_CONFIG
  15. #define I2C2_CONFIG \
  16. { \
  17. .name = "i2c2", \
  18. .scl_pin = BSP_I2C2_SCL_PIN, \
  19. .sda_pin = BSP_I2C2_SDA_PIN, \
  20. }
  21. #endif /* I2C2_CONFIG */
  22. #ifndef I2C3_CONFIG
  23. #define I2C3_CONFIG \
  24. { \
  25. .name = "i2c3", \
  26. .scl_pin = BSP_I2C3_SCL_PIN, \
  27. .sda_pin = BSP_I2C3_SDA_PIN, \
  28. }
  29. #endif /* I2C3_CONFIG */
  30. #ifndef I2C4_CONFIG
  31. #define I2C4_CONFIG \
  32. { \
  33. .name = "i2c4", \
  34. .scl_pin = BSP_I2C4_SCL_PIN, \
  35. .sda_pin = BSP_I2C4_SDA_PIN, \
  36. }
  37. #endif /* I2C4_CONFIG */
  38. #ifndef I2C6_CONFIG
  39. #define I2C6_CONFIG \
  40. { \
  41. .name = "i2c6", \
  42. .scl_pin = BSP_I2C6_SCL_PIN, \
  43. .sda_pin = BSP_I2C6_SDA_PIN, \
  44. }
  45. #endif /* I2C6_CONFIG */
  46. #endif /* defined(BSP_USING_I2C1) || defined(BSP_USING_I2C2) */
  47. enum
  48. {
  49. #ifdef BSP_USING_HW_I2C2
  50. I2C2_INDEX,
  51. #endif
  52. #ifdef BSP_USING_HW_I2C3
  53. I2C3_INDEX,
  54. #endif
  55. #ifdef BSP_USING_HW_I2C4
  56. I2C4_INDEX,
  57. #endif
  58. #ifdef BSP_USING_HW_I2C6
  59. I2C6_INDEX,
  60. #endif
  61. };
  62. struct ifx_i2c_config
  63. {
  64. char *name;
  65. rt_uint32_t scl_pin;
  66. rt_uint32_t sda_pin;
  67. };
  68. struct ifx_i2c
  69. {
  70. cyhal_i2c_t mI2C;
  71. cyhal_i2c_cfg_t mI2C_cfg;
  72. struct ifx_i2c_config *config;
  73. struct rt_i2c_bus_device i2c_bus;
  74. };
  75. static struct ifx_i2c_config i2c_config[] =
  76. {
  77. #ifdef BSP_USING_HW_I2C2
  78. I2C2_CONFIG,
  79. #endif
  80. #ifdef BSP_USING_HW_I2C3
  81. I2C3_CONFIG,
  82. #endif
  83. #ifdef BSP_USING_HW_I2C4
  84. I2C4_CONFIG,
  85. #endif
  86. #ifdef BSP_USING_HW_I2C6
  87. I2C6_CONFIG,
  88. #endif
  89. };
  90. static struct ifx_i2c i2c_objs[sizeof(i2c_config) / sizeof(i2c_config[0])] =
  91. {0};
  92. static int ifx_i2c_read(struct ifx_i2c *hi2c, rt_uint16_t slave_address, rt_uint8_t *p_buffer, rt_uint16_t data_byte)
  93. {
  94. if (cyhal_i2c_master_read(&hi2c->mI2C, slave_address, p_buffer, data_byte, 10, true) != RT_EOK)
  95. {
  96. return -RT_ERROR;
  97. }
  98. return 0;
  99. }
  100. static int ifx_i2c_write(struct ifx_i2c *hi2c, uint16_t slave_address, uint8_t *p_buffer, uint16_t data_byte)
  101. {
  102. if (cyhal_i2c_master_write(&hi2c->mI2C, slave_address, p_buffer, data_byte, 10, true) != RT_EOK)
  103. {
  104. return -RT_ERROR;
  105. }
  106. return 0;
  107. }
  108. static rt_ssize_t _i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
  109. {
  110. struct rt_i2c_msg *msg;
  111. rt_uint32_t i;
  112. struct ifx_i2c *i2c_obj;
  113. RT_ASSERT(bus != RT_NULL);
  114. RT_ASSERT(msgs != RT_NULL);
  115. i2c_obj = rt_container_of(bus, struct ifx_i2c, i2c_bus);
  116. for (i = 0; i < num; i++)
  117. {
  118. msg = &msgs[i];
  119. if (msg->flags & RT_I2C_RD)
  120. {
  121. if (ifx_i2c_read(i2c_obj, msg->addr, msg->buf, msg->len) != 0)
  122. {
  123. goto out;
  124. }
  125. }
  126. else
  127. {
  128. if (ifx_i2c_write(i2c_obj, msg->addr, msg->buf, msg->len) != 0)
  129. {
  130. goto out;
  131. }
  132. }
  133. }
  134. out:
  135. return i;
  136. }
  137. static const struct rt_i2c_bus_device_ops i2c_ops =
  138. {
  139. _i2c_xfer,
  140. RT_NULL,
  141. RT_NULL
  142. };
  143. void HAL_I2C_Init(struct ifx_i2c *obj)
  144. {
  145. cy_rslt_t result = CY_RSLT_SUCCESS;
  146. result = cyhal_i2c_init(&obj->mI2C, obj->config->sda_pin, obj->config->scl_pin, NULL);
  147. if (result != CY_RSLT_SUCCESS)
  148. {
  149. rt_kprintf("hal i2c init fail!\n");
  150. return;
  151. }
  152. result = cyhal_i2c_configure(&obj->mI2C, &obj->mI2C_cfg);
  153. if (result != CY_RSLT_SUCCESS)
  154. {
  155. rt_kprintf("hal i2c configure fail!\n");
  156. return;
  157. }
  158. }
  159. int rt_hw_i2c_init(void)
  160. {
  161. rt_err_t result = RT_EOK;
  162. for (int i = 0; i < sizeof(i2c_config) / sizeof(i2c_config[0]); i++)
  163. {
  164. i2c_objs[i].config = &i2c_config[i];
  165. i2c_objs[i].i2c_bus.parent.user_data = &i2c_config[i];
  166. i2c_objs[i].mI2C_cfg.is_slave = false;
  167. i2c_objs[i].mI2C_cfg.address = 0;
  168. i2c_objs[i].mI2C_cfg.frequencyhal_hz = (400000UL);
  169. i2c_objs[i].i2c_bus.ops = &i2c_ops;
  170. HAL_I2C_Init(&i2c_objs[i]);
  171. result = rt_i2c_bus_device_register(&i2c_objs[i].i2c_bus, i2c_config[i].name);
  172. RT_ASSERT(result == RT_EOK);
  173. }
  174. return 0;
  175. }
  176. INIT_DEVICE_EXPORT(rt_hw_i2c_init);
  177. #endif /* RT_USING_I2C */