|
| 1 | +//go:build stm32u5 |
| 2 | + |
| 3 | +package machine |
| 4 | + |
| 5 | +import ( |
| 6 | + "device/stm32" |
| 7 | + "unsafe" |
| 8 | +) |
| 9 | + |
| 10 | +// InitADC initializes the registers needed for ADC1. |
| 11 | +func InitADC() { |
| 12 | + // Enable ADC bus clock. |
| 13 | + enableAltFuncClock(unsafe.Pointer(stm32.ADC1)) |
| 14 | + |
| 15 | + // Declare VDDA analog supply valid. Without ASV the ADC LDO cannot start. |
| 16 | + stm32.PWR.SVMCR.SetBits(stm32.PWR_SVMCR_ASV) |
| 17 | + |
| 18 | + // Set ADC12_Common CCR: synchronous clock HCLK/1. |
| 19 | + // ADC12_Common is mapped as ADC_Type; CCR at offset 0x08 = .CR field. |
| 20 | + stm32.ADC12_Common.CR.Set(1 << 16) // CKMODE=01 |
| 21 | + |
| 22 | + // Exit deep power-down mode. |
| 23 | + stm32.ADC1.CR.ClearBits(stm32.ADC_CR_DEEPPWD) |
| 24 | + |
| 25 | + // Enable internal voltage regulator and wait for LDO ready. |
| 26 | + stm32.ADC1.CR.SetBits(stm32.ADC_CR_ADVREGEN) |
| 27 | + for !stm32.ADC1.ISR.HasBits(stm32.ADC_ISR_LDORDY) { |
| 28 | + } |
| 29 | + |
| 30 | + // Calibrate ADC (single-ended). |
| 31 | + stm32.ADC1.CR.SetBits(stm32.ADC_CR_ADCAL) |
| 32 | + for stm32.ADC1.CR.HasBits(stm32.ADC_CR_ADCAL) { |
| 33 | + } |
| 34 | + |
| 35 | + // 12-bit resolution, overwrite DR on overrun. |
| 36 | + stm32.ADC1.CFGR1.Set(stm32.ADC_CFGR1_RES_TwelveBit<<stm32.ADC_CFGR1_RES_Pos | |
| 37 | + stm32.ADC_CFGR1_OVRMOD) |
| 38 | + |
| 39 | + // Pre-select all channels (PCSEL must be written while ADEN=0). |
| 40 | + stm32.ADC1.PCSEL.Set(0xFFFFF) |
| 41 | + |
| 42 | + // Clear ADRDY flag, enable ADC, wait for ready. |
| 43 | + stm32.ADC1.ISR.Set(stm32.ADC_ISR_ADRDY) |
| 44 | + stm32.ADC1.CR.SetBits(stm32.ADC_CR_ADEN) |
| 45 | + for !stm32.ADC1.ISR.HasBits(stm32.ADC_ISR_ADRDY) { |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// Configure configures an ADC pin to be able to read analog data. |
| 50 | +func (a ADC) Configure(config ADCConfig) { |
| 51 | + a.Pin.Configure(PinConfig{Mode: PinInputAnalog}) |
| 52 | + |
| 53 | + ch := a.getChannel() |
| 54 | + |
| 55 | + // Set sampling time for this channel. |
| 56 | + // Channels 0-9 use SMPR1, channels 10-19 use SMPR2. |
| 57 | + // Each channel uses 3 bits. Default to 36.5 cycles for reliable reads. |
| 58 | + const smpVal = 0x4 // 36.5 ADC clock cycles |
| 59 | + if ch <= 9 { |
| 60 | + pos := uint8(ch) * 3 |
| 61 | + stm32.ADC1.SMPR1.ReplaceBits(smpVal, 0x7, pos) |
| 62 | + } else { |
| 63 | + pos := uint8(ch-10) * 3 |
| 64 | + stm32.ADC1.SMPR2.ReplaceBits(smpVal, 0x7, pos) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// Get returns the current value of an ADC pin in the range 0..0xffff. |
| 69 | +func (a ADC) Get() uint16 { |
| 70 | + ch := uint32(a.getChannel()) |
| 71 | + |
| 72 | + // PCSEL already set for all channels in InitADC (must be written while ADEN=0). |
| 73 | + |
| 74 | + // Set up regular sequence: 1 conversion, channel in SQ1 |
| 75 | + // Note: ADC_SQR1_SQ1_Pos (0xC) in the device header is wrong for the 14-bit ADC1/ADC2. |
| 76 | + // RM0456 shows SQ1 at bits [10:6] = position 6. |
| 77 | + stm32.ADC1.SQR1.Set(ch << 6) // L=0 means 1 conversion |
| 78 | + |
| 79 | + // Start conversion |
| 80 | + stm32.ADC1.CR.SetBits(stm32.ADC_CR_ADSTART) |
| 81 | + |
| 82 | + // Wait for end of conversion |
| 83 | + for stm32.ADC1.ISR.Get()&stm32.ADC_ISR_EOC == 0 { |
| 84 | + } |
| 85 | + |
| 86 | + // Read 12-bit result and scale to 16-bit |
| 87 | + result := uint16(stm32.ADC1.DR.Get()) << 4 |
| 88 | + |
| 89 | + return result |
| 90 | +} |
| 91 | + |
| 92 | +// getChannel returns the ADC1 channel number for a given GPIO pin. |
| 93 | +// STM32U585 ADC1 channel mapping (from datasheet Table 24): |
| 94 | +// |
| 95 | +// PC0=CH1, PC1=CH2, PC2=CH3, PC3=CH4 |
| 96 | +// PA0=CH5, PA1=CH6, PA2=CH7, PA3=CH8 |
| 97 | +// PA4=CH9, PA5=CH10, PA6=CH11, PA7=CH12 |
| 98 | +// PB0=CH13, PB1=CH14 |
| 99 | +func (a ADC) getChannel() uint8 { |
| 100 | + switch a.Pin { |
| 101 | + case PC0: |
| 102 | + return 1 |
| 103 | + case PC1: |
| 104 | + return 2 |
| 105 | + case PC2: |
| 106 | + return 3 |
| 107 | + case PC3: |
| 108 | + return 4 |
| 109 | + case PA0: |
| 110 | + return 5 |
| 111 | + case PA1: |
| 112 | + return 6 |
| 113 | + case PA2: |
| 114 | + return 7 |
| 115 | + case PA3: |
| 116 | + return 8 |
| 117 | + case PA4: |
| 118 | + return 9 |
| 119 | + case PA5: |
| 120 | + return 10 |
| 121 | + case PA6: |
| 122 | + return 11 |
| 123 | + case PA7: |
| 124 | + return 12 |
| 125 | + case PB0: |
| 126 | + return 13 |
| 127 | + case PB1: |
| 128 | + return 14 |
| 129 | + } |
| 130 | + return 0 |
| 131 | +} |
0 commit comments