Bus UART for Simulink

Last modified by Microchip on 2023/11/10 11:03


The BUS UART group contains blocks to configure and use the UART peripheral, allowing quick sending and visualizing of data within MATLAB®. The BUS UART group contains the following blocks:


bus uart library

UART Configuration Block

UART Configuration block configures the selected UART peripheral.

When used in a Simulink® model, the Universal Synchronous Asynchronous Receiver Transmitter (USART) Configuration Block displays a summary of its main configuration settings:

  • The peripheral instance for the block configuration (i.e., UART 1 Config).
    • Multiple Universal Asynchronous Receiver Transmitter (UART) configuration block configures multiple UART.
  • Current baud rate setting and relative baud rate error for the achieved baud rate.
  • Bytes per fundamental Simulink time (the maximum number of bytes that could be physically sent or received within one Simulink fundamental time step).

An implementation with buffer allows more bytes to be sent or received, but the average byte or step should not go beyond that reference value. 

uart configuration example

Back to top

Main Tab

The Main tab contains configuration parameters for the UART channel selected.

The total number of UART channels are displayed in the Ref UART drop-down list. Each UART channel used must have a corresponding configuration block.

The Current Baud value (indicated in brackets) is the exact baud rate achieved. The baud rates' popup field shows the error in percentage (+/- %) reach for the listed baud rate. You can specify non-standard UART baud rates by selecting the Custom option from the drop-down list. A text field to write the targeted baud rate will appear. The error for this user-defined baud rate is shown.

The Rx/Tx Pin drop-down shows pin mapping possibilities for the targeted microcontroller. Some microcontrollers do not have this feature. When Do not implement Tx (or Rx) is selected, the pin selected as Tx (or Rx) in the Main tab will not be used.


main tab

Back to top 

TX Tab

The TX tab contains transmitter implementation parameters. The Implementation drop-down provides four choices:

  • Do not implement Tx
  • Simplest (xx bytes internal buffer only)
  • Circular Buffer
  • DMA Ping-Pong Mode
  • DMA single Buffer (Possible data loss)

Direct Memory Access (DMA) options are shown for microcontrollers that support DMA. The DMA Ping-Pong Mode uses two buffers that are initialized and filled-in alternatingly. The first buffer is sent when it is completely full. With DMA single Buffer, only one buffer is used. With one buffer, the Rx block is not allowed to buffer new data during the short time during which the DMA buffer is being transmitted. This results in a loss of data. Double buffering makes it more robust at the expense of a second buffer.

tx tab

Back to top 

Advanced Tab

The Advanced tab provides further options. Communication mode can be set to full-duplex or half-duplex from the Duplex drop-down list. In Half-duplex mode, the UART is implemented using dynamic reconfiguration of the Rx/Tx pins to allow bi-directional serial communication using a single wire. The UART logic levels can be inverted separately for Rx and Tx pins by enabling the Invert Rx Pin logic or the Invert Rx Pin logic options.

Other configurable UART parameters are provided in the Parity, Stop bit, and Hardware Flow Control lists. Enabling the IrDA mode provides a mechanism for converting the UART serial interface to an infrared interface. However, the Hardware Flow Control is not available in the IrDA mode.

advanced tab

Back to top 

Initialization Sequence Tab

The Send an Initialization sequence on the UART option under the Initialization Sequence tab allows you to initialize a Slave device at startup. Typically, a Slave, with volatile memory only, might always start with a given baud rate. This option allows you to send a user-defined initialization sequence with different UART settings.

A use case is a GPS module or radio link module that always starts with a baud rate of 38400. The initialization sequence switches the device to your nominal baud rate (e.g.,115200 is sent first at 38400), then the UART peripheral is switched to the nominal baud rate to communicate with this slave device. This initialization sequence is sent only once at microcontroller startup

initialization tab

Back to top 

UART Tx Block

Note: In order to use the UART Tx block in a Simulink® model, it is necessary to first include and set up the UART Configuration block .

diagram of the Tx block

The UART Tx block sends one byte or a vector of uint8 bytes through the UART. Transmitted characters might be buffered depending on the UART configuration. The UART Ref selected must be configured using a UART Configuration block. Multiple instances of this block can be used in one Simulink model.

uart tx main tab

The number of bytes to be sent can be specified by a block input parameter by enabling the option Send first N bytes from the vector input with N a block input from the Block Input tab.

