Adding SD Flash Memory Card Functionality Using MPLAB® Code Configurator

 Objective

This tutorial shows how to add microSD card functionality to a new MPLAB® X Integrated Development Environment (IDE) project. MPLAB Code Configurator (MCC) is used to configure peripheral drivers such as Universal Asynchronous Receiver-Transmitter (UART), Serial Peripheral Interface (SPI) and the File System Library.

Overview

The tutorial makes use of MIKROE’s microSD Click BoardTM, MCP2221A Breakout Module and PIC24FJ256GA7 Curiosity Development Board to simplify hardware integration.

The tutorial showcases the capability of the File System Library to provide SD/MMC Flash memory card support and file system functionality to a new MPLAB X IDE project.

Microchip Technology’s File System Library is a generic File Allocation Table (FAT) file system developed by a third party and made available through an open-source license. It will be used to interface to a microSD card via the microSD Click board. It features a DOS/Windows® compatible FAT file system, a small footprint and various configuration options to support long file names, multiple volumes, variable sector size, multiple code pages and more. It also provides file system functionality that includes open/create, close, read, write, move and many more operations. For more details about the features of the File System Library, refer to the FatFs – Generic FAT File System User Guide.

MCC is a free graphical programming environment that generates seamless, easy-to-understand C code that is inserted into your project. It enables and configures a rich set of peripherals and functions using an intuitive interface. It is integrated into MPLAB X IDE to provide a powerful and easy-to-use development platform.

The training makes use of the PIC24FJ256GA7 Curiosity Development Board and MCP2221A Breakout Module available from Microchip Purchasing and Client Services and MIKROE microSD Click Board available from MIKROE.

The application verifies a card is present, registers, creates and opens a file, writes and reads data then closes the file and unmounts the drive.

Flowchart.png

The application is based on a state machine that uses a simple switch statement placed in an infinite loop in the main function.
If an error occurs, a message is printed to the terminal using the UART peripheral. The application will then place the state machine into an IDLE state.

This application utilizes:

  • UART1 for terminal communication
  • SPI1 for SD/MMC card data
  • General Purpose Inputs/Outputs (GPIO) for SD/MMC card control

In addition, the application utilizes a virtual communication port for user input and information output.

Two Ways to Use This Tutorial

  • Use the solution project as an example:
    • Build the solution project, microSD-app-pic24fjxxxga7-solution, and program it to the PIC24FJ256GA7 Curiosity Board to observe the expected behavior.
  • Follow the lab procedure:
    • Open the lab project, microSD-app-pic24fjxxxga7-lab, follow the lab procedure and MCC will add the missing files to complete the project.

Lab Objectives

  • Install MPLAB X IDE MCC project for a PIC24FJ256GA705 microcontroller.
  • Use MCC to configure and generate Peripheral Libraries (PLIB) code for the following peripherals:
    • SPI
    • UART
    • GPIO
  • Use the MCC PLIB Application Programming Interfaces (APIs) to implement the application.

 Materials

Hardware Tools

PIC24FJ256GA7 Curiosity Development Board

pic24fj256_curiosity.png

The PIC24FJ256GA7 Curiosity Development Board is a cost-effective, fully integrated development board featuring the low-cost and low-power 16-bit PIC24FJ256GA705 microcontroller. This board also features a fully integrated programmer/debugger, two MikroElektronika mikroBUS™ interfaces that give the user access to over 250+ add-on Click boards™, user LEDs, RGB LED, pushbuttons, potentiometer, and a prototyping area for developing a wide variety of general-purpose applications.

Note: The Curiosity series evaluation kits include an on-board, embedded debugger. No external tools are necessary to program or debug the PIC24FJ256GA705. For programming/debugging, the debugger connects to the host PC through the USB micro-B connector on the PIC24FJ256GA7 Curiosity Board.

MCP2221A USB to UART/I2C/SMBus

mcp2221a_usb_uart.png

The MCP2221A Breakout Module board demonstrates the capabilities of the MCP2221A device as a USB-to-UART/I2C/SMBus protocol converter.

MikroElectronika microSD Click Board

mikroe_microsd.png

The microSD click board from MikroElectronika has a microSD card slot for microSD cards used as a mass storage media for portable devices. The microSD click is designed to run on a 3.3 V power supply. It communicates with the target microcontroller over SPI.

Hardware Connection Setup

microSD2.png

Apart from the hardware tools listed above, the following items are required:

  • USB Type-A male to Micro-B male cable for programming and debugging.
  • USB Type-A male to Mini male cable for USB data logging from the MCP2221 module.
  • Four jumper wires to connect the UART TX pin to the MCCP2221 RX pin for data logging.

Connection Diagram

The application has the MIKROE microSD Click Board connected to GPIO and SPI1 and the MCP2221A breakout module connected to UART1.

microSD3.png

Software Tools

Note:
This project has been verified to work with the following versions of software tools:
MPLAB X IDE v5.50 (How to install MPLAB X IDE)
MPLAB XC16 Compiler v1.70 (How to install MPLAB XC16)
MCC v4.2.4 (How to install MCC)
MPLAB Data Visualizer plugin v1.2.956 (installation covered in this tutorial)
Microchip File System Library v1.1.0 (installation covered in this tutorial)
File extraction utility such as 7-Zip that supports ZIP files

Because we regularly update our tools, occasionally you may discover an issue while using the newer versions. If you suspect that to be the case, we recommend that you double-check and use the same versions that the project was tested with.

Lab Source Files and Solutions

Here are two ZIP files associated with this training. The first project, microSD-app-pic24fjxxxga7-solution, is the completed solution that can be programmed to demonstrate the lab and verify your setup. The second, microSD-app-pic24fjxxxga7-lab, is the same project with the MCC files removed. It contains the source files needed to perform the lab as per the following step-by-step instructions. Both files are stored in a single GitHub repository here. You will need to create a free account to download the files.

microSD4.png

Note: Because MCC generates source and header files and libraries under the project folder, the contents of these ZIP files can be placed in any folder of your choice.

Lab Index

Note: Steps 1 - 5 must be completed before you are ready to build, download, and run the application.

Step 1: Connect Hardware

  • Step 1.1 - Attach microSD Click Board
  • Step 1.2 - Connect USB from PIC24FJ256GA7 Curiosity Development Board to computer
  • Step 1.3 - Connect USB from MCP2221A Breakout Module to computer

Step 2: Download Example Code and Setup MCC

  • Step 2.1 - Download and Extract Example Code
  • Step 2.2 - Install MPLAB Code Configurator (MCC)
  • Step 2.3 - Install MCC File System Library

Step 3: Configure MCU Resources with MCC

  • Step 3.1 - Launch MCC
  • Step 3.2 - Add File System and SD/MMC Card MCC Libraries
  • Step 3.3 - System Module
  • Step 3.4 - UART
  • Step 3.5 - GPIO

Step 4: Configure File System (FatFs) and SD/MMC Card Libraries

  • Step 4.1 - Configure File System Library and Add SD/MMC Card Library
  • Step 4.2 - Configure SD/MMC Card Library

Step 5: Build and Program

  • Step 5.1 - Build project
  • Step 5.2 - Program MCU

Step 6: Test Application

  • Step 6.1 - Open, configure Data Visualizer
  • Step 6.2 - Select displayed parameters
  • Step 6.3 - Verify correct operation
  • Step 6.4 - Results
  • Step 6.5 - Analysis
  • Step 6.6 – Conclusions
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.