30 #if defined(PIOS_INCLUDE_ADC)
32 static intptr_t PIOS_INTERNAL_ADC_PinConfig(uint32_t internal_adc_id);
33 static void PIOS_INTERNAL_DMAConfig(uintptr_t internal_adc_id);
36 uint16_t number_of_used_pins);
37 static void PIOS_INTERNAL_ADC_Converter_Config(uintptr_t internal_adc_id);
38 static intptr_t PIOS_INTERNAL_ADC_PinGet(uint32_t internal_adc_id, uint32_t pin);
39 static uint8_t PIOS_INTERNAL_ADC_NumberOfChannels(uintptr_t internal_adc_id);
40 static float PIOS_INTERNAL_ADC_LSB_Voltage(uintptr_t internal_adc_id);
43 .
get_pin = PIOS_INTERNAL_ADC_PinGet,
44 .number_of_channels = PIOS_INTERNAL_ADC_NumberOfChannels,
45 .lsb_voltage = PIOS_INTERNAL_ADC_LSB_Voltage,
49 enum pios_internal_adc_dev_magic {
50 PIOS_INTERNAL_ADC_DEV_MAGIC = 0xBD37C124,
53 struct pios_internal_adc_dev {
55 uint16_t number_of_used_pins;
56 uint16_t dma_transfer_size;
57 volatile uint16_t *raw_data_buffer;
58 enum pios_internal_adc_dev_magic
magic;
65 static bool PIOS_INTERNAL_ADC_validate(
struct pios_internal_adc_dev * dev)
70 return (dev->magic == PIOS_INTERNAL_ADC_DEV_MAGIC);
76 static struct pios_internal_adc_dev * PIOS_INTERNAL_ADC_Allocate()
78 struct pios_internal_adc_dev *adc_dev = (
struct pios_internal_adc_dev *)
PIOS_malloc(
sizeof(*adc_dev));
81 adc_dev->magic = PIOS_INTERNAL_ADC_DEV_MAGIC;
89 static intptr_t PIOS_INTERNAL_ADC_PinConfig(uint32_t internal_adc_id)
91 struct pios_internal_adc_dev * adc_dev = (
struct pios_internal_adc_dev *) internal_adc_id;
92 if(!PIOS_INTERNAL_ADC_validate(adc_dev)) {
97 GPIO_InitTypeDef GPIO_InitStructure;
98 GPIO_StructInit(&GPIO_InitStructure);
99 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
100 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
102 for(int32_t
i = 0;
i < adc_dev->number_of_used_pins;
i++) {
103 if(adc_dev->cfg->adc_pins[
i].port == NULL )
106 if(!adc_dev->cfg->adc_pins[
i].is_master_channel)
108 GPIO_InitStructure.GPIO_Pin = adc_dev->cfg->adc_pins[
i].pin;
109 GPIO_Init(adc_dev->cfg->adc_pins[
i].port, (GPIO_InitTypeDef*) &GPIO_InitStructure);
118 static void PIOS_INTERNAL_DMAConfig(uintptr_t internal_adc_id)
120 struct pios_internal_adc_dev * adc_dev = (
struct pios_internal_adc_dev *) internal_adc_id;
121 if(!PIOS_INTERNAL_ADC_validate(adc_dev)) {
126 DMA_ITConfig(adc_dev->cfg->dma.rx.channel, adc_dev->cfg->dma.irq.flags, DISABLE);
129 DMA_DeInit(adc_dev->cfg->dma.rx.channel);
131 RCC_AHBPeriphClockCmd(adc_dev->cfg->dma.ahb_clk, ENABLE);
133 DMA_InitTypeDef DMAInit = adc_dev->cfg->dma.rx.init;
135 DMAInit.DMA_PeripheralBaseAddr = (uint32_t) &adc_dev->cfg->adc_dev_master->DR;
136 DMAInit.DMA_MemoryBaseAddr = (uint32_t) adc_dev->raw_data_buffer;
137 DMAInit.DMA_BufferSize = adc_dev->dma_transfer_size;
138 DMAInit.DMA_DIR = DMA_DIR_PeripheralSRC;
139 DMAInit.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
140 DMAInit.DMA_MemoryInc = DMA_MemoryInc_Enable;
141 DMAInit.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
142 DMAInit.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
143 DMAInit.DMA_Mode = DMA_Mode_Circular;
144 DMAInit.DMA_M2M = DMA_M2M_Disable;
146 DMA_Init(adc_dev->cfg->dma.rx.channel, &DMAInit);
149 DMA_Cmd(adc_dev->cfg->dma.rx.channel, ENABLE);
155 static void PIOS_INTERNAL_ADC_Converter_Config(uintptr_t internal_adc_id)
157 struct pios_internal_adc_dev * adc_dev = (
struct pios_internal_adc_dev *) internal_adc_id;
159 ADC_DeInit(adc_dev->cfg->adc_dev_master);
161 if(adc_dev->cfg->adc_dev_master == ADC1)
162 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
166 RCC_ADCCLKConfig(RCC_PCLK2_Div8);
168 for(int32_t
i = 0;
i < adc_dev->number_of_used_pins;
i++) {
169 ADC_RegularChannelConfig(ADC1, adc_dev->cfg->adc_pins[
i].adc_channel,
170 i + 1, ADC_SampleTime_239Cycles5);
173 ADC_InitTypeDef ADC_InitStructure;
174 ADC_StructInit(&ADC_InitStructure);
176 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
177 ADC_InitStructure.ADC_ScanConvMode = ENABLE;
178 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
179 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
180 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
181 ADC_InitStructure.ADC_NbrOfChannel = adc_dev->number_of_used_pins;
183 ADC_Init(adc_dev->cfg->adc_dev_master, &ADC_InitStructure);
185 ADC_Cmd(adc_dev->cfg->adc_dev_master, ENABLE);
188 ADC_ResetCalibration(adc_dev->cfg->adc_dev_master);
189 while(ADC_GetResetCalibrationStatus(adc_dev->cfg->adc_dev_master));
190 ADC_StartCalibration(adc_dev->cfg->adc_dev_master);
191 while (ADC_GetCalibrationStatus(adc_dev->cfg->adc_dev_master));
194 ADC_DMACmd(adc_dev->cfg->adc_dev_master, ENABLE);
197 ADC_SoftwareStartConvCmd(adc_dev->cfg->adc_dev_master, ENABLE);
205 uint16_t number_of_used_pins)
213 struct pios_internal_adc_dev * adc_dev;
214 adc_dev = PIOS_INTERNAL_ADC_Allocate();
218 adc_dev->number_of_used_pins = number_of_used_pins;
220 *internal_adc_id = (uint32_t) adc_dev;
223 adc_dev->dma_transfer_size = adc_dev->number_of_used_pins;
224 adc_dev->raw_data_buffer =
PIOS_malloc(adc_dev->dma_transfer_size *
sizeof(uint16_t));
225 if (adc_dev->raw_data_buffer == NULL )
228 if(PIOS_INTERNAL_ADC_PinConfig((uint32_t) adc_dev) != 0)
231 PIOS_INTERNAL_DMAConfig((uint32_t) adc_dev);
233 PIOS_INTERNAL_ADC_Converter_Config((uint32_t) adc_dev);
242 static uint8_t PIOS_INTERNAL_ADC_NumberOfChannels(uintptr_t internal_adc_id)
244 struct pios_internal_adc_dev * adc_dev = (
struct pios_internal_adc_dev *) internal_adc_id;
245 if(!PIOS_INTERNAL_ADC_validate(adc_dev))
247 return adc_dev->number_of_used_pins;
257 static intptr_t PIOS_INTERNAL_ADC_PinGet(uintptr_t internal_adc_id, uint32_t pin)
259 struct pios_internal_adc_dev * adc_dev = (
struct pios_internal_adc_dev *) internal_adc_id;
262 if(pin >= adc_dev->number_of_used_pins) {
269 return adc_dev->raw_data_buffer[pin];
275 static float PIOS_INTERNAL_ADC_LSB_Voltage(uintptr_t internal_adc_id)
277 struct pios_internal_adc_dev * adc_dev = (
struct pios_internal_adc_dev *) internal_adc_id;
278 if(!PIOS_INTERNAL_ADC_validate(adc_dev)) {
281 return VREF_PLUS / (((uint32_t)1 << 12) - 1);
#define PIOS_DEBUG_Assert(test)
void * PIOS_malloc(size_t size)
static struct flyingpicmd_cfg_fa cfg
const struct pios_adc_driver pios_internal_adc_driver
ADC_TypeDef * adc_dev_master
int32_t PIOS_INTERNAL_ADC_LIGHT_Init(uintptr_t *internal_adc_id, const struct pios_internal_adc_cfg *cfg, uint16_t number_of_used_pins)
int32_t(* get_pin)(uintptr_t id, uint32_t pin)
int32_t PIOS_DELAY_WaituS(uint32_t uS)