Intermediate
20 min

Break free from the constraints of traditional wired connections with BT121 and STM32F103RB

Say goodbye to wires and hello to limitless possibilities

BT Click with Nucleo 64 with STM32F103RB MCU

Published Oct 08, 2024

Click board™

BT Click

Dev. board

Nucleo 64 with STM32F103RB MCU

Compiler

NECTO Studio

MCU

STM32F103RB

Simplify your life and streamline your devices with our Bluetooth solution. It's time to connect smarter, not harder.

A

A

Hardware Overview

How does it work?

BT Click is based on the BT121, a dual-mode Bluetooth Smart Ready module solution that gives unparalleled flexibility to integrate Bluetooth Smart and Bluetooth Basic Rate/Enhanced Data Rate (BR/EDR) wireless technologies from Silicon Labs. It contains a high-performance Bluetooth radio, a low-power ARM Cortex MCU, and a Bluegiga Bluetooth Smart Ready stack software, marking it an extremely easy-to-use device. Also, it contains two configurable power-saving modes. Power Mode 1 is a shallow sleep state with all clocks and peripherals running but with the processor core stopped, while Power Mode 2 is a deep sleep state in which most peripheral devices and system clocks are Power-Down states. The BT121 module generates all the required clock signals internally. The clocks used by the internal MCU and external peripherals are synchronized to an internal 32.768kHz crystal connected to the internal RTC, which is always available to wake up the module. It will take approximately two seconds for the RTC oscillator to stabilize after power is connected. To avoid this delay, it's recommended not to turn off the power supply of the BT121 but to set the module into the lowest power mode,

providing the smallest current consumption. This Click board™ uses the UART communication interface as its default communication protocol that supports all standard baud rates up to 4Mbps. It also allows the user to use the I2C interface to configure the module and write the library himself. In addition to standard UART TX/RX pins, it also has UART RTS/CTS pins routed on the CS and PWM pins of the mikroBUS™ socket recommended for every application for reliable data transfer. It is necessary to mention the way this Click board™ works so that the user can use it correctly. In the example code that MIKROE offers to its users, the Serial Bluetooth Terminal Android application was used. By clicking on the given link, the user is directed to the free Android application on the Play Store that needs to be downloaded and installed to pair his device with BT Click. Only after successful pairing the BT Click will be visible in the Serial Bluetooth Terminal Android application. The BT121 module has built-in firmware that can use the module Update feature. The module can be updated through the UART interface by holding the built-in ARM® Cortex® MCU of the BT121 module in a Reset state via the RST pin, which typically

will free the UART communication lines to be used by the Device Firmware Update protocol (DFU). DFU protocol contains commands and events related to controlling firmware updates over the configured host interface and is available only when the module has been booted into DFU Mode. In addition to all features, the BT Click also has additional components such as 2 LED indicators as well as several onboard pushbuttons. Based on the example code, the blue LED labeled as LED1 indicates the successfully established connection visually, and the red LED labeled as LED 2 reports that establishing the connection is unsuccessful. The onboard pushbuttons don't have a precisely defined function. It can be configured to operate as a standard general-purpose digital I/O's, and they are left to the user to configure them according to their needs. 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.

BT Click hardware overview image

Features overview

Development board

Nucleo-64 with STM32F103RB MCU offers a cost-effective and adaptable platform for developers to explore new ideas and prototype their designs. This board harnesses the versatility of the STM32 microcontroller, enabling users to select the optimal balance of performance and power consumption for their projects. It accommodates the STM32 microcontroller in the LQFP64 package and includes essential components such as a user LED, which doubles as an ARDUINO® signal, alongside user and reset push-buttons, and a 32.768kHz crystal oscillator for precise timing operations. Designed with expansion and flexibility in mind, the Nucleo-64 board features an ARDUINO® Uno V3 expansion connector and ST morpho extension pin

headers, granting complete access to the STM32's I/Os for comprehensive project integration. Power supply options are adaptable, supporting ST-LINK USB VBUS or external power sources, ensuring adaptability in various development environments. The board also has an on-board ST-LINK debugger/programmer with USB re-enumeration capability, simplifying the programming and debugging process. Moreover, the board is designed to simplify advanced development with its external SMPS for efficient Vcore logic supply, support for USB Device full speed or USB SNK/UFP full speed, and built-in cryptographic features, enhancing both the power efficiency and security of projects. Additional connectivity is

