Intermediate
20 min

Receive and process signals in the 433MHz frequency band with MICRF219A and STM32F031K6

ASK/OOK 433MHz RF receiver with with Auto-Poll and RSSI capabilities

MICRF RX 2 Click with Nucleo 32 with STM32F031K6 MCU

Published Oct 01, 2024

Click board™

MICRF RX 2 Click

Dev Board

Nucleo 32 with STM32F031K6 MCU

Compiler

NECTO Studio

MCU

STM32F031K6

Ideal for automotive and secure access systems where reliable long-distance RF communication is required.

A

A

Hardware Overview

How does it work?

MICRF RX 2 Click is based on the MICRF219A, an ASK/OOK receiver with Auto-Poll and RSSI from Microchip. This super-heterodyne, image-reject RF receiver is engineered to require only a crystal, in this case, an onboard 13.52313MHz, and a minimal set of external components for operation. As mentioned, the MICRF219A can be programmed into an Auto-polling mode via register configuration. In this mode, it monitors if there is a valid incoming RF signal while holding data output (DO pin) in a LOW logic state. In this mode, the MICRF219A also goes between the sleep and polling states. Its suitability spans across various applications, such as low-power Remote Keyless Entry (RKE), Tire Pressure Monitoring Systems (TPMS), and remote actuation systems. In terms of performance, the MICRF219A distinguishes itself with a sensitivity of

-110dBm at 1kbps and a 0.1% Bit Error Rate (BER), complemented by four selectable demodulator filter bandwidths ranging from 1625Hz to 13kHz. Filter bandwidth can be selected by placing BW SEL jumpers in a corresponding position, 0 or 1, choosing the corresponding frequency based on the truth table from the attached MICRF219A datasheet (Table 1.). This feature permits the device to accommodate bit rates as high as 20kbps (from 2.5kbps up to 20kbps). Operating on a 3.3V supply from the mikroBUS™ power rail, it is optimized for 433MHz operation, drawing a typical supply current of 6mA. The MICRF219A also has a low-power shutdown mode controllable through the SH pin, reducing the supply current to an impressive 0.1µA, alongside an RSI pin to indicate received signal strength.

Besides these pins, it also uses the mentioned DO pin as its demodulated data output pin and the CLK pin as the programmed clock input pin for programming the device in combination with the DO pin. For antenna configurations, the board allows for the use of an onboard PCB antenna specifically tuned to 433MHz or an external antenna like Rubber Angle 433MHz Antenna via an SMA connector. Selection is made possible by adjusting the capacitor C4 from position A to B near the SMA connector. This Click board™ can be operated only with a 3.3V logic voltage level. The board must perform appropriate logic voltage level conversion before using MCUs with different logic levels. Also, it comes equipped with a library containing functions and an example code that can be used as a reference for further development.

MICRF RX 2 Click hardware overview image

Features overview

Development board

Nucleo 32 with STM32F031K6 MCU board provides an affordable and flexible platform for experimenting with STM32 microcontrollers in 32-pin packages. Featuring Arduino™ Nano connectivity, it allows easy expansion with specialized shields, while being mbed-enabled for seamless integration with online resources. The

board includes an on-board ST-LINK/V2-1 debugger/programmer, supporting USB reenumeration with three interfaces: Virtual Com port, mass storage, and debug port. It offers a flexible power supply through either USB VBUS or an external source. Additionally, it includes three LEDs (LD1 for USB communication, LD2 for power,

and LD3 as a user LED) and a reset push button. The STM32 Nucleo-32 board is supported by various Integrated Development Environments (IDEs) such as IAR™, Keil®, and GCC-based IDEs like AC6 SW4STM32, making it a versatile tool for developers.

Nucleo 32 with STM32F031K6 MCU double side image

Microcontroller Overview

MCU Card / MCU

default

Architecture

ARM Cortex-M0

MCU Memory (KB)

32

Silicon Vendor

STMicroelectronics

Pin count

32

RAM (Bytes)

4096

You complete me!

Accessories

