From 751485fe6b7eeb93f171c2f2b0417803f6e78c05 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Mon, 3 Aug 2015 00:23:47 +0100
Subject: [PATCH] stmhal: Add support for USART1 and conditional pins in
 make-pins.py.

Thanks to Dave Hylands for the patch.
---
 stmhal/Makefile            |  4 ++++
 stmhal/boards/make-pins.py | 23 +++++++++++++----------
 stmhal/uart.c              | 16 ++++++++++++++++
 3 files changed, 33 insertions(+), 10 deletions(-)

diff --git a/stmhal/Makefile b/stmhal/Makefile
index 5d35bfa4d..80a7c503a 100644
--- a/stmhal/Makefile
+++ b/stmhal/Makefile
@@ -310,6 +310,10 @@ GEN_CDCINF_HEADER = $(HEADER_BUILD)/pybcdc_inf.h
 # which source files might need it.
 $(OBJ): | $(HEADER_BUILD)/pins.h
 
+# With conditional pins, we may need to regenerate qstrdefs.h when config
+# options change.
+$(HEADER_BUILD)/qstrdefs.generated.h: boards/$(BOARD)/mpconfigboard.h
+
 $(BUILD)/main.o: $(GEN_CDCINF_HEADER)
 
 # Use a pattern rule here so that make will only call make-pins.py once to make
diff --git a/stmhal/boards/make-pins.py b/stmhal/boards/make-pins.py
index d9928642f..822a09340 100755
--- a/stmhal/boards/make-pins.py
+++ b/stmhal/boards/make-pins.py
@@ -24,6 +24,7 @@ CONDITIONAL_VAR = {
     'UART'  : 'MICROPY_HW_UART{num}_PORT',
     'UART5' : 'MICROPY_HW_UART5_TX_PORT',
     'USART' : 'MICROPY_HW_UART{num}_PORT',
+    'USART1': 'MICROPY_HW_UART1_TX_PORT',
 }
 
 def parse_port_pin(name_str):
@@ -55,20 +56,22 @@ def conditional_var(name_num):
     # Try the specific instance first. For example, if name_num is UART4_RX
     # then try UART4 first, and then try UART second.
     name, num = split_name_num(name_num)
-    var = None
+    var = []
+    if name in CONDITIONAL_VAR:
+        var.append(CONDITIONAL_VAR[name].format(num=num))
     if name_num in CONDITIONAL_VAR:
-        var = CONDITIONAL_VAR[name_num]
-    elif name in CONDITIONAL_VAR:
-        var = CONDITIONAL_VAR[name]
-    if var:
-        return var.format(num=num)
+        var.append(CONDITIONAL_VAR[name_num])
+    return var
 
 def print_conditional_if(cond_var, file=None):
     if cond_var:
-        if cond_var.find('ENABLE') >= 0:
-            print('#if defined({0}) && {0}'.format(cond_var), file=file)
-        else:
-            print('#if defined({0})'.format(cond_var), file=file)
+        cond_str = []
+        for var in cond_var:
+            if var.find('ENABLE') >= 0:
+                cond_str.append('(defined({0}) && {0})'.format(var))
+            else:
+                cond_str.append('defined({0})'.format(var))
+        print('#if ' + ' || '.join(cond_str), file=file)
 
 def print_conditional_endif(cond_var, file=None):
     if cond_var:
diff --git a/stmhal/uart.c b/stmhal/uart.c
index 01771be3f..ebd0b2516 100644
--- a/stmhal/uart.c
+++ b/stmhal/uart.c
@@ -132,6 +132,22 @@ STATIC bool uart_init2(pyb_uart_obj_t *uart_obj) {
             break;
         #endif
 
+        #if defined(MICROPY_HW_UART1_TX_PORT) && \
+            defined(MICROPY_HW_UART1_TX_PIN) && \
+            defined(MICROPY_HW_UART1_RX_PORT) && \
+            defined(MICROPY_HW_UART1_RX_PIN)
+        case PYB_UART_1:
+            UARTx = USART1;
+            irqn = USART1_IRQn;
+            GPIO_AF_UARTx = GPIO_AF7_USART1;
+            GPIO_Port  = MICROPY_HW_UART1_TX_PORT;
+            GPIO_Pin   = MICROPY_HW_UART1_TX_PIN;
+            GPIO_Port2 = MICROPY_HW_UART1_RX_PORT;
+            GPIO_Pin2  = MICROPY_HW_UART1_RX_PIN;
+            __USART1_CLK_ENABLE();
+            break;
+        #endif
+
         #if defined(MICROPY_HW_UART2_PORT) && defined(MICROPY_HW_UART2_PINS)
         // USART2 is on PA2/PA3 (CTS,RTS,CK on PA0,PA1,PA4), PD5/PD6 (CK on PD7)
         case PYB_UART_2:
-- 
GitLab