Skip to content

Commit d10680c

Browse files
committed
WIP
1 parent 6b9b48d commit d10680c

9 files changed

+216
-37
lines changed

src/machine/board_embedfire_py32f002b copy.go

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
1-
//go:build embedfire_py32f030
1+
//go:build embedfire_py32f002b
22

3-
// Pin mappings for the Embedfire PY32F030 board.
3+
// Pin mappings for the Embedfire PY32F002B board.
44
// Only LED and button aliases are provided.
55

66
package machine
77

88
// LEDs
99
const (
10-
LED2 = PA2
11-
LED3 = PA3
10+
LED2 = PA1
11+
LED3 = PA5
1212
LED4 = PA4
1313
LED = LED2
1414
)
1515

1616
// Buttons
1717
const (
18-
KEY1 = PA5
19-
KEY2 = PA6
18+
KEY1 = PA3
19+
KEY2 = PA0
20+
)
21+
22+
// UART
23+
const (
24+
DEFAULT_UART_TX_PIN = PA6
25+
DEFAULT_UART_RX_PIN = PA7
26+
DEFAULT_UART_TX_PIN_AF = 1
27+
DEFAULT_UART_RX_PIN_AF = 3
2028
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//go:build embedfire_py32f030
2+
3+
// Pin mappings for the Embedfire PY32F030 board.
4+
// Only LED and button aliases are provided.
5+
6+
package machine
7+
8+
// LEDs
9+
const (
10+
LED2 = PA2
11+
LED3 = PA3
12+
LED4 = PA4
13+
LED = LED2
14+
)
15+
16+
// Buttons
17+
const (
18+
KEY1 = PA5
19+
KEY2 = PA6
20+
)
21+
22+
// UART
23+
const (
24+
DEFAULT_UART_TX_PIN = PA7
25+
DEFAULT_UART_RX_PIN = PA8
26+
DEFAULT_UART_TX_PIN_AF = 8
27+
DEFAULT_UART_RX_PIN_AF = 8
28+
)

src/machine/machine_py32_pin.go

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ const (
3131
PA5
3232
PA6
3333
PA7
34+
PA8
35+
PA9
36+
PA10
37+
PA11
38+
PA12
39+
PA13
40+
PA14
41+
PA15
3442
)
3543

3644
// Port B pins.
@@ -43,6 +51,14 @@ const (
4351
PB5
4452
PB6
4553
PB7
54+
PB8
55+
PB9
56+
PB10
57+
PB11
58+
PB12
59+
PB13
60+
PB14
61+
PB15
4662
)
4763

4864
// Port C pins.
@@ -55,6 +71,14 @@ const (
5571
PC5
5672
PC6
5773
PC7
74+
PC8
75+
PC9
76+
PC10
77+
PC11
78+
PC12
79+
PC13
80+
PC14
81+
PC15
5882
)
5983

6084
// Port D pins.
@@ -67,6 +91,14 @@ const (
6791
PD5
6892
PD6
6993
PD7
94+
PD8
95+
PD9
96+
PD10
97+
PD11
98+
PD12
99+
PD13
100+
PD14
101+
PD15
70102
)
71103

72104
// Port E pins.
@@ -79,6 +111,14 @@ const (
79111
PE5
80112
PE6
81113
PE7
114+
PE8
115+
PE9
116+
PE10
117+
PE11
118+
PE12
119+
PE13
120+
PE14
121+
PE15
82122
)
83123

84124
// Port F pins.
@@ -91,6 +131,14 @@ const (
91131
PF5
92132
PF6
93133
PF7
134+
PF8
135+
PF9
136+
PF10
137+
PF11
138+
PF12
139+
PF13
140+
PF14
141+
PF15
94142
)
95143

96144
// PinMode values specific to PY32: only GPIO direction and pull configuration.
@@ -179,15 +227,6 @@ func (p Pin) Configure(config PinConfig) {
179227
}
180228
}
181229

