Intermediate
20 min

Simplify communication between various devices with TCM515U and PIC32MZ2048EFM100

Stay connected, conserve energy: The EnOcean transceiver gateway module

EnOcean 4 Click with Curiosity PIC32 MZ EF

Published Nov 08, 2023

Click board™

EnOcean 4 Click

Dev Board

Curiosity PIC32 MZ EF

Compiler

NECTO Studio

MCU

PIC32MZ2048EFM100

With a focus on efficiency and connectivity, our EnOcean transceiver gateway module opens the door to a brighter, more connected future for your IoT projects.

A

A

Hardware Overview

How does it work?

EnOcean 4 Click is based on the TCM515U, a transceiver gateway module that operates at 902MHz radio band from EnOcean. Its fully integrated radio capability enables communications with other devices by using an onboard PCB antenna, so no additional antennas are needed for testing this device. The TCM515U can work in three functional modes: Telegram Reception, Telegram Transmission, Low Power Sleep. In receive mode, TCM515U processes received radio telegrams and verifies correct frame structure and check sum. In transmit mode, TCM515U receives radio telegrams for transmission from the external host via its

ESP3 interface. TCM515U can be set into a low power sleep mode for a defined period of time , after expiry of the requested sleep period, TCM515U will automatically wake-up and transition back to receive mode. Each TMC515U module contains it own EnOcean Unique Radio ID (EURID) which can be used during transmission for data authentication. Beside this feature, its also possible setting for each module Base ID or Broadcast ID depending from the application that you are designing. Another important feature is SLF (Security Level Format) which specifies the parameters of the encryption, authentication and rolling code algorithms

used for communication with a specific device, which eanbles TCM515U to encrypt and decrypt telegrams using AES128 based on a 16 byte security key. The TCM 515U provides a transparent radio link between EnOcean radio devices and an external host connected via UART interface using the standardized EnOcean Serial Protocol V3 (ESP3) communication protocol. The default interface speed for this module of the ESP3 (UART) interface is 57600. Additionally, it is possible to change the default ESP3 interface speed at power up from 57600 Bit per second to 460800 Bit per second by setting the TURBO pin to LOW.

EnOcean 4 Click hardware overview image

Features overview

Development board

Curiosity PIC32 MZ EF development board is a fully integrated 32-bit development platform featuring the high-performance PIC32MZ EF Series (PIC32MZ2048EFM) that has a 2MB Flash, 512KB RAM, integrated FPU, Crypto accelerator, and excellent connectivity options. It includes an integrated programmer and debugger, requiring no additional hardware. Users can expand

functionality through MIKROE mikroBUS™ Click™ adapter boards, add Ethernet connectivity with the Microchip PHY daughter board, add WiFi connectivity capability using the Microchip expansions boards, and add audio input and output capability with Microchip audio daughter boards. These boards are fully integrated into PIC32’s powerful software framework, MPLAB Harmony,

which provides a flexible and modular interface to application development a rich set of inter-operable software stacks (TCP-IP, USB), and easy-to-use features. The Curiosity PIC32 MZ EF development board offers expansion capabilities making it an excellent choice for a rapid prototyping board in Connectivity, IOT, and general-purpose applications.

Curiosity PIC32MZ EF double side image

Microcontroller Overview

MCU Card / MCU

default

Architecture

PIC32

MCU Memory (KB)

2048

Silicon Vendor

Microchip

Pin count

100

RAM (Bytes)

524288

Used MCU Pins

mikroBUS™ mapper

NC
NC
AN
Reset
RA9
RST
NC
NC
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
NC
NC
INT
UART TX
RPD10
TX
UART RX
RPD15
RX
NC
NC
SCL
NC
NC
SDA
NC
NC
5V
Ground
GND
GND
1

Take a closer look

Schematic

EnOcean 4 Click Schematic schematic

Step by step

Project assembly

Curiosity PIC32MZ EF front image hardware assembly

Start by selecting your development board and Click board™. Begin with the Curiosity PIC32 MZ EF as your development board.

Curiosity PIC32MZ EF front image hardware assembly
Thermo 28 Click front image hardware assembly
Prog-cut hardware assembly
Curiosity PIC32 MZ EF 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
Curiosity PIC32 MZ EF MCU 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 EnOcean 4 Click driver.

