samd21: Get serial driver building
authorKeith Packard <keithp@keithp.com>
Mon, 7 Nov 2022 02:33:14 +0000 (18:33 -0800)
committerKeith Packard <keithp@keithp.com>
Mon, 7 Nov 2022 02:33:14 +0000 (18:33 -0800)
Signed-off-by: Keith Packard <keithp@keithp.com>
src/samd21/ao_serial.h
src/samd21/ao_serial_samd21.c

index 440b562b0b26e5c00ad3bf2ca8b22093b51e5d54..1120f65f567219e69b932b922b9ba1111cb17e02 100644 (file)
@@ -26,8 +26,7 @@
 #define AO_SERIAL_SPEED_115200 4
 
 #if HAS_SERIAL_0
-extern volatile struct ao_fifo ao_serial0_rx_fifo;
-extern volatile struct ao_fifo ao_serial0_tx_fifo;
+extern struct ao_samd21_usart  ao_samd21_usart0;
 
 char
 ao_serial0_getchar(void);
@@ -49,8 +48,7 @@ ao_serial0_set_speed(uint8_t speed);
 #endif
 
 #if HAS_SERIAL_1
-extern volatile struct ao_fifo ao_serial1_rx_fifo;
-extern volatile struct ao_fifo ao_serial1_tx_fifo;
+extern struct ao_samd21_usart  ao_samd21_usart1;
 
 char
 ao_serial1_getchar(void);
@@ -72,8 +70,7 @@ ao_serial1_set_speed(uint8_t speed);
 #endif
 
 #if HAS_SERIAL_2
-extern volatile struct ao_fifo ao_serial2_rx_fifo;
-extern volatile struct ao_fifo ao_serial2_tx_fifo;
+extern struct ao_samd21_usart  ao_samd21_usart2;
 
 char
 ao_serial2_getchar(void);
@@ -95,8 +92,7 @@ ao_serial2_set_speed(uint8_t speed);
 #endif
 
 #if HAS_SERIAL_3
-extern volatile struct ao_fifo ao_serial3_rx_fifo;
-extern volatile struct ao_fifo ao_serial3_tx_fifo;
+extern struct ao_samd21_usart  ao_samd21_usart3;
 
 char
 ao_serial3_getchar(void);
@@ -117,6 +113,50 @@ void
 ao_serial3_set_speed(uint8_t speed);
 #endif
 
+#if HAS_SERIAL_4
+extern struct ao_samd21_usart  ao_samd21_usart4;
+
+char
+ao_serial4_getchar(void);
+
+int
+_ao_serial4_pollchar(void);
+
+uint8_t
+_ao_serial4_sleep_for(uint16_t timeout);
+
+void
+ao_serial4_putchar(char c);
+
+void
+ao_serial4_drain(void);
+
+void
+ao_serial4_set_speed(uint8_t speed);
+#endif
+
+#if HAS_SERIAL_5
+extern struct ao_samd21_usart  ao_samd21_usart5;
+
+char
+ao_serial5_getchar(void);
+
+int
+_ao_serial5_pollchar(void);
+
+uint8_t
+_ao_serial5_sleep_for(uint16_t timeout);
+
+void
+ao_serial5_putchar(char c);
+
+void
+ao_serial5_drain(void);
+
+void
+ao_serial5_set_speed(uint8_t speed);
+#endif
+
 void
 ao_serial_init(void);
 
