Step 3.1: Configure the Secure Digital (SD) Card Driver
In the MHC Options tab, expand the Harmony Framework Configuration > Drivers > SD Card selection tree.
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.
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).
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
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.
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: Verify/Set the I/O Pins used by the I2S module using the Graphical Pin Manager
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.
The SPI2 has a dedicated pin for the SCK2 signal. This pin is hardcoded and configured by the SPI driver.
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
Expand the Harmony Framework Configuration > System Services > File System selection tree.
Check the "Use File System Service?" Box
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" 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.
Retain the "File System Types" at 1. Check the FAT File System as the file system type.
Observe the following screen shot of the MHC configuration for the File System.