Beginner
10 min

Transform your project into a mass storage powerhouse with microSD and STM32G071RB

Small card, big storage

microSD Click with Nucleo 64 with STM32G071RB MCU

Published Oct 08, 2024

Click board™

microSD Click

Dev. board

Nucleo 64 with STM32G071RB MCU

Compiler

NECTO Studio

MCU

STM32G071RB

Experience the freedom of limitless data storage and boost your project's storage capacity for all multimedia needs

A

A

Hardware Overview

How does it work?

microSD Click is based on the microSD card slot for microSD cards used as a mass storage media for portable devices. The microSD card slot has a push-in ejector, which helps a lot while inserting or removing the microSD card without damaging it. It also helps to hold the microSD card tight in place, thus making all internal connectors safely connected with the microSD card and securing reliable connection and data flow. The power

supply of the microSD card is properly decoupled with capacitors, which eliminates the noise on the microSD Click board and ensures your data is safe on the microSD card. microSD Click and the microSD card use the standard 4-Wire SPI serial interface to communicate with the host MCU. The CS pin is pulled up by default, and the same goes for the card detect pin of the microSD card slot. When the microSD card is inserted, you will get the

logic Low state on the CD pin of the mikroBUS™ socket. 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.

microSD Click hardware overview image

Features overview

Development board

Nucleo-64 with STM32G071RB 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 STM32G071RB MCU double side image

Microcontroller Overview

MCU Card / MCU

default

Architecture

ARM Cortex-M0

MCU Memory (KB)

128

Silicon Vendor

STMicroelectronics

Pin count

64

RAM (Bytes)

36864

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

Card Detection
PC0
AN
NC
NC
RST
SPI Chip Select
PB12
CS
SPI Clock
PB3
SCK
SPI Data OUT
PB4
MISO
SPI Data IN
PB5
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
NC
NC
INT
NC
NC
TX
NC
NC
RX
NC
NC
SCL
NC
NC
SDA
NC
NC
5V
Ground
GND
GND
1

Take a closer look

Click board™ Schematic

microSD Click Schematic schematic

Step by step

Project assembly

Click Shield for Nucleo-64 accessories 1 image hardware assembly

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

Click Shield for Nucleo-64 accessories 1 image hardware assembly
Nucleo 64 with STM32F401RE MCU front image hardware assembly
LTE IoT 5 Click front image hardware assembly
Prog-cut hardware assembly
Board mapper by product8 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 microSD Click driver.

Key functions:

  • microsd_check_card_detection - This function checks and waits for a microSD card to be inserted to the Click board™.

  • microsd_create_directory - This function creates a new directory at the selected absolute path. The path should contain a name of the directory to be created.

  • microsd_display_directory - This function reads and displays the content of the directory at the selected path.

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 microSD Click example
 *
 * # Description
 * This example demonstrates a basic file system functionality and operations with files
 * and directories using a microSD click board.
 * 
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver and logger and performs a several operations with file system
 * that demonstrates the basic usage of microSD click board.
 *
 * ## Application Task
 * Reads and displays on the USB UART the contents of all directories created
 * during application init.
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "microsd.h"

static microsd_t microsd;
static log_t logger;

// WARNING: If you change the value of below macro to 1 the inserted
// microSD card will be formatted at the beginning.
#define FORMAT_CARD 0

// Files and directories path and names definition
#define PATH_ROOT   "3:/"
#define PATH_DIR1   PATH_ROOT "_DIR1_/"
#define PATH_DIR2   PATH_DIR1 "_DIR2_/"
#define PATH_DIR3   PATH_DIR1 "_DIR3_/"
#define PATH_DIR4   PATH_ROOT "_DIR4_/"
#define PATH_DIR5   PATH_DIR1 "_DIR5_/"
#define PATH_FILE1  PATH_ROOT "_FILE1_.TXT"
#define PATH_FILE2  PATH_DIR3 "_FILE2_.CSV"

// Data buffer size used for read/write operations
#define BUFFER_SIZE 256

/**
 * @brief microSD check card detection function.
 * @details This function checks and waits for a microSD card to be inserted
 * to the click board.
 * @param[in] ctx : Click context object.
 * See #microsd_t object definition for detailed explanation.
 * @return None.
 * @note None.
 */
void microsd_check_card_detection ( microsd_t *ctx );

/**
 * @brief microSD display directory function.
 * @details This function reads and displays the content of the directory 
 * at the selected path.
 * @param[in] dir_path : The absolute path to the directory to be displayed.
 * @return None.
 * @note None.
 */
void microsd_display_directory ( uint8_t *dir_path );

