Beginner
10 min

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

Small card, big storage

microSD Click with Curiosity Nano with PIC18F47K42

Published Feb 13, 2024

Click board™

microSD Click

Dev. board

Curiosity Nano with PIC18F47K42

Compiler

NECTO Studio

MCU

PIC18F47K42TQFP

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

PIC18F47K42 Curiosity Nano evaluation kit is a cutting-edge hardware platform designed to evaluate the PIC18F47K42 microcontroller (MCU). Central to its design is the inclusion of the powerful PIC18F47K42 microcontroller (MCU), offering advanced functionalities and robust performance. Key features of this evaluation kit include a yellow user LED and a responsive mechanical user switch

providing seamless interaction and testing. The provision for a 32.768kHz crystal footprint ensures precision timing capabilities. With an onboard debugger boasting a green power and status LED, programming and debugging become intuitive and efficient. Further enhancing its utility is the Virtual serial port (CDC) and a debug GPIO channel (DGI GPIO), offering extensive connectivity options.

Powered via USB, this kit boasts an adjustable target voltage feature facilitated by the MIC5353 LDO regulator, ensuring stable operation with an output voltage ranging from 2.3V to 5.1V (limited by USB input voltage), with a maximum output current of 500mA, subject to ambient temperature and voltage constraints.

PIC18F47K42 Curiosity Nano double side image

Microcontroller Overview

MCU Card / MCU

default

Architecture

PIC

MCU Memory (KB)

128

Silicon Vendor

Microchip

Pin count

40

RAM (Bytes)

8192

You complete me!

Accessories

Curiosity Nano Base for Click boards is a versatile hardware extension platform created to streamline the integration between Curiosity Nano kits and extension boards, tailored explicitly for the mikroBUS™-standardized Click boards and Xplained Pro extension boards. This innovative base board (shield) offers seamless connectivity and expansion possibilities, simplifying experimentation and development. Key features include USB power compatibility from the Curiosity Nano kit, alongside an alternative external power input option for enhanced flexibility. The onboard Li-Ion/LiPo charger and management circuit ensure smooth operation for battery-powered applications, simplifying usage and management. Moreover, the base incorporates a fixed 3.3V PSU dedicated to target and mikroBUS™ power rails, alongside a fixed 5.0V boost converter catering to 5V power rails of mikroBUS™ sockets, providing stable power delivery for various connected devices.

Curiosity Nano Base for Click boards accessories 1 image

Used MCU Pins

mikroBUS™ mapper

Card Detection
PA0
AN
NC
NC
RST
SPI Chip Select
PD4
CS
SPI Clock
PC6
SCK
SPI Data OUT
PC5
MISO
SPI Data IN
PC4
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

Curiosity Nano Base for Click boards accessories 1 image hardware assembly

Start by selecting your development board and Click board™. Begin with the Curiosity Nano with PIC18F47K42 as your development board.

Curiosity Nano Base for Click boards accessories 1 image hardware assembly
Charger 27 Click front image hardware assembly
PIC18F47K42 Curiosity Nano 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
PIC18F57Q43 Curiosity 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.