Advanced
30 min

Create your own N-PLC system for any IoT application, thanks to SM2400 and PIC18F97J94

Narrow-band Power Line Communication (N-PLC)

N-PLC Click with Fusion for PIC v8

Published Nov 09, 2023

Click board™

N-PLC Click

Dev Board

Fusion for PIC v8

Compiler

NECTO Studio

MCU

PIC18F97J94

Revolutionize your connectivity strategy and cut expenses by leveraging your existing electrical infrastructure for seamless communication.

A

A

Hardware Overview

How does it work?

N-PLC Click is based on the SM2400, the ultimate Narrow-band Power Line Communication (N-PLC) modem from Semitech that combines cost-effective design optimized for PLC applications with a high level of programmability to address a multitude of communications schemes and evolving standards. The SM2400 system-on-chip (SoC) features a dual-core architecture for dedicated PHY signal processing and MAC layer functionality to guarantee superior communication performance while maintaining high flexibility and programmability for OFDM-based and other open standards and fully customized implementations. It has a set of firmware options implementing IEEE 1901.2 compliant PHY and MAC layers, a 6LoWPAN data link layer, PRIME, G3-PLC, and other special modes tailored for industrial IoT applications. The SM2400 combines the benefits of programmable architecture with power and efficiency by utilizing a DSP core configured specifically for N-PLC modulations and a dedicated 32-bit core that runs protocols. It contains a high-speed 256-bit AES-CCM engine to ensure standard compliance and secure communication, and all the necessary mixed-signal components, such as ADC, DAC, gain control and two OpAmp to deliver a cost-effective N-PLC system design for any application. In addition to the SM2400, this Click board™ also includes a PLC line driver, the SGM8423, a high efficiency, Class A/B, and a low distortion power line driver. It is optimized to accept a signal from a

Power Line Carrier modem, such as the SM2400, which is presented in the example code, where two such boards talk to each other in a transmitter/receiver configuration. Besides directly performing enable/shutdown control of the line driver, the SM2400 has one diagnostic signal that can indicate error conditions, such as overcurrent or overheating, reported by the line driver. In addition, it is also possible to supply high voltage (110/220VAC) as an input on the PLC CONN connector, which will be converted into a signal of the appropriate level via the N-PLC Wall Adapter, a simple yet very useful power line communication AC coupling circuit for safe and secure operation. The green PLC PWR LED signal that the N-PLC Wall Adapter is connected and powered. The SM2400 communicates with an MCU using the UART interface as its main one, with commonly used UART RX and TX pins and the optional hardware flow control pins UART CTS and RTS (Clear to Send and Ready to Send). The UART interface serves as the primary interface to a host, which can be an MCU or a converter, such as serial-to-USB. As an alternative to the UART interface, users also have at their disposal the lines of the SPI serial interface (to use this interface, it is necessary to populate the appropriate 0Ω resistors to activate the SPI lines). The SM2400 executes its firmware from internal memory, with the loaded code at a boot time. The SM2400 can boot either from an external SPI flash, the AT25FF041A, or from a host MCU via UART interface depending on

the logic state of the MD1 pin of the mikroBUS™ socket (0 - boot over UART interface that allows direct firmware download (boot from the host), 1 – boot from external SPI flash memory), with the host MCU being the Master. In addition, the SM2400 provides the possibility of a reset via the RST pin of the mikroBUS™ socket (asserting this pin causes full chip reset and reboot), as well as visual detection of the communication status via the red PHY LED, which is asserted when an incoming packet is detected. The power supply of this Click board™ is performed very simply, without any additional hardware configuration (depending on the power supply capability and the desired range). It is possible to power the board in two ways: internally and externally. By default, the board power will be provided internally over the LM5158 boost converter in the value of 15V, which is obtained from the 5V mikroBUS™ power rail. When applying an external power supply of 15VDC on the VEXT terminal, the N-PLC Click will automatically be powered from the external source thanks to the protection of the converter, which automatically recognizes the presence of the external power supply and gives it priority. 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.

N-PLC Click hardware overview image

Features overview

Development board

Fusion for PIC v8 is a development board specially designed for the needs of rapid development of embedded applications. It supports a wide range of microcontrollers, such as different PIC, dsPIC, PIC24, and PIC32 MCUs regardless of their number of pins, and a broad set of unique functions, such as the first-ever embedded debugger/programmer over WiFi. 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. Thanks to innovative manufacturing technology, Fusion for PIC v8 provides a fluid and immersive working experience, allowing access anywhere and under any

