acmp.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /******************************************************************************
  2. *
  3. * @brief providing APIs for configuring ACMP.
  4. *
  5. *******************************************************************************
  6. *
  7. * provide APIs for configuring ACMP
  8. ******************************************************************************/
  9. #include "common.h"
  10. #include "acmp.h"
  11. /******************************************************************************
  12. * Global variables
  13. ******************************************************************************/
  14. /******************************************************************************
  15. * Constants and macros
  16. ******************************************************************************/
  17. /******************************************************************************
  18. * Local types
  19. ******************************************************************************/
  20. /******************************************************************************
  21. * Local function prototypes
  22. ******************************************************************************/
  23. /******************************************************************************
  24. * Local variables
  25. ******************************************************************************/
  26. ACMP_CallbackPtr ACMP_Callback[2] = {(ACMP_CallbackPtr)NULL};
  27. /******************************************************************************
  28. * Local functions
  29. ******************************************************************************/
  30. /******************************************************************************
  31. * Global functions
  32. ******************************************************************************/
  33. void ACMP0_Isr(void);
  34. void ACMP1_Isr(void);
  35. /******************************************************************************
  36. * ACMP api list.
  37. *
  38. *//*! @addtogroup acmp_api_list
  39. * @{
  40. *******************************************************************************/
  41. /*****************************************************************************//*!
  42. *
  43. * @brief initialize ACMP as per control field.
  44. *
  45. * @param pACMPx pointer to an ACMP register base.
  46. * @param pConfig control parameters.
  47. *
  48. * @return none.
  49. *
  50. * @ Pass/ Fail criteria: none.
  51. *
  52. * @see ACMP_DeInit.
  53. *
  54. *****************************************************************************/
  55. void ACMP_Init(ACMP_Type *pACMPx, ACMP_ConfigType *pConfig)
  56. {
  57. if(ACMP0 == pACMPx)
  58. {
  59. /* enable clock to ACMP */
  60. SIM->SCGC |= SIM_SCGC_ACMP0_MASK;
  61. /* enable ACMP interrupt */
  62. if(pConfig->sCtrlStatus.bits.bIntEn)
  63. NVIC_EnableIRQ(ACMP0_IRQn);
  64. }
  65. else
  66. {
  67. SIM->SCGC |= SIM_SCGC_ACMP1_MASK;
  68. if(pConfig->sCtrlStatus.bits.bIntEn)
  69. NVIC_EnableIRQ(ACMP1_IRQn);
  70. }
  71. /* neg and pos pin are not equal */
  72. pACMPx->C0 = pConfig->sPinSelect.byte;
  73. ACMP_ConfigDAC(pACMPx, &pConfig->sDacSet );
  74. //pACMPx->C1 = pConfig->sDacSet.byte;
  75. pACMPx->C2 = pConfig->sPinEnable.byte;
  76. pACMPx->CS = pConfig->sCtrlStatus.byte;
  77. }
  78. /*****************************************************************************//*!
  79. *
  80. * @brief write ACMP register bits.
  81. *
  82. * @param pACMPx pointer to an ACMP register base.
  83. * @param pDACConfig pointer to an ACMP DAC control structure.
  84. *
  85. * @return none.
  86. *
  87. * @ Pass/ Fail criteria: none.
  88. *
  89. *****************************************************************************/
  90. void ACMP_ConfigDAC(ACMP_Type *pACMPx, ACMP_DACType *pDACConfig)
  91. {
  92. pACMPx->C1 = pDACConfig->byte;
  93. }
  94. /*****************************************************************************//*!
  95. *
  96. * @brief deinit ACMP module.
  97. *
  98. * @param pACMPx pointer to an ACMP register base.
  99. *
  100. * @return none.
  101. *
  102. * @ Pass/ Fail criteria: none.
  103. *
  104. * @see ACMP_Init.
  105. *
  106. *****************************************************************************/
  107. void ACMP_DeInit(ACMP_Type *pACMPx)
  108. {
  109. if(ACMP0 == pACMPx)
  110. {
  111. if(pACMPx->CS & ACMP_CS_ACIE_MASK)
  112. NVIC_DisableIRQ(ACMP0_IRQn);
  113. }
  114. else
  115. {
  116. if(pACMPx->CS & ACMP_CS_ACIE_MASK)
  117. NVIC_DisableIRQ(ACMP1_IRQn);
  118. }
  119. pACMPx->CS = 0;
  120. pACMPx->C0 = 0;
  121. pACMPx->C1 = 0;
  122. pACMPx->C2 = 0;
  123. if(ACMP0 == pACMPx)
  124. {
  125. SIM->SCGC &= ~SIM_SCGC_ACMP0_MASK;
  126. }
  127. else
  128. {
  129. SIM->SCGC &= ~SIM_SCGC_ACMP1_MASK;
  130. }
  131. }
  132. /*****************************************************************************//*!
  133. *
  134. * @brief set up ACMP callback routines to be called by interrupt service routine.
  135. *
  136. * @param pACMPx pointer to an ACMP register base.
  137. * @param pfnCallback callback routine.
  138. *
  139. * @return none.
  140. *
  141. * @ Pass/ Fail criteria: none.
  142. *
  143. *****************************************************************************/
  144. void ACMP_SetCallback(ACMP_Type *pACMPx, ACMP_CallbackPtr pfnCallback)
  145. {
  146. if(ACMP0 == pACMPx)
  147. {
  148. ACMP_Callback[0] = pfnCallback;
  149. }
  150. else
  151. {
  152. ACMP_Callback[1] = pfnCallback;
  153. }
  154. }
  155. /*! @} End of acmp_api_list */
  156. /*****************************************************************************//*!
  157. *
  158. * @brief ACMP0 interrupt service routine.
  159. *
  160. * @param none.
  161. *
  162. * @return none.
  163. *
  164. * @ Pass/ Fail criteria: none.
  165. *
  166. *****************************************************************************/
  167. void ACMP0_Isr(void)
  168. {
  169. if(ACMP_Callback[0])
  170. {
  171. ACMP_Callback[0](); /* call callback routine */
  172. }
  173. }
  174. /*****************************************************************************//*!
  175. *
  176. * @brief ACMP1 interrupt service routine.
  177. *
  178. * @param none.
  179. *
  180. * @return none.
  181. *
  182. * @ Pass/ Fail criteria: none.
  183. *
  184. *****************************************************************************/
  185. void ACMP1_Isr(void)
  186. {
  187. if(ACMP_Callback[1])
  188. {
  189. ACMP_Callback[1](); /* call callback routine */
  190. }
  191. }