Skip to content

Commit 012c4a0

Browse files
aykevldeadprogram
authored andcommitted
machine: switch to modern interrupt registration method
This saves about 112 bytes in flash and 288 bytes in RAM when the UART is not used.
1 parent dd0fb1d commit 012c4a0

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

src/machine/machine_atsamd51.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -636,10 +636,10 @@ func (a ADC) getADCChannel() uint8 {
636636

637637
// UART on the SAMD51.
638638
type UART struct {
639-
Buffer *RingBuffer
640-
Bus *sam.SERCOM_USART_INT_Type
641-
SERCOM uint8
642-
IRQVal uint32 // RXC interrupt
639+
Buffer *RingBuffer
640+
Bus *sam.SERCOM_USART_INT_Type
641+
SERCOM uint8
642+
Interrupt interrupt.Interrupt // RXC interrupt
643643
}
644644

645645
var (
@@ -651,18 +651,22 @@ var (
651651
Buffer: NewRingBuffer(),
652652
Bus: sam.SERCOM3_USART_INT,
653653
SERCOM: 3,
654-
IRQVal: sam.IRQ_SERCOM3_2, // RXC interrupt
655654
}
656655

657656
// The second hardware serial port on the SAMD51. Uses the SERCOM0 interface.
658657
UART2 = UART{
659658
Buffer: NewRingBuffer(),
660659
Bus: sam.SERCOM0_USART_INT,
661660
SERCOM: 0,
662-
IRQVal: sam.IRQ_SERCOM0_2, // RXC interrupt
663661
}
664662
)
665663

664+
func init() {
665+
// Register RXC interrupts.
666+
UART1.Interrupt = interrupt.New(sam.IRQ_SERCOM3_2, UART1.handleInterrupt)
667+
UART2.Interrupt = interrupt.New(sam.IRQ_SERCOM0_2, UART2.handleInterrupt)
668+
}
669+
666670
const (
667671
sampleRate16X = 16
668672
lsbFirst = 1
@@ -766,7 +770,7 @@ func (uart UART) Configure(config UARTConfig) error {
766770
// > position in the INTFLAG register of respective peripheral.
767771
// Therefore, if we only need to listen to the RXC interrupt source (in bit
768772
// position 2), we only need interrupt source 2 for this SERCOM device.
769-
arm.EnableIRQ(uart.IRQVal)
773+
uart.Interrupt.Enable()
770774

771775
return nil
772776
}
@@ -794,18 +798,10 @@ func (uart UART) WriteByte(c byte) error {
794798
return nil
795799
}
796800

797-
//go:export SERCOM3_2_IRQHandler
798-
func handleSERCOM3_2() {
799-
// should reset IRQ
800-
UART1.Receive(byte((UART1.Bus.DATA.Get() & 0xFF)))
801-
UART1.Bus.INTFLAG.SetBits(sam.SERCOM_USART_INT_INTFLAG_RXC)
802-
}
803-
804-
//go:export SERCOM0_2_IRQHandler
805-
func handleSERCOM0_2() {
801+
func (uart *UART) handleInterrupt(interrupt.Interrupt) {
806802
// should reset IRQ
807-
UART2.Receive(byte((UART2.Bus.DATA.Get() & 0xFF)))
808-
UART2.Bus.INTFLAG.SetBits(sam.SERCOM_USART_INT_INTFLAG_RXC)
803+
uart.Receive(byte((uart.Bus.DATA.Get() & 0xFF)))
804+
uart.Bus.INTFLAG.SetBits(sam.SERCOM_USART_INT_INTFLAG_RXC)
809805
}
810806

811807
// I2C on the SAMD51.

0 commit comments

Comments
 (0)