1
\$\begingroup\$

I am working with SAML22 microcontroller and I am supposed to write the transmission code using the interrupt mode. I am confused in which case I am supposed to disable the peripheral.

When changing from RX to TX mode, am I supposed to disable the receiver.

The data sheet does not tells a lot about these sequences (or maybe I am too inexperienced to understand it).

Edit: The system that I am working on has two MCUs, connected by a full-duplex UART link (TXD1-->RXD2, TXD2-->RXD1) for data exchange. There are a set of parameters that my MCU needs to send to the other MCU. The root cause of my confusion is my inexperience and the large code base that I am working on.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Can you be more specific what you are doing with the UART? 99% of cases don't require switching anything between receive and transmit but basically transmitting and receiving completely asynchronouly - unless you have a half duplex interface which you don't mention. \$\endgroup\$
    – Justme
    Commented Jun 6 at 7:36
  • 2
    \$\begingroup\$ All UARTs I've ever seen in MCUs have two separate and independent sections: a transmitter and a receiver. They often share CPU registers but they operate totally independently from each other. You might have a board with electronics that then shares the MCU RXD and TXD pins up a single wire but we don't know. Please edit your question (don't add info in comments) and explain what circuitry you have on your board, how it's connected, what you're connected to and what protocol you're trying to follow. Thanks. \$\endgroup\$
    – TonyM
    Commented Jun 6 at 8:35

1 Answer 1

2
\$\begingroup\$

Generally, in your scenario, you just enable the UART for reception and transmission and if bytes come in then read them into some buffer for processing them (if you use intrrrupts) and if you need to transmit bytes then you can do so in any way possible with your MCU, like with a polled loop or using interrupts.

The used MCU does not change these basic principles, but simple MCUs may give you a bare UART you must handle with interrupts or by polling flags, fancier MCUs offer DMA transfers and FIFOs for reducing CPU overhead.

\$\endgroup\$
11
  • 1
    \$\begingroup\$ @user23335133 That depends what options you have available and how you want to do it. Some UARTs give you a "TX empty" interrupt and/or "TX complete" interrupt. Either will do. If you have nothing to send, disable the interrupt so it does not happen. When you do have data to send, enable it, and you get interrupt when you can write next byte, if you have more bytes to send. \$\endgroup\$
    – Justme
    Commented Jun 6 at 11:05
  • 1
    \$\begingroup\$ Or don't disable the interrupt, and the interrupt handler sets a flag in software. So that next time you do want to transmit something, you already know that there's space available in the UART. \$\endgroup\$
    – Simon B
    Commented Jun 6 at 11:15
  • 1
    \$\begingroup\$ @user23335133 You normally don't need to actually disable tramsmit on a UART. If it has no data to send, it just sends a stream of stop bits, which the receiver ignores. \$\endgroup\$
    – Simon B
    Commented Jun 6 at 11:22
  • 1
    \$\begingroup\$ @SimonB the interrupts need to be disabled or you get constant interrupts about TX buffer being empty so it does not consume 100% of CPU time. The TX side itself does not need disabling. And if UART does not have any data to send, it certainly does not send a stream of stop bits, the line will idle high (although it is the same state than stop bit but a stop bit is also just a forced idle to detect the next start bit, if this is what you meant). \$\endgroup\$
    – Justme
    Commented Jun 6 at 12:13
  • 1
    \$\begingroup\$ @missedSemiColon You might not want to disable the transmitter. You can do so if you need or want it, but you have to be absolutely clear what happens on the IO pin when you disable the TX, and if that is OK to your hardware. For example, does the pin change to input or go low when you disable the TX. \$\endgroup\$
    – Justme
    Commented Jun 7 at 8:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.