35,6 → 35,7 |
uart_1_data.buffer_in_read_ind = 0; |
uart_1_data.buffer_in_write_ind = 0; |
uart_1_data.buffer_in_len = 0; |
uart_1_data.buffer_in_len_tmp = 0; |
} |
|
//void uart_2_init() { |
56,28 → 57,40 |
void UART1_Recv_Interrupt_Handler() { |
unsigned char c; |
if (PIR1bits.RC1IF) { // Check if data receive flag is set |
if (uart_1_data.buffer_in_len == MAXUARTBUF-1) { |
TXSTA1bits.TXEN = 0; // Kill anything currently sending |
DBG_PRINT_UART("UART1: buffer overflow\r\n"); |
c = RCREG1; // Read RCREG to clear it |
} else { |
c = RCREG1; |
|
// Save received data into buffer |
uart_1_data.buffer_in[uart_1_data.buffer_in_write_ind] = RCREG1; |
uart_1_data.buffer_in[uart_1_data.buffer_in_write_ind] = c; |
if (uart_1_data.buffer_in_write_ind == MAXUARTBUF-1) { |
uart_1_data.buffer_in_write_ind = 0; |
} else { |
uart_1_data.buffer_in_write_ind++; |
} |
uart_1_data.buffer_in_len++; |
|
// Store the last MAXUARTBUF values entered |
if (uart_1_data.buffer_in_len_tmp < MAXUARTBUF) { |
uart_1_data.buffer_in_len_tmp++; |
} else { |
if (uart_1_data.buffer_in_read_ind == MAXUARTBUF - 1) { |
uart_1_data.buffer_in_read_ind = 0; |
} else { |
uart_1_data.buffer_in_read_ind++; |
} |
} |
|
// Update buffer size upon receiving newline (0x0D) |
if (c == UART1_BREAK_CHAR) { |
uart_1_data.buffer_in_len = uart_1_data.buffer_in_len_tmp; |
uart_1_data.buffer_in_len_tmp = 0; |
} |
} |
|
if (RCSTAbits.OERR == 1) { |
// We've overrun the USART and must reset |
RCSTA1bits.CREN = 0; // Reset UART1 |
RCSTA1bits.CREN = 1; |
DBG_PRINT_UART("UART1: (ERROR) overrun\r\n"); |
TXSTA1bits.TXEN = 0; // Kill anything currently sending |
DBG_PRINT_UART("UART1: overrun\r\n"); |
} |
} |
|
130,8 → 143,12 |
TXSTA1bits.TXEN = 1; // Begin transmission |
} |
|
unsigned char UART1_Buffer_Len() { |
return uart_1_data.buffer_in_len; |
} |
|
/* Reader interface to the UART buffer, returns the number of bytes read */ |
unsigned char UART1_Read(char *buffer) { |
unsigned char UART1_Read_Buffer(unsigned char *buffer) { |
unsigned char i = 0; |
while (uart_1_data.buffer_in_len != 0) { |
buffer[i] = uart_1_data.buffer_in[uart_1_data.buffer_in_read_ind]; |