Bus UART for Simulink
Overview
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:
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).
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.
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.
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.
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
UART 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.
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.
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 example:
The following example will print "Hello World!" on the UART. Visit the "UART Terminal Example" page for a complete UART example.
UART Rx Block
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.
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.
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.
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.
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 Tx-Matlab Block
UART Tx-Matlab block: send variables to MATLAB® using UART interface. Values are displayed using picgui.
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 uint8, int8, uint16 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 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.
picgui Graphical Interface Block
The picgui Graphical Interface block visualizes the variables sent to MATLAB® by the UART Tx-Matlab block
The picgui block opens the picgui interface and data visualizer: