Koduino
adc.h
1 
20 #ifndef _adc_h_
21 #define _adc_h_
22 
23 #include "chip.h"
24 #include "types.h"
25 
26 #ifdef __cplusplus
27 extern "C"{
28 #endif // __cplusplus
29 
30 
37 #define ADC_TO_FLOAT(i) ((i)/((float)(1<<12)))
38 
39 // Should be called by variant init once for each ADC
40 
41 // Only does something for F4, not F3 std periph libs
42 void adcCommonInit();
43 // Should be called once per ADC
44 void adcInit(ADC_TypeDef *ADCx);
45 
46 
58 void analogReadSampleTime(uint8_t sampleTime);
59 
60 
68 uint16_t analogRead(uint8_t pin);
69 
70 
88 // const uint16_t *analogSyncRead(uint8_t pin1, uint8_t pin2, uint8_t pin3);
89 // const uint16_t *analogSyncRead2(uint8_t pin1, uint8_t pin2);
90  // end of Analog
92 
93 
94 // Init DMA for ADC
95 //
96 // numChannels must be smaller than or equal to 9 (ADC1 has 9 channels on the chip)
97 // void dmaInit(DMA_Channel_TypeDef *dmaYChannelX, uint16_t bufferSize, uint32_t periphalBaseAddr, uint32_t memoryBaseAddr);
98 
99 // typedef enum ADCTriggerType {
100 // CONTINUOUS, SOFTWARE_START
101 // } ADCTriggerType;
102 
103 
104 // Initialize ADC channels
105 // @numChannels number of ADC conversions needed
106 // @pins Array of pins of size `numChannels`
107 //
108 // This function is a departure from the Arduino API and must be called at the moment.
109 //
110 // *NOTE:* This API may change to be more Arduino-compatible.
111 //
112 // *FIXME:* Make it unnecessary to call adcInit Idea: just like with the timer system, create a data structure of all 9 or so ADC's, and calling analogRead or analogReadBatch once will add that ADC pin to the list of conversions?
113 // void adcInit(int numChannels, const uint8_t *pins);
114 
115 // Read sequentially
116 // @numChannels Number of channels
117 // @pins Array of size numChannels, to convert sequentially
118 // @out Array to store converted values in
119 //
120 // This function performs *numChannels* ADC conversions sequentially.
121 //
122 // The STM32 only has 1 ADC, but with several channels, and consequently, triggering a regular conversion converts all the channels. So, it is more efficient to do the conversion in batch (taking O(1) time) rather than sequentially (taking O(n) time).
123 //
124 // To use,
125 //
126 // * Call <adcSetMode> (or defaults to CONTINUOUS)
127 // * Call <adcInit> with all the pins required
128 // * Call <analogReadBatch> to sequentially sample all the inited ADC channels
129 // void analogReadBatch(int numChannels, const uint8_t *pins, uint16_t *out);
130 
131 
132 // Set ADC trigger type
133 // @mode CONTINUOUS (**default**) or SOFTWARE_START
134 //
135 // Set the ADC trigger type.
136 //
137 // * Must be called before <adcInit>
138 // * Calling <analogRead> or <analogReadBatch> should be faster in CONTINUOUS conversion mode, but if precise timing (or Arduino-like behavior) is required, select SOFTWARE_START. Then, the conversion is triggered by <analogRead> or <analogReadBatch>.
139 // void adcSetMode(ADCTriggerType mode);
140 
141 
142 #ifdef __cplusplus
143 } // extern "C"
144 #endif // __cplusplus
145 
146 #endif
147 
uint16_t analogRead(uint8_t pin)
Read a single pin.
Definition: adc.c:131
void analogReadSampleTime(uint8_t sampleTime)
Set ADC sample time.
Definition: adc.c:37