3,16 → 3,15 |
#include "defines.h" |
#include "SPI1.h" |
|
static SPI1_DATA *data_ptr; |
static void (*callback_function)(void); |
static SPI1_DATA *spi_data_ptr; |
|
void SPI1_Init(SPI1_DATA *data) { |
data_ptr = data; |
data_ptr->outBufferInd = 0; |
data_ptr->outBufferLen = 0; |
spi_data_ptr = data; |
spi_data_ptr->outBufferInd = 0; |
spi_data_ptr->outBufferLen = 0; |
#ifndef SPI1_WRITE_ONLY |
data_ptr->inBufferInd = 0; |
data_ptr->inBufferLen = 0; |
spi_data_ptr->inBufferInd = 0; |
spi_data_ptr->inBufferLen = 0; |
#endif |
|
INTDisableInterrupts(); |
53,7 → 52,7 |
int SPI1_Read_Buffer(unsigned char *array, unsigned int count) { |
if (count > SPI1_BUFFER_SIZE) |
return 0; |
if (data_ptr->inBufferLen == 0) |
if (spi_data_ptr->inBufferLen == 0) |
return 0; |
|
// Save previous interrupt state |
60,14 → 59,14 |
int prev = IEC0 & 0x03800000; |
// Temporarily disable interrupts |
IEC0CLR = 0x03800000; |
int ret = data_ptr->inBufferLen; |
int ret = spi_data_ptr->inBufferLen; |
int i; |
for (i = 0; i < count; i++) { |
array[i] = data_ptr->inBuffer[i]; |
array[i] = spi_data_ptr->inBuffer[i]; |
} |
// Reset buffer pointers |
data_ptr->inBufferInd = 0; |
data_ptr->inBufferLen = 0; |
spi_data_ptr->inBufferInd = 0; |
spi_data_ptr->inBufferLen = 0; |
// Restore saved interrupt state |
IEC0SET = prev; |
// Return the number of valid bytes in the buffer |
76,18 → 75,18 |
#endif |
|
int SPI1_Write(unsigned char *array, unsigned int count, void (*callback)(void)) { |
callback_function = callback; |
spi_data_ptr->callback_function = callback; |
|
if (count > SPI1_BUFFER_SIZE) |
return 0; |
if (data_ptr->outBufferLen != 0) |
if (spi_data_ptr->outBufferLen != 0) |
return 0; |
|
data_ptr->outBufferLen = count; |
data_ptr->outBufferInd = count-1; |
spi_data_ptr->outBufferLen = count; |
spi_data_ptr->outBufferInd = count-1; |
int i; |
for (i = 0; i < count; i++) { |
data_ptr->outBuffer[i] = array[i]; |
spi_data_ptr->outBuffer[i] = array[i]; |
} |
IEC0SET = 0x02000000; // Enable TX interrupt |
return 1; |
109,11 → 108,11 |
int i; |
// Read the data received from the last transfer |
int rxBufferCount = SPI1STATbits.RXBUFELM; |
if (data_ptr->inBufferLen + rxBufferCount < SPI1_BUFFER_SIZE) { |
if (spi_data_ptr->inBufferLen + rxBufferCount < SPI1_BUFFER_SIZE) { |
for (i = 0; i < rxBufferCount; i++) { |
data_ptr->inBuffer[data_ptr->inBufferInd] = SPI1BUF; |
data_ptr->inBufferInd++; |
data_ptr->inBufferLen++; |
spi_data_ptr->inBuffer[spi_data_ptr->inBufferInd] = SPI1BUF; |
spi_data_ptr->inBufferInd++; |
spi_data_ptr->inBufferLen++; |
} |
} else { |
// If buffer is full, discard data in enhanced buffer |
129,25 → 128,25 |
if (IFS0bits.SPI1TXIF) { |
int i; |
// Disable the transmit interrupt if all data has been sent |
if (data_ptr->outBufferLen == 0) { |
if (spi_data_ptr->outBufferLen == 0) { |
IEC0CLR=0x02000000; |
if (callback_function != NULL) |
(*callback_function)(); |
if (spi_data_ptr->callback_function != NULL) |
(*spi_data_ptr->callback_function)(); |
} else { |
// Start transmitting the data in the buffer |
int txBufferFree = 16 - SPI1STATbits.TXBUFELM; |
if (data_ptr->outBufferLen > txBufferFree) { |
if (spi_data_ptr->outBufferLen > txBufferFree) { |
for (i = 0; i < txBufferFree; i++) { |
SPI1BUF = data_ptr->outBuffer[data_ptr->outBufferInd]; |
data_ptr->outBufferInd--; |
SPI1BUF = spi_data_ptr->outBuffer[spi_data_ptr->outBufferInd]; |
spi_data_ptr->outBufferInd--; |
} |
data_ptr->outBufferLen -= txBufferFree; |
spi_data_ptr->outBufferLen -= txBufferFree; |
} else { |
for (i = 0; i < data_ptr->outBufferLen; i++) { |
SPI1BUF = data_ptr->outBuffer[data_ptr->outBufferInd]; |
data_ptr->outBufferInd--; |
for (i = 0; i < spi_data_ptr->outBufferLen; i++) { |
SPI1BUF = spi_data_ptr->outBuffer[spi_data_ptr->outBufferInd]; |
spi_data_ptr->outBufferInd--; |
} |
data_ptr->outBufferLen = 0; |
spi_data_ptr->outBufferLen = 0; |
} |
} |
IFS0CLR = 0x02000000; // Clear the TX flag |