provided through dedicated connectors for external SMPS experimentation, a USB connector for the ST-LINK, and a MIPI® debug connector, expanding the possibilities for hardware interfacing and experimentation. Developers will find extensive support through comprehensive free software libraries and examples, courtesy of the STM32Cube MCU Package. This, combined with compatibility with a wide array of Integrated Development Environments (IDEs), including IAR Embedded Workbench®, MDK-ARM, and STM32CubeIDE, ensures a smooth and efficient development experience, allowing users to fully leverage the capabilities of the Nucleo-64 board in their projects.

Nucleo 64 with STM32F103RB MCU double side image

Microcontroller Overview

MCU Card / MCU

default

Architecture

ARM Cortex-M3

MCU Memory (KB)

128

Silicon Vendor

STMicroelectronics

Pin count

64

RAM (Bytes)

20480

You complete me!

Accessories

Click Shield for Nucleo-64 comes equipped with two proprietary mikroBUS™ sockets, allowing all the Click board™ devices to be interfaced with the STM32 Nucleo-64 board with no effort. This way, Mikroe allows its users to add any functionality from our ever-growing range of Click boards™, such as WiFi, GSM, GPS, Bluetooth, ZigBee, environmental sensors, LEDs, speech recognition, motor control, movement sensors, and many more. More than 1537 Click boards™, which can be stacked and integrated, are at your disposal. The STM32 Nucleo-64 boards are based on the microcontrollers in 64-pin packages, a 32-bit MCU with an ARM Cortex M4 processor operating at 84MHz, 512Kb Flash, and 96KB SRAM, divided into two regions where the top section represents the ST-Link/V2 debugger and programmer while the bottom section of the board is an actual development board. These boards are controlled and powered conveniently through a USB connection to program and efficiently debug the Nucleo-64 board out of the box, with an additional USB cable connected to the USB mini port on the board. Most of the STM32 microcontroller pins are brought to the IO pins on the left and right edge of the board, which are then connected to two existing mikroBUS™ sockets. This Click Shield also has several switches that perform functions such as selecting the logic levels of analog signals on mikroBUS™ sockets and selecting logic voltage levels of the mikroBUS™ sockets themselves. Besides, the user is offered the possibility of using any Click board™ with the help of existing bidirectional level-shifting voltage translators, regardless of whether the Click board™ operates at a 3.3V or 5V logic voltage level. Once you connect the STM32 Nucleo-64 board with our Click Shield for Nucleo-64, you can access hundreds of Click boards™, working with 3.3V or 5V logic voltage levels.

Click Shield for Nucleo-64 accessories 1 image

Used MCU Pins

mikroBUS™ mapper

Analog Output
PC0
AN
Reset
PC12
RST
UART CTS
PB12
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Boot Mode
PC8
PWM
UART RTS
PC14
INT
UART TX
PA2
TX
UART RX
PA3
RX
I2C Clock
PB8
SCL
I2C Data
PB9
SDA
NC
NC
5V
Ground
GND
GND
1

Take a closer look

Click board™ Schematic

BT Click Schematic schematic

Step by step

Project assembly

Click Shield for Nucleo-64 front image hardware assembly

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

Click Shield for Nucleo-64 front image hardware assembly
Nucleo 64 with STM32F401RE MCU front image hardware assembly
EEPROM 13 Click front image hardware assembly
Prog-cut hardware assembly
Nucleo-64 with STM32XXX MCU MB 1 Mini B Conn - 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
Clicker 4 for STM32F4 HA 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

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

This library contains API for BT Click driver.

