void getsSPI2( unsigned char *rdptr, unsigned char length ) { while ( length ) // stay in loop until length = 0 { *rdptr++ = ReadSPI2(); // read a single byte length--; // reduce string length count by 1 } }
/***************************************************************************** Function: void DRV_SPI_TxRx(void) Summary: Transmits and receives SPI bytes Description: Transmits and receives N bytes of SPI data. Precondition: None Parameters: pTxBuf - pointer to SPI tx data txLen - number of bytes to Tx pRxBuf - pointer to where SPI rx data will be stored rxLen - number of SPI rx bytes caller wants copied to p_rxBuf Returns: None Remarks: Will clock out the larger of txLen or rxLen, and pad if necessary. *****************************************************************************/ void DRV_SPI_TxRx(SpiChannel chn, uint8_t *pTxBuf, uint16_t txLen, uint8_t *pRxBuf, uint16_t rxLen) { uint16_t byteCount; uint16_t i; uint8_t rxTrash; /* total number of byte to clock is whichever is larger, txLen or rxLen */ byteCount = (txLen >= rxLen)?txLen:rxLen; for (i = 0; i < byteCount; ++i) { /* if still have bytes to transmit from tx buffer */ if ((txLen > 0) && (pTxBuf != 0)) { #if defined (__C32__) SpiChnWriteC(chn, *pTxBuf++); #elif defined (__C30__) switch(chn) { case 1: WriteSPI1(*pTxBuf++); break; case 2: WriteSPI2(*pTxBuf++); break; } #endif --txLen; } /* else done writing bytes out from tx buffer */ else { #if defined (__C32__) SpiChnWriteC(chn, 0); /* clock out a "don't care" byte */ #elif defined (__C30__) switch(chn) { case 1: WriteSPI1(0x00); break; case 2: WriteSPI2(0x00); break; } #endif } /* wait until tx/rx byte to completely clock out */ WaitForDataByte(chn); /* if still have bytes to read into rx buffer */ if ((rxLen > 0) && (pRxBuf != 0)) { #if defined (__C32__) *pRxBuf++ = SpiChnReadC(chn); #elif defined (__C30__) switch(chn) { case 1: *pRxBuf++ = ReadSPI1(); break; case 2: *pRxBuf++ = ReadSPI2(); break; } #endif --rxLen; } /* else done reading bytes into rx buffer */ else { #if defined (__C32__) rxTrash = SpiChnReadC(chn); /* read and throw away byte */ #elif defined (__C30__) switch(chn) { case 1: rxTrash = ReadSPI1(); break; case 2: rxTrash = ReadSPI2(); break; } #endif } } /* end for loop */ }