Step 3.1: Configure the Secure Digital (SD) Card Driver
3.1
In the MPLAB® Harmony Configurator (MHC) Options tab, expand the Harmony Framework Configuration > Drivers > SD Card selection tree.
a
Check the Use SD Card Driver? box.
b
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.
c
Open one client of the SD card driver. Keep the Number of SD Card Driver Clients value at 1.
d
Use index 0 to identify the driver instance. Keep the SD Card Driver Index value at DRV_SDCARD_INDEX_0.
e
Retain the Maximum Driver Indices (limit 2) as 1, because you will need only one instance of the SD Card driver.
f
SD Card Data Queue Size defines the number of read/write requests that can be queued without having to wait for the previous request to complete. Retain the default value of 10.
g
Select CLK_BUS_PERIPHERAL_2 as a clock source for Clock To Use? because CLK_BUS_PERIPHERAL_2 is a clock source for the Serial Peripheral Interface (SPI) module. The SPI module is used by the SD card driver as the communication interface module.
h
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.
i
Micro SD cards do not have a write protection line. You can uncheck the Enable Write Protect Check? box. If required, a General Purpose Input/Output (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.
j
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.
k
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.
l
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 screenshot of the MHC configuration for the SD card driver:
Step 3.2: Configure the SPI Driver Used by the SD Card Driver
3.2
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.
a
Check the Use SPI Driver? box.
b
Select DYNAMIC for the Driver Implementation.
c
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.
d
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.
e
Number of Job Elements Created Per Instance selects the number of SPI requests that can be queued. Retain the default value 10.
f
Check the SPI Driver Instance 0 box.
g
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).
h
Expand the Driver Mode option and verify that the Interrupt Mode box is checked.
i
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 (I²S) to run at a higher priority than the SD card (SPI).
j
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.
k
Verify the 8-bit box under Data Width, and the Enhanced box under Buffer Mode are checked.
l
Retain the default selection for Protocol Type as DRV_SPI_PROTOCOL_TYPE_STANDARD.
m
Select CLK_BUS_PERIPHERAL_2 as clock source for the SPI module under Clock To Use because CLK_BUS_PERIPHERAL_2 is the clock source for the SPI module.
n
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.
o
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.
p
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 screenshot 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: Verify/Set the I/O Pins used by the I²S module using the Graphical Pin Manager
3.3
Verify/set the I/O pins used by the SPI 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.
b
Map SPI2 signals SDI2 and SDO2 to pins RD7 and RG8 respectively. This user's guide contains the MEB II board schematic >.
Step 3.4: Configure the File System Service used by the SD Card
3.4
Expand the Harmony Framework Configuration > System Services > File System selection tree.
a
Check the Use File System Service? box.
b
Set the value of Maximum Simultaneous File Access to 2. This allows a maximum of two 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.
c
Retain the default values for Size Of Block at 512, Size Of Media Flash Page Buffer at 2048, Total Number Of Media at 1, and Total Number Of Volumes at 1. Note that you are using only one media SD card with only one partition. Verify that Media 0 is checked.