I’ve improved upon my Software SPI function, because it was too slow… I only got around 350 kbps with a 44Mhz clock. These functions run much faster (around 4 to 6 times). I had to unravel the for loop and do things a bit different, but it works.
There are 3 functions. One for reading and writing, one for reading and one for writing.
Oh, if somebody can help me with speeding up the UART aswell, please do… I’m stuck at that (see Projects forum)…
I changed the SPI code of Brennen to accommodate these changes:
//low-level spi send function for library use
//the user should not call this function directly, but rather use one of the 8 SPI data instructions
unsigned char nrf24l01_execute_command(unsigned char instruction, unsigned char * data, unsigned int len, bool copydata)
{
unsigned char status;
nrf24l01_clear_csn();
status = spi_Send_Read(instruction);
nrf24l01_spi_send_read(data, len, copydata);
nrf24l01_set_csn();
return status;
}
//low-level spi send function for library use
//the user should not call this function directly, but rather use one of the 8 SPI data instructions
void nrf24l01_spi_send_read(unsigned char * data, unsigned int len, bool copydata)
{
unsigned int count;
for(count = 0; count < len; count++)
{
if(copydata == true)
data[count] = spi_Read(); // only read
else
{
spi_Send(data[count]); // only write
}
}
}
Here are the SPI functions:
void spi_Send(unsigned char bytein) // Written by Jaap van den Bosch
{
SPI_SCK = 0; // Ensure SCK is low
if (bytein & 0x80) // Check if next outbit is a 1
{
SPI_SO = 1; // If a 1, pull SO high
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SO = 0; // Reset SO to zero
SPI_SCK = 0; // Bring SCK low for next bit
if (bytein & 0x40) // Check if next outbit is a 1
{
SPI_SO = 1; // If a 1, pull SO high
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SO = 0; // Reset SO to zero
SPI_SCK = 0; // Bring SCK low for next bit
if (bytein & 0x20) // Check if next outbit is a 1
{
SPI_SO = 1; // If a 1, pull SO high
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SO = 0; // Reset SO to zero
SPI_SCK = 0; // Bring SCK low for next bit
if (bytein & 0x10) // Check if next outbit is a 1
{
SPI_SO = 1; // If a 1, pull SO high
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SO = 0; // Reset SO to zero
SPI_SCK = 0; // Bring SCK low for next bit
if (bytein & 0x08) // Check if next outbit is a 1
{
SPI_SO = 1; // If a 1, pull SO high
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SO = 0; // Reset SO to zero
SPI_SCK = 0; // Bring SCK low for next bit
if (bytein & 0x04) // Check if next outbit is a 1
{
SPI_SO = 1; // If a 1, pull SO high
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SO = 0; // Reset SO to zero
SPI_SCK = 0; // Bring SCK low for next bit
if (bytein & 0x02) // Check if next outbit is a 1
{
SPI_SO = 1; // If a 1, pull SO high
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SO = 0; // Reset SO to zero
SPI_SCK = 0; // Bring SCK low for next bit
if (bytein & 0x01) // Check if next outbit is a 1
{
SPI_SO = 1; // If a 1, pull SO high
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SO = 0; // Reset SO to zero
SPI_SCK = 0; // Bring SCK low for next bit
} // end of spi_Send(...)
unsigned char spi_Read(void) // Written by Jaap van den Bosch
{
unsigned char byteout = 0x00; // return byte
SPI_SO = 0; // Reset SO to zero
SPI_SCK = 0; // Ensure SCK is low
Nop();
if (SPI_SI == 1) // Check if next inbit is a 1
{
byteout |= 0x80; // If a 1, set next bit to 1
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SCK = 0; // Bring SCK low for next bit
Nop();
if (SPI_SI == 1) // Check if next inbit is a 1
{
byteout |= 0x40; // If a 1, set next bit to 1
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SCK = 0; // Bring SCK low for next bit
Nop();
if (SPI_SI == 1) // Check if next inbit is a 1
{
byteout |= 0x20; // If a 1, set next bit to 1
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SCK = 0; // Bring SCK low for next bit
Nop();
if (SPI_SI == 1) // Check if next inbit is a 1
{
byteout |= 0x10; // If a 1, set next bit to 1
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SCK = 0; // Bring SCK low for next bit
Nop();
if (SPI_SI == 1) // Check if next inbit is a 1
{
byteout |= 0x08; // If a 1, set next bit to 1
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SCK = 0; // Bring SCK low for next bit
Nop();
if (SPI_SI == 1) // Check if next inbit is a 1
{
byteout |= 0x04; // If a 1, set next bit to 1
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SCK = 0; // Bring SCK low for next bit
Nop();
if (SPI_SI == 1) // Check if next inbit is a 1
{
byteout |= 0x02; // If a 1, set next bit to 1
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SCK = 0; // Bring SCK low for next bit
Nop();
if (SPI_SI == 1) // Check if next inbit is a 1
{
byteout |= 0x01; // If a 1, set next bit to 1
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SCK = 0; // Bring SCK low for next bit
return byteout;
} // end of spi_Read(...)
unsigned char spi_Send_Read(unsigned char bytein) // Written by Jaap van den Bosch
{
unsigned char byteout = 0x00; // return byte
SPI_SCK = 0; // Ensure SCK is low
if (bytein & 0x80) // Check if next outbit is a 1
{
SPI_SO = 1; // If a 1, pull SO high
}
if (SPI_SI == 1) // Check if next inbit is a 1
{
byteout |= 0x80; // If a 1, set next bit to 1
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SO = 0; // Reset SO to zero
SPI_SCK = 0; // Bring SCK low for next bit
if (bytein & 0x40) // Check if next outbit is a 1
{
SPI_SO = 1; // If a 1, pull SO high
}
if (SPI_SI == 1) // Check if next inbit is a 1
{
byteout |= 0x40; // If a 1, set next bit to 1
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SO = 0; // Reset SO to zero
SPI_SCK = 0; // Bring SCK low for next bit
if (bytein & 0x20) // Check if next outbit is a 1
{
SPI_SO = 1; // If a 1, pull SO high
}
if (SPI_SI == 1) // Check if next inbit is a 1
{
byteout |= 0x20; // If a 1, set next bit to 1
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SO = 0; // Reset SO to zero
SPI_SCK = 0; // Bring SCK low for next bit
if (bytein & 0x10) // Check if next outbit is a 1
{
SPI_SO = 1; // If a 1, pull SO high
}
if (SPI_SI == 1) // Check if next inbit is a 1
{
byteout |= 0x10; // If a 1, set next bit to 1
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SO = 0; // Reset SO to zero
SPI_SCK = 0; // Bring SCK low for next bit
if (bytein & 0x08) // Check if next outbit is a 1
{
SPI_SO = 1; // If a 1, pull SO high
}
if (SPI_SI == 1) // Check if next inbit is a 1
{
byteout |= 0x08; // If a 1, set next bit to 1
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SO = 0; // Reset SO to zero
SPI_SCK = 0; // Bring SCK low for next bit
if (bytein & 0x04) // Check if next outbit is a 1
{
SPI_SO = 1; // If a 1, pull SO high
}
if (SPI_SI == 1) // Check if next inbit is a 1
{
byteout |= 0x04; // If a 1, set next bit to 1
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SO = 0; // Reset SO to zero
SPI_SCK = 0; // Bring SCK low for next bit
if (bytein & 0x02) // Check if next outbit is a 1
{
SPI_SO = 1; // If a 1, pull SO high
}
if (SPI_SI == 1) // Check if next inbit is a 1
{
byteout |= 0x02; // If a 1, set next bit to 1
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SO = 0; // Reset SO to zero
SPI_SCK = 0; // Bring SCK low for next bit
if (bytein & 0x01) // Check if next outbit is a 1
{
SPI_SO = 1; // If a 1, pull SO high
}
if (SPI_SI == 1) // Check if next inbit is a 1
{
byteout |= 0x01; // If a 1, set next bit to 1
}
SPI_SCK = 1; // Bring SCK high to latch bit
SPI_SO = 0; // Reset SO to zero
SPI_SCK = 0; // Bring SCK low for next bit
return byteout;
} // end of spi_Send_Read(...)
[/code]