<div dir="ltr">From: Jacob Killelea <<a href="mailto:jkillelea344@gmail.com">jkillelea344@gmail.com</a>><br><div><br></div><div>Hi all, this is my first email patch submission and my first contribution to RTEMS, so please give any feedback you have!</div><div><br></div>This patch enables interrupt driven data reception on USART ports on<br>STM32F4 series chips. This feature is gated behind the config flag<br>BSP_CONSOLE_USE_INTERRUPTS. If this flag is not set to True, the older<br>polling implementation will be used. I tested this feature on STM32F401CE<br>(blackpill) and STM32 Nucleo F411RE boards, with both capable of keeping<br>up with a 115200 baud continous data stream. With the older polling<br>implementation, both would drop bytes at 9600 baud. In addition, I<br>updated the implementation of usart_set_attributes to support changing<br>the baud rate of the USART port based on the input speed.<br>---<br> bsps/arm/stm32f4/console/usart.c    | 81 ++++++++++++++++++++++++-----<br> spec/build/bsps/arm/stm32f4/grp.yml |  2 +<br> 2 files changed, 69 insertions(+), 14 deletions(-)<br><br>diff --git a/bsps/arm/stm32f4/console/usart.c b/bsps/arm/stm32f4/console/usart.c<br>index 37566ef9d7..129249dc29 100644<br>--- a/bsps/arm/stm32f4/console/usart.c<br>+++ b/bsps/arm/stm32f4/console/usart.c<br>@@ -14,6 +14,8 @@<br> #include <bsp/irq.h><br> #include <bsp/usart.h><br> #include <bsp/stm32f4.h><br>+#include <termios.h><br>+#include <string.h><br> <br> static volatile stm32f4_usart *usart_get_regs(const console_tbl *ct)<br> {<br>@@ -27,6 +29,24 @@ static rtems_vector_number usart_get_irq_number(const console_tbl *ct)<br> }<br> #endif<br> <br>+#ifdef BSP_CONSOLE_USE_INTERRUPTS<br>+/**<br>+ * Read characters in an interrupt<br>+ */<br>+static void stm32f4_usart_interrupt(void *arg)<br>+{<br>+  rtems_termios_tty *tty = (rtems_termios_tty *) arg;<br>+  const console_tbl *ct = Console_Port_Tbl [tty->minor];<br>+  volatile stm32f4_usart *usart = usart_get_regs(ct);<br>+<br>+  while ((usart->sr & STM32F4_USART_SR_RXNE) == STM32F4_USART_SR_RXNE)<br>+  {<br>+    char data = STM32F4_USART_DR_GET(usart->dr);<br>+    rtems_termios_enqueue_raw_characters(tty, &data, sizeof(data));<br>+  }<br>+}<br>+#endif<br>+<br> static const stm32f4_rcc_index usart_rcc_index [] = {<br>   STM32F4_RCC_USART1,<br>   STM32F4_RCC_USART2,<br>@@ -128,29 +148,50 @@ static void usart_initialize(int minor)<br>   usart->cr2 = 0;<br>   usart->cr3 = 0;<br>   usart->bbr = usart_get_bbr(usart, pclk, baud);<br>-  usart->cr1 = STM32F4_USART_CR1_UE<br>-    | STM32F4_USART_CR1_TE<br>-    | STM32F4_USART_CR1_RE;<br>+  usart->cr1 = STM32F4_USART_CR1_UE // UART enable<br>+#ifdef BSP_CONSOLE_USE_INTERRUPTS<br>+    | STM32F4_USART_CR1_RXNEIE // RX interrupt<br>+#endif<br>+    | STM32F4_USART_CR1_TE  // TX enable<br>+    | STM32F4_USART_CR1_RE; // RX enable<br> }<br> <br> static int usart_first_open(int major, int minor, void *arg)<br> {<br>+  rtems_status_code sc = RTEMS_SUCCESSFUL;<br>   rtems_libio_open_close_args_t *oc = (rtems_libio_open_close_args_t *) arg;<br>-  struct rtems_termios_tty *tty = (struct rtems_termios_tty *) oc->iop->data1;<br>+  rtems_termios_tty *tty = (struct rtems_termios_tty *) oc->iop->data1;<br>   const console_tbl *ct = Console_Port_Tbl [minor];<br>   console_data *cd = &Console_Port_Data [minor];<br> <br>   cd->termios_data = tty;<br>   rtems_termios_set_initial_baud(tty, ct->ulClock);<br> <br>-  return 0;<br>+#ifdef BSP_CONSOLE_USE_INTERRUPTS<br>+  sc = rtems_interrupt_handler_install(ct->ulIntVector,<br>+      ct->sDeviceName,<br>+      RTEMS_INTERRUPT_UNIQUE,<br>+      stm32f4_usart_interrupt,<br>+      tty);<br>+#endif<br>+<br>+  return sc;<br> }<br> <br> static int usart_last_close(int major, int minor, void *arg)<br> {<br>-  return 0;<br>+  rtems_status_code sc = RTEMS_SUCCESSFUL;<br>+#ifdef BSP_CONSOLE_USE_INTERRUPTS<br>+  rtems_libio_open_close_args_t *oc = (rtems_libio_open_close_args_t *) arg;<br>+  rtems_termios_tty *tty = (struct rtems_termios_tty *) oc->iop->data1;<br>+  const console_tbl *ct = Console_Port_Tbl [minor];<br>+<br>+  sc = rtems_interrupt_handler_remove(ct->ulIntVector, stm32f4_usart_interrupt, tty);<br>+#endif<br>+  return sc;<br> }<br> <br>+#ifndef BSP_CONSOLE_USE_INTERRUPTS<br> static int usart_read_polled(int minor)<br> {<br>   const console_tbl *ct = Console_Port_Tbl [minor];<br>@@ -162,6 +203,7 @@ static int usart_read_polled(int minor)<br>     return -1;<br>   }<br> }<br>+#endif<br> <br> static void usart_write_polled(int minor, char c)<br> {<br>@@ -175,11 +217,7 @@ static void usart_write_polled(int minor, char c)<br>   usart->dr = STM32F4_USART_DR(c);<br> }<br> <br>-static ssize_t usart_write_support_polled(<br>-  int minor,<br>-  const char *s,<br>-  size_t n<br>-)<br>+static ssize_t usart_write_support_polled(int minor, const char *s, size_t n)<br> {<br>   ssize_t i = 0;<br> <br>@@ -190,19 +228,34 @@ static ssize_t usart_write_support_polled(<br>   return n;<br> }<br> <br>+/**<br>+ * Configure settings from a termios call to tcsetattr()<br>+ */<br> static int usart_set_attributes(int minor, const struct termios *term)<br> {<br>-  return -1;<br>+  console_tbl *ct = Console_Port_Tbl[minor];<br>+  volatile stm32f4_usart *usart = usart_get_regs(ct);<br>+  uint32_t pclk = usart_get_pclk(ct);<br>+  uint32_t baud = term->c_ispeed;<br>+<br>+  ct->ulClock = baud;<br>+  usart->bbr = usart_get_bbr(usart, pclk, baud);<br>+  return 0;<br> }<br> <br>-const console_fns stm32f4_usart_fns = {<br>+const console_fns stm32f4_usart_fns =<br>+{<br>   .deviceProbe = libchip_serial_default_probe,<br>   .deviceFirstOpen = usart_first_open,<br>   .deviceLastClose = usart_last_close,<br>+#ifdef BSP_CONSOLE_USE_INTERRUPTS<br>+  .deviceRead = NULL,<br>+#else<br>   .deviceRead = usart_read_polled,<br>+#endif<br>   .deviceWrite = usart_write_support_polled,<br>   .deviceInitialize = usart_initialize,<br>   .deviceWritePolled = usart_write_polled,<br>   .deviceSetAttributes = usart_set_attributes,<br>-  .deviceOutputUsesInterrupts = false<br>+  .deviceOutputUsesInterrupts = false,<br> };<br>diff --git a/spec/build/bsps/arm/stm32f4/grp.yml b/spec/build/bsps/arm/stm32f4/grp.yml<br>index 2257fdf015..27e2197e46 100644<br>--- a/spec/build/bsps/arm/stm32f4/grp.yml<br>+++ b/spec/build/bsps/arm/stm32f4/grp.yml<br>@@ -50,6 +50,8 @@ links:<br>   uid: optsysclk<br> - role: build-dependency<br>   uid: optusartbaud<br>+- role: build-dependency<br>+  uid: ../../optconsoleirq<br> - role: build-dependency<br>   uid: ../../linkcmds<br> - role: build-dependency<br>-- <br>2.34.1<br><br></div>