circumstances at any time. Each part of the Fusion for PIC v8 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 module for the development board. It can use a wide range of external power sources, including a battery, an external 12V power supply, and a power source via the USB Type-C (USB-C) connector. Communication options such as USB-UART, USB

HOST/DEVICE, CAN (on the MCU card, if supported), and Ethernet are also included, including the well-established mikroBUS™ standard, a standardized socket for the MCU card (SiBRAIN standard), and two display options (graphical and character-based LCD). Fusion for PIC v8 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.

Fusion for PIC v8 horizontal image

Microcontroller Overview

MCU Card / MCU

default

Type

8th Generation

Architecture

PIC

MCU Memory (KB)

128

Silicon Vendor

Microchip

Pin count

100

RAM (Bytes)

3862

You complete me!

Accessories

An assortement of compatible AC plugs for all parts of the world are also sold. Reliability is paramount when it comes to power supply units. That's why we opted to carry Sunny Power Supply units. Note that power supply units are sold without AC plugs. Depending on where you are in the world, or where you plan to be, we also offer a wide selection of compatible AC plugs.

N-PLC Click accessories 3 image

MIKROE offers a wide range of AC plugs for our Wall Power Supply units compatible with standards from Europe, the USA, the UK, India, China, Brazil, Australia, South Africa, Korea, and Argentina.

N-PLC Click accessories 2 image

Used MCU Pins

mikroBUS™ mapper

UART CTS
PA3
AN
Reset
PJ4
RST
SPI Chip Select
PL1
CS
SPI Clock
PD6
SCK
SPI Data OUT
PD5
MISO
SPI Data IN
PD4
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Boot Mode
PG4
PWM
UART RTS
PB0
INT
UART TX
PE1
TX
UART RX
PE0
RX
NC
NC
SCL
NC
NC
SDA
Power Supply
5V
5V
Ground
GND
GND
1

Take a closer look

Schematic

N-PLC Click Schematic schematic

Step by step

Project assembly

Fusion for PIC v8 front image hardware assembly

Start by selecting your development board and Click board™. Begin with the Fusion for PIC v8 as your development board.

Fusion for PIC v8 front image hardware assembly
GNSS2 Click front image hardware assembly
SiBRAIN for PIC32MZ1024EFK144 front image hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
v8 SiBRAIN 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
Necto image step 7 hardware assembly
Necto image step 8 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 via UART Mode

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

2. After the programming is completed, click on the Tools icon in the upper-right panel, and select the UART Terminal.

3. After opening the UART Terminal tab, first check the baud rate setting in the Options menu (default is 115200). If this parameter is correct, activate the terminal by clicking the "CONNECT" button.

4. Now terminal status changes from Disconnected to Connected in green, and the data is displayed in the Received data field.

UART_Application_Output

Software Support

Library Description

This library contains API for N-PLC Click driver.

Key functions:

  • nplc_set_mode - This function sets operation mode to command or data.

  • nplc_generic_write - This function writes a desired number of data bytes by using UART serial interface.

  • nplc_generic_read - This function reads a desired number of data bytes by using UART serial interface.

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 N-PLC Click Example.
 *
 * # Description
 * This example demonstrates the use of an N-PLC click boards by showing
 * the communication between the two click boards configured as a receiver and transmitter.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver, resets the click board to default config, displays the firmware version
 * and switches to data operation mode. After that displays the selected application mode
 * and sends an initial message in case of transmitter mode.
 *
 * ## Application Task
 * Reads all the received data and echoes them back to the transmitter. The received and echoed messages
 * will be displayed on the USB UART.
 * 
 * @note
 * Once both devices are programmed, one as a receiver and the other as a transmitter, you will need to reset
 * the transmitter board in order to start the communication by sending an initial message.
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "nplc.h"

// Comment out the line below in order to switch the application mode to receiver
// #define DEMO_APP_TRANSMITTER

#define DEMO_TEXT_MESSAGE       "MikroE - N-PLC click board"

#define PROCESS_BUFFER_SIZE     200
#define RSP_TIMEOUT_MS          20000

static nplc_t nplc;
static log_t logger;

static char app_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
static int32_t app_buf_len = 0;

/**
 * @brief N-PLC clearing application buffer.
 * @details This function clears memory of application buffer and reset its length and counter.
 * @note None.
 */
static void nplc_clear_app_buf ( void );

