diff --git a/src/machine/board_arduino_uno_q.go b/src/machine/board_arduino_uno_q.go index 4d11ca217..9e8996d9f 100644 --- a/src/machine/board_arduino_uno_q.go +++ b/src/machine/board_arduino_uno_q.go @@ -19,6 +19,14 @@ const ( A4 = PC1 A5 = PC0 + // ADC pin aliases + ADC0 = A0 + ADC1 = A1 + ADC2 = A2 + ADC3 = A3 + ADC4 = A4 + ADC5 = A5 + D0 = PB7 D1 = PB6 D2 = PB3 diff --git a/src/machine/machine_stm32_adc_u5.go b/src/machine/machine_stm32_adc_u5.go new file mode 100644 index 000000000..6620185ff --- /dev/null +++ b/src/machine/machine_stm32_adc_u5.go @@ -0,0 +1,131 @@ +//go:build stm32u5 + +package machine + +import ( + "device/stm32" + "unsafe" +) + +// InitADC initializes the registers needed for ADC1. +func InitADC() { + // Enable ADC bus clock. + enableAltFuncClock(unsafe.Pointer(stm32.ADC1)) + + // Declare VDDA analog supply valid. Without ASV the ADC LDO cannot start. + stm32.PWR.SVMCR.SetBits(stm32.PWR_SVMCR_ASV) + + // Set ADC12_Common CCR: synchronous clock HCLK/1. + // ADC12_Common is mapped as ADC_Type; CCR at offset 0x08 = .CR field. + stm32.ADC12_Common.CR.Set(1 << 16) // CKMODE=01 + + // Exit deep power-down mode. + stm32.ADC1.CR.ClearBits(stm32.ADC_CR_DEEPPWD) + + // Enable internal voltage regulator and wait for LDO ready. + stm32.ADC1.CR.SetBits(stm32.ADC_CR_ADVREGEN) + for !stm32.ADC1.ISR.HasBits(stm32.ADC_ISR_LDORDY) { + } + + // Calibrate ADC (single-ended). + stm32.ADC1.CR.SetBits(stm32.ADC_CR_ADCAL) + for stm32.ADC1.CR.HasBits(stm32.ADC_CR_ADCAL) { + } + + // 12-bit resolution, overwrite DR on overrun. + stm32.ADC1.CFGR1.Set(stm32.ADC_CFGR1_RES_TwelveBit<