chipKIT® Development Platform

Inspired by Arduino™

[Resolved]: Using SPI/Ethernet simultaneously

Created Wed, 22 Feb 2012 19:37:03 +0000 by MarkHoskins


MarkHoskins

Wed, 22 Feb 2012 19:37:03 +0000

This board has so far been a great help to me in getting this to work, but I am finding now that perhaps my particular problem has not yet been addressed in these forums.

I am using a Chipkit Max32 with the Chipkit Ethernet shield. The shield works great, I can use the USB just fine, I can use the Ethernet just fine. A little bit of wiring and I can get an SD card hooked up and run the SD examples ( Though I had to fix up the libraries a bit, they had some issues with segmentation. Which is how I found these boards, which had already solved the issue more completely than I had. )

However, if I try to run the SD examples AND Ethernet, the SD stops working after the call to Ethernet.begin. Now, I believe this is because the Ethernet shield uses some of the SPI 2A pins that I'm using to communicate with the SD card. Which brings me to the first problem I've had.

Has anyone had any luck getting any of the SPI ports working other than the default (2A)? As far as I can tell reading the pinouts, there are 4 SPI ports. Seemingly they are 1A, 2A, 3A and 4. 3A is also inaccessible because it is used by CAN unless you do a hardware mod or some kind of jumper, which I haven't been able to figure out.

So I tried 1A. I modified the Sd2Pinmap.h to be the following:

#include <peripheral/ports.h>
#include <p32xxxx.h>
#include <WProgram.h>

struct PinConfig
{
  IoPortId Port;
  uint32_t Tris;
  uint32_t Lat;
  uint32_t Bit;
};

#if defined(_BOARD_MEGA_) || defined(_BOARD_UNO_)
  const PinConfig SCKPins[ 4 ] = 
  {
    { _SPI1A_CLK_PORT, TRISD, LATD, _SPI1A_CLK_BIT }, // SPI Bank 1
    { _SPI2A_CLK_PORT, TRISG, LATG, _SPI2A_CLK_BIT }, // SPI Bank 2
    { _SPI3A_CLK_PORT, TRISF, LATF, _SPI3A_CLK_BIT }, // SPI Bank 3
    { _SPI4_CLK_PORT,  TRISF, LATF, _SPI4_CLK_BIT  }  // SPI Bank 4
  };
  
  const PinConfig SDIPins[ 4 ] = 
  {
    { _SPI1A_SDI_PORT, TRISF, LATF, _SPI1A_SDI_BIT }, // SPI Bank 1
    { _SPI2A_SDI_PORT, TRISG, LATG, _SPI2A_SDI_BIT }, // SPI Bank 2
    { _SPI3A_SDI_PORT, TRISF, LATF, _SPI3A_SDI_BIT }, // SPI Bank 3
    { _SPI4_SDI_PORT,  TRISF, LATF, _SPI4_SDI_BIT  }  // SPI Bank 4
  };
    
  const PinConfig SDOPins[ 4 ] = 
  {
    { _SPI1A_SDO_PORT, TRISF, LATF, _SPI1A_SDO_BIT }, // SPI Bank 1
    { _SPI2A_SDO_PORT, TRISG, LATG, _SPI2A_SDO_BIT }, // SPI Bank 2
    { _SPI3A_SDO_PORT, TRISF, LATF, _SPI3A_SDO_BIT }, // SPI Bank 3
    { _SPI4_SDO_PORT,  TRISF, LATF, _SPI4_SDO_BIT  }  // SPI Bank 4
  };
    
  const PinConfig SSPins[ 4 ] = 
  {
    { _SPI1A_SS_PORT, TRISD, LATD, _SPI1A_SS_BIT }, // SPI Bank 1
    { _SPI2A_SS_PORT, TRISG, LATG, _SPI2A_SS_BIT }, // SPI Bank 2
    { _SPI3A_SS_PORT, TRISF, LATF, _SPI3A_SS_BIT }, // SPI Bank 3
    { _SPI4_SS_PORT,  TRISF, LATF, _SPI4_SS_BIT  }  // SPI Bank 4
  };
  
  #define SPI_BANK_1A 0
  #define SPI_BANK_2A 1
  #define SPI_BANK_3A 2
  #define SPI_BANK_4  3
  
  #define DEFAULT_SPI_BANK SPI_BANK_1A
  
  const IoPortId prtSDO	 = SDOPins[ DEFAULT_SPI_BANK ].Port;
	//const uint32_t trisSDO = SDOPins[ DEFAULT_SPI_BANK ].Tris;
	//const uint32_t latSDO	 = SDOPins[ DEFAULT_SPI_BANK ].Lat;
	const uint32_t bnSDO	 = SDOPins[ DEFAULT_SPI_BANK ].Bit;

	const IoPortId prtSDI	 = SDIPins[ DEFAULT_SPI_BANK ].Port;
	//const uint32_t trisSDI = SDIPins[ DEFAULT_SPI_BANK ].Tris;
	//const uint32_t latSDI	 = SDIPins[ DEFAULT_SPI_BANK ].Lat;
	const uint32_t bnSDI	 = SDIPins[ DEFAULT_SPI_BANK ].Bit;

	const IoPortId prtSCK	 = SCKPins[ DEFAULT_SPI_BANK ].Port;
	//const uint32_t trisSCK = SCKPins[ DEFAULT_SPI_BANK ].Tris;
	//const uint32_t latSCK	 = SCKPins[ DEFAULT_SPI_BANK ].Lat;
	const uint32_t bnSCK	 = SCKPins[ DEFAULT_SPI_BANK ].Bit;

#else

Now I figured this would allow me to move the port to whichever and just GO, but it only works for SPI port 2A.

As far as I can tell, these are the pinouts for SPI 1A: SS - 19 SCK - 18 SDI - 0 SDO - 1

So I connected it to the SD card in the following way: SS - 49 SCK - 18 MISO - 0 MOSI - 1

Prior to attempting any SPI operations, I am setting Pin 19 to output similar to how I would set pin 53 output prior to attempting SPI operations on SPI port 2A. I am using a modified version of the CardInfo sample. If I rewire it to use Spi port 2A, and change the header to use 2A instead of 1A, the CardInfo sample works.

If I change it to 1A and rewire it, the card is not detected. The only modifications I made to the CardInfo example are to change the chipSelect to 49 and change the pinMode call to operate on pin 19 instead of 53.

Alternatively, I would accept a method that would allow me to use SPI 2A for both the Network Shield and SPI.


MarkHoskins

Thu, 23 Feb 2012 22:23:00 +0000

Managed to fix this by switching the SD library to use hardware SPI ( Using the DSPI library ) and using DSPI1.

In retrospect, it turns out the Chipkit Max32 SPI port 1A isn't used and 1 is used instead, which uses a different set of pins.


Sprigo

Sun, 22 Sep 2013 10:26:53 +0000

Managed to fix this by switching the SD library to use hardware SPI ( Using the DSPI library ) and using DSPI1. In retrospect, it turns out the Chipkit Max32 SPI port 1A isn't used and 1 is used instead, which uses a different set of pins.

I'm struggling with exactly the same problem Mark. Could you give me a bit more detail as to what you had to change in order to get it working?