From 90c1e7fcf5596af7e6db34928c62f4abaf8d05b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Mon, 6 Apr 2020 06:54:35 +0200 Subject: [PATCH] Replace Zephyr ADC driver with direct LL access Fixes IRQ conflict with COMP peripheral (#93) --- boards/arm/mppt_1210_hus/mppt_1210_hus.dts | 4 -- boards/arm/mppt_2420_hpx/dts_fixup.h | 19 -------- boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts | 39 ---------------- boards/arm/mppt_2420_lc/mppt_2420_lc.dts | 4 -- boards/arm/pwm_2420_lus/pwm_2420_lus.dts | 4 -- src/daq_driver.c | 52 +++++++++------------- zephyr/prj.conf | 2 - 7 files changed, 20 insertions(+), 104 deletions(-) delete mode 100644 boards/arm/mppt_2420_hpx/dts_fixup.h diff --git a/boards/arm/mppt_1210_hus/mppt_1210_hus.dts b/boards/arm/mppt_1210_hus/mppt_1210_hus.dts index 5c11a31..ff2c8ee 100755 --- a/boards/arm/mppt_1210_hus/mppt_1210_hus.dts +++ b/boards/arm/mppt_1210_hus/mppt_1210_hus.dts @@ -103,10 +103,6 @@ status = "okay"; }; -&adc1 { - status = "okay"; -}; - &eeprom { status = "okay"; }; diff --git a/boards/arm/mppt_2420_hpx/dts_fixup.h b/boards/arm/mppt_2420_hpx/dts_fixup.h deleted file mode 100644 index 78afe03..0000000 --- a/boards/arm/mppt_2420_hpx/dts_fixup.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * Copyright (c) 2020 Martin Jäger / Libre Solar - */ - -#define DT_ADC_1_BASE_ADDRESS DT_ST_STM32_ADC_50000000_BASE_ADDRESS -#define DT_ADC_1_IRQ DT_ST_STM32_ADC_50000000_IRQ_0 -#define DT_ADC_1_IRQ_PRI DT_ST_STM32_ADC_50000000_IRQ_0_PRIORITY -#define DT_ADC_1_NAME DT_ST_STM32_ADC_50000000_LABEL -#define DT_ADC_1_CLOCK_BITS DT_ST_STM32_ADC_50000000_CLOCK_BITS_0 -#define DT_ADC_1_CLOCK_BUS DT_ST_STM32_ADC_50000000_CLOCK_BUS_0 - -#define DT_ADC_2_BASE_ADDRESS DT_ST_STM32_ADC_50000100_BASE_ADDRESS -#define DT_ADC_2_IRQ DT_ST_STM32_ADC_50000100_IRQ_0 -#define DT_ADC_2_IRQ_PRI DT_ST_STM32_ADC_50000100_IRQ_0_PRIORITY -#define DT_ADC_2_NAME DT_ST_STM32_ADC_50000100_LABEL -#define DT_ADC_2_CLOCK_BITS DT_ST_STM32_ADC_50000100_CLOCK_BITS_0 -#define DT_ADC_2_CLOCK_BUS DT_ST_STM32_ADC_50000100_CLOCK_BUS_0 diff --git a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts index 579cabb..a1f9d4a 100644 --- a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts +++ b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts @@ -11,37 +11,6 @@ model = "Libre Solar MPPT 2420 HPX"; compatible = "st,stm32g431"; - soc { - adc1: adc@50000000 { - compatible = "st,stm32-adc"; - reg = <0x50000000 0x400>; - clocks = <&rcc STM32_CLOCK_BUS_AHB2 0x00002000>; - interrupts = <18 0>; - status = "disabled"; - label = "ADC_1"; - #io-channel-cells = <1>; - }; - - adc2: adc@50000100 { - compatible = "st,stm32-adc"; - reg = <0x50000100 0x400>; - clocks = <&rcc STM32_CLOCK_BUS_AHB2 0x00002000>; - interrupts = <18 0>; - status = "disabled"; - label = "ADC_2"; - #io-channel-cells = <1>; - }; - - dac1: dac@50000800 { - compatible = "st,stm32-dac"; - reg = <0x50000800 0x400>; - clocks = <&rcc STM32_CLOCK_BUS_AHB2 0x00010000>; - status = "disabled"; - label = "DAC_1"; - #io-channel-cells = <1>; - }; - }; - pcb { compatible = "charge-controller"; @@ -157,14 +126,6 @@ status = "okay"; }; -&adc1 { - status = "okay"; -}; - -&adc2 { - status = "okay"; -}; - /* for charge pump and PWM solar input */ &timers3 { status = "okay"; diff --git a/boards/arm/mppt_2420_lc/mppt_2420_lc.dts b/boards/arm/mppt_2420_lc/mppt_2420_lc.dts index 57fcec0..b03d9ee 100755 --- a/boards/arm/mppt_2420_lc/mppt_2420_lc.dts +++ b/boards/arm/mppt_2420_lc/mppt_2420_lc.dts @@ -114,10 +114,6 @@ status = "okay"; }; -&adc1 { - status = "okay"; -}; - &can1 { bus-speed = <250000>; status = "okay"; diff --git a/boards/arm/pwm_2420_lus/pwm_2420_lus.dts b/boards/arm/pwm_2420_lus/pwm_2420_lus.dts index fda8f40..61fb716 100644 --- a/boards/arm/pwm_2420_lus/pwm_2420_lus.dts +++ b/boards/arm/pwm_2420_lus/pwm_2420_lus.dts @@ -105,10 +105,6 @@ status = "okay"; }; -&adc1 { - status = "okay"; -}; - &eeprom { status = "okay"; }; diff --git a/src/daq_driver.c b/src/daq_driver.c index 0814809..0397424 100644 --- a/src/daq_driver.c +++ b/src/daq_driver.c @@ -12,7 +12,6 @@ #include #include -#include #include #if defined(CONFIG_SOC_SERIES_STM32L0X) @@ -29,6 +28,7 @@ #include #elif defined(CONFIG_SOC_SERIES_STM32G4X) #include +#include #include #include #include @@ -119,57 +119,45 @@ static void dac_setup() static void adc_init(ADC_TypeDef *adc) { -#if !defined(CONFIG_SOC_SERIES_STM32G4X) - struct device *dev_adc = device_get_binding(DT_ADC_1_NAME); - if (dev_adc == 0) { - printf("ADC device not found\n"); - return; - } - - struct adc_channel_cfg channel_cfg = { - .gain = ADC_GAIN_1, - .reference = ADC_REF_INTERNAL, + LL_ADC_Disable(adc); #if defined(CONFIG_SOC_SERIES_STM32F0X) - .acquisition_time = ADC_ACQ_TIME(ADC_ACQ_TIME_TICKS, 240), + LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_ADC1); + LL_ADC_SetClock(adc, LL_ADC_CLOCK_SYNC_PCLK_DIV4); #elif defined(CONFIG_SOC_SERIES_STM32L0X) - .acquisition_time = ADC_ACQ_TIME(ADC_ACQ_TIME_TICKS, 161), -#endif - .channel_id = LL_ADC_CHANNEL_0, - .differential = 0 - }; - - int ret = adc_channel_setup(dev_adc, &channel_cfg); - if (ret) { - printk("ADC channel setup error.. \n"); - } - + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1); + LL_ADC_EnableInternalRegulator(adc); + k_busy_wait(LL_ADC_DELAY_INTERNAL_REGUL_STAB_US); + LL_ADC_SetCommonClock(__LL_ADC_COMMON_INSTANCE(adc), LL_ADC_CLOCK_SYNC_PCLK_DIV4); #elif defined(CONFIG_SOC_SERIES_STM32G4X) LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_ADC12); - // Prepare for ADC calibration - LL_ADC_Disable(adc); LL_ADC_DisableDeepPowerDown(adc); LL_ADC_EnableInternalRegulator(adc); k_busy_wait(LL_ADC_DELAY_INTERNAL_REGUL_STAB_US); - LL_ADC_SetCommonClock(__LL_ADC_COMMON_INSTANCE(adc), LL_ADC_CLOCK_SYNC_PCLK_DIV4); +#endif +#if defined(CONFIG_SOC_SERIES_STM32G4X) LL_ADC_StartCalibration(adc, LL_ADC_SINGLE_ENDED); +#elif defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32L0X) + LL_ADC_StartCalibration(adc); +#endif while (LL_ADC_IsCalibrationOnGoing(adc)) {;} if (LL_ADC_IsActiveFlag_ADRDY(adc)) { LL_ADC_ClearFlag_ADRDY(adc); } -#endif - // Now Initialize the ADCs - // enable internal reference voltage and temperature + // Enable internal reference voltage and temperature LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(adc), LL_ADC_PATH_INTERNAL_VREFINT | LL_ADC_PATH_INTERNAL_TEMPSENSOR); -#if defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32L0X) +#if defined(CONFIG_SOC_SERIES_STM32F0X) LL_ADC_REG_SetSequencerChannels(adc, ADC_CHSEL); - + LL_ADC_SetSamplingTimeCommonChannels(adc, LL_ADC_SAMPLINGTIME_239CYCLES_5); +#elif defined(CONFIG_SOC_SERIES_STM32L0X) + LL_ADC_REG_SetSequencerChannels(adc, ADC_CHSEL); + LL_ADC_SetSamplingTimeCommonChannels(adc, LL_ADC_SAMPLINGTIME_160CYCLES_5); #else // Others including CONFIG_SOC_SERIES_STM32G4X 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; @@ -191,8 +179,8 @@ static void adc_init(ADC_TypeDef *adc) LL_ADC_REG_SetTriggerEdge(adc, LL_ADC_REG_TRIG_EXT_RISING); LL_ADC_REG_SetTriggerSource(adc, LL_ADC_REG_TRIG_EXT_TIM1_TRGO2); } - LL_ADC_Enable(adc); #endif + LL_ADC_Enable(adc); } static void adc_setup() diff --git a/zephyr/prj.conf b/zephyr/prj.conf index a308846..70e72db 100644 --- a/zephyr/prj.conf +++ b/zephyr/prj.conf @@ -9,8 +9,6 @@ CONFIG_SERIAL=y CONFIG_UART_1=y CONFIG_I2C=y CONFIG_I2C_1=y -CONFIG_ADC=y -CONFIG_ADC_1=y # disable log by default to save memory for STM32F0 with little RAM CONFIG_LOG=y