Key functions:

  • enocean4_response_handler_set - Handler Set function.

  • enocean4_process - Response Proccesing function.

  • enocean4_response_ready - Response Ready Check 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 
 * \brief Enocean4 Click example
 * 
 * # Description
 * This example reads and processes data from EnOcean 4 clicks.
 *
 * The demo application is composed of two sections :
 * 
 * ## Application Init 
 * Initializes the driver and configures the click board.
 * 
 * ## Application Task  
 * In the receiver mode, it waits for a telegram, then replies to it with the certain message.
 * In the transmitter mode, first it sends the telegram with the certain message, 
 * then waits for a response.
 * 
 * ## Additional Function
 * - resp_message - Displays the response message on the USB UART.
 * - packet_type - Displays the packet type message on the USB UART.
 * - make_response - Driver handler function which stores data in the response buffer.
 * - log_response - Displays a response on the USB UART.
 * - send_telegram - Allows user to send a telegram to the determined destination.
 * - check_response - Waits until response is ready, then displays it on the USB UART.
 * 
 * \author MikroE Team
 *
 */
// ------------------------------------------------------------------- INCLUDES

#include "board.h"
#include "log.h"
#include "enocean4.h"
#include "string.h"

// ------------------------------------------------------------------ VARIABLES

#define DEMO_APP_RECEIVER
// #define DEMO_APP_TRANSMITTER

#define DEMO_MESSAGE "MikroE"
#define DEMO_ANSWER  "EnOcean 4"

enocean4_packet_t response;
enocean4_packet_t message;
uint8_t response_size_cnt;
uint8_t rsp_check;
uint8_t device_mode;

static enocean4_t enocean4;
static log_t logger;

// ------------------------------------------------------- ADDITIONAL FUNCTIONS

void resp_message( uint8_t resp_byte )
{
    switch ( resp_byte )
    {
        case ENOCEAN4_RET_OK :
        {
            log_printf( &logger, "OK\r\n" );
            break;
        }
        case ENOCEAN4_RET_ERROR :
        {
            log_printf( &logger, "ERROR\r\n" );
            break;
        }
        case ENOCEAN4_RET_NOT_SUPPORTED :
        {
            log_printf( &logger, "NOT SUPPORTED\r\n" );
            break;
        }
        case ENOCEAN4_RET_WRONG_PARAM :
        {
            log_printf( &logger, "WRONG PARAM\r\n" );
            break;
        }
        case ENOCEAN4_RET_OP_DENIED :
        {
            log_printf( &logger, "OPERATION DENIED\r\n" );
            break;
        }
        case ENOCEAN4_RET_LOCK_SET :
        {
            log_printf( &logger, "LOCK SET\r\n" );
            break;
        }
        case ENOCEAN4_RET_BUFF_TO_SMALL :
        {
            log_printf( &logger, "BUFFER TO SMALL\r\n" );
            break;
        }
        case ENOCEAN4_RET_NO_FREE_BUFF :
        {
            log_printf( &logger, "NO FREE BUFFER\r\n" );
            break;
        }
        default :
        {
            log_printf( &logger, "0x%.2X\r\n", ( uint16_t ) resp_byte );
            break;
        }
    }
}

void packet_type( )
{
    switch ( response.packet_type )
    {
        case ENOCEAN4_PACK_TYPE_RADIO_ERP1 :
        {
            log_printf( &logger, "RADIO TELEGRAM : " );
            break;
        }
        case ENOCEAN4_PACK_TYPE_RESPONSE :
        {
            log_printf( &logger, "RESPONSE : " );
            break;
        }
        case ENOCEAN4_PACK_TYPE_RADIO_SUB_TEL :
        {
            log_printf( &logger, "RADIO SUBTELEGRAM : " );
            break;
        }
        case ENOCEAN4_PACK_TYPE_EVENT :
        {
            log_printf( &logger, "EVENT : " );
            break;
        }
        case ENOCEAN4_PACK_TYPE_COMMON_CMD :
        {
            log_printf( &logger, "COMMON COMMAND : " );
            break;
        }
        case ENOCEAN4_PACK_TYPE_SMART_ACK_CMD :
        {
            log_printf( &logger, "SMART ACK COMMAND : " );
            break;
        }
        case ENOCEAN4_PACK_TYPE_REMOTE_MAN_CMD :
        {
            log_printf( &logger, "REMOTE MAN COMMAND : " );
            break;
        }
        case ENOCEAN4_PACK_TYPE_RADIO_MSG :
        {
            log_printf( &logger, "RADIO MESSAGE : " );
            break;
        }
        case ENOCEAN4_PACK_TYPE_RADIO_ERP2 :
        {
            log_printf( &logger, "RADIO TELEGRAM ERP2 : " );
            break;
        }
        case ENOCEAN4_PACK_TYPE_RADIO_802_15_4 :
        {
            log_printf( &logger, "802_15_4_RAW PACKET : " );
            break;
        }
        case ENOCEAN4_PACK_TYPE_CMD_2_4 :
        {
            log_printf( &logger, "2.4 GHz COMMAND : " );
            break;
        }
        default :
        {
            log_printf( &logger, "MSC - MESSAGE : " );
            break;
        }
    }
}