/**
 * @brief microSD create directory function.
 * @details This function creates a new directory at the selected absolute path.
 * The path should contain a name of the directory to be created.
 * @param[in] dir_path : The absolute path to the directory to be created.
 * @return None.
 * @note None.
 */
void microsd_create_directory ( uint8_t *dir_path );

/**
 * @brief microSD remove path function.
 * @details This function removes a file or a directory from the selected absolute path.
 * @param[in] path : The absolute path to the file or directory to be removed.
 * @return None.
 * @note None.
 */
void microsd_remove_path ( uint8_t *path );

/**
 * @brief microSD rename path function.
 * @details This function renames a file or a directory from the selected absolute path
 * specified by the @c old_path parameter, to the absolute path specified by the 
 * @c new_path parameter.
 * @param[in] old_path : The old path to the file or directory to be renamed.
 * @param[in] new_path : The new path to the file or directory.
 * @return None.
 * @note None.
 */
void microsd_rename_path ( uint8_t *old_path, uint8_t *new_path );

/**
 * @brief microSD create file function.
 * @details This function creates a new file at the selected absolute path.
 * The path should contain a name of the file to be created.
 * @param[in] file_path : The absolute path to the file to be created.
 * @return None.
 * @note None.
 */
void microsd_create_file ( uint8_t *file_path );

/**
 * @brief microSD write to file function.
 * @details This function writes a desired number of data bytes to the end of file
 * at the selected absolute path.
 * @param[in] file_path : The absolute path to the file.
 * @param[in] data_in : The data buffer to be written.
 * @param[in] data_len : Number of data bytes to be written.
 * @return None.
 * @note None.
 */
void microsd_write_to_file ( uint8_t *file_path, uint8_t *data_in, uint32_t data_len );

/**
 * @brief microSD copy file function.
 * @details This function copies the content of a file from the selected absolute path
 * specified by the @c src_file_path parameter, to the absolute path specified by the 
 * @c dst_file_path parameter.
 * @param[in] src_file_path : The source file absolute path.
 * @param[in] dst_file_path : The destination file absolute path.
 * @return None.
 * @note None.
 */
void microsd_copy_file ( uint8_t *src_file_path, uint8_t *dst_file_path );

/**
 * @brief microSD format drive function.
 * @details This function formats the logical drive previously mounted to file system
 * with identifying drive number.
 * @param[in] path : The ROOT path of drive to be formatted.
 * @return None.
 * @note None.
 */
void microsd_format_drive ( uint8_t *path );

/**
 * @brief microSD mount drive function.
 * @details This function mounts the initialized logical drive to the initialized
 * physical drive and overall file system.
 * @param[in] ctx : Click context object.
 * See #microsd_t object definition for detailed explanation.
 * @param[in] path : The ROOT path of drive to be mounted.
 * @return None.
 * @note None.
 */
void microsd_mount_drive ( microsd_t *ctx, uint8_t *path );

/**
 * @brief microSD unmount drive function.
 * @details This function unmounts the mounted logical drive from the file system.
 * @param[in] path : The ROOT path of drive to be unmounted.
 * @return None.
 * @note None.
 */
void microsd_unmount_drive ( uint8_t *path );

void application_init ( void )
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    microsd_cfg_t microsd_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.
    microsd_cfg_setup( &microsd_cfg );
    MICROSD_MAP_MIKROBUS( microsd_cfg, MIKROBUS_1 );
    if ( MICROSD_ERROR == microsd_init( &microsd, &microsd_cfg ) )
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    microsd_check_card_detection ( &microsd );
    Delay_ms ( 1000 );
    
    // Mount the initialized logical drive to the initialized physical drive and overall file system
    microsd_mount_drive ( &microsd, PATH_ROOT );

#if FORMAT_CARD
    // Format the logical drive previously mounted to file system with identifying ROOT drive number
    microsd_format_drive ( PATH_ROOT );
