1
0

drv_i2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. * 2017-08-08 Yang the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "board.h"
  13. #include "fsl_iocon.h"
  14. #include "fsl_gpio.h"
  15. #include "fsl_i2c.h"
  16. #ifdef RT_USING_I2C
  17. #ifdef RT_USING_I2C_BITOPS
  18. struct lpc_i2c_bit_data
  19. {
  20. struct
  21. {
  22. GPIO_Type *base;
  23. uint32_t port;
  24. uint32_t pin;
  25. } scl, sda;
  26. };
  27. static void gpio_set_sda(void *data, rt_int32_t state)
  28. {
  29. struct lpc_i2c_bit_data *bd = data;
  30. if (state)
  31. {
  32. //bd->sda.base->B[bd->sda.port][bd->sda.pin] = 1;
  33. GPIO_WritePinOutput(bd->sda.base, bd->sda.port, bd->sda.pin, 1);
  34. }
  35. else
  36. {
  37. GPIO_WritePinOutput(bd->sda.base, bd->sda.port, bd->sda.pin, 0);
  38. }
  39. }
  40. static void gpio_set_scl(void *data, rt_int32_t state)
  41. {
  42. struct lpc_i2c_bit_data *bd = data;
  43. if (state)
  44. {
  45. //bd->scl.base->B[bd->sda.port][bd->sda.pin] = 1;
  46. GPIO_WritePinOutput(bd->scl.base, bd->scl.port, bd->scl.pin, 1);
  47. }
  48. else
  49. {
  50. //bd->scl.base->B[bd->sda.port][bd->sda.pin] = 0;
  51. GPIO_WritePinOutput(bd->scl.base, bd->scl.port, bd->scl.pin, 0);
  52. }
  53. }
  54. static rt_int32_t gpio_get_sda(void *data)
  55. {
  56. struct lpc_i2c_bit_data *bd = data;
  57. return GPIO_ReadPinInput(bd->sda.base, bd->sda.port, bd->sda.pin) & 0x01;
  58. }
  59. static rt_int32_t gpio_get_scl(void *data)
  60. {
  61. struct lpc_i2c_bit_data *bd = data;
  62. return GPIO_ReadPinInput(bd->scl.base, bd->scl.port, bd->scl.pin) & 0x01;
  63. }
  64. static void gpio_udelay(rt_uint32_t us)
  65. {
  66. volatile rt_int32_t i;
  67. for (; us > 0; us--)
  68. {
  69. i = 10;
  70. while (i--);
  71. }
  72. }
  73. #else /* RT_USING_I2C_BITOPS */
  74. #define IOCON_PIO_DIGITAL_EN 0x0100u /*!< Enables digital function */
  75. #define IOCON_PIO_FUNC0 0x00u /*!< Selects pin function 0 */
  76. #define IOCON_PIO_FUNC1 0x01u /*!< Selects pin function 1 */
  77. #define IOCON_PIO_FUNC6 0x06u /*!< Selects pin function 6 */
  78. #define IOCON_PIO_I2CDRIVE_HIGH 0x0400u /*!< High drive: 20 mA */
  79. #define IOCON_PIO_I2CFILTER_EN 0x00u /*!< I2C 50 ns glitch filter enabled */
  80. #define IOCON_PIO_I2CSLEW_I2C 0x00u /*!< I2C mode */
  81. #define IOCON_PIO_INPFILT_OFF 0x0200u /*!< Input filter disabled */
  82. #define IOCON_PIO_INV_DI 0x00u /*!< Input function is not inverted */
  83. #define IOCON_PIO_MODE_INACT 0x00u /*!< No addition pin function */
  84. #define IOCON_PIO_MODE_PULLUP 0x20u /*!< Selects pull-up function */
  85. #define IOCON_PIO_OPENDRAIN_DI 0x00u /*!< Open drain is disabled */
  86. #define IOCON_PIO_SLEW_FAST 0x0400u /*!< Fast mode, slew rate control is disabled */
  87. #define IOCON_PIO_SLEW_STANDARD 0x00u /*!< Standard mode, output slew rate control is enabled */
  88. #define PIN0_IDX 0u /*!< Pin number for pin 0 in a port 3 */
  89. #define PIN1_IDX 1u /*!< Pin number for pin 1 in a port 3 */
  90. #define PIN2_IDX 2u /*!< Pin number for pin 2 in a port 0 */
  91. #define PIN3_IDX 3u /*!< Pin number for pin 3 in a port 0 */
  92. #define PIN4_IDX 4u /*!< Pin number for pin 4 in a port 0 */
  93. #define PIN5_IDX 5u /*!< Pin number for pin 5 in a port 0 */
  94. #define PIN6_IDX 6u /*!< Pin number for pin 6 in a port 0 */
  95. #define PIN7_IDX 7u /*!< Pin number for pin 7 in a port 0 */
  96. #define PIN8_IDX 8u /*!< Pin number for pin 8 in a port 0 */
  97. #define PIN9_IDX 9u /*!< Pin number for pin 9 in a port 0 */
  98. #define PIN10_IDX 10u /*!< Pin number for pin 10 in a port 1 */
  99. #define PIN11_IDX 11u /*!< Pin number for pin 11 in a port 1 */
  100. #define PIN12_IDX 12u /*!< Pin number for pin 12 in a port 1 */
  101. #define PIN13_IDX 13u /*!< Pin number for pin 13 in a port 1 */
  102. #define PIN14_IDX 14u /*!< Pin number for pin 14 in a port 1 */
  103. #define PIN15_IDX 15u /*!< Pin number for pin 15 in a port 0 */
  104. #define PIN16_IDX 16u /*!< Pin number for pin 16 in a port 1 */
  105. #define PIN18_IDX 18u /*!< Pin number for pin 18 in a port 0 */
  106. #define PIN19_IDX 19u /*!< Pin number for pin 19 in a port 0 */
  107. #define PIN20_IDX 20u /*!< Pin number for pin 20 in a port 0 */
  108. #define PIN21_IDX 21u /*!< Pin number for pin 21 in a port 0 */
  109. #define PIN22_IDX 22u /*!< Pin number for pin 22 in a port 2 */
  110. #define PIN23_IDX 23u /*!< Pin number for pin 23 in a port 1 */
  111. #define PIN24_IDX 24u /*!< Pin number for pin 24 in a port 1 */
  112. #define PIN25_IDX 25u /*!< Pin number for pin 25 in a port 1 */
  113. #define PIN26_IDX 26u /*!< Pin number for pin 26 in a port 1 */
  114. #define PIN27_IDX 27u /*!< Pin number for pin 27 in a port 1 */
  115. #define PIN28_IDX 28u /*!< Pin number for pin 28 in a port 1 */
  116. #define PIN29_IDX 29u /*!< Pin number for pin 29 in a port 0 */
  117. #define PIN30_IDX 30u /*!< Pin number for pin 30 in a port 0 */
  118. #define PIN31_IDX 31u /*!< Pin number for pin 31 in a port 1 */
  119. #define PORT0_IDX 0u /*!< Port index */
  120. #define PORT1_IDX 1u /*!< Port index */
  121. #define PORT2_IDX 2u /*!< Port index */
  122. #define PORT3_IDX 3u /*!< Port index */
  123. struct lpc_i2c_bus
  124. {
  125. struct rt_i2c_bus_device parent;
  126. I2C_Type *I2C;
  127. };
  128. static rt_ssize_t lpc_i2c_xfer(struct rt_i2c_bus_device *bus,
  129. struct rt_i2c_msg msgs[], rt_uint32_t num)
  130. {
  131. struct rt_i2c_msg *msg;
  132. i2c_master_transfer_t xfer = {0};
  133. rt_uint32_t i;
  134. rt_err_t ret = -RT_ERROR;
  135. struct lpc_i2c_bus *lpc_i2c = (struct lpc_i2c_bus *)bus;
  136. for (i = 0; i < num; i++)
  137. {
  138. msg = &msgs[i];
  139. if (msg->flags & RT_I2C_RD)
  140. {
  141. xfer.slaveAddress = msg->addr;
  142. xfer.direction = kI2C_Read;
  143. xfer.subaddress = 1;
  144. xfer.subaddressSize = 1;
  145. xfer.data = msg->buf;
  146. xfer.dataSize = msg->len;
  147. xfer.flags = kI2C_TransferDefaultFlag;
  148. if (I2C_MasterTransferBlocking(lpc_i2c->I2C, &xfer) != kStatus_Success)
  149. {
  150. i2c_dbg("i2c bus write failed,i2c bus stop!\n");
  151. goto out;
  152. }
  153. }
  154. else
  155. {
  156. xfer.slaveAddress = msg->addr;
  157. xfer.direction = kI2C_Write;
  158. xfer.subaddress = 0;
  159. xfer.subaddressSize = 1;
  160. xfer.data = msg->buf;
  161. xfer.dataSize = msg->len;
  162. xfer.flags = kI2C_TransferDefaultFlag;
  163. if (I2C_MasterTransferBlocking(lpc_i2c->I2C, &xfer) != kStatus_Success)
  164. {
  165. i2c_dbg("i2c bus write failed,i2c bus stop!\n");
  166. goto out;
  167. }
  168. }
  169. }
  170. ret = i;
  171. out:
  172. i2c_dbg("send stop condition\n");
  173. return ret;
  174. }
  175. static const struct rt_i2c_bus_device_ops i2c_ops =
  176. {
  177. lpc_i2c_xfer,
  178. RT_NULL,
  179. RT_NULL
  180. };
  181. #endif /* RT_USING_I2C_BITOPS */
  182. int rt_hw_i2c_init(void)
  183. {
  184. #ifdef RT_USING_I2C_BITOPS
  185. /* register I2C1: SCL/P0_20 SDA/P0_19 */
  186. {
  187. static struct rt_i2c_bus_device i2c_device;
  188. static const struct lpc_i2c_bit_data _i2c_bdata =
  189. {
  190. /* SCL */ {GPIO, 3, 24},
  191. /* SDA */ {GPIO, 3, 23},
  192. };
  193. static const struct rt_i2c_bit_ops _i2c_bit_ops =
  194. {
  195. (void*)&_i2c_bdata,
  196. gpio_set_sda,
  197. gpio_set_scl,
  198. gpio_get_sda,
  199. gpio_get_scl,
  200. gpio_udelay,
  201. 5,
  202. 100
  203. };
  204. gpio_pin_config_t pin_config = {
  205. kGPIO_DigitalOutput, 0,
  206. };
  207. CLOCK_EnableClock(kCLOCK_Gpio3);
  208. /* Enable touch panel controller */
  209. GPIO_PinInit(GPIO, _i2c_bdata.sda.port, _i2c_bdata.sda.pin, &pin_config);
  210. GPIO_PinInit(GPIO, _i2c_bdata.scl.port, _i2c_bdata.scl.pin, &pin_config);
  211. GPIO_WritePinOutput(GPIO, _i2c_bdata.sda.port, _i2c_bdata.sda.pin, 1);
  212. GPIO_WritePinOutput(GPIO, _i2c_bdata.scl.port, _i2c_bdata.scl.pin, 1);
  213. i2c_device.priv = (void *)&_i2c_bit_ops;
  214. rt_i2c_bit_add_bus(&i2c_device, "i2c2");
  215. } /* register I2C */
  216. #else /* RT_USING_I2C_BITOPS */
  217. static struct lpc_i2c_bus lpc_i2c2;
  218. /* attach 12 MHz clock to FLEXCOMM2 (I2C master for touch controller) */
  219. CLOCK_AttachClk(kFRO12M_to_FLEXCOMM2);
  220. const uint32_t port3_pin23_config = (
  221. IOCON_PIO_FUNC1 | /* Pin is configured as FC2_CTS_SDA_SSEL0 */
  222. IOCON_PIO_I2CSLEW_I2C | /* I2C mode */
  223. IOCON_PIO_INV_DI | /* Input function is not inverted */
  224. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  225. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  226. IOCON_PIO_I2CDRIVE_HIGH | /* High drive: 20 mA */
  227. IOCON_PIO_I2CFILTER_EN /* I2C 50 ns glitch filter enabled */
  228. );
  229. IOCON_PinMuxSet(IOCON, PORT3_IDX, PIN23_IDX, port3_pin23_config); /* PORT3 PIN23 (coords: C2) is configured as FC2_CTS_SDA_SSEL0 */
  230. const uint32_t port3_pin24_config = (
  231. IOCON_PIO_FUNC1 | /* Pin is configured as FC2_RTS_SCL_SSEL1 */
  232. IOCON_PIO_I2CSLEW_I2C | /* I2C mode */
  233. IOCON_PIO_INV_DI | /* Input function is not inverted */
  234. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  235. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  236. IOCON_PIO_I2CDRIVE_HIGH | /* High drive: 20 mA */
  237. IOCON_PIO_I2CFILTER_EN /* I2C 50 ns glitch filter enabled */
  238. );
  239. IOCON_PinMuxSet(IOCON, PORT3_IDX, PIN24_IDX, port3_pin24_config); /* PORT3 PIN24 (coords: E2) is configured as FC2_RTS_SCL_SSEL1 */
  240. {
  241. i2c_master_config_t masterConfig;
  242. I2C_MasterGetDefaultConfig(&masterConfig);
  243. /* Change the default baudrate configuration */
  244. masterConfig.baudRate_Bps = 100000U;
  245. /* Initialize the I2C master peripheral */
  246. I2C_MasterInit(I2C2, &masterConfig, 12000000);
  247. }
  248. rt_memset((void *)&lpc_i2c2, 0, sizeof(struct lpc_i2c_bus));
  249. lpc_i2c2.parent.ops = &i2c_ops;
  250. lpc_i2c2.I2C = I2C2;
  251. rt_i2c_bus_device_register(&lpc_i2c2.parent, "i2c2");
  252. #endif /* RT_USING_I2C_BITOPS */
  253. return 0;
  254. }
  255. INIT_DEVICE_EXPORT(rt_hw_i2c_init);
  256. #endif /* RT_USING_I2C */