From 137836fb19623735351e411f30d7d6bd29e28012 Mon Sep 17 00:00:00 2001 From: Stephane D'Alu Date: Tue, 2 Feb 2016 13:34:47 +0100 Subject: hardware flow control from nRF51 --- os/hal/ports/NRF51/NRF51822/serial_lld.c | 38 ++++++++++++++++++++++++++------ os/hal/ports/NRF51/NRF51822/serial_lld.h | 14 ++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) (limited to 'os/hal/ports') diff --git a/os/hal/ports/NRF51/NRF51822/serial_lld.c b/os/hal/ports/NRF51/NRF51822/serial_lld.c index bc4a9b6..bddc4c3 100644 --- a/os/hal/ports/NRF51/NRF51822/serial_lld.c +++ b/os/hal/ports/NRF51/NRF51822/serial_lld.c @@ -49,9 +49,13 @@ SerialDriver SD1; * @brief Driver default configuration. */ static const SerialConfig default_config = { - .speed = 38400, - .tx_pad = NRF51_SERIAL_PAD_DISCONNECTED, - .rx_pad = NRF51_SERIAL_PAD_DISCONNECTED, + .speed = 38400, + .tx_pad = NRF51_SERIAL_PAD_DISCONNECTED, + .rx_pad = NRF51_SERIAL_PAD_DISCONNECTED, +#if (NRF51_SERIAL_USE_HWFLOWCTRL == TRUE) + .rts_pad = NRF51_SERIAL_PAD_DISCONNECTED, + .cts_pad = NRF51_SERIAL_PAD_DISCONNECTED, +#endif }; /*===========================================================================*/ @@ -68,7 +72,6 @@ static const SerialConfig default_config = { */ static void configure_uart(const SerialConfig *config) { - /* TODO: Add support for CTS/RTS! */ uint32_t speed = UART_BAUDRATE_BAUDRATE_Baud250000; switch (config->speed) { @@ -100,6 +103,16 @@ static void configure_uart(const SerialConfig *config) palSetPadMode(IOPORT1, config->rx_pad, PAL_MODE_INPUT); NRF_UART0->PSELRXD = config->rx_pad; } +#if (NRF51_SERIAL_USE_HWFLOWCTRL == TRUE) + if (config->rts_pad != NRF51_SERIAL_PAD_DISCONNECTED) { + palSetPadMode(IOPORT1, config->rts_pad, PAL_MODE_OUTPUT_PUSHPULL); + NRF_UART0->PSELRTS = config->rts_pad; + } + if (config->cts_pad != NRF51_SERIAL_PAD_DISCONNECTED) { + palSetPadMode(IOPORT1, config->cts_pad, PAL_MODE_INPUT); + NRF_UART0->PSELCTS = config->cts_pad; + } +#endif NRF_UART0->BAUDRATE = speed; NRF_UART0->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos); @@ -107,10 +120,21 @@ static void configure_uart(const SerialConfig *config) NRF_UART0->EVENTS_RXDRDY = 0; NRF_UART0->EVENTS_TXDRDY = 0; - NRF_UART0->CONFIG &= ~(UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos); +#if (NRF51_SERIAL_USE_HWFLOWCTRL == TRUE) + if ((config->rts_pad < TOTAL_GPIO_PADS) || + (config->cts_pad < TOTAL_GPIO_PADS)) { + NRF_UART0->CONFIG |= UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos; + } else { + NRF_UART0->CONFIG &= ~(UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos); + } + NRF_UART0->PSELRTS = config->rts_pad; + NRF_UART0->PSELCTS = config->cts_pad; +#else + NRF_UART0->CONFIG &= ~(UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos); - NRF_UART0->PSELRTS = NRF51_SERIAL_PAD_DISCONNECTED; - NRF_UART0->PSELCTS = NRF51_SERIAL_PAD_DISCONNECTED; + NRF_UART0->PSELRTS = NRF51_SERIAL_PAD_DISCONNECTED; + NRF_UART0->PSELCTS = NRF51_SERIAL_PAD_DISCONNECTED; +#endif if (config->rx_pad != NRF51_SERIAL_PAD_DISCONNECTED) { while (NRF_UART0->EVENTS_RXDRDY != 0) { diff --git a/os/hal/ports/NRF51/NRF51822/serial_lld.h b/os/hal/ports/NRF51/NRF51822/serial_lld.h index 8da3998..5fc5e87 100644 --- a/os/hal/ports/NRF51/NRF51822/serial_lld.h +++ b/os/hal/ports/NRF51/NRF51822/serial_lld.h @@ -39,6 +39,16 @@ * @name PLATFORM configuration options * @{ */ +/** + * @brief SD flow control enable switch. + * @details If set to @p TRUE the support for hardware flow control + * is included. + * @note The default is @p FALSE. + */ +#if !defined(NRF51_SERIAL_USE_HWFLOWCTRL) || defined(__DOXYGEN__) +#define NRF51_SERIAL_USE_HWFLOWCTRL FALSE +#endif + /** * @brief SD1 driver enable switch. * @details If set to @p TRUE the support for SD1 is included. @@ -85,6 +95,10 @@ typedef struct { /* End of the mandatory fields.*/ uint32_t tx_pad; uint32_t rx_pad; +#if (NRF51_SERIAL_USE_HWFLOWCTRL == TRUE) + uint32_t rts_pad; + uint32_t cts_pad; +#endif } SerialConfig; /** -- cgit v1.2.3 From 3c7efdb21f8c9196aa6d2ff5c21e111ad6e03907 Mon Sep 17 00:00:00 2001 From: Stephane D'Alu Date: Tue, 2 Feb 2016 21:58:31 +0100 Subject: cleaned up PINs configuration and selection --- os/hal/ports/NRF51/NRF51822/serial_lld.c | 41 +++++++++++++++++++------------- 1 file changed, 25 insertions(+), 16 deletions(-) (limited to 'os/hal/ports') diff --git a/os/hal/ports/NRF51/NRF51822/serial_lld.c b/os/hal/ports/NRF51/NRF51822/serial_lld.c index bddc4c3..1451857 100644 --- a/os/hal/ports/NRF51/NRF51822/serial_lld.c +++ b/os/hal/ports/NRF51/NRF51822/serial_lld.c @@ -94,47 +94,56 @@ static void configure_uart(const SerialConfig *config) default: osalDbgAssert(0, "invalid baudrate"); break; }; - /* Configure PINs */ + /* Configure PINs mode */ if (config->tx_pad != NRF51_SERIAL_PAD_DISCONNECTED) { palSetPadMode(IOPORT1, config->tx_pad, PAL_MODE_OUTPUT_PUSHPULL); - NRF_UART0->PSELTXD = config->tx_pad; } if (config->rx_pad != NRF51_SERIAL_PAD_DISCONNECTED) { palSetPadMode(IOPORT1, config->rx_pad, PAL_MODE_INPUT); - NRF_UART0->PSELRXD = config->rx_pad; } #if (NRF51_SERIAL_USE_HWFLOWCTRL == TRUE) if (config->rts_pad != NRF51_SERIAL_PAD_DISCONNECTED) { palSetPadMode(IOPORT1, config->rts_pad, PAL_MODE_OUTPUT_PUSHPULL); - NRF_UART0->PSELRTS = config->rts_pad; } if (config->cts_pad != NRF51_SERIAL_PAD_DISCONNECTED) { palSetPadMode(IOPORT1, config->cts_pad, PAL_MODE_INPUT); - NRF_UART0->PSELCTS = config->cts_pad; } #endif + + /* Select PINs used by UART */ + NRF_UART0->PSELTXD = config->tx_pad; + NRF_UART0->PSELRXD = config->rx_pad; +#if (NRF51_SERIAL_USE_HWFLOWCTRL == TRUE) + NRF_UART0->PSELRTS = config->rts_pad; + NRF_UART0->PSELCTS = config->cts_pad; +#else + NRF_UART0->PSELRTS = NRF51_SERIAL_PAD_DISCONNECTED; + NRF_UART0->PSELCTS = NRF51_SERIAL_PAD_DISCONNECTED; +#endif + /* Set baud rate */ NRF_UART0->BAUDRATE = speed; + + /* Set parity */ NRF_UART0->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos); - NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Enabled; - NRF_UART0->EVENTS_RXDRDY = 0; - NRF_UART0->EVENTS_TXDRDY = 0; + /* Set flow control */ #if (NRF51_SERIAL_USE_HWFLOWCTRL == TRUE) if ((config->rts_pad < TOTAL_GPIO_PADS) || (config->cts_pad < TOTAL_GPIO_PADS)) { - NRF_UART0->CONFIG |= UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos; + NRF_UART0->CONFIG |= UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos; } else { - NRF_UART0->CONFIG &= ~(UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos); + NRF_UART0->CONFIG &= ~(UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos); } - NRF_UART0->PSELRTS = config->rts_pad; - NRF_UART0->PSELCTS = config->cts_pad; #else - NRF_UART0->CONFIG &= ~(UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos); - - NRF_UART0->PSELRTS = NRF51_SERIAL_PAD_DISCONNECTED; - NRF_UART0->PSELCTS = NRF51_SERIAL_PAD_DISCONNECTED; + NRF_UART0->CONFIG &= ~(UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos); #endif + + /* Enable UART and clear events */ + NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Enabled; + NRF_UART0->EVENTS_RXDRDY = 0; + NRF_UART0->EVENTS_TXDRDY = 0; + if (config->rx_pad != NRF51_SERIAL_PAD_DISCONNECTED) { while (NRF_UART0->EVENTS_RXDRDY != 0) { -- cgit v1.2.3 From 5740779b7bc9073196154ec93c3f42488504c014 Mon Sep 17 00:00:00 2001 From: Stephane D'Alu Date: Tue, 2 Feb 2016 22:03:37 +0100 Subject: updated comments --- os/hal/ports/NRF51/NRF51822/serial_lld.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'os/hal/ports') diff --git a/os/hal/ports/NRF51/NRF51822/serial_lld.c b/os/hal/ports/NRF51/NRF51822/serial_lld.c index 1451857..550a070 100644 --- a/os/hal/ports/NRF51/NRF51822/serial_lld.c +++ b/os/hal/ports/NRF51/NRF51822/serial_lld.c @@ -124,10 +124,10 @@ static void configure_uart(const SerialConfig *config) /* Set baud rate */ NRF_UART0->BAUDRATE = speed; - /* Set parity */ + /* Set config */ NRF_UART0->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos); - /* Set flow control */ + /* Adjust flow control */ #if (NRF51_SERIAL_USE_HWFLOWCTRL == TRUE) if ((config->rts_pad < TOTAL_GPIO_PADS) || (config->cts_pad < TOTAL_GPIO_PADS)) { -- cgit v1.2.3