diff --git a/flight/pios/inc/pios_adc.h b/flight/pios/inc/pios_adc.h index bde8d11f1..0c4f2dd39 100644 --- a/flight/pios/inc/pios_adc.h +++ b/flight/pios/inc/pios_adc.h @@ -38,6 +38,7 @@ typedef void (*ADCCallback)(float *data); /* Public Functions */ void PIOS_ADC_Config(uint32_t oversampling); +void PIOS_ADC_PinSetup(uint32_t pin); int32_t PIOS_ADC_PinGet(uint32_t pin); float PIOS_ADC_PinGetVolt(uint32_t pin); int16_t *PIOS_ADC_GetRawBuffer(void); diff --git a/flight/pios/stm32f4xx/pios_adc.c b/flight/pios/stm32f4xx/pios_adc.c index caa6d9976..3bd13534a 100644 --- a/flight/pios/stm32f4xx/pios_adc.c +++ b/flight/pios/stm32f4xx/pios_adc.c @@ -99,10 +99,11 @@ static void init_dma(void); static void init_adc(void); #endif -struct dma_config { +struct pios_adc_pin_config { GPIO_TypeDef *port; uint32_t pin; uint32_t channel; + bool initialize; }; struct adc_accumulator { @@ -111,7 +112,7 @@ struct adc_accumulator { }; #if defined(PIOS_INCLUDE_ADC) -static const struct dma_config config[] = PIOS_DMA_PIN_CONFIG; +static const struct pios_adc_pin_config config[] = PIOS_DMA_PIN_CONFIG; #define PIOS_ADC_NUM_PINS (sizeof(config) / sizeof(config[0])) static struct adc_accumulator accumulator[PIOS_ADC_NUM_PINS]; @@ -123,18 +124,11 @@ static uint16_t adc_raw_buffer[2][PIOS_ADC_MAX_SAMPLES][PIOS_ADC_NUM_PINS]; #if defined(PIOS_INCLUDE_ADC) static void init_pins(void) { - /* Setup analog pins */ - GPIO_InitTypeDef GPIO_InitStructure; - - GPIO_StructInit(&GPIO_InitStructure); - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; for (uint32_t i = 0; i < PIOS_ADC_NUM_PINS; ++i) { - if (config[i].port == NULL) { + if (!config[i].initialize) { continue; } - GPIO_InitStructure.GPIO_Pin = config[i].pin; - GPIO_Init(config[i].port, &GPIO_InitStructure); + PIOS_ADC_PinSetup(i); } } @@ -454,6 +448,19 @@ void PIOS_ADC_DMA_Handler(void) #endif } +void PIOS_ADC_PinSetup(uint32_t pin) +{ + if (config[pin].port != NULL && pin < PIOS_ADC_NUM_PINS) { + /* Setup analog pin */ + GPIO_InitTypeDef GPIO_InitStructure; + + GPIO_StructInit(&GPIO_InitStructure); + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; + GPIO_InitStructure.GPIO_Pin = config[pin].pin; + GPIO_Init(config[pin].port, &GPIO_InitStructure); + } +} #endif /* PIOS_INCLUDE_ADC */ /** diff --git a/flight/targets/boards/discoveryf4bare/firmware/pios_board.c b/flight/targets/boards/discoveryf4bare/firmware/pios_board.c index 3876a28cd..5770db566 100644 --- a/flight/targets/boards/discoveryf4bare/firmware/pios_board.c +++ b/flight/targets/boards/discoveryf4bare/firmware/pios_board.c @@ -908,6 +908,17 @@ void PIOS_Board_Init(void) #include PIOS_WS2811_Init(&pios_ws2811_cfg, &pios_ws2811_pin_cfg); #endif // PIOS_INCLUDE_WS2811 +#ifdef PIOS_INCLUDE_ADC + { + uint8_t adc_config[HWSETTINGS_ADCROUTING_NUMELEM]; + HwSettingsADCRoutingArrayGet(adc_config); + for (uint32_t i = 0; i < HWSETTINGS_ADCROUTING_NUMELEM; i++) { + if (adc_config[i] != HWSETTINGS_ADCROUTING_DISABLED) { + PIOS_ADC_PinSetup(i); + } + } + } +#endif // PIOS_INCLUDE_ADC } /** diff --git a/flight/targets/boards/discoveryf4bare/pios_board.h b/flight/targets/boards/discoveryf4bare/pios_board.h index 82e8df8f9..d25c71d53 100644 --- a/flight/targets/boards/discoveryf4bare/pios_board.h +++ b/flight/targets/boards/discoveryf4bare/pios_board.h @@ -264,10 +264,10 @@ extern uint32_t pios_packet_handler; // ------------------------- #define PIOS_DMA_PIN_CONFIG \ { \ - { GPIOC, GPIO_Pin_1, ADC_Channel_11 }, \ - { GPIOC, GPIO_Pin_2, ADC_Channel_12 }, \ - { NULL, 0, ADC_Channel_Vrefint }, /* Voltage reference */ \ - { NULL, 0, ADC_Channel_TempSensor }, /* Temperature sensor */ \ + { GPIOC, GPIO_Pin_1, ADC_Channel_11, false }, \ + { GPIOC, GPIO_Pin_2, ADC_Channel_12, false }, \ + { NULL, 0, ADC_Channel_Vrefint, false }, /* Voltage reference */ \ + { NULL, 0, ADC_Channel_TempSensor, false }, /* Temperature sensor */ \ } /* we have to do all this to satisfy the PIOS_ADC_MAX_SAMPLES define in pios_adc.h */ diff --git a/flight/targets/boards/osd/firmware/pios_board.c b/flight/targets/boards/osd/firmware/pios_board.c index e125eb151..680c93c2e 100644 --- a/flight/targets/boards/osd/firmware/pios_board.c +++ b/flight/targets/boards/osd/firmware/pios_board.c @@ -449,6 +449,17 @@ void PIOS_Board_Init(void) // PIOS_TIM_InitClock(&pios_tim4_cfg); PIOS_Video_Init(&pios_video_cfg); #endif +#ifdef PIOS_INCLUDE_ADC + { + uint8_t adc_config[HWSETTINGS_ADCROUTING_NUMELEM]; + HwSettingsADCRoutingArrayGet(adc_config); + for (uint32_t i = 0; i < HWSETTINGS_ADCROUTING_NUMELEM; i++) { + if (adc_config[i] != HWSETTINGS_ADCROUTING_DISABLED) { + PIOS_ADC_PinSetup(i); + } + } + } +#endif // PIOS_INCLUDE_ADC } uint16_t supv_timer = 0; diff --git a/flight/targets/boards/osd/pios_board.h b/flight/targets/boards/osd/pios_board.h index f0068f978..ab5fb7688 100644 --- a/flight/targets/boards/osd/pios_board.h +++ b/flight/targets/boards/osd/pios_board.h @@ -208,13 +208,13 @@ extern uint32_t pios_com_telem_usb_id; #define PIOS_DMA_PIN_CONFIG \ { \ - { GPIOC, GPIO_Pin_0, ADC_Channel_10 }, \ - { GPIOC, GPIO_Pin_1, ADC_Channel_11 }, \ - { GPIOC, GPIO_Pin_2, ADC_Channel_12 }, \ - { NULL, 0, ADC_Channel_TempSensor }, /* Temperature sensor */ \ - { GPIOC, GPIO_Pin_3, ADC_Channel_13 }, \ - { GPIOA, GPIO_Pin_7, ADC_Channel_7 }, \ - { NULL, 0, ADC_Channel_Vrefint } /* Voltage reference */ \ + { GPIOC, GPIO_Pin_0, ADC_Channel_10, true }, \ + { GPIOC, GPIO_Pin_1, ADC_Channel_11, true }, \ + { GPIOC, GPIO_Pin_2, ADC_Channel_12, true }, \ + { NULL, 0, ADC_Channel_TempSensor, true }, /* Temperature sensor */ \ + { GPIOC, GPIO_Pin_3, ADC_Channel_13, true }, \ + { GPIOA, GPIO_Pin_7, ADC_Channel_7, true }, \ + { NULL, 0, ADC_Channel_Vrefint, true } /* Voltage reference */ \ } /* we have to do all this to satisfy the PIOS_ADC_MAX_SAMPLES define in pios_adc.h */ diff --git a/flight/targets/boards/revolution/firmware/pios_board.c b/flight/targets/boards/revolution/firmware/pios_board.c index 35f218a75..001550911 100644 --- a/flight/targets/boards/revolution/firmware/pios_board.c +++ b/flight/targets/boards/revolution/firmware/pios_board.c @@ -943,8 +943,18 @@ void PIOS_Board_Init(void) if (ws2811_pin_settings != HWSETTINGS_WS2811LED_OUT_DISABLED && ws2811_pin_settings < NELEMENTS(pios_ws2811_pin_cfg)) { PIOS_WS2811_Init(&pios_ws2811_cfg, &pios_ws2811_pin_cfg[ws2811_pin_settings]); } - #endif // PIOS_INCLUDE_WS2811 +#ifdef PIOS_INCLUDE_ADC + { + uint8_t adc_config[HWSETTINGS_ADCROUTING_NUMELEM]; + HwSettingsADCRoutingArrayGet(adc_config); + for (uint32_t i = 0; i < HWSETTINGS_ADCROUTING_NUMELEM; i++) { + if (adc_config[i] != HWSETTINGS_ADCROUTING_DISABLED) { + PIOS_ADC_PinSetup(i); + } + } + } +#endif // PIOS_INCLUDE_ADC } /** diff --git a/flight/targets/boards/revolution/pios_board.h b/flight/targets/boards/revolution/pios_board.h index 7f9ab9d77..0f29a72ce 100644 --- a/flight/targets/boards/revolution/pios_board.h +++ b/flight/targets/boards/revolution/pios_board.h @@ -281,23 +281,27 @@ extern uint32_t pios_packet_handler; // PIOS_ADC_PinGet(4) = VREF // PIOS_ADC_PinGet(5) = Temperature sensor // ------------------------- -#define PIOS_DMA_PIN_CONFIG \ - { \ - { GPIOC, GPIO_Pin_1, ADC_Channel_11 }, \ - { GPIOC, GPIO_Pin_2, ADC_Channel_12 }, \ - { NULL, 0, ADC_Channel_Vrefint }, /* Voltage reference */ \ - { NULL, 0, ADC_Channel_TempSensor }, /* Temperature sensor */ \ +#define PIOS_DMA_PIN_CONFIG \ + { \ + { GPIOC, GPIO_Pin_1, ADC_Channel_11, false }, /* batt/sonar pin 3 */ \ + { GPIOC, GPIO_Pin_2, ADC_Channel_12, false }, /* batt/sonar pin 4 */ \ + { GPIOA, GPIO_Pin_3, ADC_Channel_3, false }, /* Servo pin 3 */ \ + { GPIOA, GPIO_Pin_2, ADC_Channel_2, false }, /* Servo pin 4 */ \ + { GPIOA, GPIO_Pin_1, ADC_Channel_1, false }, /* Servo pin 5 */ \ + { GPIOA, GPIO_Pin_0, ADC_Channel_9, false }, /* Servo pin 6 */ \ + { NULL, 0, ADC_Channel_Vrefint, false }, /* Voltage reference */ \ + { NULL, 0, ADC_Channel_TempSensor, false }, /* Temperature sensor */ \ } /* we have to do all this to satisfy the PIOS_ADC_MAX_SAMPLES define in pios_adc.h */ /* which is annoying because this then determines the rate at which we generate buffer turnover events */ /* the objective here is to get enough buffer space to support 100Hz averaging rate */ -#define PIOS_ADC_NUM_CHANNELS 4 +#define PIOS_ADC_NUM_CHANNELS 8 #define PIOS_ADC_MAX_OVERSAMPLING 2 #define PIOS_ADC_USE_ADC2 0 #define PIOS_ADC_USE_TEMP_SENSOR -#define PIOS_ADC_TEMPERATURE_PIN 3 +#define PIOS_ADC_TEMPERATURE_PIN 7 // ------------------------- // USB diff --git a/flight/targets/boards/revoproto/firmware/pios_board.c b/flight/targets/boards/revoproto/firmware/pios_board.c index 99cdef338..1649325d3 100644 --- a/flight/targets/boards/revoproto/firmware/pios_board.c +++ b/flight/targets/boards/revoproto/firmware/pios_board.c @@ -925,6 +925,17 @@ void PIOS_Board_Init(void) default: PIOS_DEBUG_Assert(0); } +#ifdef PIOS_INCLUDE_ADC + { + uint8_t adc_config[HWSETTINGS_ADCROUTING_NUMELEM]; + HwSettingsADCRoutingArrayGet(adc_config); + for (uint32_t i = 0; i < HWSETTINGS_ADCROUTING_NUMELEM; i++) { + if (adc_config[i] != HWSETTINGS_ADCROUTING_DISABLED) { + PIOS_ADC_PinSetup(i); + } + } + } +#endif // PIOS_INCLUDE_ADC } /** diff --git a/flight/targets/boards/revoproto/pios_board.h b/flight/targets/boards/revoproto/pios_board.h index d3a421114..30cab6600 100644 --- a/flight/targets/boards/revoproto/pios_board.h +++ b/flight/targets/boards/revoproto/pios_board.h @@ -242,11 +242,11 @@ extern uint32_t pios_com_hkosd_id; // ------------------------- #define PIOS_DMA_PIN_CONFIG \ { \ - { GPIOC, GPIO_Pin_0, ADC_Channel_10 }, \ - { GPIOC, GPIO_Pin_1, ADC_Channel_11 }, \ - { NULL, 0, ADC_Channel_Vrefint }, /* Voltage reference */ \ - { NULL, 0, ADC_Channel_TempSensor }, /* Temperature sensor */ \ - { GPIOC, GPIO_Pin_2, ADC_Channel_12 } \ + { GPIOC, GPIO_Pin_0, ADC_Channel_10, true }, \ + { GPIOC, GPIO_Pin_1, ADC_Channel_11, true }, \ + { NULL, 0, ADC_Channel_Vrefint, true }, /* Voltage reference */ \ + { NULL, 0, ADC_Channel_TempSensor, true }, /* Temperature sensor */ \ + { GPIOC, GPIO_Pin_2, ADC_Channel_12, true } \ } /* we have to do all this to satisfy the PIOS_ADC_MAX_SAMPLES define in pios_adc.h */ diff --git a/shared/uavobjectdefinition/hwsettings.xml b/shared/uavobjectdefinition/hwsettings.xml index 9467964c1..ed5af59d4 100644 --- a/shared/uavobjectdefinition/hwsettings.xml +++ b/shared/uavobjectdefinition/hwsettings.xml @@ -23,7 +23,7 @@ - +