drv_i2c.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #ifdef SOC_SERIES_R7FA2E2
  31. #define R_IIC_MASTER_Open R_IIC_B_MASTER_Open
  32. #define R_IIC_MASTER_Write R_IIC_B_MASTER_Write
  33. #define R_IIC_MASTER_Read R_IIC_B_MASTER_Read
  34. #define R_IIC_MASTER_SlaveAddressSet R_IIC_B_MASTER_SlaveAddressSet
  35. #define R_IIC_MASTER_CallbackSet R_IIC_B_MASTER_CallbackSet
  36. #endif
  37. struct ra_i2c_handle
  38. {
  39. struct rt_i2c_bus_device bus;
  40. char bus_name[RT_NAME_MAX];
  41. const i2c_master_cfg_t *i2c_cfg;
  42. void *i2c_ctrl;
  43. struct rt_event event;
  44. };
  45. static struct ra_i2c_handle ra_i2cs[] =
  46. {
  47. #ifdef BSP_USING_HW_I2C0
  48. {.bus_name = "i2c0", .i2c_cfg = &g_i2c_master0_cfg, .i2c_ctrl = &g_i2c_master0_ctrl,},
  49. #endif
  50. #ifdef BSP_USING_HW_I2C1
  51. {.bus_name = "i2c1", .i2c_cfg = &g_i2c_master1_cfg, .i2c_ctrl = &g_i2c_master1_ctrl,},
  52. #endif
  53. #ifdef BSP_USING_HW_I2C2
  54. {.bus_name = "i2c2", .i2c_cfg = &g_i2c_master2_cfg, .i2c_ctrl = &g_i2c_master2_ctrl,},
  55. #endif
  56. };
  57. void i2c_master_callback(i2c_master_callback_args_t *p_args)
  58. {
  59. rt_interrupt_enter();
  60. if (NULL != p_args)
  61. {
  62. /* capture callback event for validating the i2c transfer event*/
  63. struct ra_i2c_handle *obj = (struct ra_i2c_handle *)p_args->p_context;
  64. uint32_t event = 0;
  65. RT_ASSERT(obj != RT_NULL);
  66. switch (p_args->event)
  67. {
  68. case I2C_MASTER_EVENT_ABORTED:
  69. event |= RA_SCI_EVENT_ABORTED;
  70. break;
  71. case I2C_MASTER_EVENT_RX_COMPLETE:
  72. event |= RA_SCI_EVENT_RX_COMPLETE;
  73. break;
  74. case I2C_MASTER_EVENT_TX_COMPLETE:
  75. event |= RA_SCI_EVENT_TX_COMPLETE;
  76. break;
  77. }
  78. rt_event_send(&obj->event, event);
  79. }
  80. rt_interrupt_leave();
  81. }
  82. static rt_err_t validate_i2c_event(struct ra_i2c_handle *handle)
  83. {
  84. rt_uint32_t event = 0;
  85. 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))
  86. {
  87. return -RT_ETIMEOUT;
  88. }
  89. if ((event & (RA_SCI_EVENT_ABORTED | RA_SCI_EVENT_ERROR)) == 0)
  90. {
  91. return RT_EOK;
  92. }
  93. return -RT_ERROR;
  94. }
  95. static rt_ssize_t ra_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
  96. struct rt_i2c_msg msgs[],
  97. rt_uint32_t num)
  98. {
  99. rt_size_t i;
  100. struct rt_i2c_msg *msg = msgs;
  101. RT_ASSERT(bus != RT_NULL);
  102. fsp_err_t err = FSP_SUCCESS;
  103. bool restart = false;
  104. struct ra_i2c_handle *ra_i2c = rt_container_of(bus, struct ra_i2c_handle, bus);
  105. i2c_master_ctrl_t *master_ctrl = ra_i2c->i2c_ctrl;
  106. for (i = 0; i < num; i++)
  107. {
  108. if (msg[i].flags & RT_I2C_NO_START)
  109. {
  110. restart = true;
  111. }
  112. if (msg[i].flags & RT_I2C_ADDR_10BIT)
  113. {
  114. R_IIC_MASTER_SlaveAddressSet(master_ctrl, msg[i].addr, I2C_MASTER_ADDR_MODE_10BIT);
  115. }
  116. else
  117. {
  118. R_IIC_MASTER_SlaveAddressSet(master_ctrl, msg[i].addr, I2C_MASTER_ADDR_MODE_7BIT);
  119. }
  120. if (msg[i].flags & RT_I2C_RD)
  121. {
  122. err = R_IIC_MASTER_Read(master_ctrl, msg[i].buf, msg[i].len, restart);
  123. if (FSP_SUCCESS == err)
  124. {
  125. if (RT_EOK != validate_i2c_event(ra_i2c))
  126. {
  127. LOG_E("POWER_CTL reg I2C read failed");
  128. break;
  129. }
  130. }
  131. /* handle error */
  132. else
  133. {
  134. /* Write API returns itself is not successful */
  135. LOG_E("R_I2C_MASTER_Write API failed");
  136. break;
  137. }
  138. }
  139. else
  140. {
  141. err = R_IIC_MASTER_Write(master_ctrl, msg[i].buf, msg[i].len, restart);
  142. if (FSP_SUCCESS == err)
  143. {
  144. if (RT_EOK != validate_i2c_event(ra_i2c))
  145. {
  146. LOG_E("POWER_CTL reg I2C write failed");
  147. break;
  148. }
  149. }
  150. /* handle error */
  151. else
  152. {
  153. /* Write API returns itself is not successful */
  154. LOG_E("R_I2C_MASTER_Write API failed");
  155. break;
  156. }
  157. }
  158. }
  159. return (rt_ssize_t)i;
  160. }
  161. static const struct rt_i2c_bus_device_ops ra_i2c_ops =
  162. {
  163. .master_xfer = ra_i2c_mst_xfer,
  164. .slave_xfer = RT_NULL,
  165. .i2c_bus_control = RT_NULL
  166. };
  167. int ra_hw_i2c_init(void)
  168. {
  169. fsp_err_t err = FSP_SUCCESS;
  170. for (rt_uint32_t i = 0; i < sizeof(ra_i2cs) / sizeof(ra_i2cs[0]); i++)
  171. {
  172. ra_i2cs[i].bus.ops = &ra_i2c_ops;
  173. ra_i2cs[i].bus.priv = 0;
  174. if (RT_EOK != rt_event_init(&ra_i2cs[i].event, ra_i2cs[i].bus_name, RT_IPC_FLAG_FIFO))
  175. {
  176. LOG_E("Init event failed");
  177. continue;
  178. }
  179. /* opening IIC master module */
  180. err = R_IIC_MASTER_Open(ra_i2cs[i].i2c_ctrl, ra_i2cs[i].i2c_cfg);
  181. if (FSP_SUCCESS != err)
  182. {
  183. LOG_E("R_I2C_MASTER_Open API failed,%d", err);
  184. continue;
  185. }
  186. err = R_IIC_MASTER_CallbackSet(ra_i2cs[i].i2c_ctrl, i2c_master_callback, &ra_i2cs[i], RT_NULL);
  187. /* handle error */
  188. if (FSP_SUCCESS != err)
  189. {
  190. LOG_E("R_I2C_CallbackSet API failed,%d", err);
  191. continue;
  192. }
  193. rt_i2c_bus_device_register(&ra_i2cs[i].bus, ra_i2cs[i].bus_name);
  194. }
  195. return 0;
  196. }
  197. INIT_DEVICE_EXPORT(ra_hw_i2c_init);
  198. #endif /* BSP_USING_I2C */