drv_i2c.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2006-2023, 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])] = {0};
  91. static int ifx_i2c_read(struct ifx_i2c *hi2c, rt_uint16_t slave_address, rt_uint8_t *p_buffer, rt_uint16_t data_byte)
  92. {
  93. if (cyhal_i2c_master_read(&hi2c->mI2C, slave_address, p_buffer, data_byte, 10, true) != RT_EOK)
  94. {
  95. return -RT_ERROR;
  96. }
  97. return 0;
  98. }
  99. static int ifx_i2c_write(struct ifx_i2c *hi2c, uint16_t slave_address, uint8_t *p_buffer, uint16_t data_byte)
  100. {
  101. if (cyhal_i2c_master_write(&hi2c->mI2C, slave_address, p_buffer, data_byte, 10, true) != RT_EOK)
  102. {
  103. return -RT_ERROR;
  104. }
  105. return 0;
  106. }
  107. static rt_ssize_t _i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
  108. {
  109. struct rt_i2c_msg *msg;
  110. rt_uint32_t i;
  111. struct ifx_i2c *i2c_obj;
  112. RT_ASSERT(bus != RT_NULL);
  113. RT_ASSERT(msgs != RT_NULL);
  114. i2c_obj = rt_container_of(bus, struct ifx_i2c, i2c_bus);
  115. for (i = 0; i < num; i++)
  116. {
  117. msg = &msgs[i];
  118. if (msg->flags & RT_I2C_RD)
  119. {
  120. if (ifx_i2c_read(i2c_obj, msg->addr, msg->buf, msg->len) != 0)
  121. {
  122. goto out;
  123. }
  124. }
  125. else
  126. {
  127. if (ifx_i2c_write(i2c_obj, msg->addr, msg->buf, msg->len) != 0)
  128. {
  129. goto out;
  130. }
  131. }
  132. }
  133. out:
  134. return i;
  135. }
  136. static const struct rt_i2c_bus_device_ops i2c_ops =
  137. {
  138. _i2c_xfer,
  139. RT_NULL,
  140. RT_NULL
  141. };
  142. void HAL_I2C_Init(struct ifx_i2c *obj)
  143. {
  144. cy_rslt_t result = CY_RSLT_SUCCESS;
  145. result = cyhal_i2c_init(&obj->mI2C, obj->config->sda_pin, obj->config->scl_pin, NULL);
  146. if (result != CY_RSLT_SUCCESS)
  147. {
  148. rt_kprintf("hal i2c init fail!\n");
  149. return;
  150. }
  151. result = cyhal_i2c_configure(&obj->mI2C, &obj->mI2C_cfg);
  152. if (result != CY_RSLT_SUCCESS)
  153. {
  154. rt_kprintf("hal i2c configure fail!\n");
  155. return;
  156. }
  157. }
  158. int rt_hw_i2c_init(void)
  159. {
  160. rt_err_t result = RT_EOK;
  161. for (int i = 0; i < sizeof(i2c_config) / sizeof(i2c_config[0]); i++)
  162. {
  163. i2c_objs[i].config = &i2c_config[i];
  164. i2c_objs[i].i2c_bus.parent.user_data = &i2c_config[i];
  165. i2c_objs[i].mI2C_cfg.is_slave = false;
  166. i2c_objs[i].mI2C_cfg.address = 0;
  167. i2c_objs[i].mI2C_cfg.frequencyhal_hz = (400000UL);
  168. i2c_objs[i].i2c_bus.ops = &i2c_ops;
  169. HAL_I2C_Init(&i2c_objs[i]);
  170. result = rt_i2c_bus_device_register(&i2c_objs[i].i2c_bus, i2c_config[i].name);
  171. RT_ASSERT(result == RT_EOK);
  172. }
  173. return 0;
  174. }
  175. INIT_DEVICE_EXPORT(rt_hw_i2c_init);
  176. #endif /* RT_USING_I2C */