#endif
    microsd_display_directory ( PATH_ROOT );
    Delay_ms ( 2000 );
    
    // Create DIR1 directory
    microsd_create_directory ( PATH_DIR1 );
    Delay_ms ( 500 );
    
    // Create DIR2 directory
    microsd_create_directory ( PATH_DIR2 );
    Delay_ms ( 500 );
    
    // Create DIR4 directory
    microsd_create_directory ( PATH_DIR4 );
    Delay_ms ( 500 );
    
    microsd_display_directory ( PATH_ROOT );
    Delay_ms ( 2000 );
    
    // Remove DIR4 directory
    microsd_remove_path ( PATH_DIR4 );
    Delay_ms ( 500 );
    
    microsd_display_directory ( PATH_ROOT );
    Delay_ms ( 2000 );
    
    microsd_display_directory ( PATH_DIR1 );
    Delay_ms ( 2000 );
    // Rename DIR2 to DIR3
    microsd_rename_path ( PATH_DIR2, PATH_DIR3 );
    Delay_ms ( 500 );
    
    microsd_display_directory ( PATH_DIR1 );
    Delay_ms ( 2000 );
    
    microsd_display_directory ( PATH_ROOT );
    Delay_ms ( 2000 );
    // Create the FILE1
    microsd_create_file ( PATH_FILE1 );
    Delay_ms ( 500 );
    microsd_display_directory ( PATH_ROOT );
    Delay_ms ( 2000 );
    
    uint8_t rw_buffer[ BUFFER_SIZE ] = { 0 };
    // Populate write buffer with bytes ranging from 0 to 255
    for ( uint16_t cnt = 0; cnt < BUFFER_SIZE; cnt++ ) 
    {
        rw_buffer[ cnt ] = cnt & 0xFF;
    }
    microsd_write_to_file ( PATH_FILE1, rw_buffer, BUFFER_SIZE );
    Delay_ms ( 500 );
    microsd_display_directory ( PATH_ROOT );
    Delay_ms ( 2000 );
    
    microsd_display_directory ( PATH_DIR3 );
    Delay_ms ( 2000 );
    // Create the FILE2
    microsd_create_file ( PATH_FILE2 );
    Delay_ms ( 500 );
    microsd_display_directory ( PATH_DIR3 );
    Delay_ms ( 2000 );

    // Copy data from FILE1 to FILE2
    microsd_copy_file ( PATH_FILE1, PATH_FILE2 );
    Delay_ms ( 500 );
    microsd_display_directory ( PATH_DIR3 );
    Delay_ms ( 2000 );
    
    // Create DIR5 directory
    microsd_create_directory ( PATH_DIR5 );
    Delay_ms ( 500 );
    microsd_display_directory ( PATH_DIR1 );
    Delay_ms ( 2000 );
    
    log_info( &logger, " Application Task " );
}

void application_task ( void )
{
    microsd_display_directory ( PATH_ROOT );
    Delay_ms ( 2000 );
    microsd_display_directory ( PATH_DIR1 );
    Delay_ms ( 2000 );
    microsd_display_directory ( PATH_DIR3 );
    Delay_ms ( 2000 );
    microsd_display_directory ( PATH_DIR5 );
    Delay_ms ( 5000 );
    log_printf ( &logger, "\r\n\n" );
}

