Intermediate
30 min

Revolutionize data acquisition and remote monitoring applications with MAX399 and STM32F107VCT6

One UART, four RS-232 friends: CMOS MUX marvel!

UART MUX 2 Click with EasyMx PRO v7a for STM32

Published Oct 07, 2023

Click board™

UART MUX 2 Click

Dev.Board

EasyMx PRO v7a for STM32

Compiler

NECTO Studio

MCU

STM32F107VCT6

Optimize your UART interface and simplify serial data communication by implementing our CMOS analog multiplexer, allowing four remote RS-232 transceivers to efficiently share a single UART connection

A

A

Hardware Overview

How does it work?

UART MUX 2 Click is based on the MAX399, a precise CMOS analog multiplexer that enables pseudo-multidrop RS232 transmission from Analog Devices. This multiplexer allows multiple channels, in this case, four, to share a single UART interface. It offers fast switching speeds with a transition time of less than 250ns and low on-resistance of less than 100Ω while retaining CMOS-logic input compatibility and fast switching. The dual four-to-one multiplexer permits transceiver MAX3221 to form a network with the four remote transceivers connected to terminals labeled as UART0-UART3 in the upper part of the Click board™. The circuit's supply-voltage range (3V to 5.5V) makes it compatible with 3V and 5V logic. MAX399 receives its power directly from the power terminals of MAX3221, whose ±5.5V outputs come

from an internal charge pump. The multiplexer handles rail-to-rail signals, so obtaining its power from MAX3221 ensures that RS232 signals pass directly through, regardless of amplitude. The UART MUX Click communicates with MCU through MAX3221 using the UART interface for the data transfer. The MAX3221 can run at data rates up to 250 kbps while maintaining RS232-compliant output levels. Channel selection is performed through a set of specific GPIO pins, labeled as A0 and A1, routed on the CS and RST pins of the mikroBUS™ socket. Selecting channel 1, for instance, enables MAX3221 to communicate with UART0 without being loaded by UART1 to UART3. Pulldown resistors inside the remote transceivers force the outputs of un-selected receivers to a known state. In addition to a

channel selection, this Click board™ also has an automatic power-down feature that can be turned off when ON and OFF pins are high, routed on the PWM and AN pins of the mikroBUS™ socket. Also, it uses the interrupt pin of the mikroBUS™ labeled as INV as an invalid indicator, making interfacing with the RS232 simple and easy, indicating whether a valid RS232 signal is present. This Click board™ can operate with either 3.3V or 5V logic voltage levels selected via the VCC SEL jumper. This way, both 3.3V and 5V capable MCUs can use the communication lines properly. Also, this Click board™ comes equipped with a library containing easy-to-use functions and an example code that can be used as a reference for further development.

UART MUX 2 Click top side image
UART MUX 2 Click bottom side image

Features overview

Development board

EasyMx PRO v7a for STM32 is the seventh generation of ARM development boards specially designed to develop embedded applications rapidly. It supports a wide range of 32-bit ARM microcontrollers from STMicroelectronics and a broad set of unique functions, such as the first-ever embedded debugger/programmer over USB-C. The development board is well organized and designed so that the end-user has all the necessary elements, such as switches, buttons, indicators, connectors, and others, in one place. With two different connectors for each port, EasyMx PRO v7afor STM32 allows you to connect accessory boards, sensors, and custom electronics more efficiently than ever. Each part of the EasyMx

PRO v7a for STM32 development board contains the components necessary for the most efficient operation of the same board. In addition to the advanced integrated CODEGRIP programmer/debugger module, which offers many valuable programming/debugging options and seamless integration with the Mikroe software environment, the board also includes a clean and regulated power supply block for the development board. It can use a wide range of external power sources, including an external 12V power supply, 7-23V AC or 9-32V DC via DC connector/screw terminals, and a power source via the USB Type-C (USB-C) connector. Communication options such as USB-UART, USB-HOST/DEVICE, CAN, and

Ethernet are also included, including the well-established mikroBUS™ standard, one display option for the TFT board line of products, and a standard TQFP socket for the seventh-generation MCU cards. This socket covers 32-bit ARM MCUs like STM32 Cortex-M3, -M7, and -M4 MCUs. EasyMx PRO v7afor STM32 is an integral part of the Mikroe ecosystem for rapid development. Natively supported by Mikroe software tools, it covers many aspects of prototyping and development thanks to a considerable number of different Click boards™ (over a thousand boards), the number of which is growing every day.

EasyMx PRO v7a for STM32 double side image

Microcontroller Overview

MCU Card / MCU

