From 3c0bc411643d88e60ed8d63145314daff3159ab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Thu, 20 Feb 2020 10:39:21 +0100 Subject: [PATCH] Add ADC2 channel configuration for STM32G4 board Some changes regarding DMA setup and data alignment were necessary to store all values from ADC1 and ADC2 in the same adc_readings array. --- src/daq_zephyr.c | 71 +++++++++++++++--------------------- src/pcbs/mppt_1210_hus_0v2.h | 2 + src/pcbs/mppt_1210_hus_0v4.h | 2 + src/pcbs/mppt_1210_hus_0v7.h | 2 + src/pcbs/mppt_2420_hpx_0v1.h | 54 ++++++++++++++++++--------- src/pcbs/mppt_2420_lc_0v10.h | 2 + src/pcbs/pcb_stub.h | 2 + src/pcbs/pwm_2420_lus.h | 2 + 8 files changed, 79 insertions(+), 58 deletions(-) diff --git a/src/daq_zephyr.c b/src/daq_zephyr.c index 9b59012..e521f4b 100644 --- a/src/daq_zephyr.c +++ b/src/daq_zephyr.c @@ -84,12 +84,6 @@ static const u32_t table_seq_len[] = { // for ADC and DMA extern uint16_t adc_readings[]; -#if defined(CONFIG_SOC_SERIES_STM32G4X) -// Added these temporarily. Need to change it according to the actual use of ADC2 -#define NUM_ADC_2_CH 1 -volatile uint16_t adc_readings_2[NUM_ADC_2_CH] = {}; -#endif // #if defined(CONFIG_SOC_SERIES_STM32G4X) - void adc_update_value(unsigned int pos); static void dac_setup() @@ -138,11 +132,13 @@ static void adc_init(ADC_TypeDef *adc) LL_ADC_SetCommonClock(__LL_ADC_COMMON_INSTANCE(adc), LL_ADC_CLOCK_SYNC_PCLK_DIV4); + // Calibration got stuck during testing. TODO: Check and improve! + /* // Start calibration of the ADCs LL_ADC_StartCalibration(adc, LL_ADC_SINGLE_ENDED); - while (LL_ADC_IsCalibrationOnGoing(adc)) { } + */ if (LL_ADC_IsActiveFlag_ADRDY(adc)) { LL_ADC_ClearFlag_ADRDY(adc); @@ -158,21 +154,14 @@ static void adc_init(ADC_TypeDef *adc) LL_ADC_REG_SetSequencerChannels(adc, ADC_CHSEL); #else // Others including CONFIG_SOC_SERIES_STM32G4X - if (adc == ADC1) { - for (int i = 0; i < NUM_ADC_1_CH; i++) { - LL_ADC_REG_SetSequencerRanks(adc, table_rank[i], adc_1_sequence[i]); - - LL_ADC_SetChannelSamplingTime(adc, adc_1_sequence[i], LL_ADC_SAMPLINGTIME_47CYCLES_5); - } - LL_ADC_REG_SetSequencerLength(adc, table_seq_len[NUM_ADC_1_CH]); + const uint32_t *adc_seq = (adc == ADC1) ? adc_1_sequence : adc_2_sequence; + int num_ch = (adc == ADC1) ? NUM_ADC_1_CH : NUM_ADC_2_CH; + for (int i = 0; i < num_ch; i++) { + LL_ADC_REG_SetSequencerRanks(adc, table_rank[i], adc_seq[i]); + LL_ADC_SetChannelSamplingTime(adc, adc_seq[i], LL_ADC_SAMPLINGTIME_47CYCLES_5); } - else if (adc == ADC2) { - // This needs to be adapted to the ADC2 requirements of the board. - LL_ADC_REG_SetSequencerRanks (adc, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_17); - LL_ADC_SetChannelSamplingTime(adc, LL_ADC_CHANNEL_17, LL_ADC_SAMPLINGTIME_47CYCLES_5); - LL_ADC_REG_SetSequencerLength(adc, LL_ADC_REG_SEQ_SCAN_DISABLE); - } -#endif // #if defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32L0X) + LL_ADC_REG_SetSequencerLength(adc, table_seq_len[num_ch - 1]); +#endif // defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32L0X) LL_ADC_SetDataAlignment(adc, LL_ADC_DATA_ALIGN_LEFT); LL_ADC_SetResolution(adc, LL_ADC_RESOLUTION_12B); @@ -192,16 +181,14 @@ static void adc_setup() gpio_pin_configure(dev, DT_SWITCH_V_SOLAR_GPIOS_PIN, DT_SWITCH_V_SOLAR_GPIOS_FLAGS | GPIO_OUTPUT_ACTIVE); #endif - // Initialise ADC1 + adc_init(ADC1); #if defined(CONFIG_SOC_SERIES_STM32G4X) - // Initialise ADC2 adc_init(ADC2); -#endif // #if defined(CONFIG_SOC_SERIES_STM32G4X) +#endif } - static inline void adc_trigger_conversion(struct k_timer *timer_id) { LL_ADC_REG_StartConversion(ADC1); @@ -215,7 +202,7 @@ static void DMA1_Channel1_IRQHandler(void *args) { if ((DMA1->ISR & DMA_ISR_TCIF1) != 0) // Test if transfer completed on DMA channel 1 { - for (unsigned int i = 0; i < NUM_ADC_CH; i++) { + for (unsigned int i = 0; i < NUM_ADC_1_CH; i++) { adc_update_value(i); } } @@ -225,27 +212,29 @@ static void DMA1_Channel1_IRQHandler(void *args) #if defined(CONFIG_SOC_SERIES_STM32G4X) static void DMA2_Channel1_IRQHandler(void *args) { - if ((DMA2->ISR & DMA_ISR_TCIF1) != 0) { // Test if transfer completed on DMA channel 1 - printk("ADC 2 - Read ch 0: %X\n", adc_readings_2[0]); + if ((DMA2->ISR & DMA_ISR_TCIF1) != 0) { // Test if transfer completed on DMA channel 2 + for (unsigned int i = NUM_ADC_1_CH; i < NUM_ADC_1_CH + NUM_ADC_2_CH; i++) { + adc_update_value(i); + } } DMA2->IFCR |= 0x0FFFFFFF; // clear all interrupt registers } -#endif +#endif // CONFIG_SOC_SERIES_STM32G4X // Assuming DMA1 is mapped to ADC1 and DMA2 mapped to ADC2. static void dma_init(DMA_TypeDef *dma) { #if defined(CONFIG_SOC_SERIES_STM32G4X) LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMAMUX1); - if (dma == DMA1) { // DMA1 + if (dma == DMA1) { LL_DMA_SetPeriphRequest(dma, LL_DMA_CHANNEL_1, LL_DMAMUX_REQ_ADC1); } - else if (dma == DMA2) { // DMA2 + else if (dma == DMA2) { LL_DMA_SetPeriphRequest(dma, LL_DMA_CHANNEL_1, LL_DMAMUX_REQ_ADC2); } #endif // CONFIG_SOC_SERIES_STM32G4X - if (dma == DMA1) { // DMA1 + if (dma == DMA1) { LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1); LL_DMA_ConfigAddresses(dma, LL_DMA_CHANNEL_1, @@ -254,22 +243,23 @@ static void dma_init(DMA_TypeDef *dma) LL_DMA_DIRECTION_PERIPH_TO_MEMORY); // Configure the number of DMA transfers (data length in multiples of size per transfer) - LL_DMA_SetDataLength(dma, LL_DMA_CHANNEL_1, NUM_ADC_CH); + LL_DMA_SetDataLength(dma, LL_DMA_CHANNEL_1, NUM_ADC_1_CH); } #if defined(CONFIG_SOC_SERIES_STM32G4X) - else if (dma == DMA2) { //DMA2 + else if (dma == DMA2) { LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA2); // If DMA 2 is mapped to ADC 2 LL_DMA_ConfigAddresses(dma, LL_DMA_CHANNEL_1, LL_ADC_DMA_GetRegAddr(ADC2, LL_ADC_DMA_REG_REGULAR_DATA), // source address - (uint32_t)(&(adc_readings_2[0])), // destination address + // destination address = position behind ADC_1 values + (uint32_t)(&(adc_readings[NUM_ADC_1_CH])), LL_DMA_DIRECTION_PERIPH_TO_MEMORY); // Configure the number of DMA transfers (data length in multiples of size per transfer) LL_DMA_SetDataLength(dma, LL_DMA_CHANNEL_1, NUM_ADC_2_CH); } -#endif // #if defined(CONFIG_SOC_SERIES_STM32G4X) +#endif // CONFIG_SOC_SERIES_STM32G4X LL_DMA_SetMemoryIncMode(dma, LL_DMA_CHANNEL_1, LL_DMA_MEMORY_INCREMENT); LL_DMA_SetMemorySize(dma, LL_DMA_CHANNEL_1, LL_DMA_MDATAALIGN_HALFWORD); @@ -281,17 +271,16 @@ static void dma_init(DMA_TypeDef *dma) LL_DMA_EnableChannel(dma, LL_DMA_CHANNEL_1); // Configure NVIC for DMA (priority 2: second-lowest value for STM32L0/F0) - if (dma == DMA1) { // DMA1 + if (dma == DMA1) { IRQ_CONNECT(DMA1_Channel1_IRQn, 2, DMA1_Channel1_IRQHandler, 0, 0); irq_enable(DMA1_Channel1_IRQn); - } #if defined(CONFIG_SOC_SERIES_STM32G4X) - else if (dma == DMA2) { //DMA2 + else if (dma == DMA2) { IRQ_CONNECT(DMA2_Channel1_IRQn, 2, DMA2_Channel1_IRQHandler, 0, 0); irq_enable(DMA2_Channel1_IRQn); } -#endif // #if defined(CONFIG_SOC_SERIES_STM32G4X) +#endif // CONFIG_SOC_SERIES_STM32G4X } static void dma_setup() @@ -302,7 +291,7 @@ static void dma_setup() #if defined(CONFIG_SOC_SERIES_STM32G4X) dma_init(DMA2); LL_ADC_REG_StartConversion(ADC2); -#endif //#if defined(CONFIG_SOC_SERIES_STM32G4X) +#endif // CONFIG_SOC_SERIES_STM32G4X } void daq_setup() diff --git a/src/pcbs/mppt_1210_hus_0v2.h b/src/pcbs/mppt_1210_hus_0v2.h index 5cc987f..22c54f9 100644 --- a/src/pcbs/mppt_1210_hus_0v2.h +++ b/src/pcbs/mppt_1210_hus_0v2.h @@ -100,6 +100,8 @@ enum { NUM_ADC_CH // trick to get the number of enums }; +#define NUM_ADC_1_CH NUM_ADC_CH + // selected ADC channels (has to match with above enum) #define ADC_CHSEL ( \ ADC_CHSELR_CHSEL0 | \ diff --git a/src/pcbs/mppt_1210_hus_0v4.h b/src/pcbs/mppt_1210_hus_0v4.h index 2a81c43..74d69be 100644 --- a/src/pcbs/mppt_1210_hus_0v4.h +++ b/src/pcbs/mppt_1210_hus_0v4.h @@ -111,6 +111,8 @@ enum { NUM_ADC_CH // trick to get the number of elements }; +#define NUM_ADC_1_CH NUM_ADC_CH + // selected ADC channels (has to match with above enum) #if defined(STM32F0) || defined(CONFIG_SOC_SERIES_STM32F0X) #define ADC_CHSEL ( \ diff --git a/src/pcbs/mppt_1210_hus_0v7.h b/src/pcbs/mppt_1210_hus_0v7.h index f62f921..337a87f 100644 --- a/src/pcbs/mppt_1210_hus_0v7.h +++ b/src/pcbs/mppt_1210_hus_0v7.h @@ -115,6 +115,8 @@ enum { NUM_ADC_CH // trick to get the number of elements }; +#define NUM_ADC_1_CH NUM_ADC_CH + // selected ADC channels (has to match with above enum) #define ADC_CHSEL ( \ ADC_CHSELR_CHSEL0 | \ diff --git a/src/pcbs/mppt_2420_hpx_0v1.h b/src/pcbs/mppt_2420_hpx_0v1.h index deeb4a9..e1bee64 100644 --- a/src/pcbs/mppt_2420_hpx_0v1.h +++ b/src/pcbs/mppt_2420_hpx_0v1.h @@ -74,34 +74,54 @@ const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { #define NTC_BETA_VALUE 3435 #define NTC_SERIES_RESISTOR 10000.0 -#define ADC_GAIN_V_LOW (105.6 / 5.6) // both voltage dividers: 100k + 5.6k -#define ADC_GAIN_V_HIGH (105.6 / 5.6) -#define ADC_GAIN_I_LOAD (1000 / 4 / 50) // amp gain: 50, resistor: 4 mOhm -#define ADC_GAIN_I_DCDC (1000 / 4 / 50) +#define ADC_GAIN_V_LOW (105.6 / 5.6) +#define ADC_GAIN_V_HIGH (102.2 / 2.2) +#define ADC_GAIN_V_PWM (1 + 120/12 + 120/8.2) + +#define ADC_GAIN_I_DCDC (1000 / 2 / 20) // amp gain: 20, resistor: 2 mOhm + +// op amp gain: 68/2.2, resistor: 2 mOhm +#define ADC_GAIN_I_LOAD (1000 / 2 / (68/2.2)) +#define ADC_GAIN_I_PWM (1000 / 2 / (68/2.2)) + +// to be multiplied with VREF to get absolute voltage offset +#define ADC_OFFSET_V_PWM (-120.0 / 8.2) // position in the array written by the DMA controller enum { - ADC_POS_V_LOW, // ADC 0 (PA_0) - ADC_POS_V_HIGH, // ADC 1 (PA_1) - ADC_POS_TEMP_FETS, // ADC 5 (PA_5) - ADC_POS_I_DCDC, // ADC 7 (PA_7) - ADC_POS_I_LOAD, // ADC 6 (PA_6) - ADC_POS_VREF_MCU, // ADC 17 - ADC_POS_TEMP_MCU, // ADC 16 + // ADC1 + ADC_POS_V_LOW, + ADC_POS_V_HIGH, + ADC_POS_V_SC_SENSE, + ADC_POS_V_PWM, + ADC_POS_TEMP_FETS, + ADC_POS_VREF_MCU, + ADC_POS_TEMP_MCU, + // ADC2 + ADC_POS_I_DCDC, + ADC_POS_I_LOAD, + ADC_POS_I_PWM, NUM_ADC_CH // trick to get the number of elements }; -// selected ADC 1 channels (has to match with above enum) -#define NUM_ADC_1_CH 5 +// selected ADC channels (has to match with below arrays) +#define NUM_ADC_1_CH 7 +#define NUM_ADC_2_CH 3 const uint32_t adc_1_sequence[] = { - LL_ADC_CHANNEL_12, // V_BAT - LL_ADC_CHANNEL_15, // V_SOLAR + LL_ADC_CHANNEL_12, // V_LOW + LL_ADC_CHANNEL_15, // V_HIGH + LL_ADC_CHANNEL_11, // V_SC_SENSE + LL_ADC_CHANNEL_4, // V_PWM LL_ADC_CHANNEL_6, // TEMP_FETS - LL_ADC_CHANNEL_1, // I_DCDC - LL_ADC_CHANNEL_2, // I_LOAD LL_ADC_CHANNEL_VREFINT, // VREF_MCU LL_ADC_CHANNEL_TEMPSENSOR_ADC1, // TEMP_MCU }; +const uint32_t adc_2_sequence[] = { + LL_ADC_CHANNEL_1, // I_DCDC + LL_ADC_CHANNEL_2, // I_LOAD + LL_ADC_CHANNEL_5, // I_PWM +}; + #endif diff --git a/src/pcbs/mppt_2420_lc_0v10.h b/src/pcbs/mppt_2420_lc_0v10.h index cec876e..785ab18 100644 --- a/src/pcbs/mppt_2420_lc_0v10.h +++ b/src/pcbs/mppt_2420_lc_0v10.h @@ -119,6 +119,8 @@ enum { NUM_ADC_CH // trick to get the number of enums }; +#define NUM_ADC_1_CH NUM_ADC_CH + // selected ADC channels (has to match with above enum) #define ADC_CHSEL ( \ ADC_CHSELR_CHSEL0 | \ diff --git a/src/pcbs/pcb_stub.h b/src/pcbs/pcb_stub.h index 4292c06..4b60222 100644 --- a/src/pcbs/pcb_stub.h +++ b/src/pcbs/pcb_stub.h @@ -95,4 +95,6 @@ enum { NUM_ADC_CH // trick to get the number of elements }; +#define NUM_ADC_1_CH NUM_ADC_CH + #endif diff --git a/src/pcbs/pwm_2420_lus.h b/src/pcbs/pwm_2420_lus.h index 547fb9e..4ca2add 100644 --- a/src/pcbs/pwm_2420_lus.h +++ b/src/pcbs/pwm_2420_lus.h @@ -146,6 +146,8 @@ enum { NUM_ADC_CH // trick to get the number of elements }; +#define NUM_ADC_1_CH NUM_ADC_CH + // selected ADC channels (has to match with above enum) #define ADC_CHSEL ( \ ADC_CHSELR_CHSEL0 | \