Click Shield for Nucleo-32 is the perfect way to expand your development board's functionalities with STM32 Nucleo-32 pinout. The Click Shield for Nucleo-32 provides two mikroBUS™ sockets to add any functionality from our ever-growing range of Click boards™. We are fully stocked with everything, from sensors and WiFi transceivers to motor control and audio amplifiers. The Click Shield for Nucleo-32 is compatible with the STM32 Nucleo-32 board, providing an affordable and flexible way for users to try out new ideas and quickly create prototypes with any STM32 microcontrollers, choosing from the various combinations of performance, power consumption, and features. The STM32 Nucleo-32 boards do not require any separate probe as they integrate the ST-LINK/V2-1 debugger/programmer and come with the STM32 comprehensive software HAL library and various packaged software examples. This development platform provides users with an effortless and common way to combine the STM32 Nucleo-32 footprint compatible board with their favorite Click boards™ in their upcoming projects.

Click Shield for Nucleo-32 accessories 1 image

Right angle 433MHz rubber antenna boasts a frequency range of 433MHz, ensuring optimal performance within this spectrum. With a 50Ohm impedance, it facilitates efficient signal transmission. The antenna's vertical polarization enhances signal reception in a specific orientation. Featuring a 1.5dB gain, it can improve signal strength to some extent. The antenna can handle a maximum input power of 50W, making it suitable for various applications. Its compact 50mm length minimizes spatial requirements. Equipped with an SMA male connector, it easily interfaces with compatible devices. This antenna is an adaptable solution for wireless communication needs, particularly when vertical polarization is crucial.

MICRF RX 2 Click accessories 1 image

Used MCU Pins

mikroBUS™ mapper

Received Signal Strength Monitor
PA0
AN
Shutdown
PA11
RST
ID COMM
PA4
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Programmable Clock
PA8
PWM
Receiver Data Output
PA12
INT
NC
NC
TX
NC
NC
RX
NC
NC
SCL
NC
NC
SDA
NC
NC
5V
Ground
GND
GND
1

Take a closer look

Schematic

MICRF RX 2 Click Schematic schematic

Step by step

Project assembly

Click Shield for Nucleo-144 front image hardware assembly

Start by selecting your development board and Click board™. Begin with the Nucleo 32 with STM32F031K6 MCU as your development board.

Click Shield for Nucleo-144 front image hardware assembly
Nucleo 144 with STM32L4A6ZG MCU front image hardware assembly
Stepper 22 Click front image hardware assembly
Prog-cut hardware assembly
Stepper 22 Click complete accessories setup image hardware assembly
Nucleo-32 with STM32 MCU 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 image step 5 hardware assembly
Necto image step 6 hardware assembly
STM32 M4 Clicker HA MCU/Select Step hardware assembly
Necto No Display image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Debug Image Necto Step hardware assembly

Track your results in real time

Application Output via Debug Mode

1. Once the code example is loaded, pressing the "DEBUG" button initiates the build process, programs it on the created setup, and enters Debug mode.

2. After the programming is completed, a header with buttons for various actions within the IDE becomes visible. Clicking the green "PLAY" button starts reading the results achieved with the Click board™. The achieved results are displayed in the Application Output tab.

DEBUG_Application_Output

Software Support

Library Description

This library contains API for MICRF RX 2 Click driver.

Key functions:

  • micrfrx2_enable_device - This function enables device by setting the SHD pin to low logic state

  • micrfrx2_wait_ready - This function waits for all training bytes to arrive which indicates data ready

  • micrfrx2_read_packet - This function reads data packet and stores it in a packet_buf only if the MICRFRX2_PREAMBLE bytes are received successfully

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 MICRF RX 2 Click Example.
 *
 * # Description
 * This example demonstrates the use of MICRF RX 2 click board by reading and parsing
 * packet messages received from the transmitter.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver and enables the device. 
 *
 * ## Application Task
 * Waits for a data ready indication, then reads all packet data, verifies the CRC
 * bytes in a packet, and displays its data as well as the RSSI value on the USB UART.
 *
 * @note
 * The OOK TX click board is a compatible transmitter for the MICRF RX 2 click.
 * Here are a few steps for troubleshooting if you are experiencing issues running
 * this example:
 *  - Check the MCU clock configuration, use an external oscillator instead of the MCU's
 * internal one for better accuracy on manchester data rate delay.
 *  - Measure the actual data rate on the data line and adjust the MICRFRX2_MAN_BIT_LEN_US
 * value accordingly.
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "micrfrx2.h"

#define MICRFRX2_PREAMBLE       0x5AA5  /**< Packet preamble word. */

static micrfrx2_t micrfrx2;     /**< MICRF RX 2 Click driver object. */
static log_t logger;            /**< Logger object. */
static uint8_t packet_buf[ MICRFRX2_MAX_DATA_LEN + 5 ] = { 0 };  /**< Packet buffer. */