182-
func (p Pin) SetAltFunc(af uint8) {
183-
port, pin := p.getPort()
184-
if pin >= 8 {
185-
port.AFRH.ReplaceBits(uint32(af), 0xF, (pin%8)*4)
186-
} else {
187-
port.AFRL.ReplaceBits(uint32(af), 0xF, (pin%8)*4)
188-
}
189-
}
190-
191230
func (p Pin) enableClock() {
192231
portNo := p.getPortNumber()
193232
py32.RCC.IOPENR.SetBits(1 << portNo)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build py32 && !py32f002bxx
2+
3+
package machine
4+
5+
func (p Pin) SetAltFunc(af uint8) {
6+
port, pin := p.getPort()
7+
if pin >= 8 {
8+
port.AFRH.ReplaceBits(uint32(af), 0xF, (pin%8)*4)
9+
} else {
10+
port.AFRL.ReplaceBits(uint32(af), 0xF, (pin%8)*4)
11+
}
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build py32 && py32f002bxx
2+
3+
package machine
4+
5+
func (p Pin) SetAltFunc(af uint8) {
6+
port, pin := p.getPort()
7+
port.AFRL.ReplaceBits(uint32(af), 0xF, (pin%8)*4)
8+
}

src/machine/machine_py32_uart.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//go:build py32
2+
3+
package machine
4+
5+
import (
6+
"device/py32"
7+
"runtime/interrupt"
8+
)
9+
10+
// Remember the clock used for baud rate calculations so Configure() can be
11+
// called without explicitly passing the clock.
12+
var py32UARTClockHz uint32 = 24_000_000
13+
14+
// UART implements a minimal USART1 driver for PY32 parts.
15+
type UART struct {
16+
Bus *py32.USART_Type
17+
Buffer *RingBuffer
18+
irq interrupt.Interrupt
19+
}
20+
21+
var DefaultUART = &UART{Bus: py32.USART1, Buffer: NewRingBuffer()}
22+
23+
// ConfigureWithClock initializes the UART using the provided peripheral clock
24+
// frequency (in Hz). This avoids assuming a fixed MCU clock.
25+
func (uart *UART) ConfigureWithClock(config UARTConfig, clockHz uint32) error {
26+
if config.BaudRate == 0 {
27+
config.BaudRate = 115200
28+
}
29+
30+
// Configure default pins if they weren't provided.
31+
if config.TX == 0 {
32+
ConfigureUARTPin(DEFAULT_UART_TX_PIN, DEFAULT_UART_TX_PIN_AF)
33+
}
34+
35+
if config.RX == 0 {
36+
ConfigureUARTPin(DEFAULT_UART_RX_PIN, DEFAULT_UART_RX_PIN_AF)
37+
}
38+
39+
// Enable peripheral clock.
40+
py32.RCC.APBENR2.SetBits(py32.RCC_APBENR2_USART1EN)
41+
42+
// Reset control registers to a known state.
43+
uart.Bus.CR1.Set(0)
44+
uart.Bus.CR2.Set(0)
45+
uart.Bus.CR3.Set(0)
46+
47+
// Oversampling by 16: BRR expects fck/baud.
48+
divider := (clockHz + (config.BaudRate / 2)) / config.BaudRate
49+
uart.Bus.BRR.Set(divider)
50+
51+
// Enable transmitter, receiver, RX interrupt, and the peripheral.
52+
uart.Bus.CR1.Set(py32.USART_CR1_TE | py32.USART_CR1_RE | py32.USART_CR1_RXNEIE | py32.USART_CR1_UE)
53+
54+
// Hook interrupt.
55+
uart.irq = interrupt.New(py32.IRQ_USART1, handleUartInterrupt)
56+
uart.irq.SetPriority(0xc0)
57+
uart.irq.Enable()
58+
59+
return nil
60+
}
61+
62+
// Configure uses the last stored clock (defaulting to 24 MHz). Call
63+
// ConfigureWithClock for explicit control.
64+
func (uart *UART) Configure(config UARTConfig) error {
65+
return uart.ConfigureWithClock(config, py32UARTClockHz)
66+
}
67+
68+
// InitSerialWithClock configures the default Serial using the supplied
69+
// peripheral clock frequency.
70+
func InitSerialWithClock(clockHz uint32) {
71+
py32UARTClockHz = clockHz
72+
//Serial.ConfigureWithClock(UARTConfig{}, clockHz)
73+
}
74+
75+
// Configure pin for use by UART
76+
func ConfigureUARTPin(pin Pin, af uint8) {
77+
pin.enableClock()
78+
port, n := pin.getPort()
79+
pos := (n % 16) * 2
80+
81+
// Alternate function mode is encoded as 0b10.
82+
port.MODER.ReplaceBits(2, gpioModeMask, pos)
83+
port.PUPDR.ReplaceBits(gpioPullUp, gpioPullMask, pos)
84+
port.OSPEEDR.ReplaceBits(gpioOutputSpeedHigh, gpioOutputSpeedMask, pos)
85+
pin.SetAltFunc(af)
86+
}
87+
88+
func handleUartInterrupt(interrupt.Interrupt) {
89+
uart := DefaultUART
90+
data := uint8(uart.Bus.DR.Get())
91+
uart.Receive(data)
92+
}
93+
94+
func (uart *UART) writeByte(c byte) error {
95+
for uart.Bus.SR.Get()&py32.USART_SR_TXE == 0 {
96+
}
97+
uart.Bus.DR.Set(uint32(c))
98+
return nil
99+
}
100+
101+
func (uart *UART) flush() {
102+
for uart.Bus.SR.Get()&py32.USART_SR_TC == 0 {
103+
}
104+
}

src/machine/uart.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build atmega || esp || nrf || sam || sifive || stm32 || k210 || nxp || rp2040 || rp2350
1+
//go:build atmega || esp || nrf || sam || sifive || stm32 || k210 || nxp || rp2040 || rp2350 || py32
22

33
package machine
44

src/runtime/runtime_py32.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func main() {
1515

1616
py32.RCC.SetICSCR_HSI_FS(py32.RCC_ICSCR_HSI_FS_Freq24MHz)
1717

18-
ConfigureSystemTimer(24e6)
18+
ConfigureSystemTimer(24_000_000)
1919
machine.InitSerial()
2020

2121
run()

0 commit comments

Comments
 (0)