Skip to content

Commit bb509ec

Browse files
sago35deadprogram
authored andcommitted
atsamd51, atsamd21: fix ADC.Get() value at 8bit and 10bit
1 parent 944f022 commit bb509ec

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/machine/machine_atsamd21.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,18 @@ func (a ADC) Get() uint16 {
433433
sam.ADC.CTRLA.ClearBits(sam.ADC_CTRLA_ENABLE)
434434
waitADCSync()
435435

436-
return uint16(val) << 4 // scales from 12 to 16-bit result
436+
// scales to 16-bit result
437+
switch (sam.ADC.CTRLB.Get() & sam.ADC_CTRLB_RESSEL_Msk) >> sam.ADC_CTRLB_RESSEL_Pos {
438+
case sam.ADC_CTRLB_RESSEL_8BIT:
439+
val = val << 8
440+
case sam.ADC_CTRLB_RESSEL_10BIT:
441+
val = val << 6
442+
case sam.ADC_CTRLB_RESSEL_16BIT:
443+
val = val << 4
444+
case sam.ADC_CTRLB_RESSEL_12BIT:
445+
val = val << 4
446+
}
447+
return val
437448
}
438449

439450
func (a ADC) getADCChannel() uint8 {

src/machine/machine_atsamd51.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,18 @@ func (a ADC) Get() uint16 {
863863
for bus.SYNCBUSY.HasBits(sam.ADC_SYNCBUSY_ENABLE) {
864864
}
865865

866-
return uint16(val) << 4 // scales from 12 to 16-bit result
866+
// scales to 16-bit result
867+
switch (bus.CTRLB.Get() & sam.ADC_CTRLB_RESSEL_Msk) >> sam.ADC_CTRLB_RESSEL_Pos {
868+
case sam.ADC_CTRLB_RESSEL_8BIT:
869+
val = val << 8
870+
case sam.ADC_CTRLB_RESSEL_10BIT:
871+
val = val << 6
872+
case sam.ADC_CTRLB_RESSEL_16BIT:
873+
val = val << 4
874+
case sam.ADC_CTRLB_RESSEL_12BIT:
875+
val = val << 4
876+
}
877+
return val
867878
}
868879

869880
func (a ADC) getADCBus() *sam.ADC_Type {

0 commit comments

Comments
 (0)