Also, the transmission of the characters contained in the input vector can be stopped when the first occurrence of the NUL('\0') character is detected by enabling With vector input, do not send values following the first 0.

uart tx block input tab

The number of bytes transmitted can be set as an output parameter by enabling Number of byte sent is a block output from the Block Output tab.

uart tx block output tab

Back to top 

UART Tx example:

The following example will print "Hello World!" on the UART. Visit the "UART Terminal Example" page for a complete UART example.

uart tx example "hello world"

Back to top

UART Rx Block

Note: In order to use the UART Rx block in a Simulink® model, it is necessary to first include and set up the UART Configuration block .

uart rx diagram

The UART Rx block allows receiving one byte through the UART. Received characters might be buffered depending on the UART configuration. The UART Ref selected must be configured using a UART Configuration block. Multiple instances of this block can be used in one Simulink model.

uart rx main tab

The number of characters received can be set as an output parameter by enabling the option Output Number of char read. The Vector Output size can be set to one of three options available.

If the vector output size is set to User defined with a value greater than one, the Split and Fill-in output vector when not full options become available.

The Inherit via back propagation option sets the size of the output buffer equal to the input size of the block that reads the Rx buffer.

The Inherit via internal rule option sets the Rx output buffer size depending on the sample time of the model, the sample time of the UART Rx block, and the baud rate specified in UART Configuration block.

uart rx output tab

For example, having a model with a sample time of 1 ms, UART Rx block sample time of 10 ms, and a UART baud rate of 115200, the Rx vector output size will be set to 116 bytes. UART Configuration block displays the number of bytes/step. In this example this number is equal to 11.51bytes/1ms.

Note: When the Vector Output size is set to the User defined option, make sure that the Rx buffer can accommodate the number of characters received relative to the sample time specified for the UART Rx block. One easy way to overcome this situation is to always use the option Inherit via internal rule.

All characters received through the UART can be read, including special characters when Read all received characters is selected. The other option is to stop reading when one specific character is received by selecting the option Stop reading when one specified char is detected followed by the split-character value specified in the Stop reading on character field.

When the output vector is not full, a padding value can be specified from the Fill-in output vector when not full list:

The Fill-in output vector with specified value option will pad the rest of the output vector with the character value specified. There is also one predefined negative value that can be specified: "-1" pads the rest of the output vector with the value of the last received character.

The Add one trailing 0 option will add the NUL character at the end of the output vector, transforming the vector into a string to allow easy integration with string manipulation functions.

The Do nothing option does not pad the rest of the output vector with a specific value. In the latter case, the end of the output buffer is undefined since it contains both new and old characters without a delimiter in between. In this case, the Output Number of char read option can be used to determine how many newly received characters are in the output buffer.

Back to top

UART Rx example:

The following example echoes the characters received on the UART. A complete UART example can be found in the "UART Terminal Example" page.

uart rx example

Back to top

UART Tx-Matlab Block

UART Tx-Matlab block: send variables to MATLAB® using UART interface. Values are displayed using picgui.

Note: In order to use the UART Tx-Matlab block in a Simulink® model, it is necessary to first include and set up the UART Configuration block .

uart tx matlab diagram

The UART Tx-Matlab block allows sending variables to MATLAB through UART. The protocol used allows sending up to 16 distinct channels, each channel being composed of data of type uint8int8uint16 or int16.

Each channel can be sent using a different sampling rate. Data is typically sent continuously. The MATLAB script (used in the picgui block) decodes the data that supports hot plug. The overall system allows receiving data from within MATLAB and analyzing or plotting data in pseudo-real-time using MATLAB functions. The system is also useful to log data for performing simulations based on real logged data.

uart tx matlab window

Back to top

UART Tx-Matlab Example

This example uses picgui and Tx-Matlab blocks to plot three overlapping sine waves of different frequencies and amplitudes. Tx-Matlab block is setup to use three channels, one for each signal. The live value of each signal will be displayed in picgui.

Note: The signals displayed can have different sample rates.

uart tx matlab example

Back to top

picgui Graphical Interface Block

The picgui Graphical Interface block visualizes the variables sent to MATLAB® by the UART Tx-Matlab block

Note: In order to use the picgui Graphical Interface block in a Simulink® model, first include and setup the UART Configuration block .

picgui Graphical Interface block

The picgui block opens the picgui interface and data visualizer:

Data visualizer plot from the picgui interface

Back to top