void main ( void )
{
    application_init( );

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

void microsd_check_card_detection ( microsd_t *ctx )
{
    if ( microsd_get_card_detect ( ctx ) )
    {
        log_printf ( &logger, " microSD card is not detected.\r\n" );
        log_printf ( &logger, " Make sure to insert a card correctly.\r\n" );
        log_printf ( &logger, "--------------------------------------\r\n\n" );
        while ( microsd_get_card_detect ( ctx ) );
        
        log_printf ( &logger, " microSD card is detected.\r\n" );
        log_printf ( &logger, "--------------------------------------\r\n\n" );
    }
}

void microsd_display_directory ( uint8_t *dir_path )
{
    dir_t dir1;
    DIR fatfs_dir_1;
    FILINFO info;
    
    log_printf ( &logger, " #### Display directory: %s\r\n", dir_path );
    // Open directory and make it ready for reading operation
    if ( FSS_OK == dir_open ( &dir1, &fatfs_dir_1, dir_path ) ) 
    {
        log_printf ( &logger, " Open directory\r\n" );
        log_printf ( &logger, " Content:\r\n" );
        // Read directory entries and print their type and name
        for ( ; ; ) 
        {
            if ( FSS_OK == dir_read( &dir1, &info ) ) 
            {
                if ( info.fattrib & AM_DIR ) 
                {
                    log_printf ( &logger, "\t<DIR>\tname: %s\r\n", info.fname );
                }
                else 
                {
                    log_printf ( &logger, "\t<FILE>\tname: %s size: %lu\r\n", info.fname, info.fsize );
                }
            }
            else
            {
                break;
            }
        }
        
        // Close directory
        if ( FSS_OK == dir_close ( &dir1 ) ) 
        {
            log_printf( &logger, " Close directory\r\n" );
        }
    }
    log_printf ( &logger, "--------------------------------------\r\n\n" );
}

void microsd_create_directory ( uint8_t *dir_path )
{
    if ( FSS_OK == file_system_mkdir ( dir_path ) ) 
    {
        log_printf ( &logger, " Create directory: %s\r\n", dir_path );
        log_printf ( &logger, "--------------------------------------\r\n\n" );
    }
}

void microsd_remove_path ( uint8_t *path )
{
    if ( FSS_OK == file_system_remove ( path ) ) 
    {
        log_printf ( &logger, " Remove path: %s\r\n", path );
        log_printf ( &logger, "--------------------------------------\r\n\n" );
    }
}

void microsd_rename_path ( uint8_t *old_path, uint8_t *new_path )
{
    if ( FSS_OK == file_system_rename ( old_path, new_path ) ) 
    {
        log_printf ( &logger, " Rename %s to %s\r\n", old_path, new_path );
        log_printf ( &logger, "--------------------------------------\r\n\n" );
    }
}

void microsd_create_file ( uint8_t *file_path )
{
    file_t file;
    FIL fatfs_file;
    // Create the file and make it ready for read and write operations
    if ( FSS_OK == file_open ( &file, &fatfs_file, file_path, 
                               FS_FILE_CREATE_NEW | FS_FILE_WRITE | FS_FILE_READ ) ) 
    {
        log_printf( &logger, " Create and open file: %s\r\n", file_path );
        if ( FSS_OK == file_close ( &file ) ) 
        {
            log_printf( &logger, " Close file.\r\n" );
        }
        log_printf ( &logger, "--------------------------------------\r\n\n" );
    }
}

void microsd_write_to_file ( uint8_t *file_path, uint8_t *data_in, uint32_t data_len )
{
    file_t file;
    FIL fatfs_file;
    // Open the file and make it ready for read and write operations
    if ( FSS_OK == file_open ( &file, &fatfs_file, file_path, 
                               FS_FILE_WRITE | FS_FILE_READ | FS_FILE_OPEN_APPEND ) ) 
    {
        log_printf( &logger, " Open file: %s\r\n", file_path );
        
        if ( FSS_OK == file_write ( &file, data_in, data_len ) ) 
        {
            log_printf( &logger, " Write data buffer to file\r\n" );
        }
        if ( FSS_OK == file_sync ( &file ) ) 
        {
            log_printf( &logger, " Save file changes\r\n" );
        }
        if ( FSS_OK == file_close ( &file ) ) 
        {
            log_printf( &logger, " Close file.\r\n" );
        }
        log_printf ( &logger, "--------------------------------------\r\n\n" );
    }
}

void microsd_copy_file ( uint8_t *src_file_path, uint8_t *dst_file_path )
{
    uint8_t data_read = 0;
    file_t file1, file2;
    FIL fatfs_file1, fatfs_file2;
    // Create or open the file and make it ready for read and write operations
    if ( FSS_OK == file_open ( &file1, &fatfs_file1, src_file_path, FS_FILE_WRITE | FS_FILE_READ ) ) 
    {
        log_printf( &logger, " Open source file: %s\r\n", src_file_path );
        if ( FSS_OK == file_open ( &file2, &fatfs_file2, dst_file_path, FS_FILE_WRITE | FS_FILE_READ ) ) 
        {
            log_printf( &logger, " Open destination file: %s\r\n", dst_file_path );
            
            log_printf( &logger, " Copying data from source to destination file...\r\n" );
            for ( ; ; )
            {
                if ( FSS_OK == file_read ( &file1, &data_read, 1 ) ) 
                {
                    file_write ( &file2, &data_read, 1 ); 
                }
                else
                {
                    break;
                }
            }
            
            if ( FSS_OK == file_close ( &file1 ) ) 
            {
                log_printf( &logger, " Close source file.\r\n" );
            }
            if ( FSS_OK == file_close ( &file2 ) ) 
            {
                log_printf( &logger, " Close destination file.\r\n" );
            }
        }
        log_printf ( &logger, "--------------------------------------\r\n\n" );
    }
}

void microsd_format_drive ( uint8_t *path )
{
    if ( FSS_OK == file_system_format ( path ) ) 
    {
        log_printf( &logger, " Format drive: %s\r\n", path );
        log_printf ( &logger, "--------------------------------------\r\n\n" );
    }
}

void microsd_mount_drive ( microsd_t *ctx, uint8_t *path )
{
    if ( FSS_OK == file_system_mount ( &ctx->fat_ldrive.base, path, &ctx->sd_pdrive.base ) ) 
    {
        log_printf ( &logger, " Mount drive: %s\r\n", path );
        log_printf ( &logger, "--------------------------------------\r\n\n" );
    }
}

void microsd_unmount_drive ( uint8_t *path )
{
    if ( FSS_OK == file_system_unmount ( path ) ) 
    {
        log_printf ( &logger, " Unmount drive: %s\r\n", path );
        log_printf ( &logger, "--------------------------------------\r\n\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.