drv_i2c.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * File : drv_i2c.c
  3. * This file is part of gkipc BSP for RT-Thread distribution.
  4. *
  5. * Copyright (c) 2016 Shanghai goke Microelectronics Co., Ltd.
  6. * All rights reserved
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * Visit http://www.goke.com to get contact with Fullhan.
  23. *
  24. * Change Logs:
  25. * Date Author Notes
  26. */
  27. #include <rtdevice.h>
  28. #include <rthw.h>
  29. #include "drv_i2c.h"
  30. #include "gtypes.h"
  31. #include "platform.h"
  32. #include "gd_i2c.h"
  33. /*---------------------------------------------------------------------------*/
  34. /* local defines */
  35. /*---------------------------------------------------------------------------*/
  36. //#define GK_I2C_DEBUG
  37. #ifndef GK_I2C_DEBUG
  38. #define I2C_PRINT_DBG(fmt, args...)
  39. #define I2C_PRINT_ERR(fmt, args...) rt_kprintf(fmt, ##args);
  40. #else
  41. #define I2C_PRINT_DBG(fmt, args...) rt_kprintf(fmt, ##args);
  42. #define I2C_PRINT_ERR(fmt, args...) rt_kprintf(fmt, ##args);
  43. #endif
  44. //*****************************************************************************
  45. //*****************************************************************************
  46. //** Local structures
  47. //*****************************************************************************
  48. //*****************************************************************************
  49. //*****************************************************************************
  50. //*****************************************************************************
  51. //** Global Data
  52. //*****************************************************************************
  53. //*****************************************************************************
  54. //*****************************************************************************
  55. //*****************************************************************************
  56. //** Local Data
  57. //*****************************************************************************
  58. //*****************************************************************************
  59. //*****************************************************************************
  60. //*****************************************************************************
  61. //** Local Functions Declaration
  62. //*****************************************************************************
  63. //*****************************************************************************
  64. //*****************************************************************************
  65. //*****************************************************************************
  66. //** Local Functions
  67. //*****************************************************************************
  68. //*****************************************************************************
  69. static int gk_i2c_init(struct gk_i2c_obj *i2c_obj)
  70. {
  71. int ret = RT_EOK;
  72. ret = GD_I2C_Init((GD_I2C_INIT_PARAMS_S*)&i2c_obj->config.i2cInitParams);
  73. return ret;
  74. }
  75. static rt_size_t gk_i2c_xfer(struct rt_i2c_bus_device *dev,
  76. struct rt_i2c_msg msgs[], rt_uint32_t num)
  77. {
  78. int ret;
  79. U8 address = 0;
  80. U8* buffer = NULL;
  81. U8* regbuffer = NULL;
  82. U32 regbytes = 0;
  83. U32 bytes = 0;
  84. struct gk_i2c_obj *i2c_obj = (struct gk_i2c_obj *)dev->priv;
  85. if (num < 2)
  86. {
  87. if (msgs[0].flags == RT_I2C_WR)
  88. {
  89. address = msgs[0].addr;
  90. buffer = msgs[0].buf;
  91. bytes = msgs[0].len;
  92. ret = GD_I2C_Write( (GD_HANDLE *)&i2c_obj->handle,address,buffer,bytes );
  93. if (ret != RT_EOK)
  94. {
  95. I2C_PRINT_ERR("[%s:%d]I2C Write error!\n",__func__,__LINE__);
  96. return RT_ERROR;
  97. }
  98. }
  99. }
  100. else
  101. {
  102. if ((msgs[0].flags == RT_I2C_WR) && (msgs[1].flags == RT_I2C_RD))
  103. {
  104. address = msgs[0].addr;
  105. regbuffer = msgs[0].buf;
  106. regbytes = msgs[0].len;
  107. buffer = msgs[1].buf;
  108. bytes = msgs[1].len;
  109. }
  110. else if ((msgs[0].flags == RT_I2C_RD ) && (msgs[1].flags == RT_I2C_WR))
  111. {
  112. address = msgs[1].addr;
  113. regbuffer = msgs[1].buf;
  114. regbytes = msgs[1].len;
  115. buffer = msgs[0].buf;
  116. bytes = msgs[0].len;
  117. }
  118. ret = GD_I2C_Read( (GD_HANDLE *)&i2c_obj->handle,(U8)address, (U8*)regbuffer,(U32)regbytes,(U8*) buffer, (U32)bytes );
  119. if (ret != RT_EOK)
  120. {
  121. I2C_PRINT_ERR("[%s:%d]I2C_Read error!\n",__func__,__LINE__);
  122. return RT_ERROR;
  123. }
  124. }
  125. return ret;
  126. }
  127. static const struct rt_i2c_bus_device_ops gk_i2c_ops = {
  128. .master_xfer = gk_i2c_xfer,
  129. };
  130. int gk_i2c_probe(void *priv_data)
  131. {
  132. int ret;
  133. struct rt_i2c_bus_device *i2c_bus_dev;
  134. struct gk_i2c_obj *i2c_obj = (struct gk_i2c_obj *)priv_data;
  135. char i2c_dev_name[5] = {0};
  136. i2c_bus_dev = (struct rt_i2c_bus_device *)rt_malloc(sizeof(struct rt_i2c_bus_device));
  137. rt_memset(i2c_bus_dev, 0, sizeof(struct rt_i2c_bus_device));
  138. i2c_bus_dev->ops = &gk_i2c_ops;
  139. rt_sprintf(i2c_dev_name, "%s%d", "i2c", i2c_obj->id);
  140. ret = rt_i2c_bus_device_register(i2c_bus_dev, i2c_dev_name);
  141. if (ret != RT_EOK)
  142. {
  143. I2C_PRINT_ERR("ERROR:rt_spi_bus_register failed, ret=%d\n", ret);
  144. return -RT_ENOMEM;
  145. }
  146. // priv struct init
  147. i2c_obj->lock = rt_mutex_create("i2c_mux", RT_IPC_FLAG_FIFO);
  148. gk_i2c_init(i2c_obj);
  149. return ret;
  150. }
  151. int gk_i2c_exit(void *priv_data)
  152. {
  153. struct gk_i2c_obj *i2c_obj = (struct gk_i2c_obj *)priv_data;
  154. GD_I2C_Close((GD_HANDLE *)&i2c_obj->handle);
  155. GD_I2C_Exit();
  156. return 0;
  157. }
  158. struct gk_platform_driver i2c_driver_ops = {
  159. .name = "i2c", .probe = gk_i2c_probe, .remove = gk_i2c_exit,
  160. };
  161. void rt_hw_i2c_init(void)
  162. {
  163. I2C_PRINT_DBG("%s start\n", __func__);
  164. gk_platform_driver_init(&i2c_driver_ops);
  165. I2C_PRINT_DBG("%s end\n", __func__);
  166. // fixme: never release?
  167. }
  168. //#define GK_TEST_I2C
  169. #ifdef GK_TEST_I2C
  170. static rt_err_t gk_i2c_test_read_reg(struct rt_i2c_bus_device *gk_i2c,
  171. rt_uint16_t reg, rt_uint8_t *data)
  172. {
  173. struct rt_i2c_msg msg[2];
  174. rt_uint8_t send_buf[2];
  175. rt_uint8_t recv_buf[1] = {0};
  176. I2C_PRINT_DBG("%s start\n", __func__);
  177. send_buf[0] = (reg & 0xFF);
  178. msg[0].addr = 0x51;
  179. msg[0].flags = RT_I2C_WR;
  180. msg[0].len = 1;
  181. msg[0].buf = send_buf;
  182. msg[1].addr = 0x51;
  183. msg[1].flags = RT_I2C_RD;
  184. msg[1].len = 1;
  185. msg[1].buf = recv_buf;
  186. rt_i2c_transfer(gk_i2c, msg, 2);
  187. *data = recv_buf[0];
  188. return RT_EOK;
  189. }
  190. static rt_err_t gk_i2c_test_write_reg(struct rt_i2c_bus_device *gk_i2c,
  191. rt_uint16_t reg, rt_uint8_t data)
  192. {
  193. struct rt_i2c_msg msg;
  194. rt_uint8_t send_buf[3];
  195. I2C_PRINT_DBG("%s start\n", __func__);
  196. // send_buf[0] = ((reg >> 8) & 0xff);
  197. send_buf[1] = (reg & 0xFF);
  198. send_buf[2] = data;
  199. msg.addr = 0x51;
  200. msg.flags = RT_I2C_WR;
  201. msg.len = 2;
  202. msg.buf = send_buf;
  203. rt_i2c_transfer(gk_i2c, &msg, 1);
  204. I2C_PRINT_DBG("%s end\n", __func__);
  205. return RT_EOK;
  206. }
  207. void i2c_test_sensor()
  208. {
  209. struct rt_i2c_bus_device *gk_i2c;
  210. gk_i2c_obj_s i2c_obj;
  211. rt_uint8_t data[1] = {0x00};
  212. int ret = 0;
  213. gk_i2c = rt_i2c_bus_device_find("i2c1");
  214. GD_I2C_OPEN_PARAMS_S i2c_param;
  215. i2c_param.channel = GADI_I2C_CHANNEL_ONE;
  216. i2c_param.speed = GADI_I2C_100KBPS;
  217. i2c_param.mode = GADI_I2C_INTERRUPT;//GADI_I2C_NORMAL;
  218. i2c_obj.handle = 0;
  219. ret = GD_I2C_Open((GD_I2C_OPEN_PARAMS_S*)&i2c_param, &i2c_obj.handle);
  220. if (ret != 0)
  221. {
  222. I2C_PRINT_DBG("GD_I2C_Open error!\n");
  223. return -1;
  224. }
  225. gk_i2c.priv = &i2c_obj;
  226. gk_i2c_test_write_reg(gk_i2c, 0x04, 0x02);
  227. gk_i2c_test_read_reg(gk_i2c, 0x02, data);
  228. I2C_PRINT_DBG("data read from 0x3038 is 0x%x\r\n", data[0]);
  229. I2C_PRINT_DBG("%s end\n", __func__);
  230. }
  231. #ifdef RT_USING_FINSH
  232. #include <finsh.h>
  233. FINSH_FUNCTION_EXPORT(i2c_test_sensor, sensor i2c test);
  234. #endif
  235. #endif /*GK_TEST_I2C end*/