UART Rx Block Icon
UART_Rx Block Icon The UART Rx block receives data from a configured UART module. This block outputs 8-bit data (uint8 type) with flexible size configuration including inherited sizing, user-defined vectors, and automatic buffer sizing based on baud rate and sample time. It works in conjunction with the UART Config block to provide efficient serial reception.

When to use:

  • Receive serial data โ€” Get bytes from UART into Simulink signals
  • String reception โ€” Receive text commands or messages (use “Add trailing 0” mode)
  • Binary protocols โ€” Receive packets, sensor data, or command structures
  • Buffered reception โ€” Use FIFO/DMA modes for reliable high-speed reception
  • Multi-device communication โ€” Receive from multiple UART peripherals (one Rx block per UART)

When NOT to use:

  • No UART Config block โ€” Always add UART Config first (Rx block will show warning)
  • Transmit data โ€” Use UART Tx block instead
  • Stream from MATLAB โ€” Use UART TxMatlab for bidirectional picgui interface
  • CAN/SPI/I2C reception โ€” Use protocol-specific Rx blocks instead

Block Dialog

UART Rx โ€” Main Tab
Output TabUART Rx โ€” Output Tab

Ports

Inputs: None (receives from hardware UART peripheral)

Outputs:

Port NameData TypeSizeDescriptionOptional
Nbruint8/uint16ScalarNumber of bytes received (buffer fill level)Yes (enable via Flag_Out parameter)
Rxuint8Scalar or Vector NReceived data (configurable size)No (always present)

Parameters

UART Selection

  • UART Module - Select UART/USART number (must match UART Config block)
  • Dropdown automatically populated with available UARTs from selected chip

Output Size Configuration

ParameterOptionsDescription
Rx_Size_popupUser definedInherited via back propagation
Rx_SizePositive integer or []User-defined size (when Rx_Size_popup = “User defined”)

Size Determination Methods

1. User Defined

Explicitly set output vector size:

  • Rx_Size = 1: Scalar output (single byte)
  • Rx_Size = N: Vector output N x 1
  • Rx_Size = []: Treated as “Inherited via back propagation”

2. Inherited via Back Propagation

Output size determined by connected downstream block requirements. Simulink propagates size backward from output port connection.

3. Inherited via Internal Rule

Automatically calculate optimal buffer size based on:

  • Baud Rate: From UART Config block
  • Sample Time: Block execution rate
  • Implementation Mode: Hardware FIFO or Circular Buffer size

Formula: Rx_Size = ceil((Baud_Rate / 10) * Sample_Time) + Buffer_Margin

  • Buffer_Margin: Hardware FIFO size
  • PIC32: 8 bytes (8-deep FIFO)
  • dsPIC 30F/33F: 4 bytes (4-byte buffer)
  • dsPIC 33A (dsPIC33A UART): 8 bytes (8-deep FIFO)
  • Circular Buffer: User-defined buffer size

Output Handling Options

ParameterOptionsDescription
Flag_OutOn/Off checkboxEnable ‘Nbr’ output port (number of bytes received)
OutputDefault_popupFill-in output vector with specified valueAdd one trailing 0
OutputDefaultNumeric value (e.g., 0, 255)Fill value when OutputDefault_popup = “Fill-in”

Output Fill Behavior

  • Fill-in with value: Remaining bytes set to OutputDefault
  • Add one trailing 0: Append null terminator (string mode)
  • Do nothing: Remaining bytes undefined (fastest, no overhead)

Output Data Types

The block outputs uint8 data values directly from the UART receive buffer.

Output TypeRangeDescription
uint80 to 255Raw received byte data

Note: Output vector size is configurable via Rx_Size parameter or can be automatically determined based on baud rate and sample time.

Implementation Modes

Reception behavior depends on the implementation mode selected in the UART Config block:

Mode IDImplementationDescriptionHardware FIFO
0Not ConfiguredRX disabled in UART ConfigN/A
1Bare (Simplest)Direct read from hardware FIFO/buffer1/4/8 bytes (device dependent)
2Circular BufferSoftware buffer with interrupt-driven receptionPlus user-defined circular buffer
3DMA Ping-PongTwo DMA buffers alternating (SAM only)DMA with ping-pong buffers
4DMA Single BufferNot implemented for RXN/A

Error Handling

Missing UART Config Block

If no UART Config block is found for the selected UART:

  • Block display shows “MISSING UART X Config”
  • Warning printed to console
  • Compilation warning: “RX is not implemented”

Buffer Overrun

  • Hardware FIFO: Oldest data lost (FIFO overflow error)
  • Circular Buffer: Newest data discarded if buffer full
  • DMA Ping-Pong: Data loss if both buffers full
  • Mitigation: Use “Inherited via internal rule” for automatic sizing

Size Mismatch

Problem: Rx output size doesn’t match downstream block requirements.Solutions:

  • Use “Inherited via back propagation” for automatic matching
  • Manually adjust Rx_Size to match downstream needs
  • Use buffer/reshape blocks to adapt size

