Support / Knowledge Base / How can I add an external SD card to a Rabbit?

How can I add an external SD card to a Rabbit?

See the following information.  This gives an example of adding an external SD card to an RCM4400W: 

Custom Configurations

The configuration library fat_config.lib is brought in when fat.lib is #use’d in the code. This configuration
library recognizes the macro _DRIVER_CUSTOM as a flag that a custom hardware configuration
or custom device driver is being used in the hardware/device driver arrangement set up by
fat_config.lib.

B.1 Adding SD Card Interface
As an example of a custom hardware configuration, consider the task of designing an SD card interface on
a board that uses a core module that does not natively support such an interface. This is just one example
of connecting an SD card socket to an RCM4400W core module. A device driver already exists for the SD
card interface with FAT module version 2.13 and Dynamic C version 10.21. The desired driver, which in
this case is SD_FAT.LIB, must be identified before fat.lib is #use’d in the application. One strategy
is to create a new configuration library that will be #use’d at the top of your application program. The bulk
of this library can be taken from existing configuration setups and modified for the custom application. In
this case, we get this setup section from the RCM43xx.lib file, which sets up the SD card for the
RCM43xx series of core modules. There is a fairly long parameter block which consists of defines setting
which port, pins and shadow registers are used to access the control and serial lines needed to control the
SD card.

Once this type of configuration library is created, you can use any FAT-based sample with the custom
hardware by simply replacing the #use "FAT.lib" statement in the sample with #use "customSD.lib", where
customSD.lib is the name of the new configuration library.

B.1.1 Example Code
The configuration library would have to include something similar to the following code:
// Tells fat_config.lib that a custom driver/configuration is to be used
#define _DRIVER_CUSTOM "SD_FAT.LIB"

// Configuration definitions modified to include custom initialization function
// Signature strings have been set to NULL because the SD card is removable media
#define PC_COMPATIBLE
#define _DRIVER_CUSTOM_INIT { "SD", sd_custom_init, NULL, },
#define _DEVICE_CUSTOM_0 { sd_custom_init, NULL, 0, 0, \
FDDF_MOUNT_PART_ALL|FDDF_MOUNT_DEV_0, NULL, },

// SD hardware parameter block copied from RCM43xx.lib
//*** SD hardware definitions

// Sets up the following port connections for the SD driver
// This would use external buffer and power line control similar to the SD card circuitry on the RCM4300
// Directly connecting these pins to the SD card would have possible line drive issues
// Port C, Pin 2 TxC Serial Transmit line
// Port D, Pin 0 Card Select line, active low
// Port D, Pin 1 SD Card Active LED, active high
// Port D, Pin 2 SclkC Serial Clock line
// Port D, Pin 3 TxC Serial Receive line
// Port E, Pin 3 Card Detect line, active low

// This example does not use write protect or power control

// If not using power control, it is recommended that the detect switch on the SD card socket
// be tied to appropriate circuitry for applying power to the SD card.

#define SD_CD_PORT PEDR // Card Detect set to pin PE3
#define SD_CD_PORT_FR PEFR
#define SD_CD_PORT_FRSHADOW &PEFRShadow
#define SD_CD_PORT_DDR PEDDR
#define SD_CD_PORT_DDRSHADOW &PEDDRShadow
#define SD_CD_PIN 3

// No Write Protect input
#define SD_WP_PORT 0 // WP input not used
#define SD_WP_PORT_FR 0
#define SD_WP_PORT_FRSHADOW NULL
#define SD_WP_PORT_DDR 0
#define SD_WP_PORT_DDRSHADOW NULL
#define SD_WP_PIN 0

#define SD_CS_PORT PDDR // Card Select set to pin PD0
#define SD_CS_PORT_DRSHADOW &PDDRShadow
#define SD_CS_PORT_DDR PDDDR
#define SD_CS_PORT_FR PDFR
#define SD_CS_PORT_FRSHADOW &PDFRShadow
#define SD_CS_PORT_DDRSHADOW &PDDDRShadow
#define SD_CS_PORT_DCR PDDCR
#define SD_CS_PORT_DCRSHADOW &PDDCRShadow
#define SD_CS_PIN 0
#define SD_CS_PORT_OD 0

#define SD_TX_PORT_DR PCDR // TxC set to pin PC2
#define SD_TX_PORT_DRSHADOW &PCDRShadow
#define SD_TX_PORT_FR PCFR
#define SD_TX_PORT_FRSHADOW &PCFRShadow
#define SD_TX_PORT_DDR PCDDR
#define SD_TX_PORT_DDRSHADOW &PCDDRShadow
#define SD_TX_PORT_DCR PCDCR
#define SD_TX_PORT_DCRSHADOW &PCDCRShadow
#define SD_TX_PIN 2
#define SD_TX_PORT_OD 0

