[U-Boot] [PATCH v2] stm32f4: add serial print port

kunhuahuang huangkunhua at gmail.com
Mon Apr 27 21:01:19 CEST 2015


Add the stm32F4 board's serial ports support.
User can use it easily.
The user only need to edit the number of the usart.
The patch also fix the serial print out.

Last, this version of patch fix the first patch checkpatch.pl error.
Thanks to Kamil Lulko.

Signed-off-by: kunhuahuang <huangkunhua at gmail.com>
---
 arch/arm/include/asm/arch-stm32f4/gpio.h           | 32 +++++++++
 board/st/stm32f429-discovery/stm32f429-discovery.c | 16 ++---
 drivers/serial/serial_stm32.c                      | 75 ++++++++++++++++++----
 include/configs/stm32f429-discovery.h              | 10 ++-
 4 files changed, 111 insertions(+), 22 deletions(-)

diff --git a/arch/arm/include/asm/arch-stm32f4/gpio.h b/arch/arm/include/asm/arch-stm32f4/gpio.h
index 7cd866e..dd33b96 100644
--- a/arch/arm/include/asm/arch-stm32f4/gpio.h
+++ b/arch/arm/include/asm/arch-stm32f4/gpio.h
@@ -11,6 +11,38 @@
 #ifndef _STM32_GPIO_H_
 #define _STM32_GPIO_H_
 
