From bab1c895de1e69d3fc70afaa041d5ea488f515ec Mon Sep 17 00:00:00 2001 From: patacongo Date: Sat, 2 Jun 2012 17:57:39 +0000 Subject: [PATCH] Add SD card support to the PIC32MX7 MMB board; Add regiser level debug instrumentatin for the PIC32 SPI driver git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4797 42af7a65-404d-4744-a932-0658087f49c3 --- configs/pic32mx7mmb/README.txt | 26 +++-- configs/pic32mx7mmb/nsh/defconfig | 3 + .../pic32mx7mmb/src/pic32mx7mmb_internal.h | 20 +++- configs/pic32mx7mmb/src/up_nsh.c | 11 +- configs/pic32mx7mmb/src/up_spi.c | 101 ++++++++++++------ 5 files changed, 112 insertions(+), 49 deletions(-) diff --git a/configs/pic32mx7mmb/README.txt b/configs/pic32mx7mmb/README.txt index bbf62ab8f9..3f47e0233a 100644 --- a/configs/pic32mx7mmb/README.txt +++ b/configs/pic32mx7mmb/README.txt @@ -32,8 +32,8 @@ PIN CONFIGURATIONS SIGNAL NAME ON-BOARD CONNE 7 RC2/AC2TX/T3CK EE_CS# M25P80 CS 8 RC3/AC2RX/T4CK ACL_CS# ADXL345 CS and VCC 9 RC4/SDI1/T5CK SDI1 SPI1 data IN - 10 PMA5/CN8/ECOL/RG6/SCK2/U3RTS/U6TX RG6 ? - 11 PMA4/CN9/ECRS/RG7/SDA4/SDI2/U3RX SD_CD# SD Connector + 10 PMA5/CN8/ECOL/RG6/SCK2/U3RTS/U6TX SD_WP SD card, write protect + 11 PMA4/CN9/ECRS/RG7/SDA4/SDI2/U3RX SD_CD# SD card, card detect (not) 12 PMA3/AECRSDV/AERXDV/CN10/ECRSDV/ AECRSDV LAN8720A SRS_DIV ERXDV/RG8/SCL4/SDO2/U3TX 13 MCLR MCLR Debug connector @@ -636,14 +636,19 @@ Where is one of the following: before the networking finally gives up and decides that no network is available. - 2. This example can support an FTP client. In order to build in FTP client + 2. To add SPI-based support for the SD card slot: + + CONFIG_PIC32MX_SPI1=y + CONFIG_NSH_ARCHINIT=y + + 3. This example can support an FTP client. In order to build in FTP client support simply uncomment the following lines in the appconfig file (before configuring) or in the apps/.config file (after configuring): #CONFIGURED_APPS += netutils/ftpc #CONFIGURED_APPS += examples/ftpc - 3. This example can support an FTP server. In order to build in FTP server + 4. This example can support an FTP server. In order to build in FTP server support simply uncomment the following lines in the appconfig file (before configuring) or in the apps/.config file (after configuring): @@ -654,11 +659,10 @@ Where is one of the following: CONFIG_DISABLE_POLL=n - Using a RAM disk and the USB MSC device with nsh and nsh2 - --------------------------------------------------------- - Here is an experimental change to either examples/nsh or examples/nsh2 - that will create a RAM disk and attempt to export that RAM disk as a - USB mass storage device. + Using a RAM disk and the USB MSC device to the nsh configuration + ---------------------------------------------------------------- + Here is an experimental change to examples/nsh that will create a RAM + disk and attempt to export that RAM disk as a USB mass storage device. 1. Changes to nuttx/.config @@ -737,8 +741,8 @@ Where is one of the following: Status: Open Priority: High - Adding LCD and graphics support: - ------------------------------- + Adding LCD and graphics support to the nsh configuration: + -------------------------------------------------------- appconfig (apps/.config): Enable the application configurations that you want to use. Asexamples: diff --git a/configs/pic32mx7mmb/nsh/defconfig b/configs/pic32mx7mmb/nsh/defconfig index 35bcbd89d4..0e63ccfe88 100644 --- a/configs/pic32mx7mmb/nsh/defconfig +++ b/configs/pic32mx7mmb/nsh/defconfig @@ -445,6 +445,9 @@ CONFIG_DEBUG_NET=n CONFIG_DEBUG_LCD=n CONFIG_DEBUG_GRAPHICS=n CONFIG_DEBUG_INPUT=n +CONFIG_DEBUG_FS=n +CONFIG_DEBUG_SPI=n +CONFIG_SPI_REGDEBUG=n CONFIG_HAVE_CXX=n CONFIG_HAVE_CXXINITIALIZE=n diff --git a/configs/pic32mx7mmb/src/pic32mx7mmb_internal.h b/configs/pic32mx7mmb/src/pic32mx7mmb_internal.h index db523b67fe..f44e042586 100644 --- a/configs/pic32mx7mmb/src/pic32mx7mmb_internal.h +++ b/configs/pic32mx7mmb/src/pic32mx7mmb_internal.h @@ -83,6 +83,20 @@ * RD2 LCD-BLED Backlight Light Low value turns off */ +/* SPI1 and SD Card + * + * ------ -------- ------------------------- -------------------------------- + * GPIO SIGNAL BOARD CONNECTION NOTES + * ------ -------- ------------------------- -------------------------------- + * RC4 SPI1 SD card slot SPI1 data IN + * RD0 SPO1 SD card slot SPI1 data OUT + * RD10 SCK1 SD card slot SD card, SPI clock + * + * RA9 SD_CS# SD card slot SD card, SPI chip select (active low) + * RG6 SD_WP SD card slot SD card, write protect + * RG7 SD_CD# SD card slot SD card, card detect (not) + */ + /**************************************************************************** * Public Types ****************************************************************************/ @@ -108,11 +122,13 @@ extern "C" { * Name: pic32mx_spiinitialize * * Description: - * Called to configure SPI chip select GPIO pins for the PCB Logic board. + * Called to configure SPI chip select GPIO pins for the Mikroelektronika PIC32MX7 + * MMB board. * ************************************************************************************/ -#if defined(CONFIG_PIC32MX_SPI2) +#if defined(CONFIG_PIC32MX_SPI1) || defined(CONFIG_PIC32MX_SPI2) || \ + defined(CONFIG_PIC32MX_SPI3) || defined(CONFIG_PIC32MX_SPI4) EXTERN void weak_function pic32mx_spiinitialize(void); #endif diff --git a/configs/pic32mx7mmb/src/up_nsh.c b/configs/pic32mx7mmb/src/up_nsh.c index e5a192b1c7..0d022531f8 100644 --- a/configs/pic32mx7mmb/src/up_nsh.c +++ b/configs/pic32mx7mmb/src/up_nsh.c @@ -61,13 +61,10 @@ #define CONFIG_NSH_HAVEMMCSD 1 #define CONFIG_NSH_HAVEUSBHOST 1 -/* TheMikroelektronika PIC32MX7 MMB does not have an SD slot on board. If one - * is added, then it must be specified by defining which SPI bus that it - * is connected on. - */ +/* The Mikroelektronika PIC32MX7 MMB has one SD slot on board, connected to SPI 1. */ #ifndef CONFIG_PIC32MX_MMCSDSPIPORTNO -# undef CONFIG_NSH_HAVEMMCSD +# define CONFIG_PIC32MX_MMCSDSPIPORTNO 1 #endif /* Make sure that the configuration will support the SD card */ @@ -84,7 +81,9 @@ # define CONFIG_NSH_MMCSDSPIPORTNO CONFIG_PIC32MX_MMCSDSPIPORTNO # endif - /* Make sure that the NSH configuration uses the slot */ + /* Make sure that the NSH configuration uses slot 0 (there is only one + * SD slot on the Mikroelektronica PIC32MX7 MMB). + */ # if !defined(CONFIG_NSH_MMCSDSLOTNO) || CONFIG_NSH_MMCSDSLOTNO != 0 # warning "The Mikroelektronika PIC32MX7 MMB has only one slot (0)" diff --git a/configs/pic32mx7mmb/src/up_spi.c b/configs/pic32mx7mmb/src/up_spi.c index 48e0592947..407279fd1f 100644 --- a/configs/pic32mx7mmb/src/up_spi.c +++ b/configs/pic32mx7mmb/src/up_spi.c @@ -58,24 +58,35 @@ /************************************************************************************ * Definitions ************************************************************************************/ +/* SPI1 and SD Card + * + * ------ -------- ------------------------- -------------------------------- + * GPIO SIGNAL BOARD CONNECTION NOTES + * ------ -------- ------------------------- -------------------------------- + * RC4 SPI1 SD card slot SPI1 data IN + * RD0 SPO1 SD card slot SPI1 data OUT + * RD10 SCK1 SD card slot SD card, SPI clock + * + * RA9 SD_CS# SD card slot SD card, SPI chip select (active low) + * RG6 SD_WP SD card slot SD card, write protect + * RG7 SD_CD# SD card slot SD card, card detect (not) + */ + +#define GPIO_SD_CS (GPIO_OUTPUT|GPIO_VALUE_ONE|GPIO_PORTA|GPIO_PIN9) +#define GPIO_SD_WP (GPIO_INPUT|GPIO_PORTG|GPIO_PIN6) +#define GPIO_SD_CD (GPIO_INPUT|GPIO_INT|GPIO_PORTG|GPIO_PIN7) /* The following enable debug output from this file (needs CONFIG_DEBUG too). * - * CONFIG_SPI_DEBUG - Define to enable basic SPI debug - * CONFIG_SPI_VERBOSE - Define to enable verbose SPI debug + * CONFIG_DEBUG_SPI - Define to enable basic SPI debug */ -#ifdef CONFIG_SPI_DEBUG -# define sspdbg lldbg -# ifdef CONFIG_SPI_VERBOSE -# define sspvdbg lldbg -# else -# define sspvdbg(x...) -# endif +#ifdef CONFIG_DEBUG_SPI +# define spidbg lldbg +# define spivdbg llvdbg #else -# undef CONFIG_SPI_VERBOSE -# define sspdbg(x...) -# define sspvdbg(x...) +# define spidbg(x...) +# define spivdbg(x...) #endif /************************************************************************************ @@ -90,15 +101,20 @@ * Name: pic32mx_sspinitialize * * Description: - * Called to configure SPI chip select GPIO pins for the Sure PIC32MX board. + * Called to configure SPI chip select GPIO pins for the Mikroelektronka PIC32MX7 + * MMB board. * ************************************************************************************/ void weak_function pic32mx_sspinitialize(void) { - /* Configure the SPI chip select GPIOs */ + /* Configure the SPI chip select, write protect, and card detect GPIOs */ -#warning "Missing logic" +#ifdef CONFIG_PIC32MX_SPI1 + pic32mx_configgpio(GPIO_SD_CS); + pic32mx_configgpio(GPIO_SD_WP); + pic32mx_configgpio(GPIO_SD_CD); +#endif } /************************************************************************************ @@ -135,15 +151,37 @@ enum spi_dev_e; #ifdef CONFIG_PIC32MX_SPI1 void pic32mx_spi1select(FAR struct spi_dev_s *dev, enum spi_dev_e devid, bool selected) { - sspdbg("devid: %d CS: %s\n", (int)devid, selected ? "assert" : "de-assert"); -#warning "Missing logic" + spidbg("devid: %d CS: %s\n", (int)devid, selected ? "assert" : "de-assert"); + + if (devid == SPIDEV_MMCSD) + { + pic32mx_gpiowrite(GPIO_SD_CS, !selected); + } } uint8_t pic32mx_spi1status(FAR struct spi_dev_s *dev, enum spi_dev_e devid) { - sspdbg("Returning nothing\n"); -#warning "Missing logic" - return 0; + uint8_t ret = 0; + + /* Card detect active low. */ + + if (devid == SPIDEV_MMCSD) + { + if (!pic32mx_gpioread(GPIO_SD_CD)) + { + ret = SPI_STATUS_PRESENT; + + /* A high value indicates the the card is write protected. */ + + if (pic32mx_gpioread(GPIO_SD_WP)) + { + ret |= SPI_STATUS_WRPROTECTED; + } + } + } + + spidbg("Returning %02x\n", ret); + return ret; } #ifdef CONFIG_SPI_CMDDATA int pic32mx_spi1cmddata(FAR struct spi_dev_s *dev, enum spi_dev_e devid, bool cmd) @@ -154,21 +192,22 @@ int pic32mx_spi1cmddata(FAR struct spi_dev_s *dev, enum spi_dev_e devid, bool cm #endif #endif -#ifdef CONFIG_PIC31MX_SPI1 -void pic31mx_spi1select(FAR struct spi_dev_s *dev, enum spi_dev_e devid, bool selected) +#ifdef CONFIG_PIC31MX_SPI2 +void pic31mx_spi2select(FAR struct spi_dev_s *dev, enum spi_dev_e devid, bool selected) { - sspdbg("devid: %d CS: %s\n", (int)devid, selected ? "assert" : "de-assert"); + spidbg("devid: %d CS: %s\n", (int)devid, selected ? "assert" : "de-assert"); #warning "Missing logic" } -uint8_t pic31mx_spi1status(FAR struct spi_dev_s *dev, enum spi_dev_e devid) +uint8_t pic31mx_spi2status(FAR struct spi_dev_s *dev, enum spi_dev_e devid) { - sspdbg("Returning nothing\n"); + spidbg("Returning nothing\n"); #warning "Missing logic" return 0; } + #ifdef CONFIG_SPI_CMDDATA -int pic31mx_spi1cmddata(FAR struct spi_dev_s *dev, enum spi_dev_e devid, bool cmd) +int pic31mx_spi2cmddata(FAR struct spi_dev_s *dev, enum spi_dev_e devid, bool cmd) { #warning "Missing logic" return 0; @@ -179,16 +218,17 @@ int pic31mx_spi1cmddata(FAR struct spi_dev_s *dev, enum spi_dev_e devid, bool cm #ifdef CONFIG_PIC31MX_SPI3 void pic32mx_spi3select(FAR struct spi_dev_s *dev, enum spi_dev_e devid, bool selected) { - sspdbg("devid: %d CS: %s\n", (int)devid, selected ? "assert" : "de-assert"); + spidbg("devid: %d CS: %s\n", (int)devid, selected ? "assert" : "de-assert"); #warning "Missing logic" } uint8_t pic32mx_spi3status(FAR struct spi_dev_s *dev, enum spi_dev_e devid) { - sspdbg("Returning nothing\n"); + spidbg("Returning nothing\n"); #warning "Missing logic" return 0; } + #ifdef CONFIG_SPI_CMDDATA int pic32mx_spi3cmddata(FAR struct spi_dev_s *dev, enum spi_dev_e devid, bool cmd) { @@ -201,16 +241,17 @@ int pic32mx_spi3cmddata(FAR struct spi_dev_s *dev, enum spi_dev_e devid, bool cm #ifdef CONFIG_PIC32MX_SPI4 void pic32mx_spi4select(FAR struct spi_dev_s *dev, enum spi_dev_e devid, bool selected) { - sspdbg("devid: %d CS: %s\n", (int)devid, selected ? "assert" : "de-assert"); + spidbg("devid: %d CS: %s\n", (int)devid, selected ? "assert" : "de-assert"); #warning "Missing logic" } uint8_t pic32mx_spi4status(FAR struct spi_dev_s *dev, enum spi_dev_e devid) { - sspdbg("Returning nothing\n"); + spidbg("Returning nothing\n"); #warning "Missing logic" return 0; } + #ifdef CONFIG_SPI_CMDDATA int pic32mx_spi4cmddata(FAR struct spi_dev_s *dev, enum spi_dev_e devid, bool cmd) {