Device Support

FamilySeriesArchitecture IDImplementation Files
dsPIC30F, 24F, 33F, 33E, 33C, 33A1, 2, 3, 4, 8PIC bare, Interrupt (Circular Buffer)
PIC32MK, MZ, MX, PIC32AIncluded in dsPIC archSame as dsPIC (PIC implementations)
SAM E7x/S7xSAME70, SAMS70, V71, PIC32CZ MC705SAMx7 bare, Interrupt, DMA ping-pong
SAM E5x/D5xSAME5x, SAMD5x6SAMx5 bare, Interrupt, DMA ping-pong
SAM C2x/D2xSAMC2x, SAMD2x, SAMDA17SAMx5 implementations (shared)

Examples

Example 1: Fixed-Size Reception (10 bytes)

  • Add UART Config block (configure for UART 1, Circular Buffer Rx)

  • Add UART Rx block

  • Configure UART Rx:

  • UART Module: 1

  • Rx_Size_popup: “User defined”

  • Rx_Size: 10

  • Flag_Out: On (monitor bytes received)

  • OutputDefault_popup: “Fill-in output vector with specified value”

  • OutputDefault: 0

  • Connect Rx output [10x1] to downstream processing

  • Monitor Nbr output to see actual bytes received (0-10)

Example 2: Automatic Sizing (Internal Rule)

  • UART Config: UART 1, 115200 baud, Circular Buffer (256 bytes)

  • UART Rx block:

  • UART Module: 1

  • Rx_Size_popup: “Inherited via internal rule”

  • SampleTime: 0.01 (10 ms)

  • Block automatically calculates:

  • Baud throughput: 115200 / 10 = 11520 bps

  • Bytes per sample: (11520 * 0.01) / 8 โ‰ˆ 144 bytes

Example 3: String Reception with Null Terminator

  • Configure UART Rx:

  • Rx_Size_popup: “User defined”

  • Rx_Size: 64 (max string length)

  • Flag_Out: On

  • OutputDefault_popup: “Add one trailing 0”

  • Received string automatically null-terminated

  • Use Nbr output to find actual string length

  • Compatible with C string functions

Example 4: Back Propagation Sizing

  • UART Rx configuration:

  • Rx_Size_popup: “Inherited via back propagation”

  • Rx_Size: [] (or any value, ignored)

  • Connect Rx output to a block requiring [20x1] input

  • Simulink automatically sets Rx port size to [20x1]

  • No manual sizing needed

Programmatic Setup

% Add block to model
add_block('MCHP_Blockset/BUS UART/UART Rx', [mdl '/UART_Rx']);

% Configure key parameters
set_param([mdl '/UART_Rx'], 'UART_REF', '1');
set_param([mdl '/UART_Rx'], 'Rx_Size', '16');

Troubleshooting

No Data Received

Problem: UART Rx block present but Rx output always zero/empty.Solutions:

  • Verify UART Config block exists for same UART number
  • Check RX pin configuration in UART Config block
  • Verify UART enable and RX enable bits
  • Check baud rate matches transmitter
  • Verify wire connections (TX โ†’ RX crossover)

Partial Data Loss

Problem: Some bytes missing from received data.Solutions:

  • Increase buffer size in UART Config (Circular Buffer mode)
  • Reduce sample time (read more frequently)
  • Use “Inherited via internal rule” for automatic sizing
  • Switch to DMA Ping-Pong mode (SAM devices only)

Output Vector Not Filling Correctly

Problem: Rx output vector has unexpected values in unused positions.Solutions:

  • Check OutputDefault_popup setting
  • Use “Fill-in output vector with specified value” for predictable behavior
  • Enable Flag_Out to monitor actual bytes received
  • Use “Add one trailing 0” for string reception

Implementation Details

Bare Mode (Mode 1)

Reads directly from hardware FIFO: data[i] = U%RXREG; /* Direct read from receive register */

  • Polled operation (no interrupts)
  • Limited to hardware FIFO depth
  • Fastest response, lowest latency
  • Risk of data loss if not read frequently

Circular Buffer (Mode 2)

  • Software buffer with head/tail pointers
  • Interrupt-driven: RX interrupt stores each byte
  • Buffer size configurable in UART Config block
  • Thread-safe with critical section protection
  • Recommended for most applications

DMA Ping-Pong (Mode 3 - SAM only)

  • Two DMA buffers alternate (buffer A โ†’ DMA โ†’ buffer B โ†’ DMA…)
  • DMA channel automatically configured
  • Minimal CPU overhead during reception
  • No data loss during buffer processing
  • Only available on SAM E7x/E5x devices
  • UART Config - Configure UART module (required)
  • UART Tx - Transmit data
  • UART TxMatlab - MATLAB interface for testing
  • UART Brk/Autobaud - Break detection and autobaud