/**
 * @brief N-PLC data reading function.
 * @details This function reads data from device and concatenates data to application buffer.
 * @return @li @c  0 - Read some data.
 *         @li @c -1 - Nothing is read or buffer overflow error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t nplc_process ( void );

/**
 * @brief N-PLC display response function.
 * @details This function checks if any data is received from device and displays it on the USB UART.
 * @return @li @c  0 - Read some data.
 *         @li @c -1 - Timeout error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t nplc_display_rsp ( void );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    nplc_cfg_t nplc_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.
    nplc_cfg_setup( &nplc_cfg );
    NPLC_MAP_MIKROBUS( nplc_cfg, MIKROBUS_1 );
    if ( UART_ERROR == nplc_init( &nplc, &nplc_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    } 
    nplc_process( );
    nplc_clear_app_buf( );

    log_printf( &logger, " - HW reset -\r\n" );
    nplc_hw_reset ( &nplc );
    nplc_display_rsp ( );
    
    log_printf( &logger, " - Go to command mode -\r\n" );
    nplc_set_mode ( &nplc, NPLC_MODE_COMMAND );
    nplc_display_rsp ( );
    
    log_printf( &logger, " - Factory reset -\r\n" );
    nplc_factory_reset ( &nplc );
    nplc_display_rsp ( );
    
    log_printf( &logger, " - Reboot -\r\n" );
    nplc_sw_reset ( &nplc );
    nplc_display_rsp ( );
    
    log_printf( &logger, " - Go to command mode -\r\n" );
    nplc_set_mode ( &nplc, NPLC_MODE_COMMAND );
    nplc_display_rsp ( );
    
    log_printf( &logger, " - Show firmware version -\r\n" );
    nplc_firmware_version ( &nplc );
    nplc_display_rsp ( );
    
    log_printf( &logger, " - Go to data mode -\r\n" );
    nplc_set_mode ( &nplc, NPLC_MODE_DATA );

#ifdef DEMO_APP_TRANSMITTER
    log_printf( &logger, " Application Mode: Transmitter\r\n" );
    
    log_printf( &logger, " Sending initial message: %s", ( char * ) DEMO_TEXT_MESSAGE );
    nplc_generic_write( &nplc, DEMO_TEXT_MESSAGE, strlen ( DEMO_TEXT_MESSAGE ) );
    log_printf( &logger, "\r\n--------------------------------\r\n" );
    Delay_ms( 1000 ); 
#else
    log_printf( &logger, " Application Mode: Receiver\r\n" );
#endif   
    log_info( &logger, " Application Task " );
}

void application_task ( void ) 
{
    nplc_process ( );
    if ( app_buf_len > 0 ) 
    {
        Delay_ms( 500 );
        nplc_process( );
        log_printf( &logger, " Received message: %s", app_buf );
        log_printf( &logger, "\r\n--------------------------------\r\n" );
        Delay_ms( 500 );
        log_printf( &logger, " Sending echo response: %s", app_buf );
        nplc_generic_write( &nplc, app_buf, app_buf_len );
        log_printf( &logger, "\r\n--------------------------------\r\n\n" );
        nplc_clear_app_buf( );
        Delay_ms( 1000 ); 
    }
}

void main ( void ) 
{
    application_init( );

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

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

static err_t nplc_process ( void ) 
{
    int32_t rx_size;
    char rx_buff[ PROCESS_BUFFER_SIZE ] = { 0 };
    Delay_ms ( 1 );
    rx_size = nplc_generic_read( &nplc, rx_buff, PROCESS_BUFFER_SIZE );
    Delay_ms ( 1 );
    if ( rx_size > 0 ) 
    {
        int32_t buf_cnt = 0;
        if ( ( app_buf_len + rx_size ) > PROCESS_BUFFER_SIZE ) 
        {
            nplc_clear_app_buf( );
            return NPLC_ERROR;
        } 
        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 ] ) 
            {
                app_buf[ ( buf_cnt + rx_cnt ) ] = rx_buff[ rx_cnt ];
            }
            else
            {
                app_buf_len--;
                buf_cnt--;
            }
        }
        return NPLC_OK;
    }
    return NPLC_ERROR;
}

static err_t nplc_display_rsp ( void )
{
    uint32_t timeout = RSP_TIMEOUT_MS;
    while ( timeout-- ) 
    {
        nplc_process( );
        if ( app_buf_len > 0 ) 
        {
            Delay_ms( 100 );
            nplc_process( );
            for ( int32_t buf_cnt = 0; buf_cnt < app_buf_len; buf_cnt++ )
            {
                log_printf( &logger, "%c", app_buf[ buf_cnt ] );
            }
            nplc_clear_app_buf( );
            log_printf( &logger, "\r\n--------------------------------\r\n" );
            return NPLC_OK;
        }
    }
    return NPLC_ERROR;
}

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