+#if (CONFIG_STM32_USART == 1)
+#define STM32_GPIO_PORT_X   STM32_GPIO_PORT_A
+#define STM32_GPIO_PIN_TX   STM32_GPIO_PIN_9
+#define STM32_GPIO_PIN_RX   STM32_GPIO_PIN_10
+#define STM32_GPIO_USART    STM32_GPIO_AF7
+
+#elif (CONFIG_STM32_USART == 2)
+#define STM32_GPIO_PORT_X   STM32_GPIO_PORT_D
+#define STM32_GPIO_PIN_TX   STM32_GPIO_PIN_5
+#define STM32_GPIO_PIN_RX   STM32_GPIO_PIN_6
+#define STM32_GPIO_USART    STM32_GPIO_AF7
+
+#elif (CONFIG_STM32_USART == 3)
+#define STM32_GPIO_PORT_X   STM32_GPIO_PORT_C
+#define STM32_GPIO_PIN_TX   STM32_GPIO_PIN_10
+#define STM32_GPIO_PIN_RX   STM32_GPIO_PIN_11
+#define STM32_GPIO_USART    STM32_GPIO_AF7
+
+#elif (CONFIG_STM32_USART == 6)
+#define STM32_GPIO_PORT_X   STM32_GPIO_PORT_G
+#define STM32_GPIO_PIN_TX   STM32_GPIO_PIN_14
+#define STM32_GPIO_PIN_RX   STM32_GPIO_PIN_9
+#define STM32_GPIO_USART    STM32_GPIO_AF8
+
+#else
+#define STM32_GPIO_PORT_X   STM32_GPIO_PORT_A
+#define STM32_GPIO_PIN_TX   STM32_GPIO_PIN_9
+#define STM32_GPIO_PIN_RX   STM32_GPIO_PIN_10
+#define STM32_GPIO_USART    STM32_GPIO_AF7
+
+#endif
+
 enum stm32_gpio_port {
 	STM32_GPIO_PORT_A = 0,
 	STM32_GPIO_PORT_B,
diff --git a/board/st/stm32f429-discovery/stm32f429-discovery.c b/board/st/stm32f429-discovery/stm32f429-discovery.c
index 2c4830f..2dd5d93 100644
--- a/board/st/stm32f429-discovery/stm32f429-discovery.c
+++ b/board/st/stm32f429-discovery/stm32f429-discovery.c
@@ -33,21 +33,21 @@ const struct stm32_gpio_ctl gpio_ctl_usart = {
 	.otype = STM32_GPIO_OTYPE_PP,
 	.speed = STM32_GPIO_SPEED_50M,
 	.pupd = STM32_GPIO_PUPD_UP,
-	.af = STM32_GPIO_AF7
+	.af = STM32_GPIO_USART
 };
 
-static const struct stm32_gpio_dsc usart1_gpio[] = {
-	{STM32_GPIO_PORT_A, STM32_GPIO_PIN_9},	/* TX */
-	{STM32_GPIO_PORT_A, STM32_GPIO_PIN_10},	/* RX */
+static const struct stm32_gpio_dsc usart_gpio[] = {
+	{STM32_GPIO_PORT_X, STM32_GPIO_PIN_TX},	/* TX */
+	{STM32_GPIO_PORT_X, STM32_GPIO_PIN_RX},	/* RX */
 };
 
-int uart1_setup_gpio(void)
+int uart_setup_gpio(void)
 {
 	int i;
 	int rv = 0;
 
-	for (i = 0; i < ARRAY_SIZE(usart1_gpio); i++) {
-		rv = stm32_gpio_config(&usart1_gpio[i], &gpio_ctl_usart);
+	for (i = 0; i < ARRAY_SIZE(usart_gpio); i++) {
+		rv = stm32_gpio_config(&usart_gpio[i], &gpio_ctl_usart);
 		if (rv)
 			goto out;
 	}
@@ -272,7 +272,7 @@ int board_early_init_f(void)
 {
 	int res;
 
-	res = uart1_setup_gpio();
+	res = uart_setup_gpio();
 	if (res)
 		return res;
 
diff --git a/drivers/serial/serial_stm32.c b/drivers/serial/serial_stm32.c
index 3c80096..8b2830b 100644
--- a/drivers/serial/serial_stm32.c
+++ b/drivers/serial/serial_stm32.c
@@ -10,11 +10,34 @@
 #include <serial.h>
 #include <asm/arch/stm32.h>
 
+/*
+ * Set up the usart port
+ */
+#if (CONFIG_STM32_USART >= 1) && (CONFIG_STM32_USART <= 6)
+#define USART_PORT	(CONFIG_STM32_USART - 1)
+#else
+#define USART_PORT	0
+#endif
+/*
+ * Set up the usart base address
+ *
+ * --STM32_USARTD_BASE means default setting
+ */
 #define STM32_USART1_BASE	(STM32_APB2PERIPH_BASE + 0x1000)
-#define RCC_APB2ENR_USART1EN	(1 << 4)
-
-#define USART_BASE		STM32_USART1_BASE
-#define RCC_USART_ENABLE	RCC_APB2ENR_USART1EN
+#define STM32_USART2_BASE	(STM32_APB1PERIPH_BASE + 0x4400)
+#define STM32_USART3_BASE	(STM32_APB1PERIPH_BASE + 0x4800)
+#define STM32_USART6_BASE	(STM32_APB2PERIPH_BASE + 0x1400)
+#define STM32_USARTD_BASE	STM32_USART1_BASE
+/*
+ * RCC USART specific definitions
+ *
+ * --RCC_ENR_USARTDEN means default setting
+ */
+#define RCC_ENR_USART1EN	(1 << 4)
+#define RCC_ENR_USART2EN	(1 << 17)
+#define RCC_ENR_USART3EN	(1 << 18)
+#define RCC_ENR_USART6EN	(1 <<  5)
+#define RCC_ENR_USARTDEN	RCC_ENR_USART1EN
 
 struct stm32_serial {
 	u32 sr;
@@ -39,6 +62,24 @@ struct stm32_serial {
 
 DECLARE_GLOBAL_DATA_PTR;
 
+static const unsigned long usart_base[] = {
+	STM32_USART1_BASE,
+	STM32_USART2_BASE,
+	STM32_USART3_BASE,
+	STM32_USARTD_BASE,
+	STM32_USARTD_BASE,
+	STM32_USART6_BASE
+};
+
+static const unsigned long rcc_enr_en[] = {
+	RCC_ENR_USART1EN,
+	RCC_ENR_USART2EN,
+	RCC_ENR_USART3EN,
+	RCC_ENR_USARTDEN,
+	RCC_ENR_USARTDEN,
+	RCC_ENR_USART6EN
+};
+
 static void stm32_serial_setbrg(void)
 {
 	serial_init();
@@ -46,14 +87,17 @@ static void stm32_serial_setbrg(void)
 
 static int stm32_serial_init(void)
 {
-	struct stm32_serial *usart = (struct stm32_serial *)USART_BASE;
+	struct stm32_serial *usart =
+		(struct stm32_serial *)usart_base[USART_PORT];
 	u32 clock, int_div, frac_div, tmp;
 
-	if ((USART_BASE & STM32_BUS_MASK) == STM32_APB1PERIPH_BASE) {
-		setbits_le32(&STM32_RCC->apb1enr, RCC_USART_ENABLE);
+	if ((usart_base[USART_PORT] & STM32_BUS_MASK) ==
+			STM32_APB1PERIPH_BASE) {
+		setbits_le32(&STM32_RCC->apb1enr, rcc_enr_en[USART_PORT]);
 		clock = clock_get(CLOCK_APB1);
-	} else if ((USART_BASE & STM32_BUS_MASK) == STM32_APB2PERIPH_BASE) {
-		setbits_le32(&STM32_RCC->apb2enr, RCC_USART_ENABLE);
+	} else if ((usart_base[USART_PORT] & STM32_BUS_MASK) ==
+			STM32_APB2PERIPH_BASE) {
+		setbits_le32(&STM32_RCC->apb2enr, rcc_enr_en[USART_PORT]);
 		clock = clock_get(CLOCK_APB2);
 	} else {
 		return -1;
@@ -72,7 +116,8 @@ static int stm32_serial_init(void)
 
 static int stm32_serial_getc(void)
 {
-	struct stm32_serial *usart = (struct stm32_serial *)USART_BASE;
+	struct stm32_serial *usart =
+		(struct stm32_serial *)usart_base[USART_PORT];
 	while ((readl(&usart->sr) & USART_SR_FLAG_RXNE) == 0)
 		;
 	return readl(&usart->dr);
@@ -80,7 +125,12 @@ static int stm32_serial_getc(void)
 
 static void stm32_serial_putc(const char c)
 {
-	struct stm32_serial *usart = (struct stm32_serial *)USART_BASE;
+	struct stm32_serial *usart =
+		(struct stm32_serial *)usart_base[USART_PORT];
+
+	if (c == '\n')
+		stm32_serial_putc('\r');
+
 	while ((readl(&usart->sr) & USART_SR_FLAG_TXE) == 0)
 		;
 	writel(c, &usart->dr);
@@ -88,7 +138,8 @@ static void stm32_serial_putc(const char c)
 
 static int stm32_serial_tstc(void)
 {
-	struct stm32_serial *usart = (struct stm32_serial *)USART_BASE;
+	struct stm32_serial *usart =
+		(struct stm32_serial *)usart_base[USART_PORT];
 	u8 ret;
 
 	ret = readl(&usart->sr) & USART_SR_FLAG_RXNE;
diff --git a/include/configs/stm32f429-discovery.h b/include/configs/stm32f429-discovery.h
index 7f569fd..84cc19d 100644
--- a/include/configs/stm32f429-discovery.h
+++ b/include/configs/stm32f429-discovery.h
@@ -50,8 +50,14 @@
 
 #define CONFIG_STM32_GPIO
 #define CONFIG_STM32_SERIAL
-
-#define CONFIG_STM32_USART1
+/*
+ * Configuration of the USART
+ * 1:   TX:PA9  PX:PA10
+ * 2:   TX:PD5  RX:PD6
+ * 3:   TX:PC10 RX:PC11
+ * 6:   TX:PC6  RX:PC7
+ */
+#define CONFIG_STM32_USART		1
 
 #define CONFIG_STM32_HSE_HZ		8000000
 
-- 
1.9.1



More information about the U-Boot mailing list