Skip to content

Instantly share code, notes, and snippets.

Created January 7, 2017 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/aa66874527652f67f29e07ebdca9f7a0 to your computer and use it in GitHub Desktop.
Save anonymous/aa66874527652f67f29e07ebdca9f7a0 to your computer and use it in GitHub Desktop.
MCC-generated-code-should-look-more-like-this
//-----------------------------------------------------------------------------
// SPI2 - send U8 data, return received U8 data
//-----------------------------------------------------------------------------
uint8_t SPI2_ExchangeU8( uint8_t dat )
{
while( SPI2STATbits.SPITBF );
SPI2BUF = dat;
while( SPI2STATbits.SRXMPT );
return SPI2BUF;
}
//-----------------------------------------------------------------------------
// SPI2 - send U8 data (pointer), receive U8 data (pointer), n is number of
// U8's to transfer, tdat/rdat can be NULL, n must be less than or equal the
// size of the buffer if the pointer is not NULL
//-----------------------------------------------------------------------------
void SPI2_ExchangeU8_buffer( uint8_t* tdat, uint8_t *rdat, uint16_t n )
{
uint8_t t = 0, r = 0;
uint8_t dummyr = 0, dummyt = 0;
if( tdat ) t = 1; else tdat = &dummyt;
if( rdat ) r = 1; else rdat = &dummyr;
for( ; n; n--, rdat += r, tdat += t ) *rdat = SPI2_ExchangeU8( *tdat );
}
//-----------------------------------------------------------------------------
// SPI2 - send U16 data, return received U16 data
//-----------------------------------------------------------------------------
uint16_t SPI2_ExchangeU16( uint16_t dat )
{
while( SPI2STATbits.SPITBF );
SPI2BUF = dat;
while( SPI2STATbits.SRXMPT );
return SPI2BUF;
}
//-----------------------------------------------------------------------------
// SPI2 - send U16 data (pointer), receive U16 data (pointer), n is number of
// U16's to transfer, tdat/rdat can be NULL, n must be less than or equal the
// size of the buffer if the pointer is not NULL
//-----------------------------------------------------------------------------
void SPI2_ExchangeU16_buffer( uint16_t* tdat, uint16_t *rdat, uint16_t n )
{
uint8_t t = 0, r = 0;
uint16_t dummyr = 0, dummyt = 0;
if( tdat ) t = 1; else tdat = &dummyt;
if( rdat ) r = 1; else rdat = &dummyr;
for( ; n; n--, rdat += r, tdat += t ) *rdat = SPI2_ExchangeU16( *tdat );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment