Skip to content

Instantly share code, notes, and snippets.

@rday
Last active August 8, 2016 20:45
Show Gist options
  • Save rday/4c8647140fcdf981d3e6 to your computer and use it in GitHub Desktop.
Save rday/4c8647140fcdf981d3e6 to your computer and use it in GitHub Desktop.
UART ISR w/o Driverlib
volatile uint8_t recv_buf[BUF_LEN];
volatile uint8_t recv_buf_idx = 0;
/* ... */
/* EUSCI A0 UART ISR - Echoes data back to PC host */
void euscia0_isr(void)
{
// If this interrupt is an RX interrupt
if (EUSCI_A_CMSIS(EUSCI_A0_MODULE)->rIE.r & EUSCI_A_UART_RECEIVE_INTERRUPT) {
// Clear the flag
EUSCI_A_CMSIS(EUSCI_A0_MODULE)->rIFG.r &= ~EUSCI_A_UART_RECEIVE_INTERRUPT;
// Copy the data out of the FIFO
recv_buf[recv_buf_idx++] = EUSCI_A_CMSIS(EUSCI_A0_MODULE)->rRXBUF.r;
// We are still really iffy and miss data sometimes. Maybe this
// branch still needs to be moved out.
if (recv_buf_idx==RECV_BUF_LEN) {
recv_buf_idx = 0;
}
}
}
@joshimoo
Copy link

joshimoo commented Aug 8, 2016

if you use a power of 2 as your buffer size, you can AND with the the chosen (value -1) and your index will automatically wrap.
no branching needed.

Example:
Buffer Size = 16 (0001 0000)
AND Value = 16 - 1 = 15 (1111)

Index = 14 (1110) & 15 (1111) = 14
Index = 15 (1111) & 15 (1111) = 15
Index = 16 (1 0000) & 15 (1111) = 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment