31 #if defined(PIOS_INCLUDE_ADC)
35 static int32_t PIOS_INTERNAL_ADC_PinGet(uintptr_t internal_adc_id, uint32_t
pin);
36 static uint8_t PIOS_INTERNAL_ADC_NumberOfChannels(uintptr_t internal_adc_id);
37 static float PIOS_INTERNAL_ADC_LSB_Voltage(uintptr_t internal_adc_id);
40 .
get_pin = PIOS_INTERNAL_ADC_PinGet,
41 .number_of_channels = PIOS_INTERNAL_ADC_NumberOfChannels,
42 .lsb_voltage = PIOS_INTERNAL_ADC_LSB_Voltage,
45 #define MAX_CHANNELS 8
47 struct pios_internal_adc_dev {
50 uint8_t conversion_started : 1;
51 uint8_t current_pin : 3;
53 volatile uint16_t pin_values[MAX_CHANNELS];
86 if (!PIOS_INTERNAL_ADC_validate(adc_dev)) {
90 GPIO_InitTypeDef GPIO_InitStructure;
91 GPIO_StructInit(&GPIO_InitStructure);
92 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
93 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
95 for (int32_t
i = 0;
i < adc_dev->cfg->adc_pin_count;
i++) {
96 adc_dev->pin_values[
i] = 0xffff;
98 if (adc_dev->cfg->adc_pins[
i].port == NULL )
100 GPIO_InitStructure.GPIO_Pin = adc_dev->cfg->adc_pins[
i].pin;
101 GPIO_Init(adc_dev->cfg->adc_pins[
i].port, (GPIO_InitTypeDef*) &GPIO_InitStructure);
111 ADC_DeInit(adc_dev->cfg->adc_dev);
114 ADC1->CR |= ADC_CR_ADCAL;
115 while ((ADC1->CR & ADC_CR_ADCAL) != 0);
117 ADC_InitTypeDef ADC_InitStructure;
118 ADC_StructInit(&ADC_InitStructure);
120 ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
121 ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
122 ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
123 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Left;
125 ADC_Init(adc_dev->cfg->adc_dev, &ADC_InitStructure);
127 ADC_ClockModeConfig(adc_dev->cfg->adc_dev, ADC_ClockMode_AsynClk);
128 ADC_ContinuousModeCmd(adc_dev->cfg->adc_dev, DISABLE);
129 ADC_DiscModeCmd(adc_dev->cfg->adc_dev, ENABLE);
132 ADC_TempSensorCmd(ENABLE);
133 ADC_VrefintCmd(ENABLE);
135 ADC_Cmd(adc_dev->cfg->adc_dev, ENABLE);
137 while (!ADC_GetFlagStatus(adc_dev->cfg->adc_dev, ADC_FLAG_ADRDY));
150 adc_dev = PIOS_INTERNAL_ADC_Allocate();
151 if (adc_dev == NULL )
157 adc_dev->current_pin = 0;
159 PIOS_INTERNAL_ADC_PinConfig(adc_dev);
161 PIOS_INTERNAL_ADC_Converter_Config(adc_dev);
167 if (!adc_dev->conversion_started) {
169 ADC_GetConversionValue(adc_dev->cfg->adc_dev);
172 adc_dev->cfg->adc_dev->CHSELR = adc_dev->cfg->adc_pins[adc_dev->current_pin].adc_channel;
173 adc_dev->cfg->adc_dev->SMPR = ADC_SampleTime_41_5Cycles;
177 ADC_StartOfConversion(adc_dev->cfg->adc_dev);
179 adc_dev->conversion_started = 1;
181 if (ADC_GetFlagStatus(adc_dev->cfg->adc_dev,
182 ADC_FLAG_ADSTART) == RESET) {
183 adc_dev->pin_values[adc_dev->current_pin] =
184 ADC_GetConversionValue(adc_dev->cfg->adc_dev);
186 adc_dev->current_pin++;
188 if (adc_dev->current_pin > adc_dev->cfg->adc_pin_count) {
189 adc_dev->current_pin = 0;
192 adc_dev->conversion_started = 0;
202 static uint8_t PIOS_INTERNAL_ADC_NumberOfChannels(uintptr_t internal_adc_id)
205 if (!PIOS_INTERNAL_ADC_validate(adc_dev))
207 return adc_dev->cfg->adc_pin_count;
216 static int32_t PIOS_INTERNAL_ADC_PinGet(uintptr_t internal_adc_id, uint32_t pin)
221 if (pin >= adc_dev->cfg->adc_pin_count) {
225 return adc_dev->pin_values[pin];
231 static float PIOS_INTERNAL_ADC_LSB_Voltage(uintptr_t internal_adc_id)
234 if (!PIOS_INTERNAL_ADC_validate(adc_dev)) {
240 return VREF_PLUS / (((uint32_t)1 << 16) - 16);
int32_t PIOS_INTERNAL_ADC_Init(uintptr_t *internal_adc_id, const struct pios_internal_adc_cfg *cfg)
void PIOS_INTERNAL_ADC_DoStep(pios_internal_adc_t internal_adc_id)
#define PIOS_DEBUG_Assert(test)
void * PIOS_malloc(size_t size)
struct pios_internal_adc_dev * pios_internal_adc_t
static struct flyingpicmd_cfg_fa cfg
const struct pios_adc_driver pios_internal_adc_driver
#define PIOS_Assert(test)
int32_t(* get_pin)(uintptr_t id, uint32_t pin)