how to use IO line to define SPI interface in RCM5700?

hi
I have a rcm5700 module,and i want to test ads7870 chip.
But don’t want to use spi.lib in Dynamic. I’m going to use PE0 as SCLK ,PE1 as Dout,and PE3 as Din to setup spi interface.
SO,how to set up?

head file
#define SPISCLK 0
#define SPIMOSI 1
#define SPIMISO 3

#define SET_ADC_CLK() BitWrPortI(PEDR,&PEDRShadow,1,SPISCLK)
#define CLR_ADC_CLK() BitWrPortI(PEDR,&PEDRShadow,0,SPISCLK)

#define SET_ADC_DI() BitWrPortI(PEDR,&PEDRShadow,0,SPIMOSI)
#define CLR_ADC_DI() BitWrPortI(PEDR,&PEDRShadow,1,SPIMOSI)

#define GET_ADC_DO() BitRdPortI(PEDR,SPIMISO)

The function file

void ads7870_init()
{
WrPortI(PEFR, &PEFRShadow, 0x00);

//---------------------------------------------------------------------
// Initialize PE0 & PE1 to be an outputs
//---------------------------------------------------------------------
// Initialize as normal I/O
WrPortI(PEDR, &PEDRShadow, (PEDRShadow  |  0x03));
WrPortI(PEFR, &PEFRShadow, (PEFRShadow  & ~0x03));

// Initialize as normal I/O
WrPortI(PEDDR, &PEDDRShadow, (PEDDRShadow | 0x03));

BitPortI(PEDDR,&PEDDRShadow,0,3);
WrPortI(PEDR, &PEDRShadow, (PEDRShadow | 0x03));

WrPortI(PECR, &PECRShadow, 0x00);

}

It’right?

I rolled my own SPI routines because sometimes I needed to monkey with the clock phase and I have multiple devices that are communicating from different tasks.

/*** BeginHeader spin, SPIWriteChar, SPIWrite, SPIWriteDelay, SPIRead */
// Low level (bit bang) SPI routines
void spin (int revs);
void SPIWriteChar (unsigned char b, int clock);
void SPIWrite (unsigned char buffer[], int chars, int clock);
void SPIWriteDelay (unsigned char buffer[], int chars, int clock, int delay);
void SPIRead (unsigned char buffer[], int chars, int clock, int delay);
/*** EndHeader */

/* START FUNCTION DESCRIPTION **************************************************
spin                                                               

SYNTAX: void spin (int revs);
DESCRIPTION: Variable delay nop loop

PARAMETER1: revs, number of times through the loop
RETURN VALUE: None

SEE ALSO:
KEYWORDS:

END DESCRIPTION ***************************************************************/

