i2c.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * File :_i2c.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-12-04 Haley the first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include "am_mcu_apollo.h"
  27. /* I2C0 */
  28. #define AM_I2C0_IOM_INST 0
  29. #define I2C0_GPIO_SCL 5
  30. #define I2C0_GPIO_CFG_SCK AM_HAL_PIN_5_M0SCL
  31. #define I2C0_GPIO_SDA 6
  32. #define I2C0_GPIO_CFG_SDA AM_HAL_PIN_6_M0SDA
  33. /* I2C2 */
  34. #define AM_I2C2_IOM_INST 2
  35. #define I2C2_GPIO_SCL 27
  36. #define I2C2_GPIO_CFG_SCK AM_HAL_PIN_27_M2SCL
  37. #define I2C2_GPIO_SDA 25
  38. #define I2C2_GPIO_CFG_SDA AM_HAL_PIN_25_M2SDA
  39. /* I2C3 */
  40. #define AM_I2C3_IOM_INST 3
  41. #define I2C3_GPIO_SCL 42
  42. #define I2C3_GPIO_CFG_SCK AM_HAL_PIN_42_M3SCL
  43. #define I2C3_GPIO_SDA 43
  44. #define I2C3_GPIO_CFG_SDA AM_HAL_PIN_43_M3SDA
  45. /* I2C4 */
  46. #define AM_I2C4_IOM_INST 4
  47. #define I2C4_GPIO_SCL 39
  48. #define I2C4_GPIO_CFG_SCK AM_HAL_PIN_39_M4SCL
  49. #define I2C4_GPIO_SDA 40
  50. #define I2C4_GPIO_CFG_SDA AM_HAL_PIN_40_M4SDA
  51. static am_hal_iom_config_t g_sIOMConfig =
  52. {
  53. AM_HAL_IOM_I2CMODE, // ui32InterfaceMode
  54. AM_HAL_IOM_100KHZ, // ui32ClockFrequency
  55. 0, // bSPHA
  56. 0, // bSPOL
  57. 4, // ui8WriteThreshold
  58. 60, // ui8ReadThreshold
  59. };
  60. /* AM i2c driver */
  61. struct am_i2c_bus
  62. {
  63. struct rt_i2c_bus_device parent;
  64. rt_uint32_t u32Module;
  65. };
  66. //connect am drv to rt drv.
  67. rt_size_t rt_i2c_master_xfer(struct rt_i2c_bus_device *bus,
  68. struct rt_i2c_msg *msgs,
  69. rt_uint32_t num)
  70. {
  71. struct am_i2c_bus * am_i2c_bus = (struct am_i2c_bus *)bus;
  72. struct rt_i2c_msg *msg;
  73. int i;
  74. rt_uint32_t msg_len = 0;
  75. for (i = 0; i < num; i++)
  76. {
  77. msg = &msgs[i];
  78. if (msg->flags == RT_I2C_RD)
  79. {
  80. am_hal_iom_i2c_read(am_i2c_bus->u32Module, msg->addr, (uint32_t *)msg->buf, msg->len, AM_HAL_IOM_RAW);
  81. msg_len += msg->len;
  82. }
  83. else if(msg->flags == RT_I2C_WR)
  84. {
  85. am_hal_iom_i2c_write(am_i2c_bus->u32Module, msg->addr, (uint32_t *)msg->buf, msg->len, AM_HAL_IOM_RAW);
  86. msg_len += (msg->len - 1);
  87. }
  88. }
  89. return msg_len;
  90. }
  91. rt_err_t rt_i2c_bus_control(struct rt_i2c_bus_device *bus,
  92. rt_uint32_t cmd,
  93. rt_uint32_t arg)
  94. {
  95. struct am_i2c_bus * am_i2c_bus = (struct am_i2c_bus *)bus;
  96. //rt_uint32_t ctrl_arg = (rt_uint32_t)(arg);
  97. RT_ASSERT(bus != RT_NULL);
  98. am_i2c_bus = (struct am_i2c_bus *)bus->parent.user_data;
  99. RT_ASSERT(am_i2c_bus != RT_NULL);
  100. switch (cmd)
  101. {
  102. /* I2C config */
  103. case RT_DEVICE_CTRL_CONFIG :
  104. break;
  105. }
  106. return RT_EOK;
  107. }
  108. static const struct rt_i2c_bus_device_ops am_i2c_ops =
  109. {
  110. rt_i2c_master_xfer,
  111. RT_NULL,
  112. rt_i2c_bus_control
  113. };
  114. #ifdef RT_USING_I2C0
  115. static struct am_i2c_bus am_i2c_bus_0 =
  116. {
  117. {0},
  118. AM_I2C0_IOM_INST
  119. };
  120. #endif
  121. #ifdef RT_USING_I2C1
  122. static struct am_i2c_bus am_i2c_bus_1 =
  123. {
  124. {1},
  125. AM_I2C1_IOM_INST
  126. };
  127. #endif
  128. #ifdef RT_USING_I2C2
  129. static struct am_i2c_bus am_i2c_bus_2 =
  130. {
  131. {2},
  132. AM_I2C2_IOM_INST
  133. };
  134. #endif
  135. #ifdef RT_USING_I2C3
  136. static struct am_i2c_bus am_i2c_bus_3 =
  137. {
  138. {3},
  139. AM_I2C3_IOM_INST
  140. };
  141. #endif
  142. #ifdef RT_USING_I2C4
  143. static struct am_i2c_bus am_i2c_bus_4 =
  144. {
  145. {4},
  146. AM_I2C4_IOM_INST
  147. };
  148. #endif
  149. int rt_i2c_init(void)
  150. {
  151. struct am_i2c_bus* am_i2c;
  152. #ifdef RT_USING_I2C0
  153. /* init i2c gpio */
  154. am_hal_gpio_pin_config(I2C0_GPIO_SCL, I2C0_GPIO_CFG_SCK | AM_HAL_GPIO_PULL6K);
  155. am_hal_gpio_pin_config(I2C0_GPIO_SDA, I2C0_GPIO_CFG_SDA | AM_HAL_GPIO_PULL6K);
  156. /* Initialize IOM 0 in I2C mode at 100KHz */
  157. am_hal_iom_pwrctrl_enable(AM_I2C0_IOM_INST);
  158. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_100KHZ;
  159. am_hal_iom_config(AM_I2C0_IOM_INST, &g_sIOMConfig);
  160. am_hal_iom_enable(AM_I2C0_IOM_INST);
  161. /* init i2c bus device */
  162. am_i2c = &am_i2c_bus_0;
  163. am_i2c->parent.ops = &am_i2c_ops;
  164. rt_i2c_bus_device_register(&am_i2c->parent, "i2c0");
  165. #endif
  166. #ifdef RT_USING_I2C2
  167. /* init i2c gpio */
  168. am_hal_gpio_pin_config(I2C2_GPIO_SCL, I2C2_GPIO_CFG_SCK | AM_HAL_GPIO_PULL6K);
  169. am_hal_gpio_pin_config(I2C2_GPIO_SDA, I2C2_GPIO_CFG_SDA | AM_HAL_GPIO_PULL6K);
  170. /* Initialize IOM 2 in I2C mode at 400KHz */
  171. am_hal_iom_pwrctrl_enable(AM_I2C2_IOM_INST);
  172. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_400KHZ;
  173. am_hal_iom_config(AM_I2C2_IOM_INST, &g_sIOMConfig);
  174. am_hal_iom_enable(AM_I2C2_IOM_INST);
  175. /* init i2c bus device */
  176. am_i2c = &am_i2c_bus_2;
  177. am_i2c->parent.ops = &am_i2c_ops;
  178. rt_i2c_bus_device_register(&am_i2c->parent, "i2c2");
  179. #endif
  180. #ifdef RT_USING_I2C3
  181. /* init i2c gpio */
  182. am_hal_gpio_pin_config(I2C3_GPIO_SCL, I2C3_GPIO_CFG_SCK | AM_HAL_GPIO_PULL6K);
  183. am_hal_gpio_pin_config(I2C3_GPIO_SDA, I2C3_GPIO_CFG_SDA | AM_HAL_GPIO_PULL6K);
  184. /* Initialize IOM 3 in I2C mode at 400KHz */
  185. am_hal_iom_pwrctrl_enable(AM_I2C3_IOM_INST);
  186. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_400KHZ;
  187. am_hal_iom_config(AM_I2C3_IOM_INST, &g_sIOMConfig);
  188. am_hal_iom_enable(AM_I2C3_IOM_INST);
  189. /* init i2c bus device */
  190. am_i2c = &am_i2c_bus_3;
  191. am_i2c->parent.ops = &am_i2c_ops;
  192. rt_i2c_bus_device_register(&am_i2c->parent, "i2c3");
  193. #endif
  194. #ifdef RT_USING_I2C4
  195. /* init i2c gpio */
  196. am_hal_gpio_pin_config(I2C4_GPIO_SCL, I2C4_GPIO_CFG_SCK | AM_HAL_GPIO_PULL6K);
  197. am_hal_gpio_pin_config(I2C4_GPIO_SDA, I2C4_GPIO_CFG_SDA | AM_HAL_GPIO_PULL6K);
  198. /* Initialize IOM 4 in I2C mode at 400KHz */
  199. am_hal_iom_pwrctrl_enable(AM_I2C4_IOM_INST);
  200. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_400KHZ;
  201. am_hal_iom_config(AM_I2C4_IOM_INST, &g_sIOMConfig);
  202. am_hal_iom_enable(AM_I2C4_IOM_INST);
  203. /* init i2c bus device */
  204. am_i2c = &am_i2c_bus_4;
  205. am_i2c->parent.ops = &am_i2c_ops;
  206. rt_i2c_bus_device_register(&am_i2c->parent, "i2c4");
  207. #endif
  208. //rt_kprintf("i2c_init!\n");
  209. return 0;
  210. }
  211. #ifdef RT_USING_COMPONENTS_INIT
  212. INIT_BOARD_EXPORT(rt_i2c_init);
  213. #endif
  214. /*@}*/