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