st25r3916_irq.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /******************************************************************************
  2. * \attention
  3. *
  4. * <h2><center>&copy; COPYRIGHT 2020 STMicroelectronics</center></h2>
  5. *
  6. * Licensed under ST MYLIBERTY SOFTWARE LICENSE AGREEMENT (the "License");
  7. * You may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at:
  9. *
  10. * www.st.com/myliberty
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
  15. * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. ******************************************************************************/
  21. /*
  22. * PROJECT: ST25R3916 firmware
  23. * Revision:
  24. * LANGUAGE: ISO C99
  25. */
  26. /*! \file
  27. *
  28. * \author Gustavo Patricio
  29. *
  30. * \brief ST25R3916 Interrupt handling
  31. *
  32. */
  33. /*
  34. ******************************************************************************
  35. * INCLUDES
  36. ******************************************************************************
  37. */
  38. #include "st25r3916_irq.h"
  39. #include "st25r3916_com.h"
  40. #include "st25r3916_led.h"
  41. #include "st25r3916.h"
  42. #include "utils.h"
  43. /*
  44. ******************************************************************************
  45. * LOCAL DATA TYPES
  46. ******************************************************************************
  47. */
  48. /*! Holds current and previous interrupt callback pointer as well as current Interrupt status and mask */
  49. typedef struct {
  50. void (*prevCallback)(void); /*!< call back function for ST25R3916 interrupt */
  51. void (*callback)(void); /*!< call back function for ST25R3916 interrupt */
  52. uint32_t status; /*!< latest interrupt status */
  53. uint32_t mask; /*!< Interrupt mask. Negative mask = ST25R3916 mask regs */
  54. } st25r3916Interrupt;
  55. /*
  56. ******************************************************************************
  57. * GLOBAL DEFINES
  58. ******************************************************************************
  59. */
  60. /*! Length of the interrupt registers */
  61. #define ST25R3916_INT_REGS_LEN ((ST25R3916_REG_IRQ_TARGET - ST25R3916_REG_IRQ_MAIN) + 1U)
  62. /*
  63. ******************************************************************************
  64. * GLOBAL VARIABLES
  65. ******************************************************************************
  66. */
  67. static volatile st25r3916Interrupt st25r3916interrupt; /*!< Instance of ST25R3916 interrupt */
  68. /*
  69. ******************************************************************************
  70. * GLOBAL FUNCTIONS
  71. ******************************************************************************
  72. */
  73. void st25r3916InitInterrupts(void) {
  74. platformIrqST25RPinInitialize();
  75. platformIrqST25RSetCallback(st25r3916Isr);
  76. st25r3916interrupt.callback = NULL;
  77. st25r3916interrupt.prevCallback = NULL;
  78. st25r3916interrupt.status = ST25R3916_IRQ_MASK_NONE;
  79. st25r3916interrupt.mask = ST25R3916_IRQ_MASK_NONE;
  80. }
  81. /*******************************************************************************/
  82. void st25r3916Isr(void) {
  83. st25r3916CheckForReceivedInterrupts();
  84. // Check if callback is set and run it
  85. if(NULL != st25r3916interrupt.callback) {
  86. st25r3916interrupt.callback();
  87. }
  88. }
  89. /*******************************************************************************/
  90. void st25r3916CheckForReceivedInterrupts(void) {
  91. uint8_t iregs[ST25R3916_INT_REGS_LEN];
  92. uint32_t irqStatus;
  93. /* Initialize iregs */
  94. irqStatus = ST25R3916_IRQ_MASK_NONE;
  95. ST_MEMSET(iregs, (int32_t)(ST25R3916_IRQ_MASK_ALL & 0xFFU), ST25R3916_INT_REGS_LEN);
  96. /* In case the IRQ is Edge (not Level) triggered read IRQs until done */
  97. while(platformGpioIsHigh(ST25R_INT_PORT, ST25R_INT_PIN)) {
  98. st25r3916ReadMultipleRegisters(ST25R3916_REG_IRQ_MAIN, iregs, ST25R3916_INT_REGS_LEN);
  99. irqStatus |= (uint32_t)iregs[0];
  100. irqStatus |= (uint32_t)iregs[1] << 8;
  101. irqStatus |= (uint32_t)iregs[2] << 16;
  102. irqStatus |= (uint32_t)iregs[3] << 24;
  103. }
  104. /* Forward all interrupts, even masked ones to application */
  105. platformProtectST25RIrqStatus();
  106. st25r3916interrupt.status |= irqStatus;
  107. platformUnprotectST25RIrqStatus();
  108. /* Send an IRQ event to LED handling */
  109. st25r3916ledEvtIrq(st25r3916interrupt.status);
  110. }
  111. /*******************************************************************************/
  112. void st25r3916ModifyInterrupts(uint32_t clr_mask, uint32_t set_mask) {
  113. uint8_t i;
  114. uint32_t old_mask;
  115. uint32_t new_mask;
  116. old_mask = st25r3916interrupt.mask;
  117. new_mask = ((~old_mask & set_mask) | (old_mask & clr_mask));
  118. st25r3916interrupt.mask &= ~clr_mask;
  119. st25r3916interrupt.mask |= set_mask;
  120. for(i = 0; i < ST25R3916_INT_REGS_LEN; i++) {
  121. if(((new_mask >> (8U * i)) & 0xFFU) == 0U) {
  122. continue;
  123. }
  124. st25r3916WriteRegister(
  125. ST25R3916_REG_IRQ_MASK_MAIN + i,
  126. (uint8_t)((st25r3916interrupt.mask >> (8U * i)) & 0xFFU));
  127. }
  128. return;
  129. }
  130. /*******************************************************************************/
  131. uint32_t st25r3916WaitForInterruptsTimed(uint32_t mask, uint16_t tmo) {
  132. uint32_t tmrDelay;
  133. uint32_t status;
  134. tmrDelay = platformTimerCreate(tmo);
  135. /* Run until specific interrupt has happen or the timer has expired */
  136. do {
  137. status = (st25r3916interrupt.status & mask);
  138. } while((!platformTimerIsExpired(tmrDelay) || (tmo == 0U)) && (status == 0U));
  139. platformTimerDestroy(tmrDelay);
  140. status = st25r3916interrupt.status & mask;
  141. platformProtectST25RIrqStatus();
  142. st25r3916interrupt.status &= ~status;
  143. platformUnprotectST25RIrqStatus();
  144. return status;
  145. }
  146. /*******************************************************************************/
  147. uint32_t st25r3916GetInterrupt(uint32_t mask) {
  148. uint32_t irqs;
  149. irqs = (st25r3916interrupt.status & mask);
  150. if(irqs != ST25R3916_IRQ_MASK_NONE) {
  151. platformProtectST25RIrqStatus();
  152. st25r3916interrupt.status &= ~irqs;
  153. platformUnprotectST25RIrqStatus();
  154. }
  155. return irqs;
  156. }
  157. /*******************************************************************************/
  158. void st25r3916ClearAndEnableInterrupts(uint32_t mask) {
  159. st25r3916GetInterrupt(mask);
  160. st25r3916EnableInterrupts(mask);
  161. }
  162. /*******************************************************************************/
  163. void st25r3916EnableInterrupts(uint32_t mask) {
  164. st25r3916ModifyInterrupts(mask, 0);
  165. }
  166. /*******************************************************************************/
  167. void st25r3916DisableInterrupts(uint32_t mask) {
  168. st25r3916ModifyInterrupts(0, mask);
  169. }
  170. /*******************************************************************************/
  171. void st25r3916ClearInterrupts(void) {
  172. uint8_t iregs[ST25R3916_INT_REGS_LEN];
  173. st25r3916ReadMultipleRegisters(ST25R3916_REG_IRQ_MAIN, iregs, ST25R3916_INT_REGS_LEN);
  174. platformProtectST25RIrqStatus();
  175. st25r3916interrupt.status = ST25R3916_IRQ_MASK_NONE;
  176. platformUnprotectST25RIrqStatus();
  177. return;
  178. }
  179. /*******************************************************************************/
  180. void st25r3916IRQCallbackSet(void (*cb)(void)) {
  181. st25r3916interrupt.prevCallback = st25r3916interrupt.callback;
  182. st25r3916interrupt.callback = cb;
  183. }
  184. /*******************************************************************************/
  185. void st25r3916IRQCallbackRestore(void) {
  186. st25r3916interrupt.callback = st25r3916interrupt.prevCallback;
  187. st25r3916interrupt.prevCallback = NULL;
  188. }