drv_i2c.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2006-2025, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-02-22 airm2m first version
  9. */
  10. #include <rtdevice.h>
  11. #include <rtthread.h>
  12. #include "board.h"
  13. #include <stdlib.h>
  14. #ifdef BSP_USING_HW_I2C
  15. #define DRV_DEBUG
  16. #define LOG_TAG "drv.hwi2c"
  17. #include <drv_log.h>
  18. #include <hal_data.h>
  19. #ifndef BIT
  20. #define BIT(idx) (1ul << (idx))
  21. #endif
  22. #ifndef BITS
  23. #define BITS(b,e) ((((uint32_t)-1)<<(b))&(((uint32_t)-1)>>(31-(e))))
  24. #endif
  25. #define RA_SCI_EVENT_ABORTED BIT(0)
  26. #define RA_SCI_EVENT_RX_COMPLETE BIT(1)
  27. #define RA_SCI_EVENT_TX_COMPLETE BIT(2)
  28. #define RA_SCI_EVENT_ERROR BIT(3)
  29. #define RA_SCI_EVENT_ALL BITS(0,3)
  30. struct ra_i2c_handle
  31. {
  32. struct rt_i2c_bus_device bus;
  33. char bus_name[RT_NAME_MAX];
  34. const i2c_master_cfg_t *i2c_cfg;
  35. void *i2c_ctrl;
  36. struct rt_event event;
  37. };
  38. static struct ra_i2c_handle ra_i2cs[] =
  39. {
  40. #ifdef BSP_USING_HW_I2C0
  41. {.bus_name = "i2c0", .i2c_cfg = &g_i2c_master0_cfg, .i2c_ctrl = &g_i2c_master0_ctrl,},
  42. #endif
  43. #ifdef BSP_USING_HW_I2C1
  44. {.bus_name = "i2c1", .i2c_cfg = &g_i2c_master1_cfg, .i2c_ctrl = &g_i2c_master1_ctrl,},
  45. #endif
  46. #ifdef BSP_USING_HW_I2C2
  47. {.bus_name = "i2c2", .i2c_cfg = &g_i2c_master2_cfg, .i2c_ctrl = &g_i2c_master2_ctrl,},
  48. #endif
  49. };
  50. void i2c_master_callback(i2c_master_callback_args_t *p_args)
  51. {
  52. rt_interrupt_enter();
  53. if (NULL != p_args)
  54. {
  55. /* capture callback event for validating the i2c transfer event*/
  56. struct ra_i2c_handle *obj = (struct ra_i2c_handle *)p_args->p_context;
  57. uint32_t event = 0;
  58. RT_ASSERT(obj != RT_NULL);
  59. switch (p_args->event)
  60. {
  61. case I2C_MASTER_EVENT_ABORTED:
  62. event |= RA_SCI_EVENT_ABORTED;
  63. break;
  64. case I2C_MASTER_EVENT_RX_COMPLETE:
  65. event |= RA_SCI_EVENT_RX_COMPLETE;
  66. break;
  67. case I2C_MASTER_EVENT_TX_COMPLETE:
  68. event |= RA_SCI_EVENT_TX_COMPLETE;
  69. break;
  70. }
  71. rt_event_send(&obj->event, event);
  72. }
  73. rt_interrupt_leave();
  74. }
  75. static rt_err_t validate_i2c_event(struct ra_i2c_handle *handle)
  76. {
  77. rt_uint32_t event = 0;
  78. if (RT_EOK != rt_event_recv(&handle->event, RA_SCI_EVENT_ALL, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, (int32_t)rt_tick_from_millisecond(100), &event))
  79. {
  80. return -RT_ETIMEOUT;
  81. }
  82. if ((event & (RA_SCI_EVENT_ABORTED | RA_SCI_EVENT_ERROR)) == 0)
  83. {
  84. return RT_EOK;
  85. }
  86. return -RT_ERROR;
  87. }
  88. static rt_ssize_t ra_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
  89. struct rt_i2c_msg msgs[],
  90. rt_uint32_t num)
  91. {
  92. rt_size_t i;
  93. struct rt_i2c_msg *msg = msgs;
  94. RT_ASSERT(bus != RT_NULL);
  95. fsp_err_t err = FSP_SUCCESS;
  96. bool restart = false;
  97. struct ra_i2c_handle *ra_i2c = rt_container_of(bus, struct ra_i2c_handle, bus);
  98. i2c_master_ctrl_t *master_ctrl = ra_i2c->i2c_ctrl;
  99. for (i = 0; i < num; i++)
  100. {
  101. if (msg[i].flags & RT_I2C_NO_START)
  102. {
  103. restart = true;
  104. }
  105. if (msg[i].flags & RT_I2C_ADDR_10BIT)
  106. {
  107. R_IIC_MASTER_SlaveAddressSet(master_ctrl, msg[i].addr, I2C_MASTER_ADDR_MODE_10BIT);
  108. }
  109. else
  110. {
  111. R_IIC_MASTER_SlaveAddressSet(master_ctrl, msg[i].addr, I2C_MASTER_ADDR_MODE_7BIT);
  112. }
  113. if (msg[i].flags & RT_I2C_RD)
  114. {
  115. err = R_IIC_MASTER_Read(master_ctrl, msg[i].buf, msg[i].len, restart);
  116. if (FSP_SUCCESS == err)
  117. {
  118. if (RT_EOK != validate_i2c_event(ra_i2c))
  119. {
  120. LOG_E("POWER_CTL reg I2C read failed");
  121. break;
  122. }
  123. }
  124. /* handle error */
  125. else
  126. {
  127. /* Write API returns itself is not successful */
  128. LOG_E("R_I2C_MASTER_Write API failed");
  129. break;
  130. }
  131. }
  132. else
  133. {
  134. err = R_IIC_MASTER_Write(master_ctrl, msg[i].buf, msg[i].len, restart);
  135. if (FSP_SUCCESS == err)
  136. {
  137. if (RT_EOK != validate_i2c_event(ra_i2c))
  138. {
  139. LOG_E("POWER_CTL reg I2C write failed");
  140. break;
  141. }
  142. }
  143. /* handle error */
  144. else
  145. {
  146. /* Write API returns itself is not successful */
  147. LOG_E("R_I2C_MASTER_Write API failed");
  148. break;
  149. }
  150. }
  151. }
  152. return (rt_ssize_t)i;
  153. }
  154. static const struct rt_i2c_bus_device_ops ra_i2c_ops =
  155. {
  156. .master_xfer = ra_i2c_mst_xfer,
  157. .slave_xfer = RT_NULL,
  158. .i2c_bus_control = RT_NULL
  159. };
  160. int ra_hw_i2c_init(void)
  161. {
  162. fsp_err_t err = FSP_SUCCESS;
  163. for (rt_uint32_t i = 0; i < sizeof(ra_i2cs) / sizeof(ra_i2cs[0]); i++)
  164. {
  165. ra_i2cs[i].bus.ops = &ra_i2c_ops;
  166. ra_i2cs[i].bus.priv = 0;
  167. if (RT_EOK != rt_event_init(&ra_i2cs[i].event, ra_i2cs[i].bus_name, RT_IPC_FLAG_FIFO))
  168. {
  169. LOG_E("Init event failed");
  170. continue;
  171. }
  172. /* opening IIC master module */
  173. err = R_IIC_MASTER_Open(ra_i2cs[i].i2c_ctrl, ra_i2cs[i].i2c_cfg);
  174. if (FSP_SUCCESS != err)
  175. {
  176. LOG_E("R_I2C_MASTER_Open API failed,%d", err);
  177. continue;
  178. }
  179. err = R_IIC_MASTER_CallbackSet(ra_i2cs[i].i2c_ctrl, i2c_master_callback, &ra_i2cs[i], RT_NULL);
  180. /* handle error */
  181. if (FSP_SUCCESS != err)
  182. {
  183. LOG_E("R_I2C_CallbackSet API failed,%d", err);
  184. continue;
  185. }
  186. rt_i2c_bus_device_register(&ra_i2cs[i].bus, ra_i2cs[i].bus_name);
  187. }
  188. return 0;
  189. }
  190. INIT_DEVICE_EXPORT(ra_hw_i2c_init);
  191. #endif /* BSP_USING_I2C */