void make_response( enocean4_packet_t *rsp, uint8_t *rsp_length_size )
{
    uint16_t rsp_cnt;

    for ( rsp_cnt = 0; rsp_cnt < rsp->data_length; rsp_cnt++ )
    {
        response.data_buff[ rsp_cnt ] = rsp->data_buff[ rsp_cnt ];
    }

    response.data_length = rsp->data_length;
    response.opt_length  = rsp->opt_length;
    response.packet_type = rsp->packet_type;
    response_size_cnt    = *rsp_length_size;
}

void log_response( )
{
    uint16_t rsp_cnt;

    if ( rsp_check == 1 )
    {
        if ( ( response.packet_type == ENOCEAN4_PACK_TYPE_RESPONSE ) && ( response.data_length == 1 ) )
        {
            packet_type( );
            resp_message( response.data_buff[ 0 ] );

            return;
        }

        packet_type( );
        rsp_check = 0;
    }

    if ( response.packet_type == ENOCEAN4_PACK_TYPE_RADIO_ERP1 )
    {
        for ( rsp_cnt = 1; rsp_cnt < response.data_length - 12; rsp_cnt++ )
        {
            log_printf( &logger, "%c", ( uint16_t ) response.data_buff[ rsp_cnt ] );
        }
    }
    else
    {
        for ( rsp_cnt = 0; rsp_cnt < response.data_length; rsp_cnt++ )
        {
            log_printf( &logger, "0x%.2X ", ( uint16_t ) response.data_buff[ rsp_cnt ] );
        }
    }

    if ( response_size_cnt == 0 )
    {
        log_printf( &logger, "\r\n" );
        rsp_check = 1;
    }
}

void send_telegram ( char *telegram, uint8_t tel_type )
{
    uint16_t tel_length;
    uint16_t tel_idx;
    char *chr_ptr;

    tel_length = 0;
    chr_ptr = telegram;

    while ( *chr_ptr != 0 )
    {
        tel_length++;
        chr_ptr++;
    }

    // Packet Header
    if ( tel_type == ENOCEAN4_RORG_ADT )
    {
        message.opt_length = 7;
    }
    else
    {
        message.opt_length = 0;
    }

    message.data_length = 6 + tel_length;
    message.packet_type = ENOCEAN4_PACK_TYPE_RADIO_ERP1;
    message.data_buff[ 0 ] = tel_type;

    // Telegram Data
    chr_ptr = telegram;
    
    for ( tel_idx = 0; tel_idx < tel_length; tel_idx++ )
    {
        message.data_buff[ tel_idx + 1 ] = *chr_ptr;
        chr_ptr++;
    }

    // Sender ID	
    message.data_buff[ tel_length + 1 ] = 0x04;
    message.data_buff[ tel_length + 2 ] = 0x10;
    message.data_buff[ tel_length + 3 ] = 0x0B;
    message.data_buff[ tel_length + 4 ] = 0x8B;
    
    // Status
    message.data_buff[ tel_length + 5 ] = 0x00;

    // Number of sub telegram
    message.data_buff[ tel_length + 6 ] = 0x03;
    
    // Destination ID
    message.data_buff[ tel_length + 7 ] = 0x04;
    message.data_buff[ tel_length + 8 ] = 0x10;
    message.data_buff[ tel_length + 9 ] = 0x0B;
    message.data_buff[ tel_length + 10 ] = 0x81;
    
    // dBm
    message.data_buff[ tel_length + 11 ] = 0xFF;
    
    // Security Level
    message.data_buff[ tel_length + 12 ] = 0x00;

    enocean4_send_packet( &enocean4, &message );
}

void check_response( )
{
    uint8_t response_ready;

    enocean4_uart_isr( &enocean4 );
    response_ready = enocean4_response_ready( &enocean4 );

    while ( response_ready != ENOCEAN4_RESPONSE_READY )
    {
        enocean4_uart_isr( &enocean4 );
        response_ready = enocean4_response_ready( &enocean4 );
        Delay_1ms( );
    }

    enocean4_process( &enocean4 );
    log_response( );
}