default

Type

7th Generation

Architecture

ARM Cortex-M3

MCU Memory (KB)

10

Silicon Vendor

STMicroelectronics

Pin count

100

RAM (Bytes)

100

Used MCU Pins

mikroBUS™ mapper

Force OFF
PA4
AN
UART Channel Selection
PC2
RST
UART Channel Selection
PD13
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Force ON
PA0
PWM
Valid RS232 Signal Indication
PD10
INT
UART TX
PD8
TX
UART RX
PD9
RX
NC
NC
SCL
NC
NC
SDA
Power Supply
5V
5V
Ground
GND
GND
1

Take a closer look

Schematic

UART MUX 2 Click Schematic schematic

Step by step

Project assembly

EasyMx PRO v7a for STM32 front image hardware assembly

Start by selecting your development board and Click board™. Begin with the EasyMx PRO v7a for STM32 as your development board.

EasyMx PRO v7a for STM32 front image hardware assembly
GNSS2 Click front image hardware assembly
EasyMx PRO? v7 for STM32 MCUcard with STM32F407VGT6 front image hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
EasyMx PRO v7a for STM32 Access MB 1 - upright/background hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
NECTO Compiler Selection Step Image hardware assembly
NECTO Output Selection Step Image hardware assembly
Necto image step 6 hardware assembly
EasyMx PRO v7a for STM32 MCU Selection Necto Step hardware assembly
EasyPIC PRO v7a Display Selection Necto Step hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Necto PreFlash Image hardware assembly

Track your results in real time

Application Output

After pressing the "FLASH" button on the left-side panel, it is necessary to open the UART terminal to display the achieved results. By clicking on the Tools icon in the right-hand panel, multiple different functions are displayed, among which is the UART Terminal. Click on the offered "UART Terminal" icon.

UART Application Output Step 1

Once the UART terminal is opened, the window takes on a new form. At the top of the tab are two buttons, one for adjusting the parameters of the UART terminal and the other for connecting the UART terminal. The tab's lower part is reserved for displaying the achieved results. Before connecting, the terminal has a Disconnected status, indicating that the terminal is not yet active. Before connecting, it is necessary to check the set parameters of the UART terminal. Click on the "OPTIONS" button.

UART Application Output Step 2

In the newly opened UART Terminal Options field, we check if the terminal settings are correct, such as the set port and the Baud rate of UART communication. If the data is not displayed properly, it is possible that the Baud rate value is not set correctly and needs to be adjusted to 115200. If all the parameters are set correctly, click on "CONFIGURE".

UART Application Output Step 3

The next step is to click on the "CONNECT" button, after which the terminal status changes from Disconnected to Connected in green, and the data is displayed in the Received data field.

UART Application Output Step 4

Software Support

Library Description

This library contains API for UART MUX 2 Click driver.

Key functions:

  • uartmux2_set_operation_mode - UART MUX 2 set operation mode function

  • uartmux2_set_channel - UART MUX 2 set channel function

  • uartmux2_send_data - UART MUX 2 data writing function

Open Source

Code example

This example can be found in NECTO Studio. Feel free to download the code, or you can copy the code below.

/*!
 * @file main.c
 * @brief UART MUX 2 Click Example.
 *
 * # Description
 * This library contains API for UART MUX 2 Click driver.
 * This example transmits/receives and processes data from UART MUX 2 clicks.
 * The library initializes and defines the UART bus drivers 
 * to transmit or receive data. 
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes driver and set UART channel module.
 *
 * ## Application Task
 * Transmitter/Receiver task depend on uncommented code.
 * Receiver logging each received byte to the UART for data logging,
 * while transmitted send messages every 2 seconds.
 *
 * ## Additional Function
 * - static void uartmux2_clear_app_buf ( void ) - Function clears memory of app_buf.
 * - static err_t uartmux2_process ( void ) - The general process of collecting presponce
 * that a module sends.
 *
 * @author Nenad Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "uartmux2.h"

#define PROCESS_BUFFER_SIZE 200

#define TRANSMITTER
// #define RECIEVER

static uartmux2_t uartmux2;
static log_t logger;
static uint8_t uart_ch;
static char app_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
static int32_t app_buf_len = 0;
static int32_t app_buf_cnt = 0;

unsigned char demo_message[ 9 ] = { 'M', 'i', 'k', 'r', 'o', 'E', 13, 10, 0 };

/**
 * @brief UART MUX 2 clearing application buffer.
 * @details This function clears memory of application buffer and reset it's length and counter.
 * @note None.
 */
static void uartmux2_clear_app_buf ( void );

