hw_i2cmond.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * @brief I2C monitor 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_i2cmond.h"
  34. #define DRVVERSION 0x0100
  35. /* Private data structure used for the I2C monitor 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. i2cMonCapReadyCB pCapCompCB; /*!< Capture complete callback */
  41. i2cMonSetupDMACB pDmaSetupCB; /*!< DMA setup callback */
  42. ROM_I2CMON_CAP_T *pCap; /*!< Pointer to current capture descriptor */
  43. ErrorCode_t pendingStatus; /*!< Pending monitor transfer status before clocking transfer */
  44. } I2CMON_DATACONTEXT_T;
  45. void i2cmon_transfer_handler(ROM_I2CMON_HANDLE_T pHandle)
  46. ;
  47. // **********************************************************
  48. uint32_t i2cmon_get_mem_size(void)
  49. {
  50. return sizeof(I2CMON_DATACONTEXT_T);
  51. }
  52. ROM_I2CMON_HANDLE_T i2cmon_init(void *mem, const ROM_I2CMON_INIT_T *pInit)
  53. {
  54. I2CMON_DATACONTEXT_T *pDrv;
  55. uint32_t reg;
  56. /* Verify alignment is at least 4 bytes */
  57. if (((uint32_t) mem & 0x3) != 0) {
  58. return NULL;
  59. }
  60. pDrv = (I2CMON_DATACONTEXT_T *) mem;
  61. memset(pDrv, 0, sizeof(I2CMON_DATACONTEXT_T));
  62. /* Save base of peripheral and pointer to user data */
  63. pDrv->pUserData = pInit->pUserData;
  64. pDrv->base = (LPC_I2C_T *) pInit->base;
  65. /* Clear pending monitor statuses */
  66. pDrv->base->STAT = (I2C_STAT_MONIDLE | I2C_STAT_MONOV);
  67. while ((pDrv->base->STAT & I2C_STAT_MONRDY) != 0) {
  68. /* Toss input data */
  69. reg = pDrv->base->MONRXDAT;
  70. }
  71. /* Enable I2C monitor interface */
  72. reg = pDrv->base->CFG | I2C_CFG_MONEN;
  73. if (pInit->stretch != 0) {
  74. reg |= I2C_CFG_MONCLKSTR;
  75. }
  76. pDrv->base->CFG = reg;
  77. return pDrv;
  78. }
  79. void i2cmom_register_callback(ROM_I2CMON_HANDLE_T pHandle, uint32_t cbIndex, void *pCB)
  80. {
  81. I2CMON_DATACONTEXT_T *pDrv = (I2CMON_DATACONTEXT_T *) pHandle;
  82. if (cbIndex == ROM_I2CMON_CAPTUREREADY_CB) {
  83. pDrv->pCapCompCB = (i2cMonCapReadyCB) pCB;
  84. }
  85. else if (cbIndex == ROM_I2CMON_DMASETUP_CB) {
  86. pDrv->pDmaSetupCB = (i2cMonSetupDMACB) pCB;
  87. }
  88. }
  89. ErrorCode_t i2cmom_start_log(ROM_I2CMON_HANDLE_T pHandle, ROM_I2CMON_CAP_T *pCap)
  90. {
  91. I2CMON_DATACONTEXT_T *pDrv = (I2CMON_DATACONTEXT_T *) pHandle;
  92. /* I2C master controller should be pending and idle */
  93. if (pCap == NULL) {
  94. return ERR_I2C_PARAM;
  95. }
  96. /* Verify receive buffer alignment */
  97. if ((pCap->startBuff == NULL) || ((((uint32_t) pCap->startBuff) & 0x1) != 0) || (pCap->startBuffSz == 0)) {
  98. pCap->status = ERR_I2C_PARAM;
  99. return ERR_I2C_PARAM;
  100. }
  101. pDrv->pCap = pCap;
  102. pCap->capStartBuffSz = 0;
  103. pDrv->pendingStatus = LPC_OK;
  104. pCap->status = ERR_I2C_BUSY;
  105. if ((pCap->flags & ROM_I2CMON_FLAG_FLUSH) != 0) {
  106. while ((pDrv->base->STAT & I2C_STAT_MONRDY) != 0) {
  107. /* Toss input data */
  108. volatile uint32_t reg = pDrv->base->MONRXDAT;
  109. }
  110. }
  111. /* Clear controller state */
  112. pDrv->base->STAT = (I2C_STAT_MONIDLE | I2C_STAT_MONOV);
  113. if (((pCap->flags & ROM_I2CMON_FLAG_DMARX) != 0) && (pDrv->pDmaSetupCB)) {
  114. pDrv->pDmaSetupCB(pHandle, pCap);
  115. /* Enable supported monitor interrupts */
  116. pDrv->base->INTENSET = (I2C_INTENSET_MONOV | I2C_INTENSET_MONIDLE);
  117. }
  118. else {
  119. pCap->flags &= ~ROM_I2CMON_FLAG_DMARX;
  120. /* Enable supported monitor interrupts */
  121. pDrv->base->INTENSET = (I2C_INTENSET_MONRDY | I2C_INTENSET_MONOV | I2C_INTENSET_MONIDLE);
  122. }
  123. /* Is transfer blocking? */
  124. if ((pCap->flags & ROM_I2CMON_FLAG_BLOCKING) != 0) {
  125. while (pCap->status == ERR_I2C_BUSY) {
  126. i2cmon_transfer_handler(pHandle);
  127. }
  128. }
  129. return pCap->status;
  130. }
  131. // Otime = "optimize for speed of code execution"
  132. // ...add this pragma 1 line above the interrupt service routine function.
  133. void i2cmon_transfer_handler(ROM_I2CMON_HANDLE_T pHandle)
  134. {
  135. I2CMON_DATACONTEXT_T *pDrv = (I2CMON_DATACONTEXT_T *) pHandle;
  136. ROM_I2CMON_CAP_T *pCap = pDrv->pCap;
  137. uint16_t data = 0, *pData;
  138. uint32_t status = pDrv->base->STAT;
  139. if (status & I2C_STAT_MONOV) {
  140. /* Monitor data overflow */
  141. data = pDrv->base->MONRXDAT;
  142. pDrv->pendingStatus = ERR_I2C_BUFFER_OVERFLOW;
  143. /* Clear Status Flags */
  144. pDrv->base->STAT = I2C_STAT_MONOV;
  145. }
  146. else if (status & I2C_STAT_MONRDY) {
  147. /* Monitor ready */
  148. data = pDrv->base->MONRXDAT;
  149. /* Enough room to place this data? */
  150. if (pCap->capStartBuffSz >= pCap->startBuffSz) {
  151. /* Data overflow */
  152. pDrv->pendingStatus = ERR_I2C_BUFFER_OVERFLOW;
  153. }
  154. else {
  155. pData = (uint16_t *) pCap->startBuff;
  156. pData[pCap->capStartBuffSz] = data;
  157. pCap->capStartBuffSz++;
  158. }
  159. }
  160. /* Capture complete? */
  161. if ((status & I2C_INTSTAT_MONIDLE) != 0) {
  162. pDrv->base->INTENCLR = (I2C_INTENCLR_MONRDY | I2C_INTENCLR_MONOV |
  163. I2C_INTENCLR_MONIDLE);
  164. pCap->status = pDrv->pendingStatus;
  165. if (pDrv->pCapCompCB) {
  166. pDrv->pCapCompCB(pHandle, pCap);
  167. }
  168. }
  169. }
  170. uint32_t i2cmon_get_driver_version(void)
  171. {
  172. return DRVVERSION;
  173. }
  174. // *********************************************************