Skip to content

Commit e17a2e6

Browse files
aykevldeadprogram
authored andcommitted
machine/atsamd51: use only the necessary UART interrupts
A small footnote in the datasheet says that interrupt source numbers correspond to the bit position in INTFLAG. We only need the RXC interrupt for UART. In other words, ony the _2 interrupts (RXC is in the 2nd bit position) needs to be used for UART to work correctly. In the future, more interrupts may be needed. They can then be added as necessary.
1 parent bdfa4d2 commit e17a2e6

File tree

1 file changed

+10
-52
lines changed

1 file changed

+10
-52
lines changed

src/machine/machine_atsamd51.go

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ type UART struct {
628628
Buffer *RingBuffer
629629
Bus *sam.SERCOM_USART_INT_Type
630630
SERCOM uint8
631+
IRQVal uint32 // RXC interrupt
631632
}
632633

633634
var (
@@ -639,13 +640,15 @@ var (
639640
Buffer: NewRingBuffer(),
640641
Bus: sam.SERCOM3_USART_INT,
641642
SERCOM: 3,
643+
IRQVal: sam.IRQ_SERCOM3_2, // RXC interrupt
642644
}
643645

644646
// The second hardware serial port on the SAMD51. Uses the SERCOM0 interface.
645647
UART2 = UART{
646648
Buffer: NewRingBuffer(),
647649
Bus: sam.SERCOM0_USART_INT,
648650
SERCOM: 0,
651+
IRQVal: sam.IRQ_SERCOM0_2, // RXC interrupt
649652
}
650653
)
651654

@@ -697,7 +700,7 @@ func (uart UART) Configure(config UARTConfig) error {
697700
config.TX.Configure(PinConfig{Mode: txPinMode})
698701
config.RX.Configure(PinConfig{Mode: rxPinMode})
699702

700-
// reset SERCOM0
703+
// reset SERCOM
701704
uart.Bus.CTRLA.SetBits(sam.SERCOM_USART_INT_CTRLA_SWRST)
702705
for uart.Bus.CTRLA.HasBits(sam.SERCOM_USART_INT_CTRLA_SWRST) ||
703706
uart.Bus.SYNCBUSY.HasBits(sam.SERCOM_USART_INT_SYNCBUSY_SWRST) {
@@ -747,19 +750,12 @@ func (uart UART) Configure(config UARTConfig) error {
747750
uart.Bus.INTENSET.Set(sam.SERCOM_USART_INT_INTENSET_RXC)
748751

749752
// Enable RX IRQ.
750-
switch uart.SERCOM {
751-
case 0:
752-
arm.EnableIRQ(sam.IRQ_SERCOM0_0)
753-
arm.EnableIRQ(sam.IRQ_SERCOM0_1)
754-
arm.EnableIRQ(sam.IRQ_SERCOM0_2)
755-
arm.EnableIRQ(sam.IRQ_SERCOM0_OTHER)
756-
default:
757-
// Currently assumes SERCOM3
758-
arm.EnableIRQ(sam.IRQ_SERCOM3_0)
759-
arm.EnableIRQ(sam.IRQ_SERCOM3_1)
760-
arm.EnableIRQ(sam.IRQ_SERCOM3_2)
761-
arm.EnableIRQ(sam.IRQ_SERCOM3_OTHER)
762-
}
753+
// This is a small note at the bottom of the NVIC section of the datasheet:
754+
// > The integer number specified in the source refers to the respective bit
755+
// > position in the INTFLAG register of respective peripheral.
756+
// Therefore, if we only need to listen to the RXC interrupt source (in bit
757+
// position 2), we only need interrupt source 2 for this SERCOM device.
758+
arm.EnableIRQ(uart.IRQVal)
763759

764760
return nil
765761
}
@@ -787,53 +783,15 @@ func (uart UART) WriteByte(c byte) error {
787783
return nil
788784
}
789785

790-
//go:export SERCOM3_0_IRQHandler
791-
func handleSERCOM3_0() {
792-
handleUART1()
793-
}
794-
795-
//go:export SERCOM3_1_IRQHandler
796-
func handleSERCOM3_1() {
797-
handleUART1()
798-
}
799-
800786
//go:export SERCOM3_2_IRQHandler
801787
func handleSERCOM3_2() {
802-
handleUART1()
803-
}
804-
805-
//go:export SERCOM3_OTHER_IRQHandler
806-
func handleSERCOM3_OTHER() {
807-
handleUART1()
808-
}
809-
810-
func handleUART1() {
811788
// should reset IRQ
812789
UART1.Receive(byte((UART1.Bus.DATA.Get() & 0xFF)))
813790
UART1.Bus.INTFLAG.SetBits(sam.SERCOM_USART_INT_INTFLAG_RXC)
814791
}
815792

816-
//go:export SERCOM0_0_IRQHandler
817-
func handleSERCOM0_0() {
818-
handleUART2()
819-
}
820-
821-
//go:export SERCOM0_1_IRQHandler
822-
func handleSERCOM0_1() {
823-
handleUART2()
824-
}
825-
826793
//go:export SERCOM0_2_IRQHandler
827794
func handleSERCOM0_2() {
828-
handleUART2()
829-
}
830-
831-
//go:export SERCOM0_OTHER_IRQHandler
832-
func handleSERCOM0_OTHER() {
833-
handleUART2()
834-
}
835-
836-
func handleUART2() {
837795
// should reset IRQ
838796
UART2.Receive(byte((UART2.Bus.DATA.Get() & 0xFF)))
839797
UART2.Bus.INTFLAG.SetBits(sam.SERCOM_USART_INT_INTFLAG_RXC)

0 commit comments

Comments
 (0)