// ------------------------------------------------------ APPLICATION FUNCTIONS

void application_init ( void )
{
    log_cfg_t log_cfg;
    enocean4_cfg_t cfg;

    /** 
     * 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.

    enocean4_cfg_setup( &cfg );
    ENOCEAN4_MAP_MIKROBUS( cfg, MIKROBUS_1 );
    enocean4_init( &enocean4, &cfg );

    Delay_ms ( 500 );

    enocean4_response_handler_set( &enocean4, &make_response );
    Delay_ms ( 300 );
    rsp_check = 1;
    enocean4_reset( &enocean4 );

    log_printf( &logger, "** EnOcean 4 initialization done **\r\n" );
    log_printf( &logger, "***********************************\r\n" );
    Delay_ms ( 500 );
    
    // Clearing RX buffer
    {
        uint8_t tmp_buf[ 100 ];
        enocean4_generic_read( &enocean4, tmp_buf, 100 );
    }

    log_printf( &logger, "Device version reading...\r\n" );
    message.data_length    = 0x0001;
    message.opt_length     = 0x00;
    message.packet_type    = ENOCEAN4_PACK_TYPE_COMMON_CMD;
    message.data_buff[ 0 ] = ENOCEAN4_CMD_CO_RD_VERSION;
    enocean4_send_packet( &enocean4, &message );
    check_response( );
    log_printf( &logger, "***********************************\r\n" );
    Delay_ms ( 1000 );
    Delay_ms ( 500 );

    log_printf( &logger, "Add filter to filter list...\r\n" );
    message.data_length    = 0x0007;
    message.opt_length     = 0x00;
    message.packet_type    = ENOCEAN4_PACK_TYPE_COMMON_CMD;
    message.data_buff[ 0 ] = ENOCEAN4_CMD_CO_WR_FILTER_ADD;
    message.data_buff[ 1 ] = ENOCEAN4_FILTER_TYPE_RORG;
    message.data_buff[ 2 ] = 0x00;
    message.data_buff[ 3 ] = 0x00;
    message.data_buff[ 4 ] = 0x00;
    message.data_buff[ 5 ] = ENOCEAN4_RORG_ADT;
    message.data_buff[ 6 ] = ENOCEAN4_APPLY_RADIO_INTER;
    enocean4_send_packet( &enocean4, &message );
    check_response( );
    log_printf( &logger, "***********************************\r\n" );
    Delay_ms ( 1000 );
    Delay_ms ( 500 );

    log_printf( &logger, "Supplied filters reading...\r\n" );
    message.data_length    = 0x0001;
    message.opt_length     = 0x00;
    message.packet_type    = ENOCEAN4_PACK_TYPE_COMMON_CMD;
    message.data_buff[ 0 ] = ENOCEAN4_CMD_CO_RD_FILTER;
    enocean4_send_packet( &enocean4, &message );
    check_response( );
    log_printf( &logger, "***********************************\r\n" );
    log_printf( &logger, "Ready to exchange telegrams\r\n" );
    log_printf( &logger, "***********************************\r\n" );
    Delay_ms ( 1000 );
    Delay_ms ( 500 );
}

void application_task ( void )
{
#ifdef DEMO_APP_RECEIVER
    log_printf( &logger, "Waiting for a telegram...\r\n" );
    check_response( );
    log_printf( &logger, "***********************************\r\n" );

    log_printf( &logger, "Replying to the received telegram...\r\n" );
    send_telegram( DEMO_ANSWER, ENOCEAN4_RORG_ADT );
    check_response( );
    log_printf( &logger, "***********************************\r\n" );
#endif
#ifdef DEMO_APP_TRANSMITTER
    log_printf( &logger, "Sending a telegram...\r\n" );
    send_telegram( DEMO_MESSAGE, ENOCEAN4_RORG_ADT );
    check_response( );
    log_printf( &logger, "***********************************\r\n" );

    log_printf( &logger, "Waiting for a response...\r\n" );
    check_response( );
    log_printf( &logger, "***********************************\r\n" );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
#endif
}

int main ( void ) 
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif
    
    application_init( );
    
    for ( ; ; ) 
    {
        application_task( );
    }

    return 0;
}

// ------------------------------------------------------------------------ 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.