From 137836fb19623735351e411f30d7d6bd29e28012 Mon Sep 17 00:00:00 2001
From: Stephane D'Alu <sdalu@sdalu.com>
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(-)

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