drv_i2c.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * File : drv_i2c.c
  3. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Change Logs:
  20. * Date Author Notes
  21. * 2017-08-08 Yang the first version
  22. */
  23. #include <rtthread.h>
  24. #include <rtdevice.h>
  25. #include "board.h"
  26. #include "fsl_gpio.h"
  27. #include "fsl_lpi2c.h"
  28. //#define DRV_I2C_DEBUG
  29. #ifdef RT_USING_I2C
  30. #ifdef RT_USING_I2C_BITOPS
  31. #define I2CBUS_NAME "i2c0"
  32. struct stm32_i2c_bit_data
  33. {
  34. struct
  35. {
  36. GPIO_Type *base;
  37. uint32_t pin;
  38. } scl, sda;
  39. };
  40. static void gpio_udelay(rt_uint32_t us)
  41. {
  42. volatile rt_int32_t i;
  43. for (; us > 0; us--)
  44. {
  45. i = 1000;
  46. while (i--);
  47. }
  48. }
  49. static void gpio_set_input(GPIO_Type* base, uint32_t pin)
  50. {
  51. if (base->GDIR & (1 << pin)) //output mode
  52. {
  53. base->GDIR &= ~(1 << pin);
  54. gpio_udelay(5);
  55. }
  56. }
  57. static void gpio_set_output(GPIO_Type* base, uint32_t pin)
  58. {
  59. if (!(base->GDIR & (1 << pin))) //input mode
  60. {
  61. base->GDIR |= (1 << pin);
  62. gpio_udelay(5);
  63. }
  64. }
  65. static void gpio_set_sda(void *data, rt_int32_t state)
  66. {
  67. struct stm32_i2c_bit_data *bd = data;
  68. gpio_set_output(bd->sda.base, bd->sda.pin);
  69. GPIO_PinWrite(bd->sda.base, bd->sda.pin, !!state);
  70. }
  71. static void gpio_set_scl(void *data, rt_int32_t state)
  72. {
  73. struct stm32_i2c_bit_data *bd = data;
  74. gpio_set_output(bd->scl.base, bd->scl.pin);
  75. GPIO_PinWrite(bd->scl.base, bd->scl.pin, !!state);
  76. }
  77. static rt_int32_t gpio_get_sda(void *data)
  78. {
  79. struct stm32_i2c_bit_data *bd = data;
  80. gpio_set_input(bd->sda.base, bd->sda.pin);
  81. return GPIO_ReadPinInput(bd->sda.base, bd->sda.pin);
  82. }
  83. static rt_int32_t gpio_get_scl(void *data)
  84. {
  85. struct stm32_i2c_bit_data *bd = data;
  86. gpio_set_input(bd->scl.base, bd->scl.pin);
  87. return GPIO_ReadPinInput(bd->scl.base, bd->scl.pin);
  88. }
  89. #else /* RT_USING_I2C_BITOPS */
  90. // todo : add hardware i2c
  91. #endif /* RT_USING_I2C_BITOPS */
  92. int rt_hw_i2c_init(void)
  93. {
  94. #ifdef RT_USING_I2C_BITOPS
  95. /* register I2C1: SCL/P0_20 SDA/P0_19 */
  96. {
  97. static struct rt_i2c_bus_device i2c_device;
  98. static const struct stm32_i2c_bit_data _i2c_bdata =
  99. {
  100. /* SCL */ {GPIO1, 16},
  101. /* SDA */ {GPIO1, 17},
  102. };
  103. static const struct rt_i2c_bit_ops _i2c_bit_ops =
  104. {
  105. (void*)&_i2c_bdata,
  106. gpio_set_sda,
  107. gpio_set_scl,
  108. gpio_get_sda,
  109. gpio_get_scl,
  110. gpio_udelay,
  111. 50,
  112. 1000
  113. };
  114. gpio_pin_config_t pin_config = {
  115. kGPIO_DigitalOutput, 0,
  116. };
  117. CLOCK_EnableClock(kCLOCK_Iomuxc);
  118. IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_00_GPIO1_IO16, 1U);
  119. IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B1_00_GPIO1_IO16,
  120. 0xD8B0u); /* Slew Rate Field: Slow Slew Rate
  121. Drive Strength Field: R0/6
  122. Speed Field: medium(100MHz)
  123. Open Drain Enable Field: Open Drain Enabled
  124. Pull / Keep Enable Field: Pull/Keeper Enabled
  125. Pull / Keep Select Field: Keeper
  126. Pull Up / Down Config. Field: 22K Ohm Pull Up
  127. Hyst. Enable Field: Hysteresis Disabled */
  128. IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_01_GPIO1_IO17, 1U);
  129. IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B1_01_GPIO1_IO17,
  130. 0xD8B0u); /* Slew Rate Field: Slow Slew Rate
  131. Drive Strength Field: R0/6
  132. Speed Field: medium(100MHz)
  133. Open Drain Enable Field: Open Drain Enabled
  134. Pull / Keep Enable Field: Pull/Keeper Enabled
  135. Pull / Keep Select Field: Keeper
  136. Pull Up / Down Config. Field: 22K Ohm Pull Up
  137. Hyst. Enable Field: Hysteresis Disabled */
  138. /* Enable touch panel controller */
  139. GPIO_PinInit(_i2c_bdata.sda.base, _i2c_bdata.sda.pin, &pin_config);
  140. GPIO_PinInit(_i2c_bdata.scl.base, _i2c_bdata.scl.pin, &pin_config);
  141. GPIO_PortSet(_i2c_bdata.sda.base, _i2c_bdata.sda.pin);
  142. GPIO_PortSet(_i2c_bdata.scl.base, _i2c_bdata.scl.pin);
  143. //RT_ASSERT(gpio_get_scl(&_i2c_bdata) != 0);
  144. //RT_ASSERT(gpio_get_sda(&_i2c_bdata) != 0);
  145. i2c_device.priv = (void *)&_i2c_bit_ops;
  146. rt_i2c_bit_add_bus(&i2c_device, I2CBUS_NAME);
  147. } /* register I2C */
  148. #else /* RT_USING_I2C_BITOPS */
  149. // Todo : add hardware i2c
  150. #endif /* RT_USING_I2C_BITOPS */
  151. return 0;
  152. }
  153. INIT_DEVICE_EXPORT(rt_hw_i2c_init);
  154. #if defined(RT_USING_FINSH) && defined(DRV_I2C_DEBUG)
  155. #include <finsh.h>
  156. static rt_device_t _i2c_find(const char *name)
  157. {
  158. rt_device_t dev;
  159. dev = rt_device_find(name);
  160. if (!dev)
  161. {
  162. rt_kprintf("search device failed: %s\n", name);
  163. return RT_NULL;
  164. }
  165. if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
  166. {
  167. rt_kprintf("open device failed: %s\n", name);
  168. return RT_NULL;
  169. }
  170. rt_kprintf("open i2c bus: %s\n", name);
  171. return dev;
  172. }
  173. static void _search_i2c_device(rt_device_t dev, uint8_t cmd)
  174. {
  175. int count = 0;
  176. struct rt_i2c_msg msgs[2];
  177. uint8_t buf = 0;
  178. msgs[0].flags = RT_I2C_WR;
  179. msgs[0].buf = &cmd;
  180. msgs[0].len = sizeof(cmd);
  181. msgs[1].flags = RT_I2C_RD;
  182. msgs[1].buf = &buf;
  183. msgs[1].len = 1;
  184. for (int i = 0; i <= 0x7f; i++)
  185. {
  186. int len;
  187. msgs[0].addr = i;
  188. msgs[1].addr = i;
  189. len = rt_i2c_transfer((struct rt_i2c_bus_device *)dev, msgs, 2);
  190. if (len == 2)
  191. {
  192. count++;
  193. rt_kprintf("add:%02X transfer success, id: %02X\n", i, buf);
  194. }
  195. }
  196. rt_kprintf("i2c device: %d\n", count);
  197. }
  198. static int i2c_test(const char *name, uint8_t cmd)
  199. {
  200. rt_device_t dev = _i2c_find(name);
  201. if (dev == RT_NULL)
  202. {
  203. rt_kprintf("search i2c device faild\n");
  204. return -1;
  205. }
  206. _search_i2c_device(dev, cmd);
  207. rt_device_close(dev);
  208. return 0;
  209. }
  210. FINSH_FUNCTION_EXPORT(i2c_test, e.g: i2c_test("i2c0", 0xA3));
  211. #endif
  212. #endif /* RT_USING_I2C */