#define SD_PWR_PORT_DR 0 // Power control pin not used
#define SD_PWR_PORT_DRSHADOW NULL
#define SD_PWR_PORT_FR 0
#define SD_PWR_PORT_FRSHADOW NULL
#define SD_PWR_PORT_DDR 0
#define SD_PWR_PORT_DDRSHADOW NULL
#define SD_PWR_PORT_DCR 0
#define SD_PWR_PORT_DCRSHADOW NULL
#define SD_PWR_PIN 0
#define SD_PWR_PORT_OD 0
#define SD_PWR_PORT_ON 0

#define SD_LED_PORT_DR PDDR // LED Output set to pin PD1
#define SD_LED_PORT_DRSHADOW &PDDRShadow
#define SD_LED_PORT_FR PDFR
#define SD_LED_PORT_FRSHADOW &PDFRShadow
#define SD_LED_PORT_DDR PDDDR
#define SD_LED_PORT_DDRSHADOW &PDDDRShadow
#define SD_LED_PORT_DCR PDDCR
#define SD_LED_PORT_DCRSHADOW &PDDCRShadow
#define SD_LED_PIN 1
#define SD_LED_PORT_OD 1
#define SD_LED_PORT_ON 0

#define SD_RX_PORT_DR PDDR // RxC set to pin PD3
#define SD_RX_PORT_FR PDFR
#define SD_RX_PORT_FRSHADOW &PDFRShadow
#define SD_RX_PORT_DDR PDDDR
#define SD_RX_PORT_DDRSHADOW &PDDDRShadow
#define SD_RX_PIN 3

#define SD_CLK_PORT_DR PDDR // SclkC set to pin PD2
#define SD_CLK_PORT_FR PDFR
#define SD_CLK_PORT_FRSHADOW &PDFRShadow
#define SD_CLK_PORT_DDR PDDDR
#define SD_CLK_PORT_DDRSHADOW &PDDDRShadow
#define SD_CLK_PORT_DCR PDDCR
#define SD_CLK_PORT_DCRSHADOW &PDDCRShadow
#define SD_CLK_PIN 2
#define SD_CLK_PORT_OD 0

// Setup clock & control registers for serial port
#define SD_SPI_TACRSHADOW &TACRShadow
#define SD_SPI_SERPORT SCDR
#define SD_SPI_TCREG TACR
#define SD_SPI_TCRSHADOW &TACRShadow
#define SD_SPI_TCRVALUE 0
#define SD_SPI_SERSHADOW &SCERShadow
#define SD_SPI_SERVALUE SD_SPI_CLOCK_MODE
#define SD_SPI_SCRSHADOW &SCCRShadow
#define SD_SPI_SCRVALUE SD_SPI_CONTROL_VALUE
#define SD_SPI_DIVREG TAT6R
#define SD_SPI_DIVREGSHADOW &TAT6RShadow
#define SD_SPI_DIVISOR 0

// Macros for enabling and disabling the Card Select control line
#define SD_ENABLECS(DI) BitWrPortI(DI->csport,DI->csportdrShadow, 0,
DI->cspin)
#define SD_DISABLECS(DI) BitWrPortI(DI->csport, DI->csportdrShadow, 1,
DI->cspin)
#define SD_ENABLEPOW(DI) DI // Power enable not used
#define SD_DISABLEPOW(DI) DI
//SD serial port register offsets
#define SD_AR_OFFSET 1
#define SD_SR_OFFSET 3
#define SD_CR_OFFSET 4
#define SD_ER_OFFSET 5

#define SD_SPI_CONTROL_VALUE 0x1c // Selects Port D as RxC alt. input
#define SD_SPI_TXMASK 0x80 // Control bits for starting TX or RX operations
#define SD_SPI_RXMASK 0x40
#define SD_SPI_CLOCK_MODE 0x08 // Sets Reverse Data Bit operation (MSB first)

#use "FAT.lib" // Use the necessary libraries for FAT operation
#use "SD_FAT.lib"
/*** EndHeader */

/*** BeginHeader sd_custom_init */
// This is a custom initialization function; it adds alternate routing of SCLKC to Port D pin 2
// before calling the standard SD initialization function.
// This function would only be required if an alternate routing issue could not be set by the
// definitions above.
int sd_custom_init(mbr_drvr *driver, void *device_list);
/*** EndHeader */

int sd_custom_init(mbr_drvr *driver, void *device_list)
{
// Select SCLKC output on Port D pin 2
WrPortI(PDALR, &PDALRShadow, PDALRShadow & 0xCF);
sd_InitDriver(driver, device_list);
}
Last updated: Aug 23, 2017

Filed Under

Embedded

Recently Viewed Articles

No recently viewed articles
Contact a Digi expert and get started today! Contact Us