hw_i2c.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * File :hw_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. * 2018-01-04 Sundm75 the first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include "ls1c_i2c.h"
  27. #include "../libraries/ls1c_pin.h"
  28. #ifdef RT_USING_I2C
  29. struct ls1c_i2c_bus
  30. {
  31. struct rt_i2c_bus_device parent;
  32. rt_uint32_t u32Module;
  33. };
  34. rt_size_t rt_i2c_master_xfer(struct rt_i2c_bus_device *bus,
  35. struct rt_i2c_msg *msgs,
  36. rt_uint32_t num)
  37. {
  38. struct ls1c_i2c_bus * i2c_bus = (struct ls1c_i2c_bus *)bus;
  39. ls1c_i2c_info_t i2c_info;
  40. struct rt_i2c_msg *msg;
  41. int i;
  42. rt_int32_t ret = RT_EOK;
  43. i2c_info.clock = 50000; // 50kb/s
  44. i2c_info.I2Cx = i2c_bus->u32Module;
  45. i2c_init(&i2c_info);
  46. for (i = 0; i < num; i++)
  47. {
  48. msg = &msgs[i];
  49. if (msg->flags == RT_I2C_RD)
  50. {
  51. i2c_send_start_and_addr(&i2c_info, msg->addr, LS1C_I2C_DIRECTION_READ);
  52. i2c_receive_ack(&i2c_info);
  53. i2c_receive_data(&i2c_info, (rt_uint8_t *)msg->buf, msg->len);
  54. i2c_send_stop(&i2c_info);
  55. }
  56. else if(msg->flags == RT_I2C_WR)
  57. {
  58. i2c_send_start_and_addr(&i2c_info, msg->addr, LS1C_I2C_DIRECTION_WRITE);
  59. i2c_receive_ack(&i2c_info);
  60. i2c_send_data(&i2c_info, (rt_uint8_t *)msg->buf, msg->len);
  61. i2c_send_stop(&i2c_info);
  62. }
  63. ret++;
  64. }
  65. return ret;
  66. }
  67. rt_err_t rt_i2c_bus_control(struct rt_i2c_bus_device *bus,
  68. rt_uint32_t cmd,
  69. rt_uint32_t arg)
  70. {
  71. struct ls1c_i2c_bus * i2c_bus = (struct ls1c_i2c_bus *)bus;
  72. RT_ASSERT(bus != RT_NULL);
  73. i2c_bus = (struct ls1c_i2c_bus *)bus->parent.user_data;
  74. RT_ASSERT(i2c_bus != RT_NULL);
  75. switch (cmd)
  76. {
  77. case RT_DEVICE_CTRL_CONFIG :
  78. break;
  79. }
  80. return RT_EOK;
  81. }
  82. static const struct rt_i2c_bus_device_ops ls1c_i2c_ops =
  83. {
  84. rt_i2c_master_xfer,
  85. RT_NULL,
  86. rt_i2c_bus_control
  87. };
  88. #ifdef RT_USING_I2C0
  89. static struct ls1c_i2c_bus ls1c_i2c_bus_0 =
  90. {
  91. {1},
  92. LS1C_I2C_0,
  93. };
  94. #endif
  95. #ifdef RT_USING_I2C1
  96. static struct ls1c_i2c_bus ls1c_i2c_bus_1 =
  97. {
  98. {1},
  99. LS1C_I2C_1,
  100. };
  101. #endif
  102. #ifdef RT_USING_I2C2
  103. static struct ls1c_i2c_bus ls1c_i2c_bus_2 =
  104. {
  105. {1},
  106. LS1C_I2C_2,
  107. };
  108. #endif
  109. int ls1c_hw_i2c_init(void)
  110. {
  111. struct ls1c_i2c_bus* ls1c_i2c;
  112. #ifdef RT_USING_I2C0
  113. /*
  114. pin_set_purpose(2, PIN_PURPOSE_OTHER);
  115. pin_set_purpose(3, PIN_PURPOSE_OTHER);
  116. pin_set_remap(2, PIN_REMAP_SECOND);
  117. pin_set_remap(3, PIN_REMAP_SECOND);
  118. */
  119. #endif
  120. #ifdef RT_USING_I2C1
  121. pin_set_purpose(2, PIN_PURPOSE_OTHER);
  122. pin_set_purpose(3, PIN_PURPOSE_OTHER);
  123. pin_set_remap(2, PIN_REMAP_SECOND);
  124. pin_set_remap(3, PIN_REMAP_SECOND);
  125. #endif
  126. #ifdef RT_USING_I2C2
  127. pin_set_purpose(51, PIN_PURPOSE_OTHER);
  128. pin_set_purpose(50, PIN_PURPOSE_OTHER);
  129. pin_set_remap(51, PIN_REMAP_FOURTH);
  130. pin_set_remap(50, PIN_REMAP_FOURTH);
  131. #endif
  132. #ifdef RT_USING_I2C0
  133. ls1c_i2c = &ls1c_i2c_bus_0;
  134. ls1c_i2c->parent.ops = &ls1c_i2c_ops;
  135. rt_i2c_bus_device_register(&ls1c_i2c->parent, "i2c0");
  136. rt_kprintf("i2c0_init!\n");
  137. #endif
  138. #ifdef RT_USING_I2C1
  139. ls1c_i2c = &ls1c_i2c_bus_1;
  140. ls1c_i2c->parent.ops = &ls1c_i2c_ops;
  141. rt_i2c_bus_device_register(&ls1c_i2c->parent, "i2c1");
  142. rt_kprintf("i2c1_init!\n");
  143. #endif
  144. #ifdef RT_USING_I2C2
  145. ls1c_i2c = &ls1c_i2c_bus_2;
  146. ls1c_i2c->parent.ops = &ls1c_i2c_ops;
  147. rt_i2c_bus_device_register(&ls1c_i2c->parent, "i2c2");
  148. rt_kprintf("i2c2_init!\n");
  149. #endif
  150. return RT_EOK;
  151. }
  152. INIT_BOARD_EXPORT(ls1c_hw_i2c_init);
  153. #endif