/** 
 * @brief MICRF RX 2 wait ready function.
 * @details This function waits for all training bytes to arrive which indicates data ready.
 * @param[in] ctx : Click context object.
 * See #micrfrx2_t object definition for detailed explanation.
 * @return None.
 * @note None.
 */
static void micrfrx2_wait_ready ( micrfrx2_t *ctx );

/** 
 * @brief MICRF RX 2 read packet function.
 * @details This function reads data packet and stores it in a packet_buf only if
 * the MICRFRX2_PREAMBLE bytes are received successfully.
 * @param[in] ctx : Click context object.
 * See #micrfrx2_t object definition for detailed explanation.
 * @return Number of data bytes stored in a packet_buf.
 * @note The expected packet format is as follows (MSB first, manchester IEEE 802.3):
 * TRAINING_BYTES (at least two bytes of 0xAA), PREABMLE, LEN, DATA_IN, CRC16 (calculated
 * from whole packet excluding training bytes). Training bytes are excluded from packet_buf.
 * This function must be called immediately after calling micrfrx2_wait_ready.
 */
static uint8_t micrfrx2_read_packet ( micrfrx2_t *ctx );

/** 
 * @brief Reflect bits.
 * @details This function reflects a desired number of bits in data.
 * @return Reflected data.
 * @note None.
 */
static uint16_t micrfrx2_reflect_bits( uint16_t data_in, uint8_t len );

/** 
 * @brief CRC-16/MAXIM calculation for CRC16 function.
 * @details This function calculates CRC16 with parameteres: 
 * @li @c  Width 16 bit
 * @li @c  Polynomial 0x8005 ( x16 + x15 + x2 + x0 )
 * @li @c  Initialization 0x0000
 * @li @c  Reflect input True
 * @li @c  Reflect output True
 * @li @c  Final Xor 0xFFFF
 * @li @c  Example { 69, 00 } - 0xAFD1
 * @param[in] data_buf : Array of bytes to calculate crc from.
 * @param[in] len : Number of bytes to calculate crc from.
 * @return Calculated CRC.
 * @note None.
 */