/**
 * @brief UART MUX 2 data reading function.
 * @details This function reads data from device and concats data to application buffer.
 *
 * @return @li @c  0 - Read some data.
 *         @li @c -1 - Nothing is read.
 *         @li @c -2 - Application buffer overflow.
 *
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t uartmux2_process ( void );

void application_init ( void ) {
    log_cfg_t log_cfg;  /**< Logger config object. */
    uartmux2_cfg_t uartmux2_cfg;  /**< Click config object. */

    /** 
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX 
     * are defined as HAL_PIN_NC, you will 
     * need to define them manually for log to work. 
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    LOG_MAP_USB_UART( log_cfg );
    log_init( &logger, &log_cfg );
    log_printf( &logger, "\r\n Application Init \r\n" );

    // Click initialization.

    uartmux2_cfg_setup( &uartmux2_cfg );
    UARTMUX2_MAP_MIKROBUS( uartmux2_cfg, MIKROBUS_1 );
    err_t init_flag  = uartmux2_init( &uartmux2, &uartmux2_cfg );
    if ( init_flag == UART_ERROR ) {
        log_error( &logger, " Application Init Error. " );
        log_info( &logger, " Please, run program again... " );

        for ( ; ; );
    }

    uartmux2_default_cfg ( &uartmux2 );
    app_buf_len = 0;
    app_buf_cnt = 0;
    log_printf( &logger, "\r\n Application Task \r\n" );
    log_printf( &logger, "------------------\r\n" );
    Delay_ms( 500 );
    
    #ifdef TRANSMITTER
    
        log_printf( &logger, "    Send data:    \r\n" );
        log_printf( &logger, "      mikroE      \r\n" );
        log_printf( &logger, "------------------\r\n" );
        log_printf( &logger, "  Transmit data   \r\n" );
        Delay_ms( 1000 );

    #endif

    #ifdef RECIEVER

        uart_ch = UARTMUX2_CHANNEL_0;
        log_printf( &logger, "   Receive data  \r\n" );
        log_printf( &logger, "      UART%u \r\n", ( uint16_t ) uart_ch );
        uartmux2_set_channel( &uartmux2, uart_ch );
        Delay_ms( 2000 );
    
    #endif
        
    log_printf( &logger, "------------------\r\n" );
}

void application_task ( void ) {
    #ifdef TRANSMITTER
    
    for ( uart_ch = UARTMUX2_CHANNEL_0; uart_ch <= UARTMUX2_CHANNEL_3; uart_ch++ ) {
        uartmux2_set_channel( &uartmux2, uart_ch );
        Delay_ms( 100 );
        uartmux2_send_data( &uartmux2, demo_message );
        log_printf( &logger, "  UART%u : ", ( uint16_t ) uart_ch ); 
    
        for ( uint8_t cnt = 0; cnt < 9; cnt ++ ) {
            log_printf( &logger, "%c", demo_message[ cnt ] );
            Delay_ms( 100 );
        }     
    }
    
    log_printf( &logger, "------------------\r\n" );
    Delay_ms( 100 );
    
    
    #endif
    
    #ifdef RECIEVER
    
    uartmux2_process( );

    if ( app_buf_len > 0 ) {
        log_printf( &logger, "%s", app_buf );
        uartmux2_clear_app_buf(  );
    }
    
    #endif
}

void main ( void ) {
    application_init( );

    for ( ; ; ) {
        application_task( );
    }
}

static void uartmux2_clear_app_buf ( void ) {
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
    app_buf_cnt = 0;
}

static err_t uartmux2_process ( void ) {
    int32_t rx_size;
    char rx_buff[ PROCESS_BUFFER_SIZE ] = { 0 };

    rx_size = uartmux2_generic_read( &uartmux2, rx_buff, PROCESS_BUFFER_SIZE );

    if ( rx_size > 0 ) {
        int32_t buf_cnt = 0;

        if ( app_buf_len + rx_size >= PROCESS_BUFFER_SIZE ) {
            uartmux2_clear_app_buf(  );
            return -2;
        } else {
            buf_cnt = app_buf_len;
            app_buf_len += rx_size;
        }

        for ( int32_t rx_cnt = 0; rx_cnt < rx_size; rx_cnt++ ) {
            if ( rx_buff[ rx_cnt ] != 0 ) {
                app_buf[ ( buf_cnt + rx_cnt ) ] = rx_buff[ rx_cnt ];
            } else {
                app_buf_len--;
            }

        }
        return 0;
    }
    return -1;
}

// ------------------------------------------------------------------------ END

Additional Support

Resources