Intermediate
20 min

Enable reliable Bluetooth BR/EDR and BLE connectivity with BT122-A and STM32F031K6

Bluetooth Classic and Bluetooth Low Energy (BLE) v4.2 communication solution

BT122-A Click with Nucleo 32 with STM32F031K6 MCU

Published Feb 12, 2025

Click board™

BT122-A Click

Dev Board

Nucleo 32 with STM32F031K6 MCU

Compiler

NECTO Studio

MCU

STM32F031K6

Bluetooth BR/EDR and BLE connectivity perfect for cable replacement, smart home systems, and industrial automation

A

A

Hardware Overview

How does it work?

BT122-A Click is based on the BT122-A, a dual-mode Bluetooth BR/EDR and BLE v4.2 module from Silicon Labs. This module is specifically designed for applications that demand Bluetooth Low Energy and Classic connectivity, offering communication with legacy devices supporting Bluetooth SPP or Apple® iAP2 profiles and modern devices that use Bluetooth Low Energy protocols. The BT122-A integrates a robust Bluetooth radio with up to +11dBm transmit power and -95dBm receive sensitivity, ensuring reliable communication over extended ranges. Additionally, the module features a built-in antenna and is powered by a low-power 32-bit ARM Cortex-M4 microcontroller, which, in combination with Silicon Labs' Dual Mode stack software, provides exceptional processing efficiency and advanced connectivity capabilities. The BT122-A is engineered to operate either as a standalone modem controlled by an external host

MCU or as an independent application processor. For standalone operation, developers can use the integrated ARM Cortex-M4 MCU, enabling the embedding of applications using the Silicon Labs BGScript™ scripting language. This flexibility makes the module suitable for a wide range of use cases. Whether for cable replacement, HID devices, or advanced applications like health and fitness monitoring, industrial automation, M2M connectivity, or home automation gateways, the BT122-A Click delivers high performance and adaptability to meet diverse application requirements. This Click board™ establishes communication between the BT122-A module and the host MCU through a UART interface, using standard UART RX and TX pins and hardware flow control via CTS and RTS pins. The default communication speed is set at 115200bps, ensuring efficient data exchange. The host MCU

configures communication and other features using high-level AT commands, making it easy to manage without requiring in-depth knowledge of Bluetooth protocol. Additionally, the board includes an I2C interface with a maximum clock speed of 1MHz. In addition to the interface pins, the board features a reset (RST) pin for hard-resetting the module when necessary and SWD pads designed for use with MIKROE's 6-pin Needle Cable, providing an optional flash and debug SWD (Serial Wire Debug) interface functionality. 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. It also comes equipped with a library containing functions and example code that can be used as a reference for further development.

BT122-A 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

Used MCU Pins

mikroBUS™ mapper

NC
NC
AN
ID SEL
PA11
RST
UART CTS / ID COMM
PA4
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
UART RTS
PA12
INT
UART TX
PA10
TX
UART RX
PA9
RX
I2C Clock
PB6
SCL
I2C Data
PB7
SDA
NC
NC
5V
Ground
GND
GND
1

Take a closer look

Click board™ Schematic

BT122-A 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

1. Application Output - In Debug mode, the 'Application Output' window enables real-time data monitoring, offering direct insight into execution results. Ensure proper data display by configuring the environment correctly using the provided tutorial.

2. UART Terminal - Use the UART Terminal to monitor data transmission via a USB to UART converter, allowing direct communication between the Click board™ and your development system. Configure the baud rate and other serial settings according to your project's requirements to ensure proper functionality. For step-by-step setup instructions, refer to the provided tutorial.

3. Plot Output - The Plot feature offers a powerful way to visualize real-time sensor data, enabling trend analysis, debugging, and comparison of multiple data points. To set it up correctly, follow the provided tutorial, which includes a step-by-step example of using the Plot feature to display Click board™ readings. To use the Plot feature in your code, use the function: plot(*insert_graph_name*, variable_name);. This is a general format, and it is up to the user to replace 'insert_graph_name' with the actual graph name and 'variable_name' with the parameter to be displayed.

Software Support

Library Description

BT122-A Click demo application is developed using the NECTO Studio, ensuring compatibility with mikroSDK's open-source libraries and tools. Designed for plug-and-play implementation and testing, the demo is fully compatible with all development, starter, and mikromedia boards featuring a mikroBUS™ socket.

Example Description
This example demonstrates the use of BT122-A Click board by processing data from a connected BT device.

Key functions:

  • bt122a_cfg_setup - Config Object Initialization function.

  • bt122a_init - Initialization function.

  • bt122a_set_local_name - This function sets the local name of the device.

  • bt122a_send_package - This function sends a data package to the Click board.

  • bt122a_read_package - This function waits for the command or event type of message to arrive and then reads the complete message and stores it to the pkg structure.

Application Init
Initializes the driver and configures the Click board.

