Skip to content

Commit ea5df0f

Browse files
bgoulddeadprogram
authored andcommitted
Fixes for UART2 on Metro M4 Airlift Lite (#739)
* machine/samd51: Fixes for UART2
1 parent c09724b commit ea5df0f

File tree

4 files changed

+74
-21
lines changed

4 files changed

+74
-21
lines changed

src/machine/board_feather-m4.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ const (
5353
UART_RX_PIN = D0
5454
)
5555

56+
// UART2 pins
57+
const (
58+
UART2_TX_PIN = A4
59+
UART2_RX_PIN = A5
60+
)
61+
5662
// I2C pins
5763
const (
5864
SDA_PIN = D22 // SDA: SERCOM2/PAD[0]

src/machine/board_itsybitsy-m4.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,20 @@ const (
5151
UART_RX_PIN = D0
5252
)
5353

54+
// UART1 var is on SERCOM3, defined in atsamd51.go
55+
56+
// UART2 pins
57+
const (
58+
UART2_TX_PIN = A4
59+
UART2_RX_PIN = D2
60+
)
61+
62+
// UART2 var is on SERCOM0, defined in atsamd51.go
63+
5464
// I2C pins
5565
const (
56-
SDA_PIN = PA12 // SDA: SERCOM3/PAD[0]
57-
SCL_PIN = PA13 // SCL: SERCOM3/PAD[1]
66+
SDA_PIN = PA12 // SDA: SERCOM2/PAD[0]
67+
SCL_PIN = PA13 // SCL: SERCOM2/PAD[1]
5868
)
5969

6070
// I2C on the ItsyBitsy M4.

src/machine/board_metro-m4-airlift.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,8 @@ const (
6666
NINA_RTS = PB23
6767
)
6868

69-
// UART2 on the Metro M4 Airlift Lite connects to the onboard ESP32-WROOM chip.
70-
var (
71-
UART2 = UART{
72-
Buffer: NewRingBuffer(),
73-
Bus: sam.SERCOM0_USART_INT,
74-
Mode: PinSERCOMAlt,
75-
}
76-
)
77-
78-
//go:export SERCOM0_IRQHandler
79-
func handleUART2() {
80-
UART2.Receive(byte((UART2.Bus.DATA.Get() & 0xFF)))
81-
UART2.Bus.INTFLAG.SetBits(sam.SERCOM_USART_INT_INTFLAG_RXC)
82-
}
69+
// UART2 is on SERCOM0, defined in machine_atsamd51.go, and connects to the
70+
// onboard ESP32-WROOM chip.
8371

8472
// I2C pins
8573
const (
@@ -100,6 +88,10 @@ const (
10088
SPI0_SCK_PIN = PA13 // SCK: SERCOM2/PAD[1]
10189
SPI0_MOSI_PIN = PA12 // MOSI: SERCOM2/PAD[0]
10290
SPI0_MISO_PIN = PA14 // MISO: SERCOM2/PAD[2]
91+
92+
NINA_MOSI = SPI0_MOSI_PIN
93+
NINA_MISO = SPI0_MISO_PIN
94+
NINA_SCK = SPI0_SCK_PIN
10395
)
10496

10597
// SPI on the Metro M4.
@@ -115,6 +107,7 @@ var (
115107
MOSIPinMode: PinSERCOM,
116108
SCKPinMode: PinSERCOM,
117109
}
110+
NINA_SPI = SPI0
118111
)
119112

120113
const (

src/machine/machine_atsamd51.go

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,13 @@ var (
473473
Buffer: NewRingBuffer(),
474474
Mode: PinSERCOMAlt,
475475
}
476+
477+
// The second hardware serial port on the SAMD51. Uses the SERCOM0 interface.
478+
UART2 = UART{
479+
Buffer: NewRingBuffer(),
480+
Bus: sam.SERCOM0_USART_INT,
481+
Mode: PinSERCOMAlt,
482+
}
476483
)
477484

478485
const (
@@ -522,6 +529,8 @@ func (uart UART) Configure(config UARTConfig) {
522529
}
523530

524531
switch config.RX {
532+
case PA06:
533+
rxpad = sercomRXPad2
525534
case PA07:
526535
rxpad = sercomRXPad3
527536
case PA11:
@@ -591,11 +600,20 @@ func (uart UART) Configure(config UARTConfig) {
591600
// setup interrupt on receive
592601
uart.Bus.INTENSET.Set(sam.SERCOM_USART_INT_INTENSET_RXC)
593602

594-
// Enable RX IRQ. Currently assumes SERCOM3.
595-
arm.EnableIRQ(sam.IRQ_SERCOM3_0)
596-
arm.EnableIRQ(sam.IRQ_SERCOM3_1)
597-
arm.EnableIRQ(sam.IRQ_SERCOM3_2)
598-
arm.EnableIRQ(sam.IRQ_SERCOM3_OTHER)
603+
// Enable RX IRQ.
604+
switch uart.Bus {
605+
case sam.SERCOM0_USART_INT:
606+
arm.EnableIRQ(sam.IRQ_SERCOM0_0)
607+
arm.EnableIRQ(sam.IRQ_SERCOM0_1)
608+
arm.EnableIRQ(sam.IRQ_SERCOM0_2)
609+
arm.EnableIRQ(sam.IRQ_SERCOM0_OTHER)
610+
default:
611+
// Currently assumes SERCOM3
612+
arm.EnableIRQ(sam.IRQ_SERCOM3_0)
613+
arm.EnableIRQ(sam.IRQ_SERCOM3_1)
614+
arm.EnableIRQ(sam.IRQ_SERCOM3_2)
615+
arm.EnableIRQ(sam.IRQ_SERCOM3_OTHER)
616+
}
599617
}
600618

601619
// SetBaudRate sets the communication speed for the UART.
@@ -647,6 +665,32 @@ func handleUART1() {
647665
UART1.Bus.INTFLAG.SetBits(sam.SERCOM_USART_INT_INTFLAG_RXC)
648666
}
649667

668+
//go:export SERCOM0_0_IRQHandler
669+
func handleSERCOM0_0() {
670+
handleUART2()
671+
}
672+
673+
//go:export SERCOM0_1_IRQHandler
674+
func handleSERCOM0_1() {
675+
handleUART2()
676+
}
677+
678+
//go:export SERCOM0_2_IRQHandler
679+
func handleSERCOM0_2() {
680+
handleUART2()
681+
}
682+
683+
//go:export SERCOM0_OTHER_IRQHandler
684+
func handleSERCOM0_OTHER() {
685+
handleUART2()
686+
}
687+
688+
func handleUART2() {
689+
// should reset IRQ
690+
UART2.Receive(byte((UART2.Bus.DATA.Get() & 0xFF)))
691+
UART2.Bus.INTFLAG.SetBits(sam.SERCOM_USART_INT_INTFLAG_RXC)
692+
}
693+
650694
// I2C on the SAMD51.
651695
type I2C struct {
652696
Bus *sam.SERCOM_I2CM_Type

0 commit comments

Comments
 (0)