index a52966c1249ba39dc1b29c98b4f6fcfb2bdb3d2f..0741790b17661a09389900bd6e0d29f5d697f5fa 100644 (file)
@@ -17,7 +17,7 @@
 static int
 _ao_usart_tx_start(struct ao_samd21_usart *usart)
 {
-       if (!ao_fifo_empty(&usart->tx_fifo)) {
+       if (!ao_fifo_empty(usart->tx_fifo)) {
 #if HAS_SERIAL_SW_FLOW
                if (usart->gpio_cts && ao_gpio_get(usart->gpio_cts, usart->pin_cts, foo) == 1) {
                        ao_exti_enable(usart->gpio_cts, usart->pin_cts);
@@ -28,7 +28,7 @@ _ao_usart_tx_start(struct ao_samd21_usart *usart)
                {
                        usart->tx_running = 1;
                        usart->reg->intenset = (1 << SAMD21_SERCOM_INTFLAG_DRE) | (1 << SAMD21_SERCOM_INTFLAG_TXC);
-                       usart->reg->data = ao_fifo_remove(&usart->tx_fifo);
+                       ao_fifo_remove(usart->tx_fifo, usart->reg->data);
                        ao_wakeup(&usart->tx_fifo);
                        return 1;
                }
@@ -40,8 +40,8 @@ static void
 _ao_usart_rx(struct ao_samd21_usart *usart, int is_stdin)
 {
        if (usart->reg->intflag & (1 << SAMD21_SERCOM_INTFLAG_RXC)) {
-               if (!ao_fifo_full(&usart->rx_fifo)) {
-                       ao_fifo_insert(&usart->rx_fifo, usart->reg->data);
+               if (!ao_fifo_full(usart->rx_fifo)) {
+                       ao_fifo_insert(usart->rx_fifo, usart->reg->data);
                        ao_wakeup(&usart->rx_fifo);
                        if (is_stdin)
                                ao_wakeup(&ao_stdin_ready);
@@ -90,13 +90,13 @@ static void
 ao_usart_set_speed(struct ao_samd21_usart *usart, uint8_t speed)
 {
        uint64_t        top = (uint64_t) ao_usart_speeds[speed] << (4 + 16);
-       uint16_t        baud = 65536 - (top + AO_SYSCLK/2) / AO_SYSCLK;
+       uint16_t        baud = (uint16_t) (65536 - (top + AO_SYSCLK/2) / AO_SYSCLK);
 
        usart->reg->baud = baud;
 }
 
 static void
-ao_usart_init(struct ao_samd21_usart *usart, int hw_flow, int id)
+ao_usart_init(struct ao_samd21_usart *usart, bool hw_flow, uint8_t id)
 {
        struct samd21_sercom *reg = usart->reg;
 
@@ -154,13 +154,13 @@ _ao_usart_pollchar(struct ao_samd21_usart *usart)
 {
        int     c;
 
-       if (ao_fifo_empty(&usart->rx_fifo))
+       if (ao_fifo_empty(usart->rx_fifo))
                c = AO_READ_AGAIN;
        else {
                uint8_t u;
-               u = ao_fifo_remove(&usart->rx_fifo);
+               ao_fifo_remove(usart->rx_fifo, u);
                if ((usart->reg->intenset & (1 << SAMD21_SERCOM_INTFLAG_RXC)) == 0) {
-                       if (ao_fifo_barely(&usart->rx_fifo))
+                       if (ao_fifo_barely(usart->rx_fifo))
                                usart->reg->intenset = (1 << SAMD21_SERCOM_INTFLAG_RXC);
                }
 #if HAS_SERIAL_SW_FLOW
@@ -190,9 +190,9 @@ static void
 ao_usart_putchar(struct ao_samd21_usart *usart, char c)
 {
        ao_arch_block_interrupts();
-       while (ao_fifo_full(&usart->tx_fifo))
+       while (ao_fifo_full(usart->tx_fifo))
                ao_sleep(&usart->tx_fifo);
-       ao_fifo_insert(&usart->tx_fifo, c);
+       ao_fifo_insert(usart->tx_fifo, c);
        _ao_usart_tx_start(usart);
        ao_arch_release_interrupts();
 }
@@ -201,7 +201,7 @@ static void
 ao_usart_drain(struct ao_samd21_usart *usart)
 {
        ao_arch_block_interrupts();
-       while (!ao_fifo_empty(&usart->tx_fifo) || usart->tx_running) {
+       while (!ao_fifo_empty(usart->tx_fifo) || usart->tx_running) {
                usart->draining = 1;
                ao_sleep(&usart->tx_fifo);
        }
@@ -246,6 +246,44 @@ ao_serial0_set_speed(uint8_t speed)
 }
 #endif /* HAS_SERIAL_0 */
 
+#if HAS_SERIAL_1
+
+struct ao_samd21_usart ao_samd21_usart1;
+
+void samd21_sercom1_isr(void) { ao_usart_isr(&ao_samd21_usart1, USE_SERIAL_1_STDIN); }
+
+char
+ao_serial1_getchar(void)
+{
+       return ao_usart_getchar(&ao_samd21_usart1);
+}
+
+void
+ao_serial1_putchar(char c)
+{
+       ao_usart_putchar(&ao_samd21_usart1, c);
+}
+
+int
+_ao_serial1_pollchar(void)
+{
+       return _ao_usart_pollchar(&ao_samd21_usart1);
+}
+
+void
+ao_serial1_drain(void)
+{
+       ao_usart_drain(&ao_samd21_usart1);
+}
+
+void
+ao_serial1_set_speed(uint8_t speed)
+{
+       ao_usart_drain(&ao_samd21_usart1);
+       ao_usart_set_speed(&ao_samd21_usart1, speed);
+}
+#endif /* HAS_SERIAL_1 */
+
 void
 ao_serial_init(void)
 {
@@ -269,4 +307,24 @@ ao_serial_init(void)
                     NULL);
 #endif
 #endif
+#if HAS_SERIAL_1
+
+#if SERIAL_1_PA00_PA01
+       /* Pin settings */
+       ao_enable_port(&samd21_port_a);
+       samd21_port_pmux_set(&samd21_port_a, 0, SAMD21_PORT_PMUX_FUNC_D);
+       samd21_port_pmux_set(&samd21_port_a, 1, SAMD21_PORT_PMUX_FUNC_D);
+#else
+#error "No SERIAL_1 port configuration specified"
+#endif
+
+       ao_samd21_usart1.reg = &samd21_sercom1;
+       ao_usart_init(&ao_samd21_usart1, 0, 0);
+
+#if USE_SERIAL_1_STDIN
+       ao_add_stdio(_ao_serial1_pollchar,
+                    ao_serial1_putchar,
+                    NULL);
+#endif
+#endif
 }