-
-
Save wesen/625663db27a072b7b485b12bf516751e to your computer and use it in GitHub Desktop.
test to send an array of uint8_t's via SPI using both polling and interrupts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "project.h" | |
#include <string.h> | |
enum { | |
MAX_ITEMS = 10, | |
}; | |
ring_buffer_t rx_buffer; | |
ring_buffer_t tx_buffer; | |
uint8_t xfer_cnt = 0; | |
typedef enum { | |
TX_IDLE, | |
TX_TRANSFERRING | |
} tx_state = TX_IDLE; | |
void spi_xfer_asynch(const uint8_t *data, const size_t size); | |
CY_ISR_PROTO(SPI_rx_handler); | |
CY_ISR_PROTO(SPI_tx_handler); | |
int main(void) | |
{ | |
// test data | |
const uint8_t data_spi[10] = {0x01, 0x02, 0x03, 0x04, 0x05, | |
0x06, 0x07, 0x08, 0x09, 0x0A}; | |
// SPI_rx_handler is a callback executed when we transfer 1 byte | |
isr_SPI_Rx_StartEx(SPI_rx_handler); | |
// SPI_tx_handler is a callback executed when we receive 1 byte | |
isr_SPI_Tx_StartEx(SPI_tx_handler); | |
// Enable interrupts | |
CyGlobalIntEnable; | |
// Configure and enable the peripherals | |
SPI_Start(); | |
UART_Start(); | |
UART_PutChar(0x0C); // clear the screen | |
UART_PutString("Test SPI with interrupts.\r\n"); | |
while (1) { | |
if (tx_state == TX_IDLE) { | |
spi_xfer_async("foobar", sizeof("foobar")); | |
} | |
} | |
} | |
void spi_xfer_asynch(const uint8_t *data, const size_t size) | |
{ | |
for (size_t i = 0; i < size; i++) { | |
if (ring_buffer_is_full(&rx_buffer)) { | |
// OVERFLOW | |
} else { | |
ring_buffer_add(&rx_buffer, data[i]); | |
} | |
} | |
// we need to disable interrupts because we are writing to tx_state | |
disable_interrupts(); | |
if (tx_state == TX_IDLE) { | |
tx_state = TX_TRANSFERRING; | |
SS_Write(0); | |
// transfer 1 byte so we can trigger the SPI_tx_handler | |
SPI_WriteTxData(ring_buffer_get()); | |
} | |
enable_interrupts(); | |
} | |
CY_ISR(SPI_rx_handler) | |
{ | |
// TODO | |
} | |
CY_ISR(SPI_tx_handler) | |
{ | |
if (tx_state != TX_TRANSFERRING) { | |
// WTF | |
} else { | |
if (!ring_buffer_is_empty(&rx_buffer)) { | |
SPI_WriteTxData(ring_buffer_get(&rx_buffer)); | |
} else { | |
tx_state = TX_IDLE; | |
SS_Write(1); | |
} | |
} | |
} | |
/* [] END OF FILE */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment