ls1c_i2c.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * File : ls1c_i2c.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, 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-09-06 勤为本 first version
  23. */
  24. // 封装硬件i2c接口
  25. #include "ls1c_public.h"
  26. #include "ls1c_regs.h"
  27. #include "ls1c_clock.h"
  28. #include "ls1c_i2c.h"
  29. #include "ls1c_delay.h"
  30. /*
  31. * I2C各个寄存器相对基地址的偏移
  32. * 发送数据寄存器和接收数据寄存器的偏移是相同的
  33. * 命令寄存器和状态寄存器的偏移是相同的,不同的是命令寄存器只写,状态寄存器只读
  34. */
  35. #define LS1C_I2C_PRER_LOW_OFFSET (0) // 分频锁存器低字节寄存器偏移
  36. #define LS1C_I2C_PRER_HIGH_OFFSET (1) // 分频锁存器高字节寄存器偏移
  37. #define LS1C_I2C_CONTROL_OFFSET (2) // 控制寄存器偏移
  38. #define LS1C_I2C_DATA_OFFSET (3) // 发送数据寄存器和接收数据寄存器的偏移是相同的
  39. #define LS1C_I2C_CMD_OFFSET (4) // 命令寄存器偏移,只写
  40. #define LS1C_I2C_STATUS_OFFSET (4) // 状态寄存器偏移,只读
  41. // 控制寄存器的位域
  42. #define LS1C_I2C_CONTROL_EN (0x80) // i2c模块使能
  43. #define LS1C_I2C_CONTROL_IEN (0x40) // 中断使能
  44. // 命令寄存器的位域
  45. #define LS1C_I2C_CMD_START (0x90) // 产生START信号
  46. #define LS1C_I2C_CMD_STOP (0x40) // 产生STOP信号
  47. #define LS1C_I2C_CMD_READ (0x20) // 产生读信号,即产生ACK信号
  48. #define LS1C_I2C_CMD_WRITE (0x10) // 产生写信号
  49. #define LS1C_I2C_CMD_READ_ACK (0x20) // 产生ACK信号,与读信号相同
  50. #define LS1C_I2C_CMD_READ_NACK (0x28) // 产生NACK信号
  51. #define LS1C_I2C_CMD_IACK (0x00) // 产生中断应答信号
  52. // 状态寄存器的位域
  53. #define LS1C_I2C_STATUS_IF (0x01) // 中断标志位
  54. #define LS1C_I2C_STATUS_TIP (0x02) // 指示传输的过程。1,正在传输;0,传输完成
  55. #define LS1C_I2C_STATUS_ARBLOST (0x20) // I2C核失去I2C总线的控制权
  56. #define LS1C_I2C_STATUS_BUSY (0x40) // I2C总线忙标志
  57. #define LS1C_I2C_STATUS_NACK (0x80) // 应答位标志。1,没收到应答位;0,收到应答位
  58. /*
  59. * 获取指定i2c模块的基地址
  60. * @I2Cx I2C模块的编号
  61. */
  62. void *i2c_get_base(ls1c_i2c_t I2Cx)
  63. {
  64. void *base = NULL;
  65. switch (I2Cx)
  66. {
  67. case LS1C_I2C_0:
  68. base = (void *)LS1C_I2C0_BASE;
  69. break;
  70. case LS1C_I2C_1:
  71. base = (void *)LS1C_I2C1_BASE;
  72. break;
  73. case LS1C_I2C_2:
  74. base = (void *)LS1C_I2C2_BASE;
  75. break;
  76. default:
  77. base = NULL;
  78. break;
  79. }
  80. return base;
  81. }
  82. /*
  83. * 向命令寄存器写命令
  84. * @i2c_info_p i2c模块信息
  85. * @cmd 命令
  86. */
  87. void i2c_cmd(ls1c_i2c_info_t *i2c_info_p, unsigned char cmd)
  88. {
  89. void *i2c_base = i2c_get_base(i2c_info_p->I2Cx);
  90. reg_write_8(cmd, i2c_base + LS1C_I2C_CMD_OFFSET);
  91. return ;
  92. }
  93. /*
  94. * 执行START命令,发送START信号
  95. * @i2c_info_p i2c模块信息
  96. */
  97. void i2c_cmd_start(ls1c_i2c_info_t *i2c_info_p)
  98. {
  99. i2c_cmd(i2c_info_p, LS1C_I2C_CMD_START);
  100. return ;
  101. }
  102. /*
  103. * 执行STOP命令,发送STOP信号
  104. * @i2c_info_p i2c模块信息
  105. */
  106. void i2c_cmd_stop(ls1c_i2c_info_t *i2c_info_p)
  107. {
  108. i2c_cmd(i2c_info_p, LS1C_I2C_CMD_STOP);
  109. return ;
  110. }
  111. /*
  112. * 执行写命令
  113. * @i2c_info_p i2c模块信息
  114. */
  115. void i2c_cmd_write(ls1c_i2c_info_t *i2c_info_p)
  116. {
  117. i2c_cmd(i2c_info_p, LS1C_I2C_CMD_WRITE);
  118. return ;
  119. }
  120. /*
  121. * 执行读ack命令,发送读ack信号
  122. * @i2c_info_p i2c模块信息
  123. */
  124. void i2c_cmd_read_ack(ls1c_i2c_info_t *i2c_info_p)
  125. {
  126. i2c_cmd(i2c_info_p, LS1C_I2C_CMD_READ_ACK);
  127. return ;
  128. }
  129. /*
  130. * 执行读nack命令,发送读nack信号
  131. * @i2c_info_p i2c模块信息
  132. */
  133. void i2c_cmd_read_nack(ls1c_i2c_info_t *i2c_info_p)
  134. {
  135. i2c_cmd(i2c_info_p, LS1C_I2C_CMD_READ_NACK);
  136. return ;
  137. }
  138. /*
  139. * 发送中断应答信号
  140. * @i2c_info_p i2c模块信息
  141. */
  142. void i2c_cmd_iack(ls1c_i2c_info_t *i2c_info_p)
  143. {
  144. i2c_cmd(i2c_info_p, LS1C_I2C_CMD_IACK);
  145. return ;
  146. }
  147. /*
  148. * 获取状态寄存器的值
  149. * @i2c_info_p i2c模块信息
  150. * @ret 状态寄存器的值
  151. */
  152. unsigned char i2c_get_status(ls1c_i2c_info_t *i2c_info_p)
  153. {
  154. void *i2c_base = i2c_get_base(i2c_info_p->I2Cx);
  155. return reg_read_8(i2c_base + LS1C_I2C_STATUS_OFFSET);
  156. }
  157. /*
  158. * Poll the i2c status register until the specified bit is set.
  159. * Returns 0 if timed out (100 msec).
  160. * @i2c_info_p i2c模块信息
  161. * @bit 寄存器的某一位
  162. * @ret true or false
  163. */
  164. int i2c_poll_status(ls1c_i2c_info_t *i2c_info_p, unsigned long bit)
  165. {
  166. int loop_cntr = 20000;
  167. do {
  168. delay_us(1);
  169. } while ((i2c_get_status(i2c_info_p) & bit) && (0 < --loop_cntr));
  170. return (0 < loop_cntr);
  171. }
  172. /*
  173. * 初始化指定i2c模块
  174. * @i2c_info_p 某个i2c模块的信息
  175. */
  176. void i2c_init(ls1c_i2c_info_t *i2c_info_p)
  177. {
  178. void *i2c_base = i2c_get_base(i2c_info_p->I2Cx);
  179. unsigned long i2c_clock = i2c_info_p->clock;
  180. unsigned char ctrl = reg_read_8(i2c_base + LS1C_I2C_CONTROL_OFFSET);
  181. unsigned long prescale = 0;
  182. /* make sure the device is disabled */
  183. ctrl = ctrl & ~(LS1C_I2C_CONTROL_EN | LS1C_I2C_CONTROL_IEN);
  184. reg_write_8(ctrl, i2c_base + LS1C_I2C_CONTROL_OFFSET);
  185. // 设置时钟
  186. i2c_clock = MIN(i2c_clock, LS1C_I2C_CLOCK_MAX); // 限制在最大允许范围内
  187. prescale = clk_get_apb_rate();
  188. prescale = (prescale / (5 * i2c_clock)) - 1;
  189. reg_write_8(prescale & 0xff, i2c_base + LS1C_I2C_PRER_LOW_OFFSET);
  190. reg_write_8(prescale >> 8, i2c_base + LS1C_I2C_PRER_HIGH_OFFSET);
  191. // 使能
  192. i2c_cmd_iack(i2c_info_p);
  193. ctrl = ctrl | LS1C_I2C_CONTROL_EN;
  194. reg_write_8(ctrl, i2c_base + LS1C_I2C_CONTROL_OFFSET);
  195. return ;
  196. }
  197. /*
  198. * (再发送一个字节数据之后)接收从机发送的ACK信号
  199. * @i2c_info_p i2c模块信息
  200. * @ret LS1C_I2C_ACK or LS1C_I2C_NACK
  201. */
  202. ls1c_i2c_ack_t i2c_receive_ack(ls1c_i2c_info_t *i2c_info_p)
  203. {
  204. ls1c_i2c_ack_t ret = LS1C_I2C_NACK;
  205. if (LS1C_I2C_STATUS_NACK & i2c_get_status(i2c_info_p))
  206. {
  207. ret = LS1C_I2C_NACK;
  208. }
  209. else
  210. {
  211. ret = LS1C_I2C_ACK;
  212. }
  213. return ret;
  214. }
  215. /*
  216. * 接收数据
  217. * @i2c_info_p i2c模块信息
  218. * @buf 数据缓存
  219. * @len 待接收数据的长度
  220. */
  221. ls1c_i2c_ret_t i2c_receive_data(ls1c_i2c_info_t *i2c_info_p, unsigned char *buf, int len)
  222. {
  223. void *i2c_base = i2c_get_base(i2c_info_p->I2Cx);
  224. int i = 0;
  225. for (i=0; i<len; i++)
  226. {
  227. // 开始接收
  228. if (i != (len - 1))
  229. i2c_cmd_read_ack(i2c_info_p);
  230. else
  231. i2c_cmd_read_nack(i2c_info_p);
  232. // 等待,直到接收完成
  233. if (!i2c_poll_status(i2c_info_p, LS1C_I2C_STATUS_TIP))
  234. return LS1C_I2C_RET_TIMEOUT;
  235. // 读取数据,并保存
  236. *buf++ = reg_read_8(i2c_base + LS1C_I2C_DATA_OFFSET);
  237. }
  238. return LS1C_I2C_RET_OK;
  239. }
  240. /*
  241. * 发送START信号和地址
  242. * @i2c_info_p i2c模块信息
  243. * @slave_addr 从机地址
  244. * @direction 数据传输方向(读、写)
  245. */
  246. ls1c_i2c_ret_t i2c_send_start_and_addr(ls1c_i2c_info_t *i2c_info_p,
  247. unsigned char slave_addr,
  248. ls1c_i2c_direction_t direction)
  249. {
  250. void *i2c_base = i2c_get_base(i2c_info_p->I2Cx);
  251. unsigned char data = 0;
  252. // 等待i2c总线空闲
  253. if (!i2c_poll_status(i2c_info_p, LS1C_I2C_STATUS_BUSY))
  254. return LS1C_I2C_RET_TIMEOUT;
  255. // 填充地址到数据寄存器
  256. data = (slave_addr << 1) | ((LS1C_I2C_DIRECTION_READ == direction) ? 1 : 0);
  257. reg_write_8(data , i2c_base + LS1C_I2C_DATA_OFFSET);
  258. // 开始发送
  259. i2c_cmd_start(i2c_info_p);
  260. // 等待,直到发送完成
  261. if (!i2c_poll_status(i2c_info_p, LS1C_I2C_STATUS_TIP))
  262. return LS1C_I2C_RET_TIMEOUT;
  263. return LS1C_I2C_RET_OK;
  264. }
  265. /*
  266. * 发送数据
  267. * @i2c_info_p i2c模块信息
  268. * @data 待发送的数据
  269. * @len 待发送数据的长度
  270. */
  271. ls1c_i2c_ret_t i2c_send_data(ls1c_i2c_info_t *i2c_info_p, unsigned char *data, int len)
  272. {
  273. void *i2c_base = i2c_get_base(i2c_info_p->I2Cx);
  274. int i = 0;
  275. for (i=0; i<len; i++)
  276. {
  277. // 将一个字节数据写入数据寄存器
  278. reg_write_8(*data++, i2c_base + LS1C_I2C_DATA_OFFSET);
  279. // 开始发送
  280. reg_write_8(LS1C_I2C_CMD_WRITE, i2c_base + LS1C_I2C_CMD_OFFSET);
  281. // 等待,直到发送完成
  282. if (!i2c_poll_status(i2c_info_p, LS1C_I2C_STATUS_TIP))
  283. return LS1C_I2C_RET_TIMEOUT;
  284. // 读取应答信号
  285. if (LS1C_I2C_ACK != i2c_receive_ack(i2c_info_p))
  286. return len;
  287. }
  288. return LS1C_I2C_RET_OK;
  289. }
  290. /*
  291. * 发送STOP信号
  292. * @i2c_info_p i2c模块信息
  293. */
  294. void i2c_send_stop(ls1c_i2c_info_t *i2c_info_p)
  295. {
  296. i2c_cmd_stop(i2c_info_p);
  297. return ;
  298. }