From bb509ec91dcc558752ad6768a2e26e0bc706fd71 Mon Sep 17 00:00:00 2001 From: sago35 Date: Tue, 4 May 2021 15:13:50 +0900 Subject: [PATCH] atsamd51, atsamd21: fix ADC.Get() value at 8bit and 10bit --- src/machine/machine_atsamd21.go | 13 ++++++++++++- src/machine/machine_atsamd51.go | 13 ++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/machine/machine_atsamd21.go b/src/machine/machine_atsamd21.go index e886f9373..76d6c5d49 100644 --- a/src/machine/machine_atsamd21.go +++ b/src/machine/machine_atsamd21.go @@ -433,7 +433,18 @@ func (a ADC) Get() uint16 { sam.ADC.CTRLA.ClearBits(sam.ADC_CTRLA_ENABLE) waitADCSync() - return uint16(val) << 4 // scales from 12 to 16-bit result + // scales to 16-bit result + switch (sam.ADC.CTRLB.Get() & sam.ADC_CTRLB_RESSEL_Msk) >> sam.ADC_CTRLB_RESSEL_Pos { + case sam.ADC_CTRLB_RESSEL_8BIT: + val = val << 8 + case sam.ADC_CTRLB_RESSEL_10BIT: + val = val << 6 + case sam.ADC_CTRLB_RESSEL_16BIT: + val = val << 4 + case sam.ADC_CTRLB_RESSEL_12BIT: + val = val << 4 + } + return val } func (a ADC) getADCChannel() uint8 { diff --git a/src/machine/machine_atsamd51.go b/src/machine/machine_atsamd51.go index a251f6704..0ce476ccc 100644 --- a/src/machine/machine_atsamd51.go +++ b/src/machine/machine_atsamd51.go @@ -863,7 +863,18 @@ func (a ADC) Get() uint16 { for bus.SYNCBUSY.HasBits(sam.ADC_SYNCBUSY_ENABLE) { } - return uint16(val) << 4 // scales from 12 to 16-bit result + // scales to 16-bit result + switch (bus.CTRLB.Get() & sam.ADC_CTRLB_RESSEL_Msk) >> sam.ADC_CTRLB_RESSEL_Pos { + case sam.ADC_CTRLB_RESSEL_8BIT: + val = val << 8 + case sam.ADC_CTRLB_RESSEL_10BIT: + val = val << 6 + case sam.ADC_CTRLB_RESSEL_16BIT: + val = val << 4 + case sam.ADC_CTRLB_RESSEL_12BIT: + val = val << 4 + } + return val } func (a ADC) getADCBus() *sam.ADC_Type {