wm_i2c.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**************************************************************************//**
  2. * @file wm_i2c.c
  3. * @author
  4. * @version
  5. * @date
  6. * @brief
  7. *
  8. * Copyright (c) 2014 Winner Microelectronics Co., Ltd. All rights reserved.
  9. *****************************************************************************/
  10. #include "wm_include.h"
  11. #include "wm_i2c.h"
  12. #define I2C_FREQ_MAX (400000)
  13. #define I2C_FREQ_MIN (100000)
  14. #define I2C_WRITE (0x80)
  15. #define I2C_READ (0x00)
  16. typedef struct {
  17. uint8_t addr;
  18. uint8_t dev_addr;
  19. uint8_t state;
  20. uint8_t *buf;
  21. uint16_t len;
  22. uint16_t cnt;
  23. uint8_t cmd;
  24. void (*transfer_done)(void);
  25. } i2c_desc;
  26. enum {
  27. START,
  28. RESTART,
  29. TRANSMIT,
  30. PRERECEIVE,
  31. RECEIVE,
  32. STOP,
  33. DONE,
  34. IDLE,
  35. };
  36. static i2c_desc i2c_transfer;
  37. ATTRIBUTE_ISR void i2c_I2C_IRQHandler(void)
  38. {
  39. int i2c_sr;
  40. csi_kernel_intrpt_enter();
  41. i2c_sr = I2C->CR_SR;
  42. I2C->CR_SR = 1;
  43. if (i2c_sr & 0x20)
  44. {
  45. printf("I2C AL lost\r\n");
  46. }
  47. if (i2c_sr & 0x01)
  48. {
  49. if ((i2c_sr & 0x80) == 0)
  50. {
  51. switch(i2c_transfer.state)
  52. {
  53. case START:
  54. I2C->TX_RX = i2c_transfer.addr;
  55. I2C->CR_SR = I2C_CR_WR;
  56. if ((i2c_transfer.cmd & I2C_WRITE) == I2C_WRITE)
  57. {
  58. i2c_transfer.state = TRANSMIT;
  59. }
  60. else
  61. {
  62. i2c_transfer.state = RESTART;
  63. }
  64. break;
  65. case RESTART:
  66. I2C->TX_RX = (i2c_transfer.dev_addr | 0x01);
  67. I2C->CR_SR = (I2C_CR_STA | I2C_CR_WR);
  68. i2c_transfer.state = PRERECEIVE;
  69. break;
  70. case TRANSMIT:
  71. I2C->TX_RX = i2c_transfer.buf[i2c_transfer.cnt++];
  72. I2C->CR_SR = I2C_CR_WR;
  73. if (i2c_transfer.cnt == i2c_transfer.len)
  74. {
  75. i2c_transfer.state = STOP;
  76. }
  77. break;
  78. case PRERECEIVE:
  79. i2c_transfer.state = RECEIVE;
  80. I2C->CR_SR = I2C_CR_RD;
  81. break;
  82. case RECEIVE:
  83. i2c_transfer.buf[i2c_transfer.cnt++] = I2C->TX_RX;
  84. if (i2c_transfer.cnt == (i2c_transfer.len - 1))
  85. {
  86. I2C->CR_SR = (I2C_CR_STO | I2C_CR_NAK | I2C_CR_RD);
  87. i2c_transfer.state = STOP;
  88. }
  89. else if (i2c_transfer.len == 1)
  90. {
  91. I2C->CR_SR = (I2C_CR_STO | I2C_CR_NAK | I2C_CR_RD);
  92. i2c_transfer.state = DONE;
  93. if (i2c_transfer.transfer_done)
  94. {
  95. i2c_transfer.transfer_done();
  96. }
  97. }
  98. else
  99. {
  100. I2C->CR_SR = I2C_CR_RD;
  101. }
  102. break;
  103. case STOP:
  104. I2C->CR_SR = I2C_CR_STO;
  105. i2c_transfer.state = DONE;
  106. if (i2c_transfer.transfer_done)
  107. {
  108. i2c_transfer.transfer_done();
  109. }
  110. break;
  111. }
  112. }
  113. else
  114. {
  115. if ((i2c_transfer.state == STOP) && i2c_transfer.cmd != I2C_WRITE)
  116. {
  117. i2c_transfer.buf[i2c_transfer.cnt] = I2C->TX_RX;
  118. i2c_transfer.state = DONE;
  119. if (i2c_transfer.transfer_done)
  120. {
  121. i2c_transfer.transfer_done();
  122. }
  123. }
  124. }
  125. }
  126. // if ((i2c_sr & 0x40) == 0)
  127. // {
  128. // i2c_transfer.state = IDLE;
  129. // }
  130. csi_kernel_intrpt_exit();
  131. }
  132. void tls_i2c_init(u32 freq)
  133. {
  134. u32 div = 0;
  135. tls_sys_clk clk;
  136. if (freq <= I2C_FREQ_MIN)
  137. {
  138. freq = I2C_FREQ_MIN;
  139. }
  140. else if (freq >= I2C_FREQ_MAX)
  141. {
  142. freq = I2C_FREQ_MAX;
  143. }
  144. tls_sys_clk_get(&clk);
  145. div = (clk.apbclk * 1000000)/(5 * freq) - 1;
  146. tls_reg_write32(HR_I2C_PRER_LO, div & 0xff);
  147. tls_reg_write32(HR_I2C_PRER_HI, (div>>8) & 0xff);
  148. /** enable I2C | Disable Int*/
  149. tls_reg_write32(HR_I2C_CTRL, I2C_CTRL_INT_DISABLE | I2C_CTRL_ENABLE);
  150. tls_irq_enable(I2C_IRQn);
  151. }
  152. /**
  153. * @brief send stop signal
  154. *
  155. */
  156. void tls_i2c_stop(void)
  157. {
  158. tls_reg_write32(HR_I2C_CR_SR, I2C_CR_STO);
  159. while(tls_reg_read32(HR_I2C_CR_SR) & I2C_SR_TIP);
  160. }
  161. /**
  162. * @brief waiting for ack signal
  163. * @retval
  164. * - \ref WM_FAILED
  165. * - \ref WM_SUCCESS
  166. */
  167. int tls_i2c_wait_ack(void)
  168. {
  169. u16 errtime=0;
  170. u32 value;
  171. while(tls_reg_read32(HR_I2C_CR_SR) & I2C_SR_TIP);
  172. value = tls_reg_read32(HR_I2C_CR_SR);
  173. while(value & I2C_SR_NAK)
  174. {
  175. errtime ++;
  176. if(errtime > 512)
  177. {
  178. //printf("wait ack err\n");
  179. tls_i2c_stop();
  180. return WM_FAILED;
  181. }
  182. value = tls_reg_read32(HR_I2C_CR_SR);
  183. }
  184. return WM_SUCCESS;
  185. }
  186. /**
  187. * @brief writes the data to data register of I2C module
  188. * when \ifstart one the start signal will be sent followed by the \data
  189. * when \ifstart zero only the \data will be send
  190. * @param[in] data the data will be write to the data register of I2C module
  191. * @param[in] ifstart when one send start signal, when zero don't
  192. * @retval
  193. *
  194. */
  195. void tls_i2c_write_byte(u8 data,u8 ifstart)
  196. {
  197. tls_reg_write32(HR_I2C_TX_RX, data);
  198. if(ifstart)
  199. tls_reg_write32(HR_I2C_CR_SR, I2C_CR_STA | I2C_CR_WR);
  200. else
  201. tls_reg_write32(HR_I2C_CR_SR, I2C_CR_WR);
  202. while(tls_reg_read32(HR_I2C_CR_SR) & I2C_SR_TIP);
  203. }
  204. /**
  205. * @brief get the data stored in data register of I2C module
  206. * @param[in] ifack when one send ack after reading the data register,when zero don't
  207. * @param[in] ifstop when one send stop signal after read, when zero do not send stop
  208. * @retval the received data
  209. */
  210. u8 tls_i2c_read_byte(u8 ifack,u8 ifstop)
  211. {
  212. u8 data;
  213. u32 value = I2C_CR_RD;
  214. if(!ifack)
  215. value |= I2C_CR_NAK;
  216. if(ifstop)
  217. value |= I2C_CR_STO;
  218. tls_reg_write32(HR_I2C_CR_SR, value);
  219. /** Waiting finish */
  220. while(tls_reg_read32(HR_I2C_CR_SR) & I2C_SR_TIP);
  221. data = tls_reg_read32(HR_I2C_TX_RX);
  222. return data;
  223. }
  224. /**
  225. * @brief start write through int mode
  226. * @param[in] devaddr the device address
  227. * @param[in] wordaddr when one send stop signal after read, when zero do not send stop
  228. * @param[in] buf the address point where data shoule be stored
  229. * @param[in] len the length of data will be received
  230. * @retval
  231. * - \ref WM_FAILED
  232. * - \ref WM_SUCCESS
  233. */
  234. int wm_i2c_start_write_it(uint8_t devaddr, uint8_t wordaddr, uint8_t * buf, uint16_t len)
  235. {
  236. if (buf == NULL)
  237. {
  238. return WM_FAILED;
  239. }
  240. I2C->TX_RX = devaddr;
  241. i2c_transfer.dev_addr = devaddr;
  242. i2c_transfer.state = START;
  243. i2c_transfer.cmd = I2C_WRITE;
  244. i2c_transfer.buf = buf;
  245. i2c_transfer.len = len;
  246. i2c_transfer.cnt = 0;
  247. i2c_transfer.addr = wordaddr;
  248. I2C->CR_SR = I2C_CR_STA | I2C_CR_WR;
  249. return WM_SUCCESS;
  250. }
  251. /**
  252. * @brief start read through int mode
  253. * @param[in] devaddr the device address
  254. * @param[in] wordaddr when one send stop signal after read, when zero do not send stop
  255. * @param[in] buf the address point where data shoule be stored
  256. * @param[in] len the length of data will be received
  257. * @retval
  258. * - \ref WM_FAILED
  259. * - \ref WM_SUCCESS
  260. */
  261. int wm_i2c_start_read_it(uint8_t devaddr, uint8_t wordaddr, uint8_t * buf, uint16_t len)
  262. {
  263. if (buf == NULL)
  264. {
  265. return WM_FAILED;
  266. }
  267. I2C->TX_RX = devaddr;
  268. i2c_transfer.dev_addr = devaddr;
  269. i2c_transfer.state = START;
  270. i2c_transfer.cmd = I2C_READ;
  271. i2c_transfer.buf = buf;
  272. i2c_transfer.len = len;
  273. i2c_transfer.cnt = 0;
  274. i2c_transfer.addr = wordaddr;
  275. I2C->CR_SR = I2C_CR_STA | I2C_CR_WR;
  276. return WM_SUCCESS;
  277. }
  278. /**
  279. * @brief This function is used to register i2c transfer done callback function.
  280. * @param[in] done is the i2c transfer done callback function.
  281. * @retval None
  282. * @note None
  283. */
  284. void wm_i2c_transfer_done_register(void (*done)(void))
  285. {
  286. i2c_transfer.transfer_done = done;
  287. }
  288. /*** (C) COPYRIGHT 2014 Winner Microelectronics Co., Ltd. ***/