1
0

drv_i2c.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-07-29 zdzn first version
  9. */
  10. #include "raspi.h"
  11. #include "drv_i2c.h"
  12. //Maybe redefined
  13. typedef unsigned long rt_ubase_t;
  14. typedef rt_ubase_t rt_size_t;
  15. rt_uint8_t i2c_read_or_write(volatile rt_uint32_t base, rt_uint8_t* buf, rt_uint32_t len, rt_uint8_t flag)
  16. {
  17. rt_uint32_t status;
  18. rt_uint32_t remaining = len;
  19. rt_uint32_t i = 0;
  20. rt_uint8_t reason = BCM283X_I2C_REASON_OK;
  21. /* Clear FIFO */
  22. BCM283X_BSC_C(base) |= (BSC_C_CLEAR_1 & BSC_C_CLEAR_1);
  23. /* Clear Status */
  24. BCM283X_BSC_S(base) = BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE;
  25. /* Set Data Length */
  26. BCM283X_BSC_DLEN(base) = len;
  27. if (flag)
  28. {
  29. /* Start read */
  30. BCM283X_BSC_C(base) = BSC_C_I2CEN | BSC_C_ST | BSC_C_READ;
  31. /* wait for transfer to complete */
  32. while (!(BCM283X_BSC_S(base) & BSC_S_DONE))
  33. {
  34. /* we must empty the FIFO as it is populated and not use any delay */
  35. while (remaining && (BCM283X_BSC_S(base) & BSC_S_RXD))
  36. {
  37. /* Read from FIFO, no barrier */
  38. buf[i] = BCM283X_BSC_FIFO(base);
  39. i++;
  40. remaining--;
  41. }
  42. }
  43. /* transfer has finished - grab any remaining stuff in FIFO */
  44. while (remaining && (BCM283X_BSC_S(base) & BSC_S_RXD))
  45. {
  46. /* Read from FIFO, no barrier */
  47. buf[i] = BCM283X_BSC_FIFO(base);
  48. i++;
  49. remaining--;
  50. }
  51. }
  52. else
  53. {
  54. /* pre populate FIFO with max buffer */
  55. while (remaining && (i < BSC_FIFO_SIZE))
  56. {
  57. BCM283X_BSC_FIFO(base) = buf[i];
  58. i++;
  59. remaining--;
  60. }
  61. /* Enable device and start transfer */
  62. BCM283X_BSC_C(base) = BSC_C_I2CEN | BSC_C_ST;
  63. /* Transfer is over when BCM2835_BSC_S_DONE */
  64. while (!(BCM283X_BSC_S(base) & BSC_S_DONE))
  65. {
  66. while (remaining && (BCM283X_BSC_S(base) & BSC_S_TXD))
  67. {
  68. /* Write to FIFO */
  69. BCM283X_BSC_FIFO(base) = buf[i];
  70. i++;
  71. remaining--;
  72. }
  73. }
  74. }
  75. status = BCM283X_BSC_S(base);
  76. if (status & BSC_S_ERR)
  77. {
  78. reason = BCM283X_I2C_REASON_ERROR_NACK;
  79. }
  80. else if (status & BSC_S_CLKT)
  81. {
  82. reason = BCM283X_I2C_REASON_ERROR_CLKT;
  83. }
  84. else if (remaining)
  85. {
  86. reason = BCM283X_I2C_REASON_ERROR_DATA;
  87. }
  88. BCM283X_BSC_C(base) |= (BSC_S_DONE & BSC_S_DONE);
  89. return reason;
  90. }
  91. struct raspi_i2c_hw_config
  92. {
  93. rt_uint8_t bsc_num;
  94. rt_uint8_t sdl_pin;
  95. rt_uint8_t scl_pin;
  96. rt_uint8_t sdl_mode;
  97. rt_uint8_t scl_mode;
  98. };
  99. #if (defined(BSP_USING_I2C0) || defined(BSP_USING_I2C1))
  100. static rt_ssize_t raspi_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
  101. struct rt_i2c_msg msgs[],
  102. rt_uint32_t num);
  103. static rt_ssize_t raspi_i2c_slv_xfer(struct rt_i2c_bus_device *bus,
  104. struct rt_i2c_msg msgs[],
  105. rt_uint32_t num);
  106. static rt_err_t raspi_i2c_bus_control(struct rt_i2c_bus_device *bus,
  107. int cmd,
  108. void *args);
  109. static rt_uint32_t i2c_byte_wait_us = 0;
  110. static rt_ssize_t raspi_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
  111. struct rt_i2c_msg msgs[],
  112. rt_uint32_t num)
  113. {
  114. rt_size_t i;
  115. rt_uint8_t reason;
  116. RT_ASSERT(bus != RT_NULL);
  117. volatile rt_base_t base = (volatile rt_base_t)(bus->parent.user_data);
  118. if (bus->parent.user_data == 0)
  119. base = BCM283X_BSC0_BASE;
  120. else
  121. base = BCM283X_BSC1_BASE;
  122. BCM283X_BSC_A(base) = msgs->addr;
  123. for (i = 0; i < num; i++)
  124. {
  125. if (msgs[i].flags & RT_I2C_RD)
  126. reason = i2c_read_or_write(base, msgs->buf, msgs->len, 1);
  127. else
  128. reason = i2c_read_or_write(base, msgs->buf, msgs->len, 0);
  129. }
  130. return (reason == 0)? i : 0;
  131. }
  132. static rt_ssize_t raspi_i2c_slv_xfer(struct rt_i2c_bus_device *bus,
  133. struct rt_i2c_msg msgs[],
  134. rt_uint32_t num)
  135. {
  136. return 0;
  137. }
  138. static rt_err_t raspi_i2c_bus_control(struct rt_i2c_bus_device *bus,
  139. int cmd,
  140. void *args)
  141. {
  142. return RT_EOK;
  143. }
  144. static const struct rt_i2c_bus_device_ops raspi_i2c_ops =
  145. {
  146. .master_xfer = raspi_i2c_mst_xfer,
  147. .slave_xfer = raspi_i2c_slv_xfer,
  148. .i2c_bus_control = raspi_i2c_bus_control,
  149. };
  150. static rt_err_t raspi_i2c_configure(struct raspi_i2c_hw_config *cfg)
  151. {
  152. RT_ASSERT(cfg != RT_NULL);
  153. volatile rt_uint32_t base = cfg->scl_mode ? BCM283X_BSC1_BASE : BCM283X_BSC0_BASE;
  154. GPIO_FSEL(cfg->sdl_pin, cfg->sdl_mode); /* SDA */
  155. GPIO_FSEL(cfg->scl_pin, cfg->scl_mode); /* SCL */
  156. /* use 0xFFFE mask to limit a max value and round down any odd number */
  157. rt_uint32_t divider = (BCM283X_CORE_CLK_HZ / 10000) & 0xFFFE;
  158. BCM283X_BSC_DIV(base) = (rt_uint16_t) divider;
  159. i2c_byte_wait_us = (divider * 1000000 * 9 / BCM283X_CORE_CLK_HZ);
  160. return RT_EOK;
  161. }
  162. #endif
  163. #if defined (BSP_USING_I2C0)
  164. #define I2C0_BUS_NAME "i2c0"
  165. static struct raspi_i2c_hw_config hw_device0 =
  166. {
  167. .bsc_num = 0,
  168. .sdl_pin = RPI_GPIO_P1_27,
  169. .scl_pin = RPI_GPIO_P1_28,
  170. .sdl_mode = BCM283X_GPIO_FSEL_ALT0,
  171. .scl_mode = BCM283X_GPIO_FSEL_ALT0,
  172. };
  173. struct rt_i2c_bus_device device0 =
  174. {
  175. .ops = &raspi_i2c_ops,
  176. };
  177. #endif
  178. #if defined (BSP_USING_I2C1)
  179. #define I2C1_BUS_NAME "i2c1"
  180. static struct raspi_i2c_hw_config hw_device1 =
  181. {
  182. .bsc_num = 1,
  183. .sdl_pin = RPI_GPIO_P1_03,
  184. .scl_pin = RPI_GPIO_P1_05,
  185. .sdl_mode = BCM283X_GPIO_FSEL_ALT0,
  186. .scl_mode = BCM283X_GPIO_FSEL_ALT0,
  187. };
  188. struct rt_i2c_bus_device device1 =
  189. {
  190. .ops = &raspi_i2c_ops,
  191. };
  192. #endif
  193. int rt_hw_i2c_init(void)
  194. {
  195. #if defined(BSP_USING_I2C0)
  196. device0.parent.user_data = (void *)0;
  197. raspi_i2c_configure(&hw_device0);
  198. rt_i2c_bus_device_register(&device0, I2C0_BUS_NAME);
  199. #endif
  200. #if defined(BSP_USING_I2C1)
  201. device1.parent.user_data = (void *)1;
  202. raspi_i2c_configure(&hw_device1);
  203. rt_i2c_bus_device_register(&device1, I2C1_BUS_NAME);
  204. #endif
  205. return 0;
  206. }
  207. INIT_DEVICE_EXPORT(rt_hw_i2c_init);