1,11 → 1,9 |
#include <xc.h> |
#include <plib.h> |
#include "defines.h" |
#include "SPI1.h" |
|
static SPI1_DATA *spi_data_ptr; |
|
void SPI1_Init(SPI1_DATA *data, void (*rx_callback)(char)) { |
void SPI1_Init(SPI1_DATA *data, void (*rx_callback)(uint8_t)) { |
spi_data_ptr = data; |
spi_data_ptr->buffer_out_ind = 0; |
spi_data_ptr->buffer_out_len = 0; |
29,7 → 27,7 |
// FSCK = FPB / (2 * (SPIxBRG + 1)) |
IEC0CLR = 0x03800000; // Disable all SPI interrupts |
SPI1CON = 0; // Stops and resets the SPI1. |
int tmp = SPI1BUF; // Clears the receive buffer |
uint32_t tmp = SPI1BUF; // Clears the receive buffer |
IFS0CLR = 0x03800000; // Clear any existing event |
IPC5CLR = 0x1F000000; // Clear the priority |
IPC5SET = 0x19000000; // Set IPL=6, Subpriority 1 |
45,7 → 43,7 |
INTEnableInterrupts(); |
} |
|
int SPI1_Write(char *array, int length, void (*tx_callback)(void)) { |
uint8_t SPI1_Write(uint8_t *array, uint32_t length, void (*tx_callback)(void)) { |
spi_data_ptr->tx_callback = tx_callback; |
|
if (length > SPI1_BUFFER_OUT_SIZE) |
56,7 → 54,7 |
// Put the data to send into the outbound buffer |
spi_data_ptr->buffer_out_len = length; |
spi_data_ptr->buffer_out_ind = length-1; |
int i; |
int32_t i; |
for (i = 0; i < length; i++) { |
spi_data_ptr->buffer_out[i] = array[i]; |
} |
77,11 → 75,11 |
|
// Process SPI1 receive flag |
if (IFS0bits.SPI1RXIF) { |
int i; |
int32_t i; |
// Read the data received from the last transfer |
int rxBufferCount = SPI1STATbits.RXBUFELM; |
int32_t rxBufferCount = SPI1STATbits.RXBUFELM; |
for (i = 0; i < rxBufferCount; i++) { |
char c = SPI1BUF; |
int8_t c = SPI1BUF; |
// Call the RX callback function on the received data |
if (spi_data_ptr->rx_callback != NULL) |
(*spi_data_ptr->rx_callback)(c); |
92,7 → 90,7 |
|
// Process SPI1 transmit flag |
if (IFS0bits.SPI1TXIF && IEC0bits.SPI1TXIE) { |
int i; |
int32_t i; |
// Disable the transmit interrupt if all data has been sent |
if (spi_data_ptr->buffer_out_len == 0) { |
IEC0CLR=0x02000000; |
100,7 → 98,7 |
(*spi_data_ptr->tx_callback)(); |
} else { |
// Start transmitting the data in the buffer |
int txBufferFree = 16 - SPI1STATbits.TXBUFELM; |
int32_t txBufferFree = 16 - SPI1STATbits.TXBUFELM; |
if (spi_data_ptr->buffer_out_len > txBufferFree) { |
for (i = 0; i < txBufferFree; i++) { |
SPI1BUF = spi_data_ptr->buffer_out[spi_data_ptr->buffer_out_ind]; |