i2c_001.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. * @brief I2C driver functions
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2012
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licensor disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. */
  31. #include "i2c_001.h"
  32. /*****************************************************************************
  33. * Private types/enumerations/variables
  34. ****************************************************************************/
  35. /* I2C device configuration structure type */
  36. typedef struct {
  37. union {
  38. I2C_M_SETUP_Type txrx_setup_master; /* Transmission setup */
  39. I2C_S_SETUP_Type txrx_setup_slave; /* Transmission setup */
  40. };
  41. int32_t dir; /* Current direction phase, 0 - write, 1 - read */
  42. } I2C_CFG_T;
  43. #define BLOCKING_TIMEOUT (0x000FFFFFUL)
  44. #define RESTRANSMISSION_MAX (0x000000FFUL)
  45. /* I2C driver data for I2C0, I2C1 */
  46. static I2C_CFG_T i2cdat[3];
  47. static bool I2C_MasterComplete[3];
  48. static bool I2C_SlaveComplete[3];
  49. /*****************************************************************************
  50. * Public types/enumerations/variables
  51. ****************************************************************************/
  52. /*****************************************************************************
  53. * Private functions
  54. ****************************************************************************/
  55. /* Generate a start condition on I2C bus (in master mode only) */
  56. static uint32_t IP_I2C_Start(IP_I2C_001_Type *LPC_I2C, I2C_TRANSFER_OPT_Type Opt)
  57. {
  58. uint32_t cnt = 0;
  59. /* Reset STA, STO, SI */
  60. LPC_I2C->CONCLR = I2C_I2CONCLR_SIC | I2C_I2CONCLR_STOC | I2C_I2CONCLR_STAC;
  61. /* Enter to Master Transmitter mode */
  62. LPC_I2C->CONSET = I2C_I2CONSET_STA;
  63. if (Opt == I2C_TRANSFER_POLLING) {
  64. /* Wait for complete */
  65. while (!(LPC_I2C->CONSET & I2C_I2CONSET_SI)) {
  66. if (++cnt > BLOCKING_TIMEOUT) {
  67. return I2C_STAT_CODE_ERROR;
  68. }
  69. }
  70. }
  71. return LPC_I2C->STAT & I2C_STAT_CODE_BITMASK;
  72. }
  73. /* Generate a stop condition on I2C bus (in master mode only) */
  74. static Status IP_I2C_Stop(IP_I2C_001_Type *LPC_I2C, I2C_TRANSFER_OPT_Type Opt)
  75. {
  76. uint32_t cnt = 0;
  77. /* Make sure start bit is not active */
  78. if (LPC_I2C->CONSET & I2C_I2CONSET_STA) {
  79. LPC_I2C->CONCLR = I2C_I2CONCLR_STAC;
  80. }
  81. LPC_I2C->CONSET = I2C_I2CONSET_STO;
  82. LPC_I2C->CONCLR = I2C_I2CONCLR_SIC;
  83. if (Opt == I2C_TRANSFER_POLLING) {
  84. /* wait for stop is sent */
  85. while (LPC_I2C->CONSET & I2C_I2CONSET_STO) {
  86. if (LPC_I2C->CONSET & I2C_I2CONSET_SI) {
  87. LPC_I2C->CONCLR = I2C_I2CONCLR_SIC;
  88. }
  89. if (++cnt > BLOCKING_TIMEOUT) {
  90. return ERROR;
  91. }
  92. }
  93. }
  94. return SUCCESS;
  95. }
  96. /* I2C send byte subroutine */
  97. static uint32_t IP_I2C_SendByte(IP_I2C_001_Type *LPC_I2C, uint8_t databyte)
  98. {
  99. uint32_t CodeStatus = LPC_I2C->STAT & I2C_STAT_CODE_BITMASK;
  100. if ((CodeStatus != I2C_I2STAT_M_TX_START) &&
  101. (CodeStatus != I2C_I2STAT_M_TX_RESTART) &&
  102. (CodeStatus != I2C_I2STAT_M_TX_SLAW_ACK) &&
  103. (CodeStatus != I2C_I2STAT_M_TX_DAT_ACK) ) {
  104. return CodeStatus;
  105. }
  106. LPC_I2C->DAT = databyte & I2C_I2DAT_BITMASK;
  107. LPC_I2C->CONSET = I2C_I2CONSET_AA;
  108. LPC_I2C->CONCLR = I2C_I2CONCLR_SIC;
  109. return LPC_I2C->STAT & I2C_STAT_CODE_BITMASK;
  110. }
  111. /* I2C get byte subroutine */
  112. static uint32_t IP_I2C_GetByte(IP_I2C_001_Type *LPC_I2C, uint8_t *retdat, bool ack)
  113. {
  114. *retdat = (uint8_t) (LPC_I2C->DAT & I2C_I2DAT_BITMASK);
  115. if (ack == true) {
  116. LPC_I2C->CONSET = I2C_I2CONSET_AA;
  117. }
  118. else {
  119. LPC_I2C->CONCLR = I2C_I2CONCLR_AAC;
  120. }
  121. LPC_I2C->CONCLR = I2C_I2CONCLR_SIC;
  122. return LPC_I2C->STAT & I2C_STAT_CODE_BITMASK;
  123. }
  124. /* Handle I2C Master states */
  125. static int32_t IP_I2C_MasterHanleStates(IP_I2C_001_Type *LPC_I2C,
  126. uint32_t CodeStatus,
  127. I2C_M_SETUP_Type *TransferCfg,
  128. I2C_TRANSFER_OPT_Type Opt)
  129. {
  130. uint8_t *txdat;
  131. uint8_t *rxdat;
  132. uint8_t tmp;
  133. int32_t Ret = I2C_OK;
  134. /* get buffer to send/receive */
  135. txdat = (uint8_t *) &TransferCfg->tx_data[TransferCfg->tx_count];
  136. rxdat = (uint8_t *) &TransferCfg->rx_data[TransferCfg->rx_count];
  137. switch (CodeStatus) {
  138. case I2C_I2STAT_M_TX_START:
  139. case I2C_I2STAT_M_TX_RESTART:
  140. // case I2C_I2STAT_M_RX_START:
  141. // case I2C_I2STAT_M_RX_RESTART
  142. /* Send data first */
  143. if (TransferCfg->tx_count < TransferCfg->tx_length) {
  144. /* Send slave address + WR direction bit = 0 ----------------------------------- */
  145. IP_I2C_SendByte(LPC_I2C, (TransferCfg->sl_addr7bit << 1));
  146. Ret = I2C_BYTE_SENT;
  147. }
  148. else if (TransferCfg->rx_count < TransferCfg->rx_length) {
  149. /* Send slave address + RD direction bit = 1 ----------------------------------- */
  150. IP_I2C_SendByte(LPC_I2C, ((TransferCfg->sl_addr7bit << 1) | 0x01));
  151. Ret = I2C_BYTE_SENT;
  152. }
  153. /* Clear STA bit after the slave address is sent */
  154. LPC_I2C->CONCLR = I2C_I2CONCLR_STAC;
  155. break;
  156. case I2C_I2STAT_M_TX_SLAW_ACK:
  157. case I2C_I2STAT_M_TX_DAT_ACK:
  158. if (TransferCfg->tx_count < TransferCfg->tx_length) {
  159. IP_I2C_SendByte(LPC_I2C, *txdat);
  160. txdat++;
  161. TransferCfg->tx_count++;
  162. Ret = I2C_BYTE_SENT;
  163. }
  164. else {
  165. if (TransferCfg->rx_count >= TransferCfg->rx_length) {
  166. IP_I2C_Stop(LPC_I2C, Opt);
  167. }
  168. Ret = I2C_SEND_END;
  169. }
  170. break;
  171. case I2C_I2STAT_M_TX_DAT_NACK:
  172. if (TransferCfg->rx_count >= TransferCfg->rx_length) {
  173. IP_I2C_Stop(LPC_I2C, Opt);
  174. }
  175. Ret = I2C_SEND_END;
  176. break;
  177. case I2C_I2STAT_M_RX_ARB_LOST:
  178. case I2C_I2STAT_S_RX_ARB_LOST_M_GENCALL:
  179. case I2C_I2STAT_S_TX_ARB_LOST_M_SLA:
  180. // case I2C_I2STAT_M_TX_ARB_LOST:
  181. IP_I2C_Stop(LPC_I2C, Opt);
  182. Ret = I2C_ERR;
  183. break;
  184. case I2C_I2STAT_M_RX_SLAR_ACK:
  185. if (TransferCfg->rx_length > 1) {
  186. LPC_I2C->CONSET = I2C_I2CONSET_AA;
  187. }
  188. else {
  189. LPC_I2C->CONCLR = I2C_I2CONCLR_AAC;
  190. }
  191. LPC_I2C->CONCLR = I2C_I2CONCLR_SIC;
  192. Ret = I2C_BYTE_RECV;
  193. break;
  194. case I2C_I2STAT_M_RX_DAT_ACK:
  195. if (TransferCfg->rx_count < TransferCfg->rx_length) {
  196. if ((TransferCfg->rx_length > 1) && (TransferCfg->rx_count < (TransferCfg->rx_length - 2))) {
  197. IP_I2C_GetByte(LPC_I2C, &tmp, true);
  198. Ret = I2C_BYTE_RECV;
  199. }
  200. else { /* the next byte is the last byte, send NACK instead */
  201. IP_I2C_GetByte(LPC_I2C, &tmp, false);
  202. Ret = I2C_BYTE_RECV;
  203. }
  204. *rxdat++ = tmp;
  205. TransferCfg->rx_count++;
  206. }
  207. else {
  208. IP_I2C_Stop(LPC_I2C, Opt);
  209. Ret = I2C_RECV_END;
  210. }
  211. break;
  212. case I2C_I2STAT_M_RX_DAT_NACK:
  213. IP_I2C_GetByte(LPC_I2C, &tmp, false);
  214. if (TransferCfg->rx_count < TransferCfg->rx_length) {
  215. *rxdat++ = tmp;
  216. TransferCfg->rx_count++;
  217. }
  218. IP_I2C_Stop(LPC_I2C, Opt);
  219. Ret = I2C_RECV_END;
  220. break;
  221. case I2C_I2STAT_M_RX_SLAR_NACK:
  222. case I2C_I2STAT_M_TX_SLAW_NACK:
  223. case I2C_I2STAT_BUS_ERROR:
  224. /* Send STOP condition */
  225. IP_I2C_Stop(LPC_I2C, Opt);
  226. Ret = I2C_ERR;
  227. break;
  228. /* No status information */
  229. case I2C_I2STAT_NO_INF:
  230. if ((TransferCfg->tx_count < TransferCfg->tx_length) ||
  231. (TransferCfg->rx_count < TransferCfg->rx_length)) {
  232. IP_I2C_Stop(LPC_I2C, Opt);
  233. Ret = I2C_ERR;
  234. }
  235. else {
  236. Ret = I2C_RECV_END;
  237. }
  238. break;
  239. default:
  240. LPC_I2C->CONCLR = I2C_I2CONCLR_SIC;
  241. break;
  242. }
  243. return Ret;
  244. }
  245. /* Handle I2C Master states */
  246. static int32_t IP_I2C_SlaveHanleStates(IP_I2C_001_Type *LPC_I2C, uint32_t CodeStatus, I2C_S_SETUP_Type *TransferCfg)
  247. {
  248. int32_t Ret = I2C_OK;
  249. uint8_t *txdat;
  250. uint8_t *rxdat;
  251. /* get buffer to send/receive */
  252. txdat = (uint8_t *) &TransferCfg->tx_data[TransferCfg->tx_count];
  253. rxdat = (uint8_t *) &TransferCfg->rx_data[TransferCfg->rx_count];
  254. switch (CodeStatus) {
  255. /* Reading phase -------------------------------------------------------- */
  256. /* Own SLA+R has been received, ACK has been returned */
  257. case I2C_I2STAT_S_RX_SLAW_ACK:
  258. /* General call address has been received, ACK has been returned */
  259. case I2C_I2STAT_S_RX_GENCALL_ACK:
  260. LPC_I2C->CONSET = I2C_I2CONSET_AA;
  261. LPC_I2C->CONCLR = I2C_I2CONCLR_SIC;
  262. break;
  263. /* Arbitration has been lost in Slave Address + R/W bit as bus Master. General Call has
  264. been received and ACK has been returned.*/
  265. case I2C_I2STAT_S_RX_ARB_LOST_M_GENCALL:
  266. LPC_I2C->CONSET = I2C_I2CONSET_AA | I2C_I2CONSET_STA;
  267. LPC_I2C->CONCLR = I2C_I2CONCLR_SIC;
  268. break;
  269. /* Previously addressed with own SLA;
  270. * DATA byte has been received;
  271. * ACK has been returned */
  272. case I2C_I2STAT_S_RX_ARB_LOST_M_SLA:
  273. case I2C_I2STAT_S_RX_PRE_SLA_DAT_ACK:
  274. /*
  275. * All data bytes that over-flow the specified receive
  276. * data length, just ignore them.
  277. */
  278. if ((TransferCfg->rx_count < TransferCfg->rx_length) && (TransferCfg->rx_data != NULL)) {
  279. *rxdat++ = (uint8_t) LPC_I2C->DAT;
  280. TransferCfg->rx_count++;
  281. Ret = I2C_BYTE_RECV;
  282. }
  283. if (TransferCfg->rx_count == (TransferCfg->rx_length) ) {
  284. LPC_I2C->CONCLR = I2C_I2CONCLR_AAC | I2C_I2CONCLR_SIC;
  285. Ret = I2C_BYTE_RECV;
  286. }
  287. else {
  288. LPC_I2C->CONSET = I2C_I2CONSET_AA;
  289. LPC_I2C->CONCLR = I2C_I2CONCLR_SIC;
  290. }
  291. break;
  292. /* DATA has been received, Only the first data byte will be received with ACK.
  293. * Additional data will be received with NOT ACK. */
  294. case I2C_I2STAT_S_RX_PRE_GENCALL_DAT_ACK:
  295. if ((TransferCfg->rx_count < TransferCfg->rx_length) && (TransferCfg->rx_data != NULL)) {
  296. *rxdat++ = (uint8_t) LPC_I2C->DAT;
  297. TransferCfg->rx_count++;
  298. Ret = I2C_BYTE_RECV;
  299. }
  300. LPC_I2C->CONCLR = I2C_I2CONCLR_AAC | I2C_I2CONCLR_SIC;
  301. break;
  302. /* Writing phase -------------------------------------------------------- */
  303. /* Own SLA+R has been received, ACK has been returned */
  304. case I2C_I2STAT_S_TX_SLAR_ACK:
  305. /* Data has been transmitted, ACK has been received */
  306. case I2C_I2STAT_S_TX_DAT_ACK:
  307. /*
  308. * All data bytes that over-flow the specified receive
  309. * data length, just ignore them.
  310. */
  311. if ((TransferCfg->tx_count < TransferCfg->tx_length) && (TransferCfg->tx_data != NULL)) {
  312. LPC_I2C->DAT = *txdat++;
  313. TransferCfg->tx_count++;
  314. Ret = I2C_BYTE_SENT;
  315. }
  316. LPC_I2C->CONSET = I2C_I2CONSET_AA;
  317. LPC_I2C->CONCLR = I2C_I2CONCLR_SIC;
  318. break;
  319. /* Arbitration lost in Slave Address and R/W bit as bus Master.
  320. * Own Slave Address + Read has been received, ACK has been returned. */
  321. case I2C_I2STAT_S_TX_ARB_LOST_M_SLA:
  322. if ((TransferCfg->tx_count < TransferCfg->tx_length) && (TransferCfg->tx_data != NULL)) {
  323. LPC_I2C->DAT = *txdat++;
  324. TransferCfg->tx_count++;
  325. Ret = I2C_BYTE_SENT;
  326. }
  327. LPC_I2C->CONSET = I2C_I2CONSET_AA | I2C_I2CONSET_STA;
  328. LPC_I2C->CONCLR = I2C_I2CONCLR_SIC;
  329. break;
  330. case I2C_I2STAT_S_TX_LAST_DAT_ACK:
  331. /* Data has been transmitted, NACK has been received,
  332. * that means there's no more data to send, exit now */
  333. /*
  334. * Note: Don't wait for stop event since in slave transmit mode,
  335. * since there no proof lets us know when a stop signal has been received
  336. * on slave side.
  337. */
  338. case I2C_I2STAT_S_TX_DAT_NACK:
  339. LPC_I2C->CONSET = I2C_I2CONSET_AA;
  340. LPC_I2C->CONCLR = I2C_I2CONCLR_SIC;
  341. Ret = I2C_SEND_END;
  342. break;
  343. /* Previously addressed with own SLA;
  344. * DATA byte has been received;
  345. * NOT ACK has been returned */
  346. case I2C_I2STAT_S_RX_PRE_SLA_DAT_NACK:
  347. /* DATA has been received, NOT ACK has been returned */
  348. case I2C_I2STAT_S_RX_PRE_GENCALL_DAT_NACK:
  349. LPC_I2C->CONSET = I2C_I2CONSET_AA;
  350. LPC_I2C->CONCLR = I2C_I2CONCLR_SIC;
  351. Ret = I2C_RECV_END;
  352. break;
  353. /*
  354. * Note that: Return code only let us know a stop condition mixed
  355. * with a repeat start condition in the same code value.
  356. * So we should provide a time-out. In case this is really a stop
  357. * condition, this will return back after time out condition. Otherwise,
  358. * next session that is slave receive data will be completed.
  359. */
  360. /* A Stop or a repeat start condition */
  361. case I2C_I2STAT_S_RX_STA_STO_SLVREC_SLVTRX:
  362. LPC_I2C->CONSET = I2C_I2CONSET_AA;
  363. LPC_I2C->CONCLR = I2C_I2CONCLR_SIC;
  364. Ret = I2C_STA_STO_RECV;
  365. break;
  366. /* No status information */
  367. case I2C_I2STAT_NO_INF:
  368. /* Other status must be captured */
  369. default:
  370. LPC_I2C->CONSET = I2C_I2CONSET_AA;
  371. LPC_I2C->CONCLR = I2C_I2CONCLR_SIC;
  372. break;
  373. }
  374. return Ret;
  375. }
  376. /*****************************************************************************
  377. * Public functions
  378. ****************************************************************************/
  379. /* Initializes the LPC_I2C peripheral */
  380. void IP_I2C_Init(IP_I2C_001_Type *LPC_I2C)
  381. {
  382. /* Set I2C operation to default */
  383. LPC_I2C->CONCLR = (I2C_I2CONCLR_AAC | I2C_I2CONCLR_SIC | I2C_I2CONCLR_STAC | I2C_I2CONCLR_I2ENC);
  384. }
  385. /* De-initializes the I2C peripheral registers to their default reset values */
  386. void IP_I2C_DeInit(IP_I2C_001_Type *LPC_I2C)
  387. {
  388. /* Disable I2C control */
  389. LPC_I2C->CONCLR = I2C_I2CONCLR_I2ENC;
  390. }
  391. /* Set up clock rate for I2Cx */
  392. void IP_I2C_SetClockRate(IP_I2C_001_Type *LPC_I2C, uint32_t SCLValue)
  393. {
  394. LPC_I2C->SCLH = (uint32_t) (SCLValue / 2);
  395. LPC_I2C->SCLL = (uint32_t) (SCLValue - LPC_I2C->SCLH);
  396. }
  397. /* Enable or disable I2C peripheral's operation */
  398. void IP_I2C_Cmd(IP_I2C_001_Type *LPC_I2C, I2C_Mode Mode, FunctionalState NewState)
  399. {
  400. if (NewState == ENABLE) {
  401. if (Mode != I2C_SLAVE_MODE) {
  402. LPC_I2C->CONSET = I2C_I2CONSET_I2EN;
  403. }
  404. else {
  405. LPC_I2C->CONSET = I2C_I2CONSET_I2EN | I2C_I2CONSET_AA;
  406. }
  407. }
  408. else {
  409. LPC_I2C->CONCLR = I2C_I2CONCLR_I2ENC;
  410. }
  411. }
  412. /* General Master Interrupt handler for I2C peripheral */
  413. void IP_I2C_Interrupt_MasterHandler(IP_I2C_001_Type *LPC_I2C, I2C_ID_Type I2C_Num)
  414. {
  415. uint32_t returnCode;
  416. I2C_M_SETUP_Type *txrx_setup;
  417. int32_t Ret = I2C_OK;
  418. txrx_setup = (I2C_M_SETUP_Type *) &i2cdat[I2C_Num].txrx_setup_master;
  419. while (!(LPC_I2C->CONSET & I2C_I2CONSET_SI)) {}
  420. returnCode = (uint32_t) (LPC_I2C->STAT & I2C_STAT_CODE_BITMASK);
  421. /* Save current status */
  422. txrx_setup->status = returnCode;
  423. Ret = IP_I2C_MasterHanleStates(LPC_I2C, returnCode, txrx_setup, I2C_TRANSFER_INTERRUPT);
  424. if (I2C_CheckError(Ret)) {
  425. if (txrx_setup->retransmissions_count < txrx_setup->retransmissions_max) {
  426. /* Retry */
  427. txrx_setup->retransmissions_count++;
  428. txrx_setup->tx_count = 0;
  429. txrx_setup->rx_count = 0;
  430. /* Reset STA, STO, SI */
  431. IP_I2C_Start(LPC_I2C, I2C_TRANSFER_INTERRUPT);
  432. return;
  433. }
  434. else {
  435. goto s_int_end;
  436. }
  437. }
  438. else if (Ret & I2C_SEND_END) {
  439. /* If no need to wait for data from Slave */
  440. if (txrx_setup->rx_count >= (txrx_setup->rx_length)) {
  441. goto s_int_end;
  442. }
  443. else { /* Start to wait for data from Slave */
  444. /* Reset STA, STO, SI */
  445. IP_I2C_Start(LPC_I2C, I2C_TRANSFER_INTERRUPT);
  446. return;
  447. }
  448. }
  449. else if (Ret & I2C_RECV_END) {
  450. goto s_int_end;
  451. }
  452. else {
  453. return;
  454. }
  455. s_int_end:
  456. LPC_I2C->CONCLR = I2C_I2CONCLR_AAC | I2C_I2CONCLR_SIC | I2C_I2CONCLR_STAC;
  457. I2C_MasterComplete[I2C_Num] = true;
  458. }
  459. /* General Slave Interrupt handler for I2C peripheral */
  460. void IP_I2C_Interrupt_SlaveHandler(IP_I2C_001_Type *LPC_I2C, I2C_ID_Type I2C_Num)
  461. {
  462. uint32_t returnCode;
  463. I2C_S_SETUP_Type *txrx_setup;
  464. int32_t Ret = I2C_OK;
  465. txrx_setup = (I2C_S_SETUP_Type *) &i2cdat[I2C_Num].txrx_setup_slave;
  466. while (!(LPC_I2C->CONSET & I2C_I2CONSET_SI)) {}
  467. returnCode = (uint32_t) (LPC_I2C->STAT & I2C_STAT_CODE_BITMASK);
  468. /* Save current status */
  469. txrx_setup->status = returnCode;
  470. Ret = IP_I2C_SlaveHanleStates(LPC_I2C, returnCode, txrx_setup);
  471. if ((I2C_CheckError(Ret)) || (Ret & I2C_STA_STO_RECV) || (Ret & I2C_SEND_END)) {
  472. goto s_int_end;
  473. }
  474. else {
  475. return;
  476. }
  477. s_int_end:
  478. LPC_I2C->CONCLR = I2C_I2CONCLR_AAC | I2C_I2CONCLR_SIC | I2C_I2CONCLR_STAC;
  479. I2C_SlaveComplete[I2C_Num] = true;
  480. }
  481. /* Transmit and Receive data in master mode */
  482. Status IP_I2C_MasterTransferData(IP_I2C_001_Type *LPC_I2C,
  483. I2C_ID_Type I2C_Num,
  484. I2C_M_SETUP_Type *TransferCfg,
  485. I2C_TRANSFER_OPT_Type Opt)
  486. {
  487. uint32_t CodeStatus;
  488. int32_t Ret = I2C_OK;
  489. /* Reset I2C setup value to default state */
  490. TransferCfg->tx_count = 0;
  491. TransferCfg->rx_count = 0;
  492. TransferCfg->status = 0;
  493. if (Opt == I2C_TRANSFER_POLLING) {
  494. /* First Start condition -------------------------------------------------------------- */
  495. TransferCfg->retransmissions_count = 0;
  496. retry:
  497. /* Reset I2C setup value to default state */
  498. TransferCfg->tx_count = 0;
  499. TransferCfg->rx_count = 0;
  500. /* Start command */
  501. CodeStatus = IP_I2C_Start(LPC_I2C, I2C_TRANSFER_POLLING);
  502. while (1) { /* send data first and then receive data from Slave */
  503. Ret = IP_I2C_MasterHanleStates(LPC_I2C, CodeStatus, TransferCfg, I2C_TRANSFER_POLLING);
  504. if (I2C_CheckError(Ret)) {
  505. TransferCfg->retransmissions_count++;
  506. if (TransferCfg->retransmissions_count > TransferCfg->retransmissions_max) {
  507. /* save status */
  508. TransferCfg->status = CodeStatus | I2C_SETUP_STATUS_NOACKF;
  509. goto error;
  510. }
  511. else {
  512. goto retry;
  513. }
  514. }
  515. else if ( (Ret & I2C_BYTE_SENT) ||
  516. (Ret & I2C_BYTE_RECV)) {
  517. /* Wait for sending ends/ Wait for next byte */
  518. while (!(LPC_I2C->CONSET & I2C_I2CONSET_SI)) {}
  519. }
  520. else if (Ret & I2C_SEND_END) { /* already send all data */
  521. /* If no need to wait for data from Slave */
  522. if (TransferCfg->rx_count >= (TransferCfg->rx_length)) {
  523. break;
  524. }
  525. else {
  526. IP_I2C_Start(LPC_I2C, I2C_TRANSFER_POLLING);
  527. }
  528. }
  529. else if (Ret & I2C_RECV_END) { /* already receive all data */
  530. break;
  531. }
  532. CodeStatus = LPC_I2C->STAT & I2C_STAT_CODE_BITMASK;
  533. }
  534. return SUCCESS;
  535. error:
  536. return ERROR;
  537. }
  538. else if (Opt == I2C_TRANSFER_INTERRUPT) {
  539. I2C_MasterComplete[I2C_Num] = false;
  540. /* Setup tx_rx data, callback and interrupt handler */
  541. i2cdat[I2C_Num].txrx_setup_master = *TransferCfg;
  542. /* Set direction phase, write first */
  543. i2cdat[I2C_Num].dir = 0;
  544. /* First Start condition -------------------------------------------------------------- */
  545. /* Reset STA, STO, SI */
  546. LPC_I2C->CONCLR = I2C_I2CONCLR_SIC | I2C_I2CONCLR_STOC | I2C_I2CONCLR_STAC;
  547. LPC_I2C->CONSET = I2C_I2CONSET_STA;
  548. return SUCCESS;
  549. }
  550. return ERROR;
  551. }
  552. /* Receive and Transmit data in slave mode */
  553. Status IP_I2C_SlaveTransferData(IP_I2C_001_Type *LPC_I2C,
  554. I2C_ID_Type I2C_Num,
  555. I2C_S_SETUP_Type *TransferCfg,
  556. I2C_TRANSFER_OPT_Type Opt)
  557. {
  558. int32_t Ret = I2C_OK;
  559. uint32_t CodeStatus = 0;
  560. /* Reset I2C setup value to default state */
  561. TransferCfg->tx_count = 0;
  562. TransferCfg->rx_count = 0;
  563. TransferCfg->status = 0;
  564. /* Polling option */
  565. if (Opt == I2C_TRANSFER_POLLING) {
  566. /* Set AA bit to ACK command on I2C bus */
  567. LPC_I2C->CONSET = I2C_I2CONSET_AA;
  568. /* Clear SI bit to be ready ... */
  569. LPC_I2C->CONCLR = (I2C_I2CONCLR_SIC | I2C_I2CONCLR_STAC | I2C_I2CONCLR_STOC);
  570. while (1) {
  571. /* Check SI flag ready */
  572. if (LPC_I2C->CONSET & I2C_I2CONSET_SI) {
  573. CodeStatus = (LPC_I2C->STAT & I2C_STAT_CODE_BITMASK);
  574. Ret = IP_I2C_SlaveHanleStates(LPC_I2C, CodeStatus, TransferCfg);
  575. if (I2C_CheckError(Ret)) {
  576. goto s_error;
  577. }
  578. else if ((Ret & I2C_STA_STO_RECV) || (Ret & I2C_SEND_END)) {
  579. goto s_end_stage;
  580. }
  581. }
  582. }
  583. s_end_stage:
  584. /* Clear AA bit to disable ACK on I2C bus */
  585. LPC_I2C->CONCLR = I2C_I2CONCLR_AAC;
  586. /* Check if there's no error during operation
  587. * Update status
  588. */
  589. TransferCfg->status = CodeStatus | I2C_SETUP_STATUS_DONE;
  590. return SUCCESS;
  591. s_error:
  592. /* Clear AA bit to disable ACK on I2C bus */
  593. LPC_I2C->CONCLR = I2C_I2CONCLR_AAC;
  594. /* Update status */
  595. TransferCfg->status = CodeStatus;
  596. return ERROR;
  597. }
  598. else if (Opt == I2C_TRANSFER_INTERRUPT) {
  599. I2C_SlaveComplete[I2C_Num] = false;
  600. /* Setup tx_rx data, callback and interrupt handler */
  601. i2cdat[I2C_Num].txrx_setup_slave = *TransferCfg;
  602. /* Set direction phase, read first */
  603. i2cdat[I2C_Num].dir = 1;
  604. /* Enable AA */
  605. LPC_I2C->CONSET = I2C_I2CONSET_AA;
  606. LPC_I2C->CONCLR = I2C_I2CONCLR_SIC | I2C_I2CONCLR_STAC;
  607. return SUCCESS;
  608. }
  609. return ERROR;
  610. }
  611. /* Get status of Master Transfer */
  612. bool IP_I2C_Interrupt_MasterTransferComplete(I2C_ID_Type I2C_Num)
  613. {
  614. bool retval;
  615. retval = I2C_MasterComplete[I2C_Num];
  616. I2C_MasterComplete[I2C_Num] = false;
  617. return retval;
  618. }
  619. /* Get status of Slave Transfer */
  620. bool IP_I2C_Interrupt_SlaveTransferComplete(I2C_ID_Type I2C_Num)
  621. {
  622. bool retval;
  623. retval = I2C_SlaveComplete[I2C_Num];
  624. I2C_SlaveComplete[I2C_Num] = false;
  625. return retval;
  626. }
  627. /* Set Own slave address in I2C peripheral corresponding to parameter specified in OwnSlaveAddrConfigStruct */
  628. void IP_I2C_SetOwnSlaveAddr(IP_I2C_001_Type *LPC_I2C, I2C_OWNSLAVEADDR_CFG_Type *OwnSlaveAddrConfigStruct)
  629. {
  630. uint32_t tmp;
  631. tmp = (((uint32_t) (OwnSlaveAddrConfigStruct->SlaveAddr_7bit << 1)) \
  632. | ((OwnSlaveAddrConfigStruct->GeneralCallState == ENABLE) ? 0x01 : 0x00)) & I2C_I2ADR_BITMASK;
  633. switch (OwnSlaveAddrConfigStruct->SlaveAddrChannel) {
  634. case 0:
  635. LPC_I2C->ADR0 = tmp;
  636. LPC_I2C->MASK[0] =
  637. I2C_I2MASK_MASK((uint32_t) (OwnSlaveAddrConfigStruct->SlaveAddrMaskValue));
  638. break;
  639. case 1:
  640. LPC_I2C->ADR1 = tmp;
  641. LPC_I2C->MASK[1] = I2C_I2MASK_MASK((uint32_t) (OwnSlaveAddrConfigStruct->SlaveAddrMaskValue));
  642. break;
  643. case 2:
  644. LPC_I2C->ADR2 = tmp;
  645. LPC_I2C->MASK[2] = I2C_I2MASK_MASK((uint32_t) (OwnSlaveAddrConfigStruct->SlaveAddrMaskValue));
  646. break;
  647. case 3:
  648. LPC_I2C->ADR3 = tmp;
  649. LPC_I2C->MASK[3] = I2C_I2MASK_MASK((uint32_t) (OwnSlaveAddrConfigStruct->SlaveAddrMaskValue));
  650. break;
  651. }
  652. }