i2c_8xx.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * @brief LPC15xx I2C Common driver
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2014
  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 "chip.h"
  32. /*****************************************************************************
  33. * Private types/enumerations/variables
  34. ****************************************************************************/
  35. /*****************************************************************************
  36. * Public types/enumerations/variables
  37. ****************************************************************************/
  38. /*****************************************************************************
  39. * Private functions
  40. ****************************************************************************/
  41. /* Get the RESET ID corresponding to the given I2C base */
  42. static CHIP_SYSCTL_PERIPH_RESET_T I2C_GetResetID(LPC_I2C_T *pI2C)
  43. {
  44. uint32_t base = (uint32_t) pI2C;
  45. switch (base) {
  46. case LPC_I2C1_BASE:
  47. return RESET_I2C1;
  48. case LPC_I2C2_BASE:
  49. return RESET_I2C2;
  50. case LPC_I2C3_BASE:
  51. return RESET_I2C3;
  52. default:
  53. return RESET_I2C0;
  54. }
  55. }
  56. /* Get the CLOCK ID corresponding to the given I2C base */
  57. static CHIP_SYSCTL_CLOCK_T I2C_GetClockID(LPC_I2C_T *pI2C)
  58. {
  59. uint32_t base = (uint32_t) pI2C;
  60. switch (base) {
  61. case LPC_I2C1_BASE:
  62. return SYSCTL_CLOCK_I2C1;
  63. case LPC_I2C2_BASE:
  64. return SYSCTL_CLOCK_I2C2;
  65. case LPC_I2C3_BASE:
  66. return SYSCTL_CLOCK_I2C3;
  67. default:
  68. return SYSCTL_CLOCK_I2C0;
  69. }
  70. }
  71. /**
  72. * @brief Sets HIGH and LOW duty cycle registers
  73. * @param pI2C : Pointer to selected I2C peripheral
  74. * @param sclH : Number of I2C_PCLK cycles for the SCL HIGH time value between (2 - 9).
  75. * @param sclL : Number of I2C_PCLK cycles for the SCL LOW time value between (2 - 9).
  76. * @return Nothing
  77. * @note The I2C clock divider should be set to the appropriate value before calling this function
  78. * The I2C baud is determined by the following formula: <br>
  79. * I2C_bitFrequency = (I2C_PCLK)/(I2C_CLKDIV * (sclH + sclL)) <br>
  80. * where I2C_PCLK is the frequency of the System clock and I2C_CLKDIV is I2C clock divider
  81. */
  82. static void Chip_I2CM_SetDutyCycle(LPC_I2C_T *pI2C, uint16_t sclH, uint16_t sclL)
  83. {
  84. /* Limit to usable range of timing values */
  85. if (sclH < 2) {
  86. sclH = 2;
  87. }
  88. else if (sclH > 9) {
  89. sclH = 9;
  90. }
  91. if (sclL < 2) {
  92. sclL = 2;
  93. }
  94. else if (sclL > 9) {
  95. sclL = 9;
  96. }
  97. pI2C->MSTTIME = (((sclH - 2) & 0x07) << 4) | ((sclL - 2) & 0x07);
  98. }
  99. /*****************************************************************************
  100. * Public functions
  101. ****************************************************************************/
  102. /* Initializes the LPC_I2C peripheral */
  103. void Chip_I2C_Init(LPC_I2C_T *pI2C)
  104. {
  105. /* Enable I2C clock */
  106. Chip_Clock_EnablePeriphClock(I2C_GetClockID(pI2C));
  107. /* Peripheral reset control to I2C */
  108. Chip_SYSCTL_PeriphReset(I2C_GetResetID(pI2C));
  109. }
  110. /* Shuts down the I2C controller block */
  111. void Chip_I2C_DeInit(LPC_I2C_T *pI2C)
  112. {
  113. /* Disable I2C clock */
  114. Chip_Clock_DisablePeriphClock(I2C_GetClockID(pI2C));
  115. }
  116. /* Set up bus speed for LPC_I2C interface */
  117. void Chip_I2CM_SetBusSpeed(LPC_I2C_T *pI2C, uint32_t busSpeed)
  118. {
  119. uint32_t scl = Chip_Clock_GetSystemClockRate() / (Chip_I2C_GetClockDiv(pI2C) * busSpeed);
  120. Chip_I2CM_SetDutyCycle(pI2C, (scl >> 1), (scl - (scl >> 1)));
  121. }
  122. /* Master transfer state change handler handler */
  123. uint32_t Chip_I2CM_XferHandler(LPC_I2C_T *pI2C, I2CM_XFER_T *xfer)
  124. {
  125. uint32_t status = Chip_I2CM_GetStatus(pI2C);
  126. /* Master Lost Arbitration */
  127. if (status & I2C_STAT_MSTRARBLOSS) {
  128. /* Set transfer status as Arbitration Lost */
  129. xfer->status = I2CM_STATUS_ARBLOST;
  130. /* Clear Status Flags */
  131. Chip_I2CM_ClearStatus(pI2C, I2C_STAT_MSTRARBLOSS);
  132. }
  133. /* Master Start Stop Error */
  134. else if (status & I2C_STAT_MSTSTSTPERR) {
  135. /* Set transfer status as Bus Error */
  136. xfer->status = I2CM_STATUS_BUS_ERROR;
  137. /* Clear Status Flags */
  138. Chip_I2CM_ClearStatus(pI2C, I2C_STAT_MSTSTSTPERR);
  139. }
  140. /* Master is Pending */
  141. else if (status & I2C_STAT_MSTPENDING) {
  142. /* Branch based on Master State Code */
  143. switch (Chip_I2CM_GetMasterState(pI2C)) {
  144. /* Master idle */
  145. case I2C_STAT_MSTCODE_IDLE:
  146. /* Do Nothing */
  147. break;
  148. /* Receive data is available */
  149. case I2C_STAT_MSTCODE_RXREADY:
  150. /* Read Data */
  151. *xfer->rxBuff++ = pI2C->MSTDAT;
  152. xfer->rxSz--;
  153. if (xfer->rxSz) {
  154. /* Set Continue if there is more data to read */
  155. Chip_I2CM_MasterContinue(pI2C);
  156. }
  157. else {
  158. /* Set transfer status as OK */
  159. xfer->status = I2CM_STATUS_OK;
  160. /* No data to read send Stop */
  161. Chip_I2CM_SendStop(pI2C);
  162. }
  163. break;
  164. /* Master Transmit available */
  165. case I2C_STAT_MSTCODE_TXREADY:
  166. if (xfer->txSz) {
  167. /* If Tx data available transmit data and continue */
  168. pI2C->MSTDAT = *xfer->txBuff++;
  169. xfer->txSz--;
  170. Chip_I2CM_MasterContinue(pI2C);
  171. }
  172. else {
  173. /* If receive queued after transmit then initiate master receive transfer*/
  174. if (xfer->rxSz) {
  175. /* Write Address and RW bit to data register */
  176. Chip_I2CM_WriteByte(pI2C, (xfer->slaveAddr << 1) | 0x1);
  177. /* Enter to Master Transmitter mode */
  178. Chip_I2CM_SendStart(pI2C);
  179. }
  180. else {
  181. /* If no receive queued then set transfer status as OK */
  182. xfer->status = I2CM_STATUS_OK;
  183. /* Send Stop */
  184. Chip_I2CM_SendStop(pI2C);
  185. }
  186. }
  187. break;
  188. case I2C_STAT_MSTCODE_NACKADR:
  189. /* Set transfer status as NACK on address */
  190. xfer->status = I2CM_STATUS_NAK_ADR;
  191. Chip_I2CM_SendStop(pI2C);
  192. break;
  193. case I2C_STAT_MSTCODE_NACKDAT:
  194. /* Set transfer status as NACK on data */
  195. xfer->status = I2CM_STATUS_NAK_DAT;
  196. Chip_I2CM_SendStop(pI2C);
  197. break;
  198. default:
  199. /* Default case should not occur*/
  200. xfer->status = I2CM_STATUS_ERROR;
  201. break;
  202. }
  203. }
  204. else {
  205. /* Default case should not occur */
  206. xfer->status = I2CM_STATUS_ERROR;
  207. }
  208. return xfer->status != I2CM_STATUS_BUSY;
  209. }
  210. /* Transmit and Receive data in master mode */
  211. void Chip_I2CM_Xfer(LPC_I2C_T *pI2C, I2CM_XFER_T *xfer)
  212. {
  213. /* set the transfer status as busy */
  214. xfer->status = I2CM_STATUS_BUSY;
  215. /* Clear controller state. */
  216. Chip_I2CM_ClearStatus(pI2C, I2C_STAT_MSTRARBLOSS | I2C_STAT_MSTSTSTPERR);
  217. /* Write Address and RW bit to data register */
  218. Chip_I2CM_WriteByte(pI2C, (xfer->slaveAddr << 1) | (xfer->txSz == 0));
  219. /* Enter to Master Transmitter mode */
  220. Chip_I2CM_SendStart(pI2C);
  221. }
  222. /* Transmit and Receive data in master mode */
  223. uint32_t Chip_I2CM_XferBlocking(LPC_I2C_T *pI2C, I2CM_XFER_T *xfer)
  224. {
  225. uint32_t ret = 0;
  226. /* start transfer */
  227. Chip_I2CM_Xfer(pI2C, xfer);
  228. while (ret == 0) {
  229. /* wait for status change interrupt */
  230. while (!Chip_I2CM_IsMasterPending(pI2C)) {}
  231. /* call state change handler */
  232. ret = Chip_I2CM_XferHandler(pI2C, xfer);
  233. }
  234. return ret;
  235. }
  236. /* Slave transfer state change handler */
  237. uint32_t Chip_I2CS_XferHandler(LPC_I2C_T *pI2C, const I2CS_XFER_T *xfers)
  238. {
  239. uint32_t done = 0;
  240. uint8_t data;
  241. uint32_t state;
  242. /* transfer complete? */
  243. if ((Chip_I2C_GetPendingInt(pI2C) & I2C_INTENSET_SLVDESEL) != 0) {
  244. Chip_I2CS_ClearStatus(pI2C, I2C_STAT_SLVDESEL);
  245. xfers->slaveDone();
  246. }
  247. else {
  248. /* Determine the current I2C slave state */
  249. state = Chip_I2CS_GetSlaveState(pI2C);
  250. switch (state) {
  251. case I2C_STAT_SLVCODE_ADDR: /* Slave address received */
  252. /* Get slave address that needs servicing */
  253. data = Chip_I2CS_GetSlaveAddr(pI2C, Chip_I2CS_GetSlaveMatchIndex(pI2C));
  254. /* Call address callback */
  255. xfers->slaveStart(data);
  256. break;
  257. case I2C_STAT_SLVCODE_RX: /* Data byte received */
  258. /* Get received data */
  259. data = Chip_I2CS_ReadByte(pI2C);
  260. done = xfers->slaveRecv(data);
  261. break;
  262. case I2C_STAT_SLVCODE_TX: /* Get byte that needs to be sent */
  263. /* Get data to send */
  264. done = xfers->slaveSend(&data);
  265. Chip_I2CS_WriteByte(pI2C, data);
  266. break;
  267. }
  268. }
  269. if (done == 0) {
  270. Chip_I2CS_SlaveContinue(pI2C);
  271. }
  272. else {
  273. Chip_I2CS_SlaveNACK(pI2C);
  274. }
  275. return done;
  276. }