Key functions:

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

  • bt_send_package - This function sends a data package to the click board.

  • bt_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 pkg structure.

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 BT Click Example.
 *
 * # Description
 * This example demonstrates the use of BT click board.
 *
 * 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 be just displayed on the USB UART.
 * The event handler will display all messages sent from the remote device on the USB UART and 
 * send back the predefined response message "DONE".
 * There are two specific commands which can be sent from the remote device:
 * "led blink" - calls @b bt_led_blink function for a 5 seconds time period.
 * "check buttons" - calls @b bt_check_buttons function.
 *
 * ## Additional Function
 * - static void bt_event_handler ( bt_t *ctx )
 * - static void bt_led_blink ( bt_t *ctx )
 * - static void bt_check_buttons ( bt_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 "bt.h"

static bt_t bt;
static log_t logger;

/**
 * @brief BT event handler function.
 * @details This function handles most of the events required for this example.
 * It will respond to all messages sent from the remote device with a message "DONE".
 * There are two specific commands which can be sent from the remote device:
 * "led blink" - calls @b bt_led_blink function for a 5 seconds time frame.
 * "check buttons" - calls @b bt_check_buttons function.
 * @param[in] ctx : Click context object.
 * See #bt_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 bt_event_handler ( bt_t *ctx );

/**
 * @brief BT led blink function.
 * @details This function toggles the LEDs on the click board.
 * @param[in] ctx : Click context object.
 * See #bt_t object definition for detailed explanation.
 * @param[in] time_s : LED blinking time in seconds.
 * @return None.
 * @note None.
 */
static void bt_led_blink ( bt_t *ctx, uint16_t time_s );

/**
 * @brief BT check buttons function.
 * @details This function checks all buttons state and displays the result on the USB UART.
 * @param[in] ctx : Click context object.
 * See #bt_t object definition for detailed explanation.
 * @return None.
 * @note None.
 */
static void bt_check_buttons ( bt_t *ctx );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    bt_cfg_t bt_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 );
    Delay_ms( 100 );
    log_info( &logger, " Application Init " );

    // Click initialization.

    bt_cfg_setup( &bt_cfg );
    BT_MAP_MIKROBUS( bt_cfg, MIKROBUS_1 );
    err_t init_flag  = bt_init( &bt, &bt_cfg );
    if ( UART_ERROR == init_flag ) 
    {
        log_error( &logger, " Application Init Error. " );
        log_info( &logger, " Please, run program again... " );

        for ( ; ; );
    }

    log_printf( &logger, " Default Config : %s\r\n\n", ( char * )
                         ( BT_OK == bt_default_cfg ( &bt ) ? "OK" : "FAIL" ) );

    log_printf( &logger, " Set Local Name : %s\r\n\n", ( char * )
                        ( BT_OK == bt_set_local_name ( &bt, "MikroE - BT click" ) ? "OK" : "FAIL" ) );
    
    log_printf( &logger, " Delete Bondings : %s\r\n\n", ( char * )
                        ( BT_OK == bt_delete_bondings ( &bt ) ? "OK" : "FAIL" ) );
    
    log_printf( &logger, " Set Bondable Mode : %s\r\n\n", ( char * )
                        ( BT_OK == bt_set_bondable_mode ( &bt, BT_SM_SET_BONDABLE_ALLOWED ) ? "OK" : "FAIL" ) );
    
    log_printf( &logger, " Set GAP Mode : %s\r\n\n", ( char * )
                        ( BT_OK == bt_set_gap_mode ( &bt, BT_GAP_MODE_CONNECTABLE, 
                                                          BT_GAP_MODE_DISCOVERABLE, 
                                                          BT_GAP_MODE_NOT_LIMITED ) ? "OK" : "FAIL" ) );
    
    log_printf( &logger, " RFCOMM Start Server : %s\r\n\n", ( char * )
                        ( BT_OK == bt_rfcomm_start_server ( &bt, BT_RFCOMM_SERVER_DEF_SDP_ID, 
                                                                 BT_RFCOMM_SERVER_DEF_STREAM_DEST ) ? "OK" : "FAIL" ) );

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

void application_task ( void ) 
{
    bt_event_handler( &bt );
}

