drv_i2c.c 5.7 KB

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