i2c.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #include "board.h"
  28. /* I2C0 */
  29. #define AM_I2C0_IOM_INST 0
  30. #define I2C0_GPIO_SCL 5
  31. #define I2C0_GPIO_CFG_SCK AM_HAL_PIN_5_M0SCL
  32. #define I2C0_GPIO_SDA 6
  33. #define I2C0_GPIO_CFG_SDA AM_HAL_PIN_6_M0SDA
  34. /* I2C2 */
  35. #define AM_I2C2_IOM_INST 2
  36. #define I2C2_GPIO_SCL 27
  37. #define I2C2_GPIO_CFG_SCK AM_HAL_PIN_27_M2SCL
  38. #define I2C2_GPIO_SDA 25
  39. #define I2C2_GPIO_CFG_SDA AM_HAL_PIN_25_M2SDA
  40. /* I2C4 */
  41. #define AM_I2C4_IOM_INST 4
  42. #define I2C4_GPIO_SCL 39
  43. #define I2C4_GPIO_CFG_SCK AM_HAL_PIN_39_M4SCL
  44. #define I2C4_GPIO_SDA 40
  45. #define I2C4_GPIO_CFG_SDA AM_HAL_PIN_40_M4SDA
  46. static am_hal_iom_config_t g_sIOMConfig =
  47. {
  48. AM_HAL_IOM_I2CMODE, // ui32InterfaceMode
  49. AM_HAL_IOM_100KHZ, // ui32ClockFrequency
  50. 0, // bSPHA
  51. 0, // bSPOL
  52. 4, // ui8WriteThreshold
  53. 60, // ui8ReadThreshold
  54. };
  55. /* AM i2c driver */
  56. struct am_i2c_bus
  57. {
  58. struct rt_i2c_bus_device parent;
  59. rt_uint32_t u32Module;
  60. };
  61. //connect am drv to rt drv.
  62. rt_size_t rt_i2c_master_xfer(struct rt_i2c_bus_device *bus,
  63. struct rt_i2c_msg *msgs,
  64. rt_uint32_t num)
  65. {
  66. struct am_i2c_bus * am_i2c_bus = (struct am_i2c_bus *)bus;
  67. struct rt_i2c_msg *msg;
  68. int i;
  69. rt_int32_t ret = RT_EOK;
  70. for (i = 0; i < num; i++)
  71. {
  72. msg = &msgs[i];
  73. if (msg->flags == RT_I2C_RD)
  74. {
  75. am_hal_iom_i2c_read(am_i2c_bus->u32Module, msg->addr, (uint32_t *)msg->buf, msg->len, AM_HAL_IOM_RAW);
  76. }
  77. else if(msg->flags == RT_I2C_WR)
  78. {
  79. am_hal_iom_i2c_write(am_i2c_bus->u32Module, msg->addr, (uint32_t *)msg->buf, msg->len, AM_HAL_IOM_RAW);
  80. }
  81. ret++;
  82. }
  83. return ret;
  84. }
  85. rt_err_t rt_i2c_bus_control(struct rt_i2c_bus_device *bus,
  86. rt_uint32_t cmd,
  87. rt_uint32_t arg)
  88. {
  89. struct am_i2c_bus * am_i2c_bus = (struct am_i2c_bus *)bus;
  90. //rt_uint32_t ctrl_arg = (rt_uint32_t)(arg);
  91. RT_ASSERT(bus != RT_NULL);
  92. am_i2c_bus = (struct am_i2c_bus *)bus->parent.user_data;
  93. RT_ASSERT(am_i2c_bus != RT_NULL);
  94. switch (cmd)
  95. {
  96. /* I2C config */
  97. case RT_DEVICE_CTRL_CONFIG :
  98. break;
  99. }
  100. return RT_EOK;
  101. }
  102. static const struct rt_i2c_bus_device_ops am_i2c_ops =
  103. {
  104. rt_i2c_master_xfer,
  105. RT_NULL,
  106. rt_i2c_bus_control
  107. };
  108. #ifdef RT_USING_I2C0
  109. static struct am_i2c_bus am_i2c_bus_0 =
  110. {
  111. {0},
  112. AM_I2C0_IOM_INST
  113. };
  114. #endif
  115. #ifdef RT_USING_I2C1
  116. static struct am_i2c_bus am_i2c_bus_1 =
  117. {
  118. {0},
  119. AM_I2C1_IOM_INST
  120. };
  121. #endif
  122. #ifdef RT_USING_I2C2
  123. static struct am_i2c_bus am_i2c_bus_2 =
  124. {
  125. {1},
  126. AM_I2C2_IOM_INST
  127. };
  128. #endif
  129. #ifdef RT_USING_I2C3
  130. static struct am_i2c_bus am_i2c_bus_3 =
  131. {
  132. {2},
  133. AM_I2C3_IOM_INST
  134. };
  135. #endif
  136. #ifdef RT_USING_I2C4
  137. static struct am_i2c_bus am_i2c_bus_4 =
  138. {
  139. {3},
  140. AM_I2C4_IOM_INST
  141. };
  142. #endif
  143. int rt_i2c_init(void)
  144. {
  145. struct am_i2c_bus* am_i2c;
  146. #ifdef RT_USING_I2C0
  147. /* init i2c gpio */
  148. am_hal_gpio_pin_config(I2C0_GPIO_SCL, I2C0_GPIO_CFG_SCK | AM_HAL_GPIO_PULL6K);
  149. am_hal_gpio_pin_config(I2C0_GPIO_SDA, I2C0_GPIO_CFG_SDA | AM_HAL_GPIO_PULL6K);
  150. /* Initialize IOM 0 in I2C mode at 100KHz */
  151. am_hal_iom_pwrctrl_enable(AM_I2C0_IOM_INST);
  152. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_100KHZ;
  153. am_hal_iom_config(AM_I2C0_IOM_INST, &g_sIOMConfig);
  154. am_hal_iom_enable(AM_I2C0_IOM_INST);
  155. /* init i2c bus device */
  156. am_i2c = &am_i2c_bus_0;
  157. am_i2c->parent.ops = &am_i2c_ops;
  158. rt_i2c_bus_device_register(&am_i2c->parent, "i2c0");
  159. #endif
  160. #ifdef RT_USING_I2C2
  161. /* init i2c gpio */
  162. am_hal_gpio_pin_config(I2C2_GPIO_SCL, I2C2_GPIO_CFG_SCK | AM_HAL_GPIO_PULL6K);
  163. am_hal_gpio_pin_config(I2C2_GPIO_SDA, I2C2_GPIO_CFG_SDA | AM_HAL_GPIO_PULL6K);
  164. /* Initialize IOM 2 in I2C mode at 400KHz */
  165. am_hal_iom_pwrctrl_enable(AM_I2C2_IOM_INST);
  166. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_400KHZ;
  167. am_hal_iom_config(AM_I2C2_IOM_INST, &g_sIOMConfig);
  168. am_hal_iom_enable(AM_I2C2_IOM_INST);
  169. /* init i2c bus device */
  170. am_i2c = &am_i2c_bus_2;
  171. am_i2c->parent.ops = &am_i2c_ops;
  172. rt_i2c_bus_device_register(&am_i2c->parent, "i2c2");
  173. #endif
  174. #ifdef RT_USING_I2C4
  175. /* init i2c gpio */
  176. am_hal_gpio_pin_config(I2C4_GPIO_SCL, I2C4_GPIO_CFG_SCK | AM_HAL_GPIO_PULL6K);
  177. am_hal_gpio_pin_config(I2C4_GPIO_SDA, I2C4_GPIO_CFG_SDA | AM_HAL_GPIO_PULL6K);
  178. /* Initialize IOM 4 in I2C mode at 400KHz */
  179. am_hal_iom_pwrctrl_enable(AM_I2C4_IOM_INST);
  180. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_400KHZ;
  181. am_hal_iom_config(AM_I2C4_IOM_INST, &g_sIOMConfig);
  182. am_hal_iom_enable(AM_I2C4_IOM_INST);
  183. /* init i2c bus device */
  184. am_i2c = &am_i2c_bus_4;
  185. am_i2c->parent.ops = &am_i2c_ops;
  186. rt_i2c_bus_device_register(&am_i2c->parent, "i2c4");
  187. #endif
  188. rt_kprintf("i2c_init!\n");
  189. return 0;
  190. }
  191. #ifdef RT_USING_COMPONENTS_INIT
  192. INIT_BOARD_EXPORT(rt_i2c_init);
  193. #endif
  194. /*@}*/