void main ( void ) 
{
    application_init( );

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

static void bt_event_handler ( bt_t *ctx )
{
    bt_package_t pkg;
    
    if ( BT_OK == bt_read_package ( ctx, &pkg ) ) 
    {
        if ( BT_MSG_TYPE_EVENT == pkg.msg_type )
        {
            if ( BT_MSG_CLASS_ENDPOINT == pkg.msg_class )
            {
                if ( BT_MSG_ID_EVT_ENDPOINT_DATA == pkg.msg_id )
                {
                    log_printf( &logger, " The endpoint 0x%.2X received data: ", ( uint16_t ) pkg.payload[ 0 ] );
                    for ( uint8_t cnt = 0; cnt < pkg.payload[ 1 ]; cnt++ )
                    {
                        log_printf( &logger, "%c", pkg.payload[ cnt + 2 ] );
                    }
                    if ( memcmp ( &pkg.payload[ 2 ], "check buttons", 13 ) == 0 )
                    {
                        bt_check_buttons( ctx );
                    }
                    if ( memcmp ( &pkg.payload[ 2 ], "led blink", 9 ) == 0 )
                    {
                        bt_led_blink( ctx, 5 );
                    }
                    log_printf( &logger, " Sending the message \"DONE\" back to the remote device : %s\r\n\n", ( char * )
                                        ( BT_OK == bt_endpoint_send_data ( &bt, &pkg.payload[ 0 ], "DONE\r\n" ) ? "OK" : "FAIL" ) );
                }  
            }
            else if ( BT_MSG_CLASS_CONNECTION == pkg.msg_class )
            {
                if ( BT_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 ( BT_MSG_ID_EVT_CONNECTION_CLOSED == pkg.msg_id )
                {
                    log_printf( &logger, " Connection 0x%.2X: CLOSED\r\n\n", ( uint16_t ) pkg.payload[ 2 ] );
                }
            }
            else if ( BT_MSG_CLASS_SM == pkg.msg_class )
            {
                if ( BT_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" ) );
                }
            }
            else if ( BT_MSG_CLASS_RFCOMM == pkg.msg_class )
            {
                if ( BT_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 ( BT_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 ] );
                }
            }
            else
            {
                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" );
                }
            }
        }
        else
        {
            log_error( &logger, " No event message type.\r\n" );
            log_printf( &logger, " Message Type: 0x%.2X\r\n", ( uint16_t ) pkg.msg_type );
        }
    }
    else
    {
        log_error( &logger, " Reading package.\r\n" );
    }
    Delay_ms( 100 );
}

static void bt_led_blink ( bt_t *ctx, uint16_t time_s )
{
    for ( uint16_t cnt = 0; cnt < time_s; cnt++ )
    {
        bt_hardware_write_gpio ( ctx, BT_HARDWARE_CONFIG_PORT_B, 
                                      BT_HARDWARE_ALL_LEDS_PIN_MASK, 
                                      BT_HARDWARE_ALL_LEDS_PIN_MASK );
        Delay_ms ( 500 );
        bt_hardware_write_gpio ( ctx, BT_HARDWARE_CONFIG_PORT_B, 
                                      BT_HARDWARE_ALL_LEDS_PIN_MASK, 
                                      0 );
        Delay_ms ( 500 );
    }
}

static void bt_check_buttons ( bt_t *ctx )
{
    uint16_t port_data = 0;
    
    if ( BT_OK == bt_hardware_read_gpio ( ctx, BT_HARDWARE_CONFIG_PORT_B, 
                                               BT_HARDWARE_ALL_BUTTONS_PIN_MASK, 
                                               &port_data ) )
    {
        log_printf( &logger, "\r\n Buttons pressed : - " );
    
        if ( port_data & BT_HARDWARE_BUTTON1_PIN_MASK )
        {
            log_printf( &logger, "1 - " );
        }
        
        if ( port_data & BT_HARDWARE_BUTTON2_PIN_MASK )
        {
            log_printf( &logger, "2 - " );
        }
        
        if ( port_data & BT_HARDWARE_BUTTON3_PIN_MASK )
        {
            log_printf( &logger, "3 - " );
        }
        
        if ( port_data & BT_HARDWARE_BUTTON4_PIN_MASK )
        {
            log_printf( &logger, "4 - " );
        }
        
        if ( port_data & BT_HARDWARE_BUTTON5_PIN_MASK )
        {
            log_printf( &logger, "5 - " );
        }
        
        if ( ( port_data & BT_HARDWARE_ALL_BUTTONS_PIN_MASK ) == 0 )
        {
            log_printf( &logger, "None - " );
        }
        log_printf( &logger, "\r\n\n" );
    }
    else
    {
        log_error( &logger, " Reading GPIO.\r\n" );
    }
}

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