Electronics > Microcontrollers

SAMD21g18: How to configure interrupt on UART byte sent?

(1/3) > >>

reneman:
Hi, I've been reading the samd21 datasheet but I cannot seem to find if this is possible.
I would like to have a sercom configured as UART to send a byte (over the TX Line) and trigger an interrupt once the transmission has been completed.

Do you have any sample code to do this, or could you point me to the part of the datasheet where this is explained (if it's supported)?

Thank you

mikerj:
This is certainly possible, though I don't have any example code.  The interrupt flag you need is called TXC (transmit complete) and it's action is (briefly) described in section 26.6.2.5 of the family datasheet. 

There is one interrupt vector for each USART, you have configure the INTENSET register for the events you want to raise an interrupt, TXC in your case.  Additionally you will need to configure the NVIC interrupt priority (NVIC_SetPriority) and enable the interrupt (NVIC_EnableIRQ) for the USART you wish to use.

In the interrupt handler, you need to check which event has caused the interrupt by reading the INTFLAG register and testing the bits.  You then should clear the interrupt by writing a 1 to the bit.

Note that if you want to send multiple bytes, then waiting for TXC after each one will not provide the best throughput.  The data register is double buffered, so you can load the next byte to send whilst the previous one is being transmitted, and you should use the Data Register Empty (INTFLAG.DRE) interrupt in this case to load the next byte in.  You'd likely only want to use TXC on the last byte in this case, so you can tell when it has been shifted out.

ataradov:
Here is an example of a complete interrupt-driven UART driver https://github.com/ataradov/vcp/blob/master/samd11/uart.c

reneman:

--- Quote from: ataradov on July 29, 2021, 04:15:17 pm ---Here is an example of a complete interrupt-driven UART driver https://github.com/ataradov/vcp/blob/master/samd11/uart.c

--- End quote ---

Could you provide an example of how to use the driver and configure the interrupt routine that is called when a byte is sent over uart?

Thank you

ataradov:
This is the example. I don't understand what else do you need. UART_SERCOM_IRQ_HANDLER() is the interrupt handler, and "if (flags & SERCOM_USART_INTFLAG_RXC)" is the part that is executed when the byte is received.

Note that "#define UART_SERCOM_IRQ_HANDLER  irq_handler_sercom0" must match the actual name of the interrupt handler in the vector table. This depends on your startup code.

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version