Application Task
Handles most of the events required for this example, the packages of events not supported in this example will just be displayed on the USB UART. The event handler will display all messages received from the remote device on the USB UART and echo the same message to the connected device.

Open Source

Code example

The complete application code and a ready-to-use project are available through the NECTO Studio Package Manager for direct installation in the NECTO Studio. The application code can also be found on the MIKROE GitHub account.

/*!
 * @file main.c
 * @brief BT122-A Click Example.
 *
 * # Description
 * This example demonstrates the use of BT122-A Click board by processing data
 * from a connected BT device.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver and configures the Click board.
 *
 * ## Application Task
 * Handles most of the events required for this example, the packages of events not 
 * supported in this example will just be displayed on the USB UART.
 * The event handler will display all messages received from the remote device on
 * the USB UART and echo the same message to the connected device.
 *
 * ## Additional Function
 * - static void bt122a_check_response ( err_t error_flag )
 * - static void bt122a_event_handler ( bt122a_t *ctx )
 *
 * @note
 * We have used the Serial Bluetooth Terminal smartphone application for the test. 
 * A smartphone and the Click board must be paired in order to exchange messages with each other.
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "bt122a.h"

// Local device name.
#define DEVICE_NAME     "BT122-A Click"

static bt122a_t bt122a;
static log_t logger;
err_t error_flag = BT122A_OK;

/**
 * @brief BT122A check response function.
 * @details This function checks the error flag and logs OK or FAIL message on USB UART.
 * @param[in] error_flag : Function error flag.
 * @return None.
 * @note None.
 */
static void bt122a_check_response ( err_t error_flag );

/**
 * @brief BT122A event handler function.
 * @details This function handles most of the events required for this example.
 * @param[in] ctx : Click context object.
 * See #bt122a_t object definition for detailed explanation.
 * @return None.
 * @note The Click board must be configured and the remote device must be connected to it.
 */
static void bt122a_event_handler ( bt122a_t *ctx );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    bt122a_cfg_t bt122a_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.
    bt122a_cfg_setup( &bt122a_cfg );
    BT122A_MAP_MIKROBUS( bt122a_cfg, MIKROBUS_1 );
    if ( UART_ERROR == bt122a_init( &bt122a, &bt122a_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }

    log_printf( &logger, ">>> Set Local Name to \"%s\"\r\n", ( char * ) DEVICE_NAME );
    error_flag = bt122a_set_local_name ( &bt122a, DEVICE_NAME );
    bt122a_check_response ( error_flag );
    
    log_printf( &logger, ">>> Delete Bondings\r\n" );
    error_flag = bt122a_delete_bondings ( &bt122a );
    bt122a_check_response ( error_flag );
    
    log_printf( &logger, ">>> Set Bondable Mode\r\n" );
    error_flag = bt122a_set_bondable_mode ( &bt122a, BT122A_SM_SET_BONDABLE_ALLOWED );
    bt122a_check_response ( error_flag );
    
    log_printf( &logger, ">>> Set GAP Mode\r\n" );
    error_flag = bt122a_set_gap_mode ( &bt122a, BT122A_GAP_MODE_CONNECTABLE, 
                                                BT122A_GAP_MODE_DISCOVERABLE, 
                                                BT122A_GAP_MODE_NOT_LIMITED );
    bt122a_check_response ( error_flag );
    
    log_printf( &logger, ">>> RFCOMM Start Server\r\n" );
    error_flag = bt122a_rfcomm_start_server ( &bt122a, BT122A_RFCOMM_SERVER_DEF_SDP_ID, 
                                                       BT122A_RFCOMM_SERVER_DEF_STREAM_DEST );
    bt122a_check_response ( error_flag );
    
    log_info( &logger, " Application Task " );
}

void application_task ( void ) 
{
    bt122a_event_handler( &bt122a );
}

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;
}

static void bt122a_check_response ( err_t error_flag )
{
    log_printf( &logger, "<<< %s\r\n\n", ( char * ) 
                ( ( BT122A_OK == error_flag ) ? "OK" : "FAIL" ) );
}

