From 531e6f50086ef8e1be83168743d9374213622422 Mon Sep 17 00:00:00 2001 From: Dan Evans Date: Mon, 11 Sep 2017 18:38:19 -0600 Subject: [PATCH] sam0:usart - wake from sleep on receive line --- boards/arduino-mkr-common/include/periph_conf.h | 2 +- boards/arduino-zero/include/periph_conf.h | 4 ++-- boards/samd21-xpro/include/periph_conf.h | 6 +++--- boards/saml21-xpro/include/periph_conf.h | 4 ++-- boards/samr21-xpro/include/periph_conf.h | 4 ++-- boards/sodaq-autonomo/include/periph_conf.h | 8 ++++---- cpu/sam0_common/include/periph_cpu_common.h | 11 ++++++++++- cpu/sam0_common/periph/uart.c | 12 +++++++++--- 8 files changed, 33 insertions(+), 18 deletions(-) diff --git a/boards/arduino-mkr-common/include/periph_conf.h b/boards/arduino-mkr-common/include/periph_conf.h index a538729ad6..b13064bc45 100644 --- a/boards/arduino-mkr-common/include/periph_conf.h +++ b/boards/arduino-mkr-common/include/periph_conf.h @@ -110,7 +110,7 @@ static const uart_conf_t uart_config[] = { .mux = GPIO_MUX_D, .rx_pad = UART_PAD_RX_3, .tx_pad = UART_PAD_TX_2, - .runstdby = 0, + .flags = UART_FLAG_NONE, .gclk_src = GCLK_CLKCTRL_GEN_GCLK0 } }; diff --git a/boards/arduino-zero/include/periph_conf.h b/boards/arduino-zero/include/periph_conf.h index a34cd81e83..af474ea8ea 100644 --- a/boards/arduino-zero/include/periph_conf.h +++ b/boards/arduino-zero/include/periph_conf.h @@ -113,7 +113,7 @@ static const uart_conf_t uart_config[] = { .mux = GPIO_MUX_D, .rx_pad = UART_PAD_RX_3, .tx_pad = UART_PAD_TX_2, - .runstdby = 0, + .flags = UART_FLAG_NONE, .gclk_src = GCLK_CLKCTRL_GEN_GCLK0 }, { @@ -123,7 +123,7 @@ static const uart_conf_t uart_config[] = { .mux = GPIO_MUX_C, .rx_pad = UART_PAD_RX_3, .tx_pad = UART_PAD_TX_2, - .runstdby = 0, + .flags = UART_FLAG_NONE, .gclk_src = GCLK_CLKCTRL_GEN_GCLK0 } }; diff --git a/boards/samd21-xpro/include/periph_conf.h b/boards/samd21-xpro/include/periph_conf.h index 02e138ae64..1f6e96664b 100644 --- a/boards/samd21-xpro/include/periph_conf.h +++ b/boards/samd21-xpro/include/periph_conf.h @@ -118,7 +118,7 @@ static const uart_conf_t uart_config[] = { .mux = GPIO_MUX_C, .rx_pad = UART_PAD_RX_1, .tx_pad = UART_PAD_TX_0, - .runstdby = 0, + .flags = UART_FLAG_NONE, .gclk_src = GCLK_CLKCTRL_GEN_GCLK0 }, { /* EXT1 */ @@ -128,7 +128,7 @@ static const uart_conf_t uart_config[] = { .mux = GPIO_MUX_D, .rx_pad = UART_PAD_RX_1, .tx_pad = UART_PAD_TX_0, - .runstdby = 0, + .flags = UART_FLAG_NONE, .gclk_src = GCLK_CLKCTRL_GEN_GCLK0 }, { /* EXT2/3 */ @@ -138,7 +138,7 @@ static const uart_conf_t uart_config[] = { .mux = GPIO_MUX_D, .rx_pad = UART_PAD_RX_3, .tx_pad = UART_PAD_TX_2, - .runstdby = 0, + .flags = UART_FLAG_NONE, .gclk_src = GCLK_CLKCTRL_GEN_GCLK0 } }; diff --git a/boards/saml21-xpro/include/periph_conf.h b/boards/saml21-xpro/include/periph_conf.h index d3b77a23b0..98fa46acc9 100644 --- a/boards/saml21-xpro/include/periph_conf.h +++ b/boards/saml21-xpro/include/periph_conf.h @@ -60,7 +60,7 @@ static const uart_conf_t uart_config[] = { .mux = GPIO_MUX_C, .rx_pad = UART_PAD_RX_1, .tx_pad = UART_PAD_TX_0, - .runstdby = 0, + .flags = UART_FLAG_NONE, .gclk_src = GCLK_PCHCTRL_GEN_GCLK0 }, { /* EXT1 header */ @@ -70,7 +70,7 @@ static const uart_conf_t uart_config[] = { .mux = GPIO_MUX_D, .rx_pad = UART_PAD_RX_1, .tx_pad = UART_PAD_TX_0, - .runstdby = 0, + .flags = UART_FLAG_NONE, .gclk_src = GCLK_PCHCTRL_GEN_GCLK0 } }; diff --git a/boards/samr21-xpro/include/periph_conf.h b/boards/samr21-xpro/include/periph_conf.h index 18881a5b85..140c50ff76 100644 --- a/boards/samr21-xpro/include/periph_conf.h +++ b/boards/samr21-xpro/include/periph_conf.h @@ -118,7 +118,7 @@ static const uart_conf_t uart_config[] = { .mux = GPIO_MUX_D, .rx_pad = UART_PAD_RX_1, .tx_pad = UART_PAD_TX_0, - .runstdby = 0, + .flags = UART_FLAG_NONE, .gclk_src = GCLK_CLKCTRL_GEN_GCLK0 }, { @@ -128,7 +128,7 @@ static const uart_conf_t uart_config[] = { .mux = GPIO_MUX_D, .rx_pad = UART_PAD_RX_1, .tx_pad = UART_PAD_TX_0, - .runstdby = 0, + .flags = UART_FLAG_NONE, .gclk_src = GCLK_CLKCTRL_GEN_GCLK0 } }; diff --git a/boards/sodaq-autonomo/include/periph_conf.h b/boards/sodaq-autonomo/include/periph_conf.h index e8e77dbcaf..78725cc74d 100644 --- a/boards/sodaq-autonomo/include/periph_conf.h +++ b/boards/sodaq-autonomo/include/periph_conf.h @@ -109,7 +109,7 @@ static const uart_conf_t uart_config[] = { .mux = GPIO_MUX_C, .rx_pad = UART_PAD_RX_1, .tx_pad = UART_PAD_TX_2, - .runstdby = 0, + .flags = UART_FLAG_NONE, .gclk_src = GCLK_CLKCTRL_GEN_GCLK0 }, { @@ -119,7 +119,7 @@ static const uart_conf_t uart_config[] = { .mux = GPIO_MUX_D, .rx_pad = UART_PAD_RX_1, .tx_pad = UART_PAD_TX_0_RTS_2_CTS_3, - .runstdby = 0, + .flags = UART_FLAG_NONE, .gclk_src = GCLK_CLKCTRL_GEN_GCLK0 }, { @@ -129,7 +129,7 @@ static const uart_conf_t uart_config[] = { .mux = GPIO_MUX_C, .rx_pad = UART_PAD_RX_1, .tx_pad = UART_PAD_TX_2, - .runstdby = 0, + .flags = UART_FLAG_NONE, .gclk_src = GCLK_CLKCTRL_GEN_GCLK0 }, { @@ -139,7 +139,7 @@ static const uart_conf_t uart_config[] = { .mux = GPIO_MUX_C, .rx_pad = UART_PAD_RX_1, .tx_pad = UART_PAD_TX_2, - .runstdby = 0, + .flags = UART_FLAG_NONE, .gclk_src = GCLK_CLKCTRL_GEN_GCLK0 } }; diff --git a/cpu/sam0_common/include/periph_cpu_common.h b/cpu/sam0_common/include/periph_cpu_common.h index 212063765c..cc88d47071 100644 --- a/cpu/sam0_common/include/periph_cpu_common.h +++ b/cpu/sam0_common/include/periph_cpu_common.h @@ -117,6 +117,15 @@ typedef enum { * and CTS on pad 3 */ } uart_txpad_t; +/** + * @brief Available SERCOM UART flag selections + */ +typedef enum { + UART_FLAG_NONE = 0x0, /**< No flags set */ + UART_FLAG_RUN_STANDBY = 0x1, /**< run SERCOM in standby mode */ + UART_FLAG_WAKEUP = 0x2, /**< wake from sleep on receive */ +} uart_flag_t; + /** * @brief UART device configuration */ @@ -127,7 +136,7 @@ typedef struct { gpio_mux_t mux; /**< alternative function for pins */ uart_rxpad_t rx_pad; /**< pad selection for RX line */ uart_txpad_t tx_pad; /**< pad selection for TX line */ - uint8_t runstdby; /**< allow SERCOM to run in standby mode */ + uart_flag_t flags; /**< set optional SERCOM flags */ uint32_t gclk_src; /**< GCLK source which supplys SERCOM */ } uart_conf_t; diff --git a/cpu/sam0_common/periph/uart.c b/cpu/sam0_common/periph/uart.c index a26c2cb293..9e26acf806 100644 --- a/cpu/sam0_common/periph/uart.c +++ b/cpu/sam0_common/periph/uart.c @@ -80,9 +80,11 @@ int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg) SERCOM_USART_CTRLA_SAMPR(0x1) | SERCOM_USART_CTRLA_TXPO(uart_config[uart].tx_pad) | SERCOM_USART_CTRLA_RXPO(uart_config[uart].rx_pad) | - SERCOM_USART_CTRLA_MODE(0x1) | - (uart_config[uart].runstdby ? - SERCOM_USART_CTRLA_RUNSTDBY : 0)); + SERCOM_USART_CTRLA_MODE(0x1)); + /* Set run in standby mode if enabled */ + if (uart_config[uart].flags & UART_FLAG_RUN_STANDBY) { + dev(uart)->CTRLA.reg |= SERCOM_USART_CTRLA_RUNSTDBY; + } /* calculate and set baudrate */ uint32_t baud = ((((uint32_t)CLOCK_CORECLOCK * 10) / baudrate) / 16); @@ -98,6 +100,10 @@ int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg) NVIC_EnableIRQ(SERCOM0_IRQn + sercom_id(dev(uart))); dev(uart)->CTRLB.reg |= SERCOM_USART_CTRLB_RXEN; dev(uart)->INTENSET.reg |= SERCOM_USART_INTENSET_RXC; + /* set wakeup receive from sleep if enabled */ + if (uart_config[uart].flags & UART_FLAG_WAKEUP) { + dev(uart)->CTRLB.reg |= SERCOM_USART_CTRLB_SFDE; + } } while (dev(uart)->SYNCBUSY.reg & SERCOM_USART_SYNCBUSY_CTRLB) {}