From 0e6d45e8771593096229cd2b141b074987621cce Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 15 Feb 2023 15:51:58 +0100 Subject: [PATCH] fixup! cpu/sam0_common: implement 16 bit mode by oversampling --- cpu/sam0_common/include/periph_cpu_common.h | 16 +++++++++++----- cpu/sam0_common/periph/adc.c | 4 +++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/cpu/sam0_common/include/periph_cpu_common.h b/cpu/sam0_common/include/periph_cpu_common.h index 0d2abddc91604..758f80497f515 100644 --- a/cpu/sam0_common/include/periph_cpu_common.h +++ b/cpu/sam0_common/include/periph_cpu_common.h @@ -821,11 +821,17 @@ typedef enum { ADC_RES_10BIT = ADC_CTRLC_RESSEL_10BIT_Val, /**< ADC resolution: 10 bit */ ADC_RES_12BIT = ADC_CTRLC_RESSEL_12BIT_Val, /**< ADC resolution: 12 bit */ #endif - ADC_RES_16BIT_2SAMPL = ( 1 << 2) | 0x1, /**< sum of 2 12 bit samples */ - ADC_RES_16BIT_4SAMPL = ( 2 << 2) | 0x1, /**< sum of 4 12 bit samples */ - ADC_RES_16BIT_8SAMPL = ( 3 << 2) | 0x1, /**< sum of 8 12 bit samples */ - ADC_RES_16BIT_16SAMPL = ( 4 << 2) | 0x1, /**< sum of 16 12 bit samples */ - ADC_RES_14BIT = 0xfe, /**< not supported */ + ADC_RES_16BIT_2SAMPL = ( 0x1 << 2) | 0x1, /**< sum of 2 12 bit samples */ + ADC_RES_16BIT_4SAMPL = ( 0x2 << 2) | 0x1, /**< sum of 4 12 bit samples */ + ADC_RES_16BIT_8SAMPL = ( 0x3 << 2) | 0x1, /**< sum of 8 12 bit samples */ + ADC_RES_16BIT_16SAMPL = ( 0x4 << 2) | 0x1, /**< sum of 16 12 bit samples */ + ADC_RES_16BIT_32SAMPL = ( 0x5 << 2) | 0x1, /**< sum of 32 12 bit samples */ + ADC_RES_16BIT_64SAMPL = ( 0x6 << 2) | 0x1, /**< sum of 64 12 bit samples */ + ADC_RES_16BIT_128SAMPL = ( 0x7 << 2) | 0x1, /**< sum of 128 12 bit samples */ + ADC_RES_16BIT_256SAMPL = ( 0x8 << 2) | 0x1, /**< sum of 256 12 bit samples */ + ADC_RES_16BIT_512SAMPL = ( 0x9 << 2) | 0x1, /**< sum of 512 12 bit samples */ + ADC_RES_16BIT_1024SAMPL = ( 0xA << 2) | 0x1, /**< sum of 1024 12 bit samples */ + ADC_RES_14BIT = 0xfe, /**< not supported */ } adc_res_t; #define ADC_RES_16BIT ADC_RES_16BIT_16SAMPL /**< default to 16x oversampling */ diff --git a/cpu/sam0_common/periph/adc.c b/cpu/sam0_common/periph/adc.c index 75c9d9da45e61..4ba002db0a467 100644 --- a/cpu/sam0_common/periph/adc.c +++ b/cpu/sam0_common/periph/adc.c @@ -23,6 +23,7 @@ #include "periph/gpio.h" #include "periph/adc.h" #include "periph_conf.h" +#include "macros/utils.h" #include "mutex.h" #define ENABLE_DEBUG 0 @@ -339,7 +340,8 @@ int32_t adc_sample(adc_t line, adc_res_t res) /* 16 bit mode is implemented as oversampling */ if ((res & 0x3) == 1) { - result <<= (4 - (res >> 2)); + /* ADC does automatic right shifts beyond 16 samples */ + result <<= (4 - MIN(4, res >> 2)); } return result;