hw_i2cmd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * @brief I2C master ROM API declarations and functions
  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 <stdint.h>
  32. #include <string.h>
  33. #include "hw_i2cmd.h"
  34. #define DRVVERSION 0x0100
  35. /* Private data structure used for the I2C master driver, holds the driver and
  36. peripheral context */
  37. typedef struct {
  38. void *pUserData; /*!< Pointer to user data used by driver instance, use NULL if not used */
  39. LPC_I2C_T *base; /*!< Base address of I2C peripheral to use */
  40. i2cMasterCompleteCB pXferCompCB; /*!< Transfer complete callback */
  41. i2cMasterTransmitStartCB pTranStartCb; /*!< Transmit data start callback */
  42. i2cMasterReceiveStartCB pTranRecvCb; /*!< Receive data start callback */
  43. ROM_I2CM_XFER_T *pXfer; /*!< Pointer to current transfer */
  44. ErrorCode_t pendingStatus; /*!< Pending master transfer status before clocking transfer */
  45. uint16_t sendIdx;
  46. uint16_t recvIdx;
  47. } I2CM_DATACONTEXT_T;
  48. #define _rom_i2cmEnable(pI2C) (pI2C->CFG |= I2C_CFG_MSTEN);
  49. #define _rom_i2cmGetMasterState(pI2C) ((pI2C->STAT & I2C_STAT_MSTSTATE) >> 1)
  50. /* Sets I2C Clock Divider registers */
  51. static void _rom_i2cmSetClockDiv(LPC_I2C_T *pI2C, uint32_t clkdiv)
  52. {
  53. if ((clkdiv >= 1) && (clkdiv <= 65536)) {
  54. pI2C->CLKDIV = clkdiv - 1;
  55. }
  56. else {
  57. pI2C->CLKDIV = 0;
  58. }
  59. }
  60. /* Sets HIGH and LOW duty cycle registers */
  61. static void _rom_i2cmSetDutyCycle(LPC_I2C_T *pI2C, uint16_t sclH, uint16_t sclL)
  62. {
  63. /* Limit to usable range of timing values */
  64. if (sclH < 2) {
  65. sclH = 2;
  66. }
  67. else if (sclH > 9) {
  68. sclH = 9;
  69. }
  70. if (sclL < 2) {
  71. sclL = 2;
  72. }
  73. else if (sclL > 9) {
  74. sclL = 9;
  75. }
  76. pI2C->MSTTIME = (((sclH - 2) & 0x07) << 4) | ((sclL - 2) & 0x07);
  77. }
  78. // **********************************************************
  79. uint32_t i2cm_get_mem_size(void)
  80. {
  81. return sizeof(I2CM_DATACONTEXT_T);
  82. }
  83. ROM_I2CM_HANDLE_T i2cm_init(void *mem, const ROM_I2CM_INIT_T *pInit)
  84. {
  85. I2CM_DATACONTEXT_T *pDrv;
  86. /* Verify alignment is at least 4 bytes */
  87. if (((uint32_t) mem & 0x3) != 0) {
  88. return NULL;
  89. }
  90. pDrv = (I2CM_DATACONTEXT_T *) mem;
  91. memset(pDrv, 0, sizeof(I2CM_DATACONTEXT_T));
  92. /* Save base of peripheral and pointer to user data */
  93. pDrv->pUserData = pInit->pUserData;
  94. pDrv->base = (LPC_I2C_T *) pInit->base;
  95. /* Pick a safe clock divider until clock rate is setup */
  96. _rom_i2cmSetClockDiv(pDrv->base, 8);
  97. /* Clear pending master statuses */
  98. pDrv->base->STAT = (I2C_STAT_MSTRARBLOSS | I2C_STAT_MSTSTSTPERR);
  99. /* Enable I2C master interface */
  100. _rom_i2cmEnable(pDrv->base);
  101. return pDrv;
  102. }
  103. uint32_t i2cm_set_clock_rate(ROM_I2CM_HANDLE_T pHandle, uint32_t inRate, uint32_t i2cRate)
  104. {
  105. uint32_t scl, div;
  106. I2CM_DATACONTEXT_T *pDrv = (I2CM_DATACONTEXT_T *) pHandle;
  107. /* Determine the best I2C clock dividers to generate the target I2C master clock */
  108. /* The maximum SCL and SCH dividers are 7, for a maximum divider set of 14 */
  109. /* The I2C master divider is between 1 and 65536. */
  110. /* Pick a main I2C divider that allows centered SCL/SCH dividers */
  111. div = inRate / (i2cRate << 3);
  112. if (div == 0) {
  113. div = 1;
  114. }
  115. _rom_i2cmSetClockDiv(pDrv->base, div);
  116. /* Determine SCL/SCH dividers */
  117. scl = inRate / (div * i2cRate);
  118. _rom_i2cmSetDutyCycle(pDrv->base, (scl >> 1), (scl - (scl >> 1)));
  119. return inRate / (div * scl);
  120. }
  121. void i2cm_register_callback(ROM_I2CM_HANDLE_T pHandle, uint32_t cbIndex, void *pCB)
  122. {
  123. I2CM_DATACONTEXT_T *pDrv = (I2CM_DATACONTEXT_T *) pHandle;
  124. if (cbIndex == ROM_I2CM_DATACOMPLETE_CB) {
  125. pDrv->pXferCompCB = (i2cMasterCompleteCB) pCB;
  126. }
  127. else if (cbIndex == ROM_I2CM_DATATRANSMITSTART_CB) {
  128. pDrv->pTranStartCb = (i2cMasterTransmitStartCB) pCB;
  129. }
  130. else if (cbIndex == ROM_I2CM_DATATRECEIVESTART_CB) {
  131. pDrv->pTranRecvCb = (i2cMasterReceiveStartCB) pCB;
  132. }
  133. }
  134. ErrorCode_t i2cm_transfer(ROM_I2CM_HANDLE_T pHandle, ROM_I2CM_XFER_T *pXfer)
  135. {
  136. I2CM_DATACONTEXT_T *pDrv = (I2CM_DATACONTEXT_T *) pHandle;
  137. /* Is transfer NULL? */
  138. if (pXfer == NULL) {
  139. return ERR_I2C_PARAM;
  140. }
  141. /* I2C master controller should be pending and idle */
  142. if ((pDrv->base->STAT & I2C_STAT_MSTPENDING) == 0) {
  143. pXfer->status = ERR_I2C_GENERAL_FAILURE;
  144. return ERR_I2C_GENERAL_FAILURE;
  145. }
  146. if (_rom_i2cmGetMasterState(pDrv->base) != I2C_STAT_MSTCODE_IDLE) {
  147. pXfer->status = ERR_I2C_GENERAL_FAILURE;
  148. return ERR_I2C_GENERAL_FAILURE;
  149. }
  150. /* Save transfer descriptor */
  151. pDrv->pXfer = pXfer;
  152. pXfer->status = ERR_I2C_BUSY;
  153. pDrv->sendIdx = 0;
  154. pDrv->recvIdx = 0;
  155. /* Pending status for completion of trasnfer */
  156. pDrv->pendingStatus = ERR_I2C_GENERAL_FAILURE;
  157. /* Clear controller state */
  158. pDrv->base->STAT = (I2C_STAT_MSTRARBLOSS | I2C_STAT_MSTSTSTPERR);
  159. /* Will always transisiton to idle at start or end of transfer */
  160. if (pXfer->txSz) {
  161. /* Call transmit start callback to setup TX DMA if needed */
  162. if (pDrv->pTranStartCb) {
  163. pDrv->pTranStartCb(pHandle, pXfer);
  164. }
  165. /* Start transmit state */
  166. pDrv->base->MSTDAT = (uint32_t) (pXfer->slaveAddr << 1);
  167. pDrv->base->MSTCTL = I2C_MSTCTL_MSTSTART;
  168. }
  169. else if (pXfer->rxSz) {
  170. /* Start receive state with start ot repeat start */
  171. pDrv->base->MSTDAT = (uint32_t) (pXfer->slaveAddr << 1) | 0x1;
  172. pDrv->base->MSTCTL = I2C_MSTCTL_MSTSTART;
  173. /* Call receive start callback to setup RX DMA if needed */
  174. if (pDrv->pTranRecvCb) {
  175. pDrv->pTranRecvCb(pHandle, pXfer);
  176. }
  177. }
  178. else {
  179. /* No data - either via data callbacks or a slave query only */
  180. pDrv->base->MSTDAT = (uint32_t) (pXfer->slaveAddr << 1);
  181. pDrv->base->MSTCTL = I2C_MSTCTL_MSTSTART;
  182. }
  183. /* Enable supported master interrupts */
  184. pDrv->base->INTENSET = (I2C_INTENSET_MSTPENDING | I2C_INTENSET_MSTRARBLOSS |
  185. I2C_INTENSET_MSTSTSTPERR);
  186. /* Does the driver need to block? */
  187. if ((pXfer->flags & ROM_I2CM_FLAG_BLOCKING) != 0) {
  188. while (pXfer->status == ERR_I2C_BUSY) {
  189. i2cm_transfer_handler(pHandle);
  190. }
  191. }
  192. return pXfer->status;
  193. }
  194. // Otime = "optimize for speed of code execution"
  195. // ...add this pragma 1 line above the interrupt service routine function.
  196. void i2cm_transfer_handler(ROM_I2CM_HANDLE_T pHandle)
  197. {
  198. I2CM_DATACONTEXT_T *pDrv = (I2CM_DATACONTEXT_T *) pHandle;
  199. ROM_I2CM_XFER_T *pXfer = pDrv->pXfer;
  200. uint32_t status = pDrv->base->STAT;
  201. if (status & I2C_STAT_MSTRARBLOSS) {
  202. /* Master Lost Arbitration */
  203. /* Set transfer status as Arbitration Lost */
  204. pDrv->pendingStatus = ERR_I2C_LOSS_OF_ARBRITRATION;
  205. /* Clear Status Flags */
  206. pDrv->base->STAT = I2C_STAT_MSTRARBLOSS;
  207. pDrv->base->INTENCLR = (I2C_INTENSET_MSTPENDING | I2C_INTENSET_MSTRARBLOSS |
  208. I2C_INTENSET_MSTSTSTPERR);
  209. pXfer->status = pDrv->pendingStatus;
  210. if (pDrv->pXferCompCB != NULL) {
  211. pDrv->pXferCompCB(pHandle, pXfer);
  212. }
  213. }
  214. else if (status & I2C_STAT_MSTSTSTPERR) {
  215. /* Master Start Stop Error */
  216. /* Set transfer status as Bus Error */
  217. pDrv->pendingStatus = ERR_I2C_GENERAL_FAILURE;
  218. /* Clear Status Flags */
  219. pDrv->base->STAT = I2C_STAT_MSTSTSTPERR;
  220. pDrv->base->INTENCLR = (I2C_INTENSET_MSTPENDING | I2C_INTENSET_MSTRARBLOSS |
  221. I2C_INTENSET_MSTSTSTPERR);
  222. pXfer->status = pDrv->pendingStatus;
  223. if (pDrv->pXferCompCB != NULL) {
  224. pDrv->pXferCompCB(pHandle, pXfer);
  225. }
  226. }
  227. else if (status & I2C_STAT_MSTPENDING) {
  228. /* Master is Pending */
  229. /* Branch based on Master State Code */
  230. switch (_rom_i2cmGetMasterState(pDrv->base)) {
  231. case I2C_STAT_MSTCODE_IDLE: /* Master idle */
  232. /* Idle state is only called on completion of transfer */
  233. /* Disable interrupts */
  234. pDrv->base->INTENCLR = (I2C_INTENSET_MSTPENDING | I2C_INTENSET_MSTRARBLOSS |
  235. I2C_INTENSET_MSTSTSTPERR);
  236. /* Update status and call transfer completion callback */
  237. pXfer->status = pDrv->pendingStatus;
  238. if (pDrv->pXferCompCB != NULL) {
  239. pDrv->pXferCompCB(pHandle, pXfer);
  240. }
  241. break;
  242. case I2C_STAT_MSTCODE_RXREADY: /* Receive data is available */
  243. if (((pXfer->flags & ROM_I2CM_FLAG_DMARX) != 0) && (pXfer->rxSz > 0)) {
  244. /* Use DMA for receive */
  245. pDrv->base->MSTCTL = I2C_MSTCTL_MSTDMA;
  246. pXfer->flags &= ~ROM_I2CM_FLAG_DMARX;
  247. pXfer->rxSz = 0;
  248. return;
  249. }
  250. else if (pXfer->rxSz) {
  251. uint8_t *p8 = pXfer->rxBuff;
  252. p8[pDrv->recvIdx] = (uint8_t) pDrv->base->MSTDAT & 0xFF;
  253. pDrv->recvIdx++;
  254. pXfer->rxSz--;
  255. }
  256. if (pXfer->rxSz) {
  257. pDrv->base->MSTCTL = I2C_MSTCTL_MSTCONTINUE;
  258. }
  259. else {
  260. /* Last byte to receive, send stop after byte received */
  261. pDrv->base->MSTCTL = I2C_MSTCTL_MSTCONTINUE | I2C_MSTCTL_MSTSTOP;
  262. pDrv->pendingStatus = LPC_OK;
  263. }
  264. break;
  265. case I2C_STAT_MSTCODE_TXREADY: /* Master Transmit available */
  266. if (((pXfer->flags & ROM_I2CM_FLAG_DMATX) != 0) && (pXfer->txSz > 0)) {
  267. /* Use DMA for transmit */
  268. pDrv->base->MSTCTL = I2C_MSTCTL_MSTDMA;
  269. pXfer->flags &= ~ROM_I2CM_FLAG_DMATX;
  270. pXfer->txSz = 0;
  271. return;
  272. }
  273. else if (pXfer->txSz) {
  274. uint8_t *p8 = (uint8_t *) pXfer->txBuff;
  275. /* If Tx data available transmit data and continue */
  276. pDrv->base->MSTDAT = (uint32_t) p8[pDrv->sendIdx];
  277. pDrv->base->MSTCTL = I2C_MSTCTL_MSTCONTINUE;
  278. pDrv->sendIdx++;
  279. pXfer->txSz--;
  280. }
  281. else if (pXfer->rxSz == 0) {
  282. pDrv->base->MSTCTL = I2C_MSTCTL_MSTSTOP;
  283. pDrv->pendingStatus = LPC_OK;
  284. }
  285. else {
  286. /* Start receive state with repeat start */
  287. pDrv->base->MSTDAT = (uint32_t) (pXfer->slaveAddr << 1) | 0x1;
  288. pDrv->base->MSTCTL = I2C_MSTCTL_MSTSTART;
  289. /* Call receive start callback to setup RX DMA if needed */
  290. if (pDrv->pTranRecvCb) {
  291. pDrv->pTranRecvCb(pHandle, pXfer);
  292. }
  293. }
  294. break;
  295. case I2C_STAT_MSTCODE_NACKADR: /* Slave address was NACK'ed */
  296. /* Set transfer status as NACK on address */
  297. pDrv->pendingStatus = ERR_I2C_SLAVE_NOT_ADDRESSED;
  298. pDrv->base->MSTCTL = I2C_MSTCTL_MSTSTOP;
  299. break;
  300. case I2C_STAT_MSTCODE_NACKDAT: /* Slave data was NACK'ed */
  301. /* Set transfer status as NACK on data */
  302. pDrv->pendingStatus = ERR_I2C_NAK;
  303. pDrv->base->MSTCTL = I2C_MSTCTL_MSTSTOP;
  304. break;
  305. default:
  306. /* Illegal I2C master state machine case. This should never happen.
  307. Disable and re-enable controller to clear state machine */
  308. pDrv->pendingStatus = ERR_I2C_GENERAL_FAILURE;
  309. break;
  310. }
  311. }
  312. }
  313. uint32_t i2cm_get_driver_version(void)
  314. {
  315. return DRVVERSION;
  316. }
  317. // *********************************************************