nodebug root void spin (int revs) {
int i;
  for (i=0; i

SYNTAX: void SPIWriteChar (unsigned char b, char clock);
DESCRIPTION: Bit-bang SPI Write routine

PARAMETER1: uchar b - Byte to be sent
PARAMETER2: char clock - 0 for latching on falling edge
RETURN VALUE: None

SEE ALSO:  SPIWrite, SPIWriteDelay, SPIRead
KEYWORDS:

END DESCRIPTION ***************************************************************/

// SpinVal controls the bitrate of the SPI clock.  Using 2 for 100kHz.
#define SpinVal 2

_IO_debug root void SPIWriteChar (unsigned char b, int clock) {
int j;
int x;

  for (j=0; j<8; j++) {
    x = (b & 0x80) == 0x80;               //get the data bit
    b <<= 1;                        //shift the data for the next bit
    BitWrPortI(PCDR, &PCDRShadow, x, PC_SPI_MOSI);    //set up data line
    BitWrPortI(PDDR, &PDDRShadow, !clock, PD_SPI_CLK);      //Clock high
    spin (SpinVal);
    BitWrPortI(PDDR, &PDDRShadow, clock, PD_SPI_CLK);     //Clock low to latch data
    spin (SpinVal);
    }

  }

/* START FUNCTION DESCRIPTION **************************************************
SPIWrite                                                           

SYNTAX: void SPIWrite (unsigned char buffer[], int chars, char clock);
DESCRIPTION: Write multiple characters via SPI

PARAMETER1: uchar buffer[] - Bytes to be sent
PARAMETER2: int chars - Number of bytes to send
PARAMETER3: char clock - 0 for latching on falling edge
RETURN VALUE: None

SEE ALSO:  SPIWriteChar, SPIWriteDelay, SPIRead
KEYWORDS:

END DESCRIPTION ***************************************************************/

_IO_debug root void SPIWrite (unsigned char buffer[], int chars, int clock) {
int i;
  for (i=0; i

SYNTAX: void SPIWriteDelay (unsigned char buffer[], int chars, char clock, char delay);
DESCRIPTION: Write multiple characters via SPI

PARAMETER1: uchar buffer[] - Bytes to be sent
PARAMETER2: int chars - Number of bytes to send
PARAMETER3: char clock - 0 for latching on falling edge
PARAMETER4: char delay - us to delay between characters
RETURN VALUE: None

SEE ALSO:  SPIWriteChar, SPIWrite, SPIRead
KEYWORDS:

END DESCRIPTION ***************************************************************/

_IO_debug root void SPIWriteDelay (unsigned char buffer[], int chars, int clock, int delay) {
int i;

  spin (delay + delay >> 1);

  for (i=0; i> 1);
    }
  }


/* START FUNCTION DESCRIPTION **************************************************
SPIRead                                                            

SYNTAX: void SPIRead (unsigned char buffer[], int chars, char clock, char delay);
DESCRIPTION: Bit-bang SPI Read routine

PARAMETER1: uchar buffer[] - Destination of bytes that are read
PARAMETER2: int chars - Number of bytes to read
PARAMETER3: char clock - 0 for latching on falling edge
PARAMETER4: char delay - us delay before clocking in next character
RETURN VALUE: None

SEE ALSO:  SPIWriteChar, SPIWrite, SPIWriteDelay
KEYWORDS:

END DESCRIPTION ***************************************************************/

_IO_debug root void SPIRead (unsigned char buffer[], int chars, int clock, int delay) {
int i;
int j;
char b;
int x;

  BitWrPortI(PCDR, &PCDRShadow, 1, PC_SPI_MOSI);    //set up data line
  for (i=0; i 0) {
      spin (delay + delay >> 1);
      }
    b = 0;
    for (j=0; j<8; j++) {
      BitWrPortI(PDDR, &PDDRShadow, !clock, PD_SPI_CLK);      //Clock high
      spin (SpinVal);
      BitWrPortI(PDDR, &PDDRShadow, clock, PD_SPI_CLK);     //Clock low to latch data
      spin (SpinVal);
      b <<= 1;                        //Shift the existing data
      x = BitRdPortI(PDDR, PD_SPI_MISO);          //receive the bit
      b |= (char) x;                    //save the data bit
      }
    buffer[i] = b;
    }
  }
//#endif


Then each device had a couple driver routines like the one below. The SPIBusy flag keeps the routines from stepping on each other. Don’t forget the chip select line to each SPI device.

_IO_debug void MotorRdConfig (int axis) {
char retries;

  if (axis < 1 || axis > 2) return;

  while (SPIBusy);
  SPIBusy = 1;

  BitWrPortI(PDDR, &PDDRShadow, Mtr_clk_ph, PD_SPI_CLK);   //fix the clock

  SMConfig[axis].start = 0;
  retries = 3;
  while ((retries > 0) && (SMConfig[axis].start != 0x4D)) {
    BitWrPortI(PDDR, &PDDRShadow, 0, axis - 1);    // chip select low
    SPIWriteChar (0x40, Mtr_clk_ph);         // read all command
    SPIRead((char *)&SMConfig[axis], 17, Mtr_clk_ph, Mtr_char_delay);
    BitWrPortI(PDDR, &PDDRShadow, 1, axis - 1);    // chip select high

    retries -= 1;
    if (SMConfig[axis].start != 0x4D)
      spin (100);     // kill some time before attempting retry
    }

  SPIBusy = 0;
  }


brdInit:


  /////////////////////////////////////////////////////////////////////////
  // Configure Port C -- Leave PC6 and PC7 untouched (used for other purposes)
  /////////////////////////////////////////////////////////////////////////
  WrPortI(PCFR,  &PCFRShadow,  0x41);                   // special functions
                                                          // TX for ser A, D.
                                                          // Bitbang SPI on SerC
  WrPortI(PCAHR, &PCAHRShadow, 0x00);                   // set alternate outputs
  WrPortI(PCALR, &PCALRShadow, 0x00);                   // to mode 0 (Tx)
  WrPortI(PCDCR, &PCDCRShadow, 0x00);           // clear bits to drive
                                                        //  high and low
  WrPortI(PCDR,  &PCDRShadow,  0x7D);                   // set outputs high
  WrPortI(PCDDR, &PCDDRShadow, 0x7D);                   // set up I/0s

  /////////////////////////////////////////////////////////////////////////
  // Configure Port D -- PD4, PD5 and PD6 set up by serial flash driver
  /////////////////////////////////////////////////////////////////////////
  WrPortI(PDCR,  &PDCRShadow,  0x00);                   // clear bits to perclk/2
  WrPortI(PDDCR, &PDDCRShadow, 0x00);                   // clear bits to drive
                                                        //  high and low
  WrPortI(PDALR, &PDALRShadow, 0xCF & RdPortI(PDALR));  // SCLKC on bit 2
  WrPortI(PDDR,  &PDDRShadow,  0xF7 | RdPortI(PDDR));   // set outputs high
  WrPortI(PDFR,  &PDFRShadow,  0x70 & RdPortI(PDFR));   // sflash on PD4,5,6
  WrPortI(PDDDR, &PDDDRShadow, 0x07 | RdPortI(PDDDR));  // set up Outputs
  WrPortI(PDDDR, &PDDDRShadow, 0xF7 & RdPortI(PDDDR));  // set up Input