altos/telelco-v3.0: Merge info into one screen
[fw/altos] / src / samd21 / ao_serial_samd21.c
index 0741790b17661a09389900bd6e0d29f5d697f5fa..97d8379083ce9c529efe3977f8c076f5a0c15324 100644 (file)
@@ -40,8 +40,9 @@ static void
 _ao_usart_rx(struct ao_samd21_usart *usart, int is_stdin)
 {
        if (usart->reg->intflag & (1 << SAMD21_SERCOM_INTFLAG_RXC)) {
+               uint8_t data = (uint8_t) usart->reg->data;
                if (!ao_fifo_full(usart->rx_fifo)) {
-                       ao_fifo_insert(usart->rx_fifo, usart->reg->data);
+                       ao_fifo_insert(usart->rx_fifo, data);
                        ao_wakeup(&usart->rx_fifo);
                        if (is_stdin)
                                ao_wakeup(&ao_stdin_ready);
@@ -54,8 +55,6 @@ _ao_usart_rx(struct ao_samd21_usart *usart, int is_stdin)
                                usart->rts = 0;
                        }
 #endif
-               } else {
-                       usart->reg->intenclr = (1 << SAMD21_SERCOM_INTFLAG_RXC);
                }
        }
 }
@@ -89,14 +88,26 @@ static const uint32_t ao_usart_speeds[] = {
 static void
 ao_usart_set_speed(struct ao_samd21_usart *usart, uint8_t speed)
 {
+       struct samd21_sercom *reg = usart->reg;
        uint64_t        top = (uint64_t) ao_usart_speeds[speed] << (4 + 16);
        uint16_t        baud = (uint16_t) (65536 - (top + AO_SYSCLK/2) / AO_SYSCLK);
+       uint32_t        ctrla = reg->ctrla;
 
+       if (ctrla & (1UL << SAMD21_SERCOM_CTRLA_ENABLE)) {
+               usart->reg->ctrla = ctrla & ~(1UL << SAMD21_SERCOM_CTRLA_ENABLE);
+               while (reg->syncbusy & (1 << SAMD21_SERCOM_SYNCBUSY_ENABLE))
+                       ;
+       }
        usart->reg->baud = baud;
+       if (ctrla & (1UL << SAMD21_SERCOM_CTRLA_ENABLE)) {
+               usart->reg->ctrla = ctrla;
+               while (reg->syncbusy & (1 << SAMD21_SERCOM_SYNCBUSY_ENABLE))
+                       ;
+       }
 }
 
 static void
-ao_usart_init(struct ao_samd21_usart *usart, bool hw_flow, uint8_t id)
+ao_usart_init(struct ao_samd21_usart *usart, bool hw_flow, uint8_t id, uint8_t txpo, uint8_t rxpo)
 {
        struct samd21_sercom *reg = usart->reg;
 
@@ -137,8 +148,8 @@ ao_usart_init(struct ao_samd21_usart *usart, bool hw_flow, uint8_t id)
                      (1 << SAMD21_SERCOM_CTRLA_RUNSTDBY) |
                      (0 << SAMD21_SERCOM_CTRLA_IBON) |
                      (0 << SAMD21_SERCOM_CTRLA_SAMPR) |
-                     (1 << SAMD21_SERCOM_CTRLA_TXPO) | /* pad[2] */
-                     (3 << SAMD21_SERCOM_CTRLA_RXPO) | /* pad[3] */
+                     (txpo << SAMD21_SERCOM_CTRLA_TXPO) |      /* pad[2] */
+                     (rxpo << SAMD21_SERCOM_CTRLA_RXPO) |      /* pad[3] */
                      (0 << SAMD21_SERCOM_CTRLA_SAMPA) |
                      (0 << SAMD21_SERCOM_CTRLA_FORM) | /* no parity */
                      (0 << SAMD21_SERCOM_CTRLA_CMODE) | /* async */
@@ -147,6 +158,10 @@ ao_usart_init(struct ao_samd21_usart *usart, bool hw_flow, uint8_t id)
 
        /* Enable receive interrupt */
        reg->intenset = (1 << SAMD21_SERCOM_INTFLAG_RXC);
+
+       while (reg->syncbusy & (1 << SAMD21_SERCOM_SYNCBUSY_ENABLE))
+               ;
+
 }
 
 static int
@@ -159,10 +174,6 @@ _ao_usart_pollchar(struct ao_samd21_usart *usart)
        else {
                uint8_t u;
                ao_fifo_remove(usart->rx_fifo, u);
-               if ((usart->reg->intenset & (1 << SAMD21_SERCOM_INTFLAG_RXC)) == 0) {
-                       if (ao_fifo_barely(usart->rx_fifo))
-                               usart->reg->intenset = (1 << SAMD21_SERCOM_INTFLAG_RXC);
-               }
 #if HAS_SERIAL_SW_FLOW
                /* If we've cleared RTS, check if there's space now and turn it back on */
                if (usart->gpio_rts && usart->rts == 0 && ao_fifo_barely(usart->rx_fifo)) {
@@ -287,6 +298,7 @@ ao_serial1_set_speed(uint8_t speed)
 void
 ao_serial_init(void)
 {
+       uint8_t txpo, rxpo;
 #if HAS_SERIAL_0
 
 #if SERIAL_0_PA10_PA11
@@ -294,12 +306,21 @@ ao_serial_init(void)
        ao_enable_port(&samd21_port_a);
        samd21_port_pmux_set(&samd21_port_a, 10, SAMD21_PORT_PMUX_FUNC_C);
        samd21_port_pmux_set(&samd21_port_a, 11, SAMD21_PORT_PMUX_FUNC_C);
+       txpo = SAMD21_SERCOM_CTRLA_TXPO_TX_2; /* pad 2 */
+       rxpo = SAMD21_SERCOM_CTRLA_RXPO_RX_3; /* pad 3 */
+#elif SERIAL_0_PA08_PA09
+       /* Pin settings */
+       ao_enable_port(&samd21_port_a);
+       samd21_port_pmux_set(&samd21_port_a, 8, SAMD21_PORT_PMUX_FUNC_C);
+       samd21_port_pmux_set(&samd21_port_a, 9, SAMD21_PORT_PMUX_FUNC_C);
+       txpo = SAMD21_SERCOM_CTRLA_TXPO_TX_0; /* pad 0 */
+       rxpo = SAMD21_SERCOM_CTRLA_RXPO_RX_1; /* pad 1 */
 #else
 #error "No SERIAL_0 port configuration specified"
 #endif
 
        ao_samd21_usart0.reg = &samd21_sercom0;
-       ao_usart_init(&ao_samd21_usart0, 0, 0);
+       ao_usart_init(&ao_samd21_usart0, 0, 0, txpo, rxpo);
 
 #if USE_SERIAL_0_STDIN
        ao_add_stdio(_ao_serial0_pollchar,
@@ -314,12 +335,14 @@ ao_serial_init(void)
        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);
+       txpo = SAMD21_SERCOM_CTRLA_TXPO_TX_0;
+       rxpo = SAMD21_SERCOM_CTRLA_RXPO_RX_1;
 #else
 #error "No SERIAL_1 port configuration specified"
 #endif
 
        ao_samd21_usart1.reg = &samd21_sercom1;
-       ao_usart_init(&ao_samd21_usart1, 0, 0);
+       ao_usart_init(&ao_samd21_usart1, 0, 1, txpo, rxpo);
 
 #if USE_SERIAL_1_STDIN
        ao_add_stdio(_ao_serial1_pollchar,