static void bt122a_event_handler ( bt122a_t *ctx )
{
    bt122a_package_t pkg;
    uint8_t byte_cnt = 0;
    
    if ( BT122A_OK != bt122a_read_package ( ctx, &pkg ) ) 
    {
        return;
    }

    if ( BT122A_MSG_TYPE_EVENT != pkg.msg_type )
    {
        log_printf( &logger, "<<< Unknown Message Type: 0x%.2X\r\n\n", ( uint16_t ) pkg.msg_type );
        return;
    }

    switch ( pkg.msg_class )
    {
        case BT122A_MSG_CLASS_ENDPOINT:
        {
            if ( BT122A_MSG_ID_EVT_ENDPOINT_DATA == pkg.msg_id )
            {
                log_printf( &logger, "<<< The endpoint 0x%.2X received data: ", 
                            ( uint16_t ) pkg.payload[ 0 ] );
                for ( byte_cnt = 0; byte_cnt < pkg.payload[ 1 ]; byte_cnt++ )
                {
                    log_printf( &logger, "%c", pkg.payload[ byte_cnt + 2 ] );
                }
                pkg.payload[ pkg.payload[ 1 ] + 2 ] = 0;
                log_printf( &logger, "\r\n>>> Echo back the received message\r\n" );
                error_flag = bt122a_endpoint_send_data ( &bt122a, &pkg.payload[ 0 ], &pkg.payload[ 2 ] );
                bt122a_check_response ( error_flag );
            }
            break;
        }
        case BT122A_MSG_CLASS_CONNECTION:
        {
            if ( BT122A_MSG_ID_EVT_CONNECTION_OPENED == pkg.msg_id )
            {
                log_printf( &logger, "<<< Connection 0x%.2X: OPENED\r\n", ( uint16_t ) pkg.payload[ 7 ]  );
                log_printf( &logger, "    Address of remote device: " );
                log_printf( &logger, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X\r\n", ( uint16_t ) pkg.payload[ 5 ],
                                                                          ( uint16_t ) pkg.payload[ 4 ], 
                                                                          ( uint16_t ) pkg.payload[ 3 ],
                                                                          ( uint16_t ) pkg.payload[ 2 ],
                                                                          ( uint16_t ) pkg.payload[ 1 ],
                                                                          ( uint16_t ) pkg.payload[ 0 ] );
                log_printf( &logger, "    Role of the local device : %s\r\n", ( char * )
                            ( 0 == pkg.payload[ 6 ] ? "Peripheral" : "Central" ) );
                
                log_printf( &logger, "    Bonded : %s\r\n\n", 
                            ( 0xFF == pkg.payload[ 8 ] ) ? "NO" : "YES" );
            }
            else if ( BT122A_MSG_ID_EVT_CONNECTION_CLOSED == pkg.msg_id )
            {
                log_printf( &logger, "<<< Connection 0x%.2X: CLOSED\r\n\n", ( uint16_t ) pkg.payload[ 2 ] );
            }
            break;
        }
        case BT122A_MSG_CLASS_SM:
        {
            if ( BT122A_MSG_ID_EVT_SM_BONDED == pkg.msg_id )
            {
                log_printf( &logger, "<<< Connection 0x%.2X: %s\r\n\n", 
                            ( uint16_t ) pkg.payload[ 0 ], ( char * )
                            ( 0xFF != pkg.payload[ 1 ] ? "BONDED" : "NOT BONDED" ) );
            }
            break;
        }
        case BT122A_MSG_CLASS_RFCOMM:
        {
            if ( BT122A_MSG_ID_EVT_RFCOMM_OPENED == pkg.msg_id )
            {
                log_printf( &logger, "<<< RFCOMM connection 0x%.2X: OPENED\r\n", ( uint16_t ) pkg.payload[ 0 ] );
                log_printf( &logger, "    Address of remote device: " );
                log_printf( &logger, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X\r\n\n", ( uint16_t ) pkg.payload[ 6 ],
                                                                            ( uint16_t ) pkg.payload[ 5 ], 
                                                                            ( uint16_t ) pkg.payload[ 4 ],
                                                                            ( uint16_t ) pkg.payload[ 3 ],
                                                                            ( uint16_t ) pkg.payload[ 2 ],
                                                                            ( uint16_t ) pkg.payload[ 1 ] );
            }
            else if ( BT122A_MSG_ID_EVT_RFCOMM_MODEM_STATUS == pkg.msg_id )
            {
                log_printf( &logger, "<<< RFCOMM connection 0x%.2X status: 0x%.2X\r\n\n", 
                            ( uint16_t ) pkg.payload[ 0 ], 
                            ( uint16_t ) pkg.payload[ 1 ] );
            }
            break;
        }
        default:
        {
            log_printf( &logger, "<<< Message Type: 0x%.2X\r\n", ( uint16_t ) pkg.msg_type );
            log_printf( &logger, "    Payload len: 0x%.2X\r\n", ( uint16_t ) pkg.payload_len );
            log_printf( &logger, "    Message Class: 0x%.2X\r\n", ( uint16_t ) pkg.msg_class );
            log_printf( &logger, "    Message ID: 0x%.2X\r\n", ( uint16_t ) pkg.msg_id );
            
            if ( pkg.payload_len > 0 )
            {
                log_printf( &logger, "   Payload: " );
                for ( uint8_t cnt = 0; cnt < pkg.payload_len; cnt++ )
                {
                    log_printf( &logger, " 0x%.2X ", ( uint16_t ) pkg.payload[ cnt ] );
                }
                log_printf( &logger, "\r\n\n" );
            }
            break;
        }
    }
}

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