31 #if defined(PIOS_INCLUDE_ADC)
34 enum pios_internal_adc_dev_magic {
35 PIOS_INTERNAL_ADC_DEV_MAGIC = 0x58375124,
38 struct pios_internal_adc_dev {
39 enum pios_internal_adc_dev_magic
magic;
42 uint8_t number_used_master_channels;
43 uint8_t number_used_slave_channels;
44 uint8_t regular_group_size;
45 struct adc_accumulator **channel_map;
46 struct adc_accumulator *accumulator;
47 uint16_t dma_half_buffer_index;
48 uint16_t dma_transfer_size;
49 uint16_t *raw_data_buffer;
50 uint32_t accumulator_increment;
51 uint32_t accumulator_scan_size;
56 static void PIOS_INTERNAL_ADC_PinConfig(
struct pios_internal_adc_dev *adc_dev);
57 static void PIOS_INTERNAL_DMAConfig(
struct pios_internal_adc_dev *adc_dev);
58 static void PIOS_INTERNAL_ADC_Converter_Config(
59 struct pios_internal_adc_dev *adc_dev);
60 static int32_t PIOS_INTERNAL_ADC_PinGet(uintptr_t internal_adc_id, uint32_t pin);
61 static uint8_t PIOS_INTERNAL_ADC_NumberOfChannels(uintptr_t internal_adc_id);
62 static float PIOS_INTERNAL_ADC_LSB_Voltage(uintptr_t internal_adc_id);
65 .
get_pin = PIOS_INTERNAL_ADC_PinGet,
66 .number_of_channels = PIOS_INTERNAL_ADC_NumberOfChannels,
67 .lsb_voltage = PIOS_INTERNAL_ADC_LSB_Voltage,
70 struct adc_accumulator {
71 volatile uint32_t accumulator;
72 volatile uint32_t
count;
79 static bool PIOS_INTERNAL_ADC_validate(
struct pios_internal_adc_dev * dev)
84 return (dev->magic == PIOS_INTERNAL_ADC_DEV_MAGIC);
90 static struct pios_internal_adc_dev * PIOS_INTERNAL_ADC_Allocate()
92 struct pios_internal_adc_dev *adc_dev = (
struct pios_internal_adc_dev *)
PIOS_malloc(
sizeof(*adc_dev));
95 adc_dev->magic = PIOS_INTERNAL_ADC_DEV_MAGIC;
103 static void PIOS_INTERNAL_ADC_PinConfig(
struct pios_internal_adc_dev *adc_dev)
105 if (!PIOS_INTERNAL_ADC_validate(adc_dev)) {
109 GPIO_InitTypeDef GPIO_InitStructure;
110 GPIO_StructInit(&GPIO_InitStructure);
111 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
112 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
114 for (int32_t
i = 0;
i < adc_dev->cfg->adc_pin_count;
i++) {
115 if (adc_dev->cfg->adc_pins[
i].port == NULL )
117 GPIO_InitStructure.GPIO_Pin = adc_dev->cfg->adc_pins[
i].pin;
118 GPIO_Init(adc_dev->cfg->adc_pins[
i].port, (GPIO_InitTypeDef*) &GPIO_InitStructure);
125 static void PIOS_INTERNAL_DMAConfig(
struct pios_internal_adc_dev *adc_dev)
127 if (!PIOS_INTERNAL_ADC_validate(adc_dev)) {
131 DMA_ITConfig(adc_dev->cfg->dma.rx.channel, adc_dev->cfg->dma.irq.flags, DISABLE);
134 DMA_DeInit(adc_dev->cfg->dma.rx.channel);
135 DMA_InitTypeDef DMAInit = adc_dev->cfg->dma.rx.init;
136 if (adc_dev->cfg->adc_dev_slave) {
137 if (adc_dev->cfg->adc_dev_master == ADC1 )
138 DMAInit.DMA_PeripheralBaseAddr = (uint32_t) &ADC1_2->CDR;
140 DMAInit.DMA_PeripheralBaseAddr = (uint32_t) &ADC3_4->CDR;
143 DMAInit.DMA_PeripheralBaseAddr = (uint32_t) &adc_dev->cfg->adc_dev_master->DR;
145 DMAInit.DMA_MemoryBaseAddr = (uint32_t) adc_dev->raw_data_buffer;
146 DMAInit.DMA_BufferSize = adc_dev->dma_transfer_size;
147 DMAInit.DMA_DIR = DMA_DIR_PeripheralSRC;
148 DMAInit.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
149 DMAInit.DMA_MemoryInc = DMA_MemoryInc_Enable;
150 if (adc_dev->cfg->adc_dev_slave) {
151 DMAInit.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
152 DMAInit.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
154 DMAInit.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
155 DMAInit.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
157 DMAInit.DMA_Mode = DMA_Mode_Circular;
158 DMAInit.DMA_M2M = DMA_M2M_Disable;
160 DMA_Init(adc_dev->cfg->dma.rx.channel, &DMAInit);
163 DMA_Cmd(adc_dev->cfg->dma.rx.channel, ENABLE);
166 DMA_ITConfig(adc_dev->cfg->dma.rx.channel, DMA_IT_TC, ENABLE);
167 DMA_ITConfig(adc_dev->cfg->dma.rx.channel, DMA_IT_HT, ENABLE);
170 NVIC_InitTypeDef NVICInit = adc_dev->cfg->dma.irq.init;
171 NVIC_Init(&NVICInit);
178 static void PIOS_INTERNAL_ADC_Converter_Config(
179 struct pios_internal_adc_dev *adc_dev)
181 ADC_DeInit(adc_dev->cfg->adc_dev_master);
182 if (adc_dev->cfg->adc_dev_slave)
183 ADC_DeInit(adc_dev->cfg->adc_dev_slave);
185 if (adc_dev->cfg->adc_dev_master == ADC1 || adc_dev->cfg->adc_dev_master == ADC2 )
186 RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div32);
188 RCC_ADCCLKConfig(RCC_ADC34PLLCLK_Div32);
190 ADC_VoltageRegulatorCmd(adc_dev->cfg->adc_dev_master, ENABLE);
192 ADC_SelectCalibrationMode(adc_dev->cfg->adc_dev_master, ADC_CalibrationMode_Single);
193 ADC_StartCalibration(adc_dev->cfg->adc_dev_master);
194 while (ADC_GetCalibrationStatus(adc_dev->cfg->adc_dev_master) != RESET)
197 if (adc_dev->cfg->adc_dev_slave) {
198 ADC_VoltageRegulatorCmd(adc_dev->cfg->adc_dev_slave, ENABLE);
200 ADC_SelectCalibrationMode(adc_dev->cfg->adc_dev_slave, ADC_CalibrationMode_Single);
201 ADC_StartCalibration(adc_dev->cfg->adc_dev_slave);
202 while (ADC_GetCalibrationStatus(adc_dev->cfg->adc_dev_slave) != RESET)
207 ADC_CommonInitTypeDef ADC_CommonInitStructure;
208 ADC_CommonStructInit(&ADC_CommonInitStructure);
209 if (adc_dev->cfg->adc_dev_slave) {
210 ADC_CommonInitStructure.ADC_Mode = ADC_Mode_RegSimul;
211 ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
213 ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
214 ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
217 ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode;
218 ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_Circular;
219 ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0;
220 if (adc_dev->cfg->adc_dev_slave == NULL )
221 ADC_DMAConfig(adc_dev->cfg->adc_dev_master, ADC_DMAMode_Circular);
222 ADC_CommonInit(adc_dev->cfg->adc_dev_master, &ADC_CommonInitStructure);
224 ADC_InitTypeDef ADC_InitStructure;
225 ADC_StructInit(&ADC_InitStructure);
226 ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
227 ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable;
228 ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;
229 ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
230 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Left;
232 ADC_InitStructure.ADC_NbrOfRegChannel = adc_dev->regular_group_size;
234 ADC_Init(adc_dev->cfg->adc_dev_master, &ADC_InitStructure);
236 if (adc_dev->cfg->adc_dev_slave)
237 ADC_Init(adc_dev->cfg->adc_dev_slave, &ADC_InitStructure);
240 ADC_DMACmd(adc_dev->cfg->adc_dev_master, ENABLE);
257 uint32_t current_index = 0;
258 if (!adc_dev->cfg->adc_dev_slave) {
259 for (uint32_t
i = 0;
i < adc_dev->cfg->adc_pin_count;
i++) {
260 ADC_RegularChannelConfig(adc_dev->cfg->adc_dev_master, adc_dev->cfg->adc_pins[
i].adc_channel, current_index + 1, ADC_SampleTime_61Cycles5);
261 adc_dev->channel_map[current_index] = &adc_dev->accumulator[current_index];
267 uint32_t acc_index = 0;
269 for (uint32_t
i = 0;
i < adc_dev->cfg->adc_pin_count;
i++) {
270 if (adc_dev->cfg->adc_pins[
i].is_master_channel) {
271 ADC_RegularChannelConfig(adc_dev->cfg->adc_dev_master, adc_dev->cfg->adc_pins[
i].adc_channel, current_index + 1, ADC_SampleTime_61Cycles5);
272 adc_dev->channel_map[acc_index] = &adc_dev->accumulator[
i];
276 == (adc_dev->number_used_master_channels > adc_dev->number_used_slave_channels ?
277 adc_dev->number_used_master_channels : adc_dev->number_used_slave_channels)) {
288 for (uint32_t
i = 0;
i < adc_dev->cfg->adc_pin_count;
i++) {
289 if (!adc_dev->cfg->adc_pins[
i].is_master_channel) {
290 ADC_RegularChannelConfig(adc_dev->cfg->adc_dev_slave, adc_dev->cfg->adc_pins[
i].adc_channel, current_index + 1, ADC_SampleTime_61Cycles5);
291 adc_dev->channel_map[acc_index] = &adc_dev->accumulator[
i];
295 == (adc_dev->number_used_master_channels > adc_dev->number_used_slave_channels ?
296 adc_dev->number_used_master_channels : adc_dev->number_used_slave_channels)) {
304 ADC_Cmd(adc_dev->cfg->adc_dev_master, ENABLE);
305 if (adc_dev->cfg->adc_dev_slave)
306 ADC_Cmd(adc_dev->cfg->adc_dev_slave, ENABLE);
307 while (!ADC_GetFlagStatus(adc_dev->cfg->adc_dev_master, ADC_FLAG_RDY))
309 if (adc_dev->cfg->adc_dev_slave)
310 while (!ADC_GetFlagStatus(adc_dev->cfg->adc_dev_slave, ADC_FLAG_RDY))
312 ADC_StartConversion(adc_dev->cfg->adc_dev_master);
322 struct pios_internal_adc_dev * adc_dev;
323 adc_dev = PIOS_INTERNAL_ADC_Allocate();
324 if (adc_dev == NULL )
328 *internal_adc_id = (uintptr_t) adc_dev;
329 adc_dev->number_used_master_channels = 0;
330 adc_dev->number_used_slave_channels = 0;
332 for (uint8_t
i = 0;
i < adc_dev->cfg->adc_pin_count; ++
i) {
333 if (adc_dev->cfg->adc_pins[
i].is_master_channel)
334 ++adc_dev->number_used_master_channels;
336 ++adc_dev->number_used_slave_channels;
338 adc_dev->regular_group_size = adc_dev->number_used_master_channels > adc_dev->number_used_slave_channels ? adc_dev->number_used_master_channels : adc_dev->number_used_slave_channels;
339 if (adc_dev->cfg->adc_dev_slave) {
341 adc_dev->dma_transfer_size = 2 * adc_dev->cfg->oversampling * adc_dev->regular_group_size;
344 adc_dev->dma_half_buffer_index = adc_dev->dma_transfer_size;
345 adc_dev->accumulator_increment = 2;
346 adc_dev->accumulator_scan_size = adc_dev->regular_group_size * 2;
347 adc_dev->raw_data_buffer =
PIOS_malloc(adc_dev->dma_transfer_size *
sizeof(uint32_t));
350 adc_dev->dma_transfer_size = 2 * adc_dev->cfg->oversampling * adc_dev->cfg->adc_pin_count;
353 adc_dev->dma_half_buffer_index = adc_dev->cfg->oversampling * adc_dev->cfg->adc_pin_count;
355 adc_dev->accumulator_increment = 1;
356 adc_dev->accumulator_scan_size = adc_dev->regular_group_size;
357 adc_dev->raw_data_buffer =
PIOS_malloc(adc_dev->dma_transfer_size *
sizeof(uint16_t));
359 if (adc_dev->raw_data_buffer == NULL )
362 adc_dev->accumulator =
PIOS_malloc(adc_dev->cfg->adc_pin_count *
sizeof(
struct adc_accumulator));
363 if (adc_dev->accumulator == NULL )
365 if (adc_dev->cfg->adc_dev_slave)
366 adc_dev->channel_map =
PIOS_malloc(adc_dev->regular_group_size * 2 *
sizeof(
struct adc_accumulator *));
368 adc_dev->channel_map =
PIOS_malloc(adc_dev->regular_group_size *
sizeof(
struct adc_accumulator *));
369 if (adc_dev->channel_map == NULL )
372 PIOS_INTERNAL_ADC_PinConfig(adc_dev);
373 PIOS_INTERNAL_DMAConfig(adc_dev);
374 PIOS_INTERNAL_ADC_Converter_Config(adc_dev);
384 static uint8_t PIOS_INTERNAL_ADC_NumberOfChannels(uintptr_t internal_adc_id)
386 struct pios_internal_adc_dev * adc_dev = (
struct pios_internal_adc_dev *) internal_adc_id;
387 if (!PIOS_INTERNAL_ADC_validate(adc_dev))
389 return adc_dev->cfg->adc_pin_count;
399 static int32_t PIOS_INTERNAL_ADC_PinGet(uintptr_t internal_adc_id, uint32_t pin)
401 struct pios_internal_adc_dev * adc_dev = (
struct pios_internal_adc_dev *) internal_adc_id;
404 if (pin >= adc_dev->cfg->adc_pin_count) {
407 result = adc_dev->accumulator[pin].accumulator / (adc_dev->accumulator[pin].count ? : 1);
408 adc_dev->accumulator[pin].accumulator = result;
409 adc_dev->accumulator[pin].count = 1;
416 static void accumulate(
struct pios_internal_adc_dev *adc_dev, uint16_t *buffer)
422 for (uint32_t
i = 0;
i < adc_dev->cfg->oversampling; ++
i) {
423 sp = buffer + adc_dev->regular_group_size *
i * adc_dev->accumulator_increment;
424 for (uint8_t scan_index = 0; scan_index < adc_dev->accumulator_scan_size; ++scan_index) {
425 adc_dev->channel_map[scan_index]->accumulator += *sp;
426 adc_dev->channel_map[scan_index]->count++;
432 if (adc_dev->channel_map[scan_index]->accumulator >= (1 << 31)) {
433 adc_dev->channel_map[scan_index]->accumulator /= 2;
434 adc_dev->channel_map[scan_index]->count /= 2;
448 struct pios_internal_adc_dev * adc_dev = (
struct pios_internal_adc_dev *) internal_adc_id;
450 if (!PIOS_INTERNAL_ADC_validate(adc_dev)) {
455 if (DMA_GetFlagStatus(adc_dev->cfg->full_flag)) {
456 DMA_ClearFlag(adc_dev->cfg->full_flag);
457 accumulate(adc_dev, adc_dev->raw_data_buffer + adc_dev->dma_half_buffer_index);
458 }
else if (DMA_GetFlagStatus(adc_dev->cfg->half_flag )) {
459 DMA_ClearFlag(adc_dev->cfg->half_flag);
460 accumulate(adc_dev, adc_dev->raw_data_buffer);
463 DMA_ClearFlag(adc_dev->cfg->dma.irq.flags );
470 static float PIOS_INTERNAL_ADC_LSB_Voltage(uintptr_t internal_adc_id)
472 struct pios_internal_adc_dev * adc_dev = (
struct pios_internal_adc_dev *) internal_adc_id;
473 if (!PIOS_INTERNAL_ADC_validate(adc_dev)) {
476 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)
#define PIOS_DEBUG_Assert(test)
void * PIOS_malloc(size_t size)
void PIOS_INTERNAL_ADC_DMA_Handler()
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)
int32_t PIOS_DELAY_WaituS(uint32_t uS)