static uint16_t micrfrx2_calculate_crc16 ( uint8_t *data_buf, uint16_t len );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    micrfrx2_cfg_t micrfrx2_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_info( &logger, " Application Init " );

    // Click initialization.
    micrfrx2_cfg_setup( &micrfrx2_cfg );
    MICRFRX2_MAP_MIKROBUS( micrfrx2_cfg, MIKROBUS_1 );
    if ( DIGITAL_OUT_UNSUPPORTED_PIN == micrfrx2_init( &micrfrx2, &micrfrx2_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    micrfrx2_enable_device ( &micrfrx2 );

    log_info( &logger, " Application Task " );
}

void application_task ( void ) 
{
    static float rssi_v = 0;
    static uint8_t packet_len = 0;
    static uint8_t msg_cnt = 0;
    static uint16_t crc = 0;

    log_printf( &logger, "\r\n Waiting for data ready...\r\n" );
    micrfrx2_wait_ready ( &micrfrx2 );
    packet_len = micrfrx2_read_packet ( &micrfrx2 );
    if ( packet_len )
    {
        micrfrx2_read_rssi_voltage ( &micrfrx2, &rssi_v );
        crc = ( ( uint16_t ) packet_buf[ packet_len - 2 ] << 8 ) | packet_buf[ packet_len - 1 ];
        if ( crc == micrfrx2_calculate_crc16 ( packet_buf, packet_len - 2 ) )
        {
            log_printf( &logger, " Received message: " );
            for ( msg_cnt = 0; msg_cnt < packet_buf[ 2 ]; msg_cnt++ )
            {
                log_printf( &logger, "%c", ( uint16_t ) packet_buf[ msg_cnt + 3 ] );
            }
            log_printf( &logger, "\r\n RSSI: %.1f dBm\r\n", MICRFRX2_RSSI_V_TO_DBM ( rssi_v ) );
        }
    }
    Delay_ms ( 100 );
}

void main ( void ) 
{
    application_init( );

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

static void micrfrx2_wait_ready ( micrfrx2_t *ctx )
{
    uint16_t time_cnt = 0;
    uint16_t training_high_cnt = 0;
    uint16_t training_low_cnt = 0;
    // Loop until at least two 0xAA bytes of training data is detected
    for ( ; ; )
    {
        // Measure time in steps of MICRFRX2_MAN_BIT_LEN_US / 40 for high signal
        for ( time_cnt = 0; micrfrx2_get_data_pin ( ctx ); time_cnt++ )
        {
            Delay_us ( MICRFRX2_MAN_BIT_LEN_US / 40 );
        }
        if ( time_cnt > 30 )
        {
            // Increment training_high_cnt if signal width is at least 75% of MICRFRX2_MAN_BIT_LEN_US
            training_high_cnt++;
        }
        else
        {
            // Reset counters if any invalid signal is detected
            training_high_cnt = 0;
            training_low_cnt = 0;
        }

        // Measure time in steps of MICRFRX2_MAN_BIT_LEN_US / 40 for low signal
        for ( time_cnt = 0; ( training_high_cnt > 0 ) && !micrfrx2_get_data_pin ( ctx ); time_cnt++ )
        {
            Delay_us ( MICRFRX2_MAN_BIT_LEN_US / 40 );
        }
        if ( time_cnt > 30 )
        {
            // Increment training_low_cnt if signal width is at least 75% of MICRFRX2_MAN_BIT_LEN_US
            training_low_cnt++;
        }
        else if ( ( training_high_cnt >= 8 ) && ( training_high_cnt == ( training_low_cnt + 1 ) ) )
        {
            // At least two 0xAA bytes are detected ending with shorter low signal, so break the loop here.
            // Waiting for 12.5% of MICRFRX2_MAN_BIT_LEN_US as an offset for packet reading.
            Delay_us ( MICRFRX2_MAN_BIT_LEN_US / 8 );
            break;
        }
    }
}

static uint8_t micrfrx2_read_packet ( micrfrx2_t *ctx )
{
    uint8_t byte_cnt = 0;
    uint8_t bit_cnt = 0;
    // Loop until all data bytes are received or a bad PREAMBLE word is detected
    for ( ; ; )
    {
        if ( !micrfrx2_get_data_pin ( ctx ) )
        {
            // Store data in packet_buf in manchester IEEE 802.3 format, MSB first
            packet_buf[ byte_cnt ] |= ( 0x80 >> bit_cnt );
        }
        
        if ( 8 == ++bit_cnt )
        {
            // Reset bit counter and increment byte counter if 8 bits are received
            bit_cnt = 0;
            byte_cnt++;
        }

        if ( 2 == byte_cnt )
        {
            // Two bytes are received, check PREAMBLE word
            if ( MICRFRX2_PREAMBLE != ( ( ( uint16_t ) packet_buf[ 0 ] << 8 ) | packet_buf[ 1 ] ) )
            {
                byte_cnt = 0;
                break;
            }
        }
        else if ( ( packet_buf[ 2 ] + 5 ) == byte_cnt )
        {
            // Break the loop if all packet data are received (PREAMBLE + DATA_LEN + DATA + CRC)
            break;
        }

        // Move to the next manchester clock high state by delaying for MICRFRX2_MAN_BIT_LEN_US
        Delay_us ( MICRFRX2_MAN_BIT_LEN_US );
    }
    return byte_cnt;
}

static uint16_t micrfrx2_reflect_bits( uint16_t data_in, uint8_t len )
{
    uint16_t data_out = 0;
    for ( uint16_t cnt = 0; cnt < len; cnt++ )
    {
        data_out |= ( ( data_in >> cnt ) & 1 ) << ( len - cnt - 1 );
    }
    return data_out;
}

static uint16_t micrfrx2_calculate_crc16( uint8_t *data_buf, uint16_t len )
{
    uint16_t crc16 = 0x0000;
    for ( uint16_t cnt = 0; cnt < len; cnt++ )
    {
        crc16 ^= ( micrfrx2_reflect_bits( data_buf[ cnt ], 8 ) << 8 );
        for ( uint8_t bit_cnt = 0; bit_cnt < 8; bit_cnt++ )
        {
            if ( crc16 & 0x8000 )
            {
                crc16 = ( crc16 << 1 ) ^ 0x8005;
            }
            else
            {
                crc16 <<= 1;
            }
        }
    }
    return micrfrx2_reflect_bits( crc16, 16 ) ^ 0xFFFF;
}

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

Additional Support

Resources

Love this project?

'Buy This Kit' button takes you directly to the shopping cart where you can easily add or remove products.