Hierodule 1.6.2
Utility module set for STM32 MCUs
Loading...
Searching...
No Matches
hierodule_adc.c
Go to the documentation of this file.
1
12#include <hierodule_adc.h>
13
28
33#ifdef __STM32F103xB_H
36#endif
46#ifdef HIERODULE_ADC_SMOOTHENING_FILTER
48{
49 Wrapper->Data = (Wrapper->Data*Wrapper->FilterWeight) +
50 (Wrapper->_ADC->DR*(1.0-Wrapper->FilterWeight));
51}
53#endif
68 void (*ISR)(uint16_t))
69{
71
72 uint32_t ADC_Address = (uint32_t)_ADC;
73 switch(ADC_Address)
74 {
75 case ( (uint32_t)ADC1 ):
77 break;
79 #ifdef __STM32F103xB_H
80 case ( (uint32_t)ADC2 ):
82 break;
84 #endif
85 default:
86 return NULL;
87 break;
88 }
89
90 (*Wrapper) = (HIERODULE_ADC_Wrapper*)malloc(sizeof(HIERODULE_ADC_Wrapper));
91
92 (*Wrapper)->_ADC = _ADC;
93
94 (*Wrapper)->Data = 0;
96 #ifdef HIERODULE_ADC_SMOOTHENING_FILTER
97 (*Wrapper)->FilterWeight = 0.5;
99 #endif
101 (*Wrapper)->Data_Handler = ISR;
102
103 return Wrapper;
104}
105
115
119{
121 #if ( (defined __STM32F103xB_H) || (defined __STM32F401xC_H) )
122 SET_BIT(Wrapper->_ADC->CR1, ADC_CR1_EOCIE);
123 CLEAR_BIT(Wrapper->_ADC->SR, ADC_SR_EOC);
124 SET_BIT(Wrapper->_ADC->CR2, ADC_CR2_ADON);
125
127 #ifdef __STM32F103xB_H
128 SET_BIT(Wrapper->_ADC->CR2, (ADC_CR2_SWSTART | ADC_CR2_EXTTRIG));
130 #elif defined __STM32F401xC_H
131 SET_BIT(Wrapper->_ADC->CR2, ADC_CR2_SWSTART);
133 #endif
134
135 #elif defined __STM32F030x6_H
136 SET_BIT(Wrapper->_ADC->IER, ADC_IER_EOCIE);
137 CLEAR_BIT(Wrapper->_ADC->ISR, ADC_ISR_EOC);
138 SET_BIT(Wrapper->_ADC->CR, ADC_CR_ADEN);
139 SET_BIT(Wrapper->_ADC->CR, ADC_CR_ADSTART);
141 #endif
142}
143
147{
149 #if ( (defined __STM32F103xB_H) || (defined __STM32F401xC_H) )
150 CLEAR_BIT(Wrapper->_ADC->CR1, ADC_CR1_EOCIE);
151 CLEAR_BIT(Wrapper->_ADC->CR2, ADC_CR2_ADON);
152
154 #ifdef __STM32F103xB_H
155 CLEAR_BIT(Wrapper->_ADC->CR2, (ADC_CR2_SWSTART | ADC_CR2_EXTTRIG));
157 #elif defined __STM32F401xC_H
158 CLEAR_BIT(Wrapper->_ADC->CR2, ADC_CR2_SWSTART);
160 #endif
161
162 #elif defined __STM32F030x6_H
163 SET_BIT(Wrapper->_ADC->CR, ADC_CR_ADDIS);
164 SET_BIT(Wrapper->_ADC->CR, ADC_CR_ADSTP);
166 #endif
167}
168
175#ifdef __STM32F103xB_H
176extern void ADC1_2_IRQHandler(void)
177{
178 if(ADC1_Wrapper != NULL)
179 {
180 if (READ_BIT(ADC1_Wrapper->_ADC->CR1, ADC_CR1_EOCIE) == (ADC_CR1_EOCIE))
181 {
182 if (READ_BIT(ADC1_Wrapper->_ADC->SR, ADC_SR_EOC) == (ADC_SR_EOC))
183 {
185 #ifdef HIERODULE_ADC_SMOOTHENING_FILTER
188 #else
191 #endif
193 if(ADC1_Wrapper->Data_Handler != NULL)
194 {
196 }
197 }
198 }
199 }
200
201 if(ADC2_Wrapper != NULL)
202 {
203 if (READ_BIT(ADC2_Wrapper->_ADC->CR1, ADC_CR1_EOCIE) == (ADC_CR1_EOCIE))
204 {
205 if (READ_BIT(ADC2_Wrapper->_ADC->SR, ADC_SR_EOC) == (ADC_SR_EOC))
206 {
208 #ifdef HIERODULE_ADC_SMOOTHENING_FILTER
211 #else
214 #endif
216 if(ADC2_Wrapper->Data_Handler != NULL)
217 {
219 }
220 }
221 }
222 }
223}
225#endif
226
227#ifdef __STM32F401xC_H
233extern void ADC_IRQHandler(void)
234{
235 if(ADC1_Wrapper != NULL)
236 {
237 if (READ_BIT(ADC1_Wrapper->_ADC->CR1, ADC_CR1_EOCIE) == (ADC_CR1_EOCIE))
238 {
239 if (READ_BIT(ADC1_Wrapper->_ADC->SR, ADC_SR_EOC) == (ADC_SR_EOC))
240 {
242 #ifdef HIERODULE_ADC_SMOOTHENING_FILTER
245 #else
248 #endif
250 if(ADC1_Wrapper->Data_Handler != NULL)
251 {
253 }
254 }
255 }
256 }
257}
259#endif
260
261#ifdef __STM32F030x6_H
267extern void ADC1_IRQHandler(void)
268{
269 if(ADC1_Wrapper != NULL)
270 {
271 if (READ_BIT(ADC1_Wrapper->_ADC->IER, ADC_IER_EOCIE) == (ADC_IER_EOCIE))
272 {
273 if (READ_BIT(ADC1_Wrapper->_ADC->ISR, ADC_ISR_EOC) == (ADC_ISR_EOC))
274 {
276 #ifdef HIERODULE_ADC_SMOOTHENING_FILTER
279 #else
282 #endif
284 if(ADC1_Wrapper->Data_Handler != NULL)
285 {
287 }
288 }
289 }
290 }
291}
293#endif
void Smoothen(HIERODULE_ADC_Wrapper *Wrapper)
Applies a cumulative-smoothening filter before updating the wrapper data field.
static HIERODULE_ADC_Wrapper * ADC2_Wrapper
ADC wrapper pointer meant for ADC2. Requires the device specific macro __STM32F103xB_H to be defined.
static HIERODULE_ADC_Wrapper * ADC1_Wrapper
ADC wrapper pointer meant for ADC1.
void ADC_IRQHandler(void)
ADC IRQ implementation. Requires the device specific macro __STM32F401xC_H to be defined.
HIERODULE_ADC_Wrapper ** HIERODULE_ADC_InitWrapper(ADC_TypeDef *_ADC, void(*ISR)(uint16_t))
Initializes a wrapper for the specified ADC peripheral.
void ADC1_2_IRQHandler(void)
ADC1_2 IRQ implementation. Requires the device specific macro __STM32F103xB_H to be defined.
void HIERODULE_ADC_Enable(HIERODULE_ADC_Wrapper *Wrapper)
Updates relevant control register bits to start ADC action.
void ADC1_IRQHandler(void)
ADC1 IRQ implementation. Requires the device specific macro __STM32F030x6_H to be defined.
void HIERODULE_ADC_ReleaseWrapper(HIERODULE_ADC_Wrapper *Wrapper)
Frees the memory allocated to an ADC wrapper.
void HIERODULE_ADC_Disable(HIERODULE_ADC_Wrapper *Wrapper)
Updates relevant control register bits to stop ADC action.
HIERODULE_USB_Wrapper Wrapper
Extern declaration for the wrapper instance in the source file.
: Header file for the ADC module.
Struct that keeps variables for the ADC data, a pointer to the ADC peripheral and a pointer to the IS...
ADC_TypeDef * _ADC
Pointer to the ADC peripheral.
void(* Data_Handler)(uint16_t)
Pointer to the ISR for EOC.
uint16_t Data
Variable to assign the received ADC data.