|
| 1 | +//go:build stm32u031 |
| 2 | + |
| 3 | +package machine |
| 4 | + |
| 5 | +import ( |
| 6 | + "device/stm32" |
| 7 | + "runtime/interrupt" |
| 8 | + "runtime/volatile" |
| 9 | + "unsafe" |
| 10 | +) |
| 11 | + |
| 12 | +var deviceIDAddr = []uintptr{0x1FFF3E50, 0x1FFF3E54, 0x1FFF3E58} |
| 13 | + |
| 14 | +// Pin constants for all stm32u0 package sizes |
| 15 | +const ( |
| 16 | + PA0 = portA + 0 |
| 17 | + PA1 = portA + 1 |
| 18 | + PA2 = portA + 2 |
| 19 | + PA3 = portA + 3 |
| 20 | + PA4 = portA + 4 |
| 21 | + PA5 = portA + 5 |
| 22 | + PA6 = portA + 6 |
| 23 | + PA7 = portA + 7 |
| 24 | + PA8 = portA + 8 |
| 25 | + PA9 = portA + 9 |
| 26 | + PA10 = portA + 10 |
| 27 | + PA11 = portA + 11 |
| 28 | + PA12 = portA + 12 |
| 29 | + PA13 = portA + 13 |
| 30 | + PA14 = portA + 14 |
| 31 | + PA15 = portA + 15 |
| 32 | + |
| 33 | + PB0 = portB + 0 |
| 34 | + PB1 = portB + 1 |
| 35 | + PB2 = portB + 2 |
| 36 | + PB3 = portB + 3 |
| 37 | + PB4 = portB + 4 |
| 38 | + PB5 = portB + 5 |
| 39 | + PB6 = portB + 6 |
| 40 | + PB7 = portB + 7 |
| 41 | + PB8 = portB + 8 |
| 42 | + PB9 = portB + 9 |
| 43 | + PB10 = portB + 10 |
| 44 | + PB11 = portB + 11 |
| 45 | + PB12 = portB + 12 |
| 46 | + PB13 = portB + 13 |
| 47 | + PB14 = portB + 14 |
| 48 | + PB15 = portB + 15 |
| 49 | + |
| 50 | + PC0 = portC + 0 |
| 51 | + PC1 = portC + 1 |
| 52 | + PC2 = portC + 2 |
| 53 | + PC3 = portC + 3 |
| 54 | + PC4 = portC + 4 |
| 55 | + PC5 = portC + 5 |
| 56 | + PC6 = portC + 6 |
| 57 | + PC7 = portC + 7 |
| 58 | + PC8 = portC + 8 |
| 59 | + PC9 = portC + 9 |
| 60 | + PC10 = portC + 10 |
| 61 | + PC11 = portC + 11 |
| 62 | + PC12 = portC + 12 |
| 63 | + PC13 = portC + 13 |
| 64 | + PC14 = portC + 14 |
| 65 | + PC15 = portC + 15 |
| 66 | + |
| 67 | + PD0 = portD + 0 |
| 68 | + PD1 = portD + 1 |
| 69 | + PD2 = portD + 2 |
| 70 | + PD3 = portD + 3 |
| 71 | + PD4 = portD + 4 |
| 72 | + PD5 = portD + 5 |
| 73 | + PD6 = portD + 6 |
| 74 | + PD7 = portD + 7 |
| 75 | + PD8 = portD + 8 |
| 76 | + PD9 = portD + 9 |
| 77 | + PD10 = portD + 10 |
| 78 | + PD11 = portD + 11 |
| 79 | + PD12 = portD + 12 |
| 80 | + PD13 = portD + 13 |
| 81 | + PD14 = portD + 14 |
| 82 | + PD15 = portD + 15 |
| 83 | + |
| 84 | + PE0 = portE + 0 |
| 85 | + PE1 = portE + 1 |
| 86 | + PE2 = portE + 2 |
| 87 | + PE3 = portE + 3 |
| 88 | + PE4 = portE + 4 |
| 89 | + PE5 = portE + 5 |
| 90 | + PE6 = portE + 6 |
| 91 | + PE7 = portE + 7 |
| 92 | + PE8 = portE + 8 |
| 93 | + PE9 = portE + 9 |
| 94 | + PE10 = portE + 10 |
| 95 | + PE11 = portE + 11 |
| 96 | + PE12 = portE + 12 |
| 97 | + PE13 = portE + 13 |
| 98 | + PE14 = portE + 14 |
| 99 | + PE15 = portE + 15 |
| 100 | + |
| 101 | + PF0 = portF + 0 |
| 102 | + PF1 = portF + 1 |
| 103 | + PF2 = portF + 2 |
| 104 | + PF3 = portF + 3 |
| 105 | + PF4 = portF + 4 |
| 106 | + PF5 = portF + 5 |
| 107 | + PF6 = portF + 6 |
| 108 | + PF7 = portF + 7 |
| 109 | + PF8 = portF + 8 |
| 110 | + PF9 = portF + 9 |
| 111 | + PF10 = portF + 10 |
| 112 | + PF11 = portF + 11 |
| 113 | + PF12 = portF + 12 |
| 114 | + PF13 = portF + 13 |
| 115 | + PF14 = portF + 14 |
| 116 | + PF15 = portF + 15 |
| 117 | +) |
| 118 | + |
| 119 | +func (p Pin) getPort() *stm32.GPIO_Type { |
| 120 | + switch p / 16 { |
| 121 | + case 0: |
| 122 | + return stm32.GPIOA |
| 123 | + case 1: |
| 124 | + return stm32.GPIOB |
| 125 | + case 2: |
| 126 | + return stm32.GPIOC |
| 127 | + case 3: |
| 128 | + return stm32.GPIOD |
| 129 | + case 4: |
| 130 | + return stm32.GPIOE |
| 131 | + case 5: |
| 132 | + return stm32.GPIOF |
| 133 | + default: |
| 134 | + panic("machine: unknown port") |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// enableClock enables the clock for this desired GPIO port. |
| 139 | +func (p Pin) enableClock() { |
| 140 | + switch p / 16 { |
| 141 | + case 0: |
| 142 | + stm32.RCC.IOPENR.SetBits(stm32.RCC_IOPENR_GPIOAEN) |
| 143 | + case 1: |
| 144 | + stm32.RCC.IOPENR.SetBits(stm32.RCC_IOPENR_GPIOBEN) |
| 145 | + case 2: |
| 146 | + stm32.RCC.IOPENR.SetBits(stm32.RCC_IOPENR_GPIOCEN) |
| 147 | + case 3: |
| 148 | + stm32.RCC.IOPENR.SetBits(stm32.RCC_IOPENR_GPIODEN) |
| 149 | + case 4: |
| 150 | + stm32.RCC.IOPENR.SetBits(stm32.RCC_IOPENR_GPIOEEN) |
| 151 | + case 5: |
| 152 | + stm32.RCC.IOPENR.SetBits(stm32.RCC_IOPENR_GPIOFEN) |
| 153 | + default: |
| 154 | + panic("machine: unknown port") |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func enableAltFuncClock(bus unsafe.Pointer) { |
| 159 | + switch bus { |
| 160 | + case unsafe.Pointer(stm32.I2C1): |
| 161 | + stm32.RCC.APBENR1.SetBits(stm32.RCC_APBENR1_I2C1EN) |
| 162 | + case unsafe.Pointer(stm32.I2C2): |
| 163 | + stm32.RCC.APBENR1.SetBits(stm32.RCC_APBENR1_I2C2EN) |
| 164 | + case unsafe.Pointer(stm32.I2C3): |
| 165 | + stm32.RCC.APBENR1.SetBits(stm32.RCC_APBENR1_I2C3EN) |
| 166 | + case unsafe.Pointer(stm32.USART1): |
| 167 | + stm32.RCC.APBENR2.SetBits(stm32.RCC_APBENR2_USART1EN) |
| 168 | + case unsafe.Pointer(stm32.USART2): |
| 169 | + stm32.RCC.APBENR1.SetBits(stm32.RCC_APBENR1_USART2EN) |
| 170 | + case unsafe.Pointer(stm32.USART3): |
| 171 | + stm32.RCC.APBENR1.SetBits(stm32.RCC_APBENR1_USART3EN) |
| 172 | + case unsafe.Pointer(stm32.USART4): |
| 173 | + stm32.RCC.APBENR1.SetBits(stm32.RCC_APBENR1_USART4EN) |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +var ( |
| 178 | + // TODO: implement output channels |
| 179 | + |
| 180 | + TIM1 = TIM{ |
| 181 | + EnableRegister: &stm32.RCC.APBENR2, |
| 182 | + EnableFlag: stm32.RCC_APBENR2_TIM1EN, |
| 183 | + Device: stm32.TIM1, |
| 184 | + Channels: [4]TimerChannel{ |
| 185 | + TimerChannel{Pins: []PinFunction{}}, |
| 186 | + TimerChannel{Pins: []PinFunction{}}, |
| 187 | + TimerChannel{Pins: []PinFunction{}}, |
| 188 | + TimerChannel{Pins: []PinFunction{}}, |
| 189 | + }, |
| 190 | + busFreq: 4e6, |
| 191 | + } |
| 192 | + |
| 193 | + TIM2 = TIM{ |
| 194 | + EnableRegister: &stm32.RCC.APBENR1, |
| 195 | + EnableFlag: stm32.RCC_APBENR1_TIM2EN, |
| 196 | + Device: stm32.TIM2, |
| 197 | + Channels: [4]TimerChannel{ |
| 198 | + TimerChannel{Pins: []PinFunction{}}, |
| 199 | + TimerChannel{Pins: []PinFunction{}}, |
| 200 | + TimerChannel{Pins: []PinFunction{}}, |
| 201 | + TimerChannel{Pins: []PinFunction{}}, |
| 202 | + }, |
| 203 | + busFreq: 4e6, |
| 204 | + } |
| 205 | + |
| 206 | + TIM3 = TIM{ |
| 207 | + EnableRegister: &stm32.RCC.APBENR1, |
| 208 | + EnableFlag: stm32.RCC_APBENR1_TIM3EN, |
| 209 | + Device: stm32.TIM3, |
| 210 | + Channels: [4]TimerChannel{ |
| 211 | + TimerChannel{Pins: []PinFunction{}}, |
| 212 | + TimerChannel{Pins: []PinFunction{}}, |
| 213 | + TimerChannel{Pins: []PinFunction{}}, |
| 214 | + TimerChannel{Pins: []PinFunction{}}, |
| 215 | + }, |
| 216 | + busFreq: 4e6, |
| 217 | + } |
| 218 | + |
| 219 | + TIM6 = TIM{ |
| 220 | + EnableRegister: &stm32.RCC.APBENR1, |
| 221 | + EnableFlag: stm32.RCC_APBENR1_TIM6EN, |
| 222 | + Device: stm32.TIM6, |
| 223 | + Channels: [4]TimerChannel{ |
| 224 | + TimerChannel{Pins: []PinFunction{}}, |
| 225 | + TimerChannel{Pins: []PinFunction{}}, |
| 226 | + TimerChannel{Pins: []PinFunction{}}, |
| 227 | + TimerChannel{Pins: []PinFunction{}}, |
| 228 | + }, |
| 229 | + busFreq: 4e6, |
| 230 | + } |
| 231 | + |
| 232 | + TIM7 = TIM{ |
| 233 | + EnableRegister: &stm32.RCC.APBENR1, |
| 234 | + EnableFlag: stm32.RCC_APBENR1_TIM7EN, |
| 235 | + Device: stm32.TIM7, |
| 236 | + Channels: [4]TimerChannel{ |
| 237 | + TimerChannel{Pins: []PinFunction{}}, |
| 238 | + TimerChannel{Pins: []PinFunction{}}, |
| 239 | + TimerChannel{Pins: []PinFunction{}}, |
| 240 | + TimerChannel{Pins: []PinFunction{}}, |
| 241 | + }, |
| 242 | + busFreq: 4e6, |
| 243 | + } |
| 244 | + |
| 245 | + TIM15 = TIM{ |
| 246 | + EnableRegister: &stm32.RCC.APBENR2, |
| 247 | + EnableFlag: stm32.RCC_APBENR2_TIM15EN, |
| 248 | + Device: stm32.TIM15, |
| 249 | + Channels: [4]TimerChannel{ |
| 250 | + TimerChannel{Pins: []PinFunction{}}, |
| 251 | + TimerChannel{Pins: []PinFunction{}}, |
| 252 | + TimerChannel{Pins: []PinFunction{}}, |
| 253 | + TimerChannel{Pins: []PinFunction{}}, |
| 254 | + }, |
| 255 | + busFreq: 4e6, |
| 256 | + } |
| 257 | + |
| 258 | + TIM16 = TIM{ |
| 259 | + EnableRegister: &stm32.RCC.APBENR2, |
| 260 | + EnableFlag: stm32.RCC_APBENR2_TIM16EN, |
| 261 | + Device: stm32.TIM16, |
| 262 | + Channels: [4]TimerChannel{ |
| 263 | + TimerChannel{Pins: []PinFunction{}}, |
| 264 | + TimerChannel{Pins: []PinFunction{}}, |
| 265 | + TimerChannel{Pins: []PinFunction{}}, |
| 266 | + TimerChannel{Pins: []PinFunction{}}, |
| 267 | + }, |
| 268 | + busFreq: 4e6, |
| 269 | + } |
| 270 | +) |
| 271 | + |
| 272 | +func (t *TIM) registerUPInterrupt() interrupt.Interrupt { |
| 273 | + switch t { |
| 274 | + case &TIM1: |
| 275 | + return interrupt.New(stm32.IRQ_TIM1_BRK_UP_TRG_COM, TIM1.handleUPInterrupt) |
| 276 | + case &TIM2: |
| 277 | + return interrupt.New(stm32.IRQ_TIM2, TIM2.handleUPInterrupt) |
| 278 | + case &TIM3: |
| 279 | + return interrupt.New(stm32.IRQ_TIM3, TIM3.handleUPInterrupt) |
| 280 | + case &TIM6: |
| 281 | + return interrupt.New(stm32.IRQ_TIM6_DAC_LPTIM1, TIM6.handleUPInterrupt) |
| 282 | + case &TIM7: |
| 283 | + return interrupt.New(stm32.IRQ_TIM7_LPTIM2, TIM7.handleUPInterrupt) |
| 284 | + case &TIM15: |
| 285 | + return interrupt.New(stm32.IRQ_TIM15_LPTIM3, TIM15.handleUPInterrupt) |
| 286 | + case &TIM16: |
| 287 | + return interrupt.New(stm32.IRQ_TIM16, TIM16.handleUPInterrupt) |
| 288 | + } |
| 289 | + |
| 290 | + return interrupt.Interrupt{} |
| 291 | +} |
| 292 | + |
| 293 | +func (t *TIM) registerOCInterrupt() interrupt.Interrupt { |
| 294 | + switch t { |
| 295 | + case &TIM1: |
| 296 | + return interrupt.New(stm32.IRQ_TIM1_CC, TIM1.handleOCInterrupt) |
| 297 | + case &TIM2: |
| 298 | + return interrupt.New(stm32.IRQ_TIM2, TIM2.handleOCInterrupt) |
| 299 | + case &TIM3: |
| 300 | + return interrupt.New(stm32.IRQ_TIM3, TIM3.handleOCInterrupt) |
| 301 | + case &TIM6: |
| 302 | + return interrupt.New(stm32.IRQ_TIM6_DAC_LPTIM1, TIM6.handleOCInterrupt) |
| 303 | + case &TIM7: |
| 304 | + return interrupt.New(stm32.IRQ_TIM7_LPTIM2, TIM7.handleOCInterrupt) |
| 305 | + case &TIM15: |
| 306 | + return interrupt.New(stm32.IRQ_TIM15_LPTIM3, TIM15.handleOCInterrupt) |
| 307 | + case &TIM16: |
| 308 | + return interrupt.New(stm32.IRQ_TIM16, TIM16.handleOCInterrupt) |
| 309 | + } |
| 310 | + |
| 311 | + return interrupt.Interrupt{} |
| 312 | +} |
| 313 | + |
| 314 | +func (t *TIM) enableMainOutput() { |
| 315 | + t.Device.BDTR.SetBits(stm32.TIM_BDTR_MOE) |
| 316 | +} |
| 317 | + |
| 318 | +type arrtype = uint16 |
| 319 | +type psctype = uint16 |
| 320 | +type arrRegType = volatile.Register16 |
| 321 | + |
| 322 | +const ( |
| 323 | + ARR_MAX = 0x10000 |
| 324 | + PSC_MAX = 0x10000 |
| 325 | +) |
| 326 | + |
| 327 | +const ( |
| 328 | + UART_TX_PIN = NoPin |
| 329 | + UART_RX_PIN = NoPin |
| 330 | +) |
| 331 | + |
| 332 | +func (uart *UART) configurePins(config UARTConfig) { |
| 333 | + panic("unimplemented: UART") |
| 334 | +} |
| 335 | + |
| 336 | +func (uart *UART) getBaudRateDivisor(baudRate uint32) uint32 { |
| 337 | + panic("unimplemented: UART") |
| 338 | +} |
| 339 | + |
| 340 | +func (uart *UART) setRegisters() uint32 { |
| 341 | + panic("unimplemented: UART") |
| 342 | +} |
| 343 | + |
| 344 | +const ( |
| 345 | + I2C0_SCL_PIN = NoPin |
| 346 | + I2C0_SDA_PIN = NoPin |
| 347 | +) |
| 348 | + |
| 349 | +func (i2c I2C) getFreqRange(br uint32) uint32 { |
| 350 | + panic("unimplemented: I2C") |
| 351 | +} |
0 commit comments