add UART RX

This commit is contained in:
King Kévin 2022-10-13 13:02:53 +02:00
parent 02e5fbdbf5
commit b689c8d94c
1 changed files with 25 additions and 0 deletions

25
main.c
View File

@ -76,6 +76,9 @@ static volatile uint8_t nec_msg[4] = {0};
// flag set if NEC message has been received
static volatile bool nec_flag = false;
// set when data is received over UART
static volatile char uart_c = 0;
// blocking wait (in 10 us steps, up to UINT32_MAX / 10)
static void wait_10us(uint32_t us10)
{
@ -285,6 +288,10 @@ void main(void)
UART1->BRR2.reg = 0x0B; // set baud rate to 115200 (at 16 MHz)
UART1->BRR1.reg = 0x08; // set baud rate to 115200 (at 16 MHz)
UART1->CR2.fields.TEN = 1; // enable TX
UART1->CR2.fields.REN = 1; // enable RX
UART1->CR2.fields.RIEN = 1; // enable RX interrupt
char uart_cmd[10]; // buffer for received data
uint8_t uart_used = 0; // how much of the buffer is used
// configure IR demodulator pin
IRM_ON_PORT->ODR.reg &= ~IRM_ON_PIN; // switch IR demodulator off
@ -421,6 +428,16 @@ void main(void)
led_blue(0); // ensure LED is off
halt();
}
if (uart_c) { // data received over UART
putc(uart_c); // echo back
if ('\r' == uart_c || '\n' == uart_c) { // end of line received
uart_used = 0; // reset buffer
} else if (uart_used < ARRAY_LENGTH(uart_cmd)) {
uart_cmd[uart_used++] = uart_c;
}
uart_c = 0; // clear flag
action = true; // redo main loop
}
if (action) { // something has been performed, check if other flags have been set meanwhile
action = false; // clear flag
} else { // nothing down
@ -495,3 +512,11 @@ void nec_cc_isr(void) __interrupt(IRQ_TIM1_CC)
TIM1->SR1.fields.CC2IF = 0; // clear flag
}
}
// UART RX interrupt
void rx_isr(void) __interrupt(IRQ_UART1_RX)
{
if (UART1->SR.fields.RXNE) { // there is data
uart_c = UART1->DR.reg; // read received data, also clears flag
}
}