Step 3: Configure the SD Card Driver, SPI Driver & File System
Step 3.1: Configure the Secure Digital (SD) Card Driver
In the MHC Options tab, expand the
Harmony Framework Configuration > Drivers > SD Card
Check the "Use SD Card Driver?" box.
The Dynamic "Driver Implementation" is selected by default and greyed out. The SD card driver does not support static implementation, but it may in the future.
Open one client of the SD card driver. Keep the "Number of SD Card Driver Clients" value at 1.
Use index 0 to identify the driver instance. Keep the "SD Card Driver Index" value at DRV_SDCARD_INDEX_0.
Retain the "Maximum Driver Indices (limit 2)" as 1, because you will need only one instance of the SD Card driver.
"SD Card Data Queue Size" defines the number of read/write requests that can be queued without having to wait for previous request to complete. Retain the default value of 10.
Select CLK_BUS_PERIPHERAL_2 as a clock source for "Clock To Use?" because CLK_BUS_PERIPHERAL_2" is a clock source for the SPI module. The SPI module is used by the SD card driver as the communication interface module.
"SD Card Speed (Hz)", defines the speed at which the SD card is going to operate. This should be less than the maximum SPI frequency and should be supported by the SD card used. Retain the default value of 20 MHz.
Micro SD cards do not have a write protection line. You can uncheck the "Enable Write Protect Check?" box. If required, a GPIO may be used to set up write protection and the SD card driver can be notified of this GPIO by checking this box and specifying the appropriate "Write Protect Port" and "Write Protect Port Bit" under the "Enable Write Protect Check?" option.
For the SD card operating in SPI mode, the DATA3/CD line of the SD card is used as chip select line, and the same is connected to the PIC32's port pin RB14. Select PORT_CHANNEL_B as the "Chip Select Port" and PORTS_BIT_POS_14 as the "Chip Select Port Bit".
Note that the SD card initialization routine will set the appropriate direction (output) for the chip select line and overwrite any settings for this pin in the Pin Settings tab in MHC.
Retain the "SPI Driver Instance to use for SD Card Driver" as 0. This informs the SD card driver which SPI instance to use for communication with the SD card.
Keep the "Register With File System?" option checked. This allows the SD card driver to register its services with the File System.
Observe the following screen shot of the MHC configuration for the SD card driver:
Step 3.2: Configure the SPI Driver Used by the SD Card Driver
Configure the SPI interface driver. In the SD card driver configurations, we specified that SPI instance 0 should be used by the SD card driver.
Expand the Harmony Framework Configuration > Drivers > SPI selection tree.
Check the "Use SPI Driver?" box.
Note after you check the "Use SPI Driver?" box, the SPI selection tree is highlighted in red. This indicates a selection made in this selection tree is incompatible with a selection in another tree (I2S in this case). You will change this incompatible selection in the following steps.
MHC selects the SPI1 (SPI_ID_1) peripheral to be used by the "SPI Driver Instance 0" by default. You have already configured the Audio CODEC to use this same peripheral in a previous step.
Select DYNAMIC for the "Driver Implementation".
Next, check the following boxes:
"Use Interrupt Mode?"
"Use Master Mode?"
"Use Enhanced Buffer (FIFO) Mode?"
"Use 8-bit Mode?"
This configures the SPI driver to operate in Master mode. The interrupt mode allows the SPI driver state machine to be run from the SPI interrupt. The demo uses the Enhanced Buffer (FIFO) mode supported by the device.
You need only one SPI driver and client instance. Retain the values for "Number of SPI Driver Instances" and "Number of SPI Driver Clients" at 1.
"Number of job elements created per instance" selects the number of SPI requests that can be queued. Retain the default value 10.
Check the "SPI Driver Instance 0" box.
Select the "SPI Module ID" as SPI_ID_2. This links the SPI hardware peripheral with the SPI driver instance (Peripheral SPI2 with SPI driver instance 0 in this case).
Note when you change the SPI peripheral used for SPI Driver Instance 0, the MHC selection conflict is resolved (no more red highlight). The I2S and SPI drivers are no longer using the same SPI module.
Expand the "Driver Mode" option and verify that the"Interrupt Mode" box is checked.
Change the "TX Interrupt Priority", "RX Interrupt Priority" and "Error Interrupt Priority" to INT_PRIORITY_LEVEL3. Also, change the "TX Interrupt Sub-priority", "RX Interrupt Sub-priority" and "Error Interrupt Sub-priority" to INT_SUBPRIORITY_LEVEL3. This allows the audio codec (I2S) to run at a higher priority than the SD card (SPI).
The SD card driver acts as an SPI Master communicating to the SD card as an SPI Slave. Verify that the Master box is checked under "Master/Slave Mode"
Verify the 8-bit box under "Data Width", and the Enhanced box under "Buffer Mode" are checked.
Retain the default selection for "Protocol Type": DRV_SPI_PROTOCOL_TYPE_STANDARD
Use the Peripheral Bus Clock to drive the SPI baud rate generator. For the "Baud Clock Source", select SPI_BAUD_RATE_PBCLK_CLOCK. The SPI Baud Rate Generator can be driven by Peripheral Bus Clock #2 (PBCLK) or Reference Clock 1 (MCLK).
The value for "SPI Clock Rate – Hz" can be left at the default value. This is because when SPI is used with the SD card, the SD card driver sets the SPI clock rate based on the "SD Card Speed(Hz)" (under the SD card tree) setting and the "SPI Clock Rate - Hz" value is ignored.
Set the SPI clock phase and polarity under "Clock Mode" to
DRV_SPI_CLOCK_MODE_IDLE_LOW_EDGE_FALL and "Input Phase" to
SPI_INPUT_SAMPLING_PHASE_AT_END. You are advised to refer to the micro SD card spec for appropriate selection of "Clock Mode" and "Input Phase".
Retain the default values of 10 and 1 for "Max Jobs In Queue" and "Minimum Number Of Job Queue Reserved For Instance" respectively.
Observe the following screen shot of the MHC SPI driver configuration:
The driver level configuration is used to include various driver capabilities. For example, if you have selected two SPI instances with one of the SPI instances configured for Master mode and another configured for Slave mode, then both "Use Master Mode?" and "Use Slave Mode?" need to be selected in the driver level configuration. In other words, the driver level configuration must be a superset of (i.e., must include) all instance level configurations. For this tutorial, you are going to use only one instance of SPI so the driver level configuration and the instance level configuration will have to be the same.
Step 3.3: Map the SPI peripheral I/Os to I/O Pins
Set the I/O pins used by the SPI2 module using the Graphical Pin Manager
Click on MHC's Pin Diagram tab. In the MHC Output pane (found at the bottom of MPLAB X IDE window) select the Pin Table tab.
Map SPI2 signals SCK2, SDI2 and SDO2 to pins RG6 (Pin 14), RD7 (Pin 121) and RG8 (Pin 16) respectively. Click on the boxes to connect the peripheral functions with the pins as shown in this figure. Green boxes indicate a selection is made. Blue boxes indicate all possible selections. This user's guide contains the MEB II board schematic >.
Step 3.4: Configure the SD Card's File System Service
Expand the Harmony Framework Configuration > System Services > File System selection tree.
Verify the "Use File System Service?" box is already checked since, the SD Card was configured to register itself with the file system service.
Set the value of "Maximum Simultaneous File Access" to 2. This allows a maximum of 2 files to be opened simultaneously. You need to be able to open the second file before closing the first file. This is needed for changing tracks.
Retain the default values for Size Of Block to 512 and Size Of Media Flash Page Buffer to 2048.
Check the Use File System Auto Mount Feature? option.
Retain the Total Number Of Media to 1 for now. This will be changed to 2 later when you will add file system support on a USB mass storage device.
Next, you need to configure the media. Media 0 is checked by default. Expand it.
Expand Media Configuration (0). Retain the Media Type to SYS_FS_MEDIA_TYPE_SD_CARD since your media is a SD Card.
Retain the File system Type for this media to FAT.
You will have only one partition on the SD Card. Retain the default value of 1 for the Number of Volumes.
Volume 1 is checked. Expand it.
Set the Device Name to /dev/mmcblka1 and the Media Mount Name to /mnt/myDrive1.
Keep the File System Types value at 1. Note that this setting is different from the File System Type setting present under Media 0.
Some applications could use multiple medias (e.g., one flash drive and one SD card), each using either a FAT file system or the Microchip File System (MPFS). In this case, you would need to enable support for both the FAT File System and the Microchip File System under the File System tab; and then select one of these file systems for each Media under the individual media configuration (example: Media 0) tab.
Table of Contents