2929
3030#include <stdint.h>
3131
32- /* Driver hardcoded to work on UART3 (PD8/PD9) */
33- #define UART3 (0x40004800)
34- #define UART3_PIN_AF 7
35- #define UART3_RX_PIN 9
36- #define UART3_TX_PIN 8
37-
38- #define UART3_SR (*(volatile uint32_t *)(UART3))
39- #define UART3_DR (*(volatile uint32_t *)(UART3 + 0x04))
40- #define UART3_BRR (*(volatile uint32_t *)(UART3 + 0x08))
41- #define UART3_CR1 (*(volatile uint32_t *)(UART3 + 0x0c))
42- #define UART3_CR2 (*(volatile uint32_t *)(UART3 + 0x10))
43-
32+ /* Common UART Config */
33+ #if !defined(USE_UART1 ) && !defined(USE_UART3 )
34+ #define USE_UART3
35+ #endif
36+ #define UART_PIN_AF 7
4437#define UART_CR1_UART_ENABLE (1 << 13)
4538#define UART_CR1_SYMBOL_LEN (1 << 12)
4639#define UART_CR1_PARITY_ENABLED (1 << 10)
5144#define UART_SR_TX_EMPTY (1 << 7)
5245#define UART_SR_RX_NOTEMPTY (1 << 5)
5346
54-
47+ #ifndef CLOCK_SPEED
5548#define CLOCK_SPEED (168000000)
49+ #endif
50+
51+ /* Common GPIO Config */
52+ #define GPIO_MODE_AF (2)
5653
57- #define APB1_CLOCK_ER (*(volatile uint32_t *)(0x40023840))
58- #define UART3_APB1_CLOCK_ER_VAL (1 << 18)
54+ /* UART1 Config */
55+ #ifdef USE_UART1
56+ #define UART_RX_PIN 7
57+ #define UART_TX_PIN 6
58+
59+ #define UART1 (0x40011000)
60+ #define UART_SR (*(volatile uint32_t *)(UART1))
61+ #define UART_DR (*(volatile uint32_t *)(UART1 + 0x04))
62+ #define UART_BRR (*(volatile uint32_t *)(UART1 + 0x08))
63+ #define UART_CR1 (*(volatile uint32_t *)(UART1 + 0x0c))
64+ #define UART_CR2 (*(volatile uint32_t *)(UART1 + 0x10))
65+
66+ #define UART_CLOCK_ER (*(volatile uint32_t *)(0x40023844))
67+ #define UART_CLOCK_ER_VAL (1 << 4)
68+
69+ #define GPIO_CLOCK_ER (*(volatile uint32_t *)(0x40023830))
70+ #define GPIO_CLOCK_ER_VAL (1 << 1)
71+ #define GPIOB_BASE 0x40020400
72+ #define GPIO_MODE (*(volatile uint32_t *)(GPIOB_BASE + 0x00))
73+ #define GPIO_AFL (*(volatile uint32_t *)(GPIOB_BASE + 0x20))
74+ #define GPIO_AFH (*(volatile uint32_t *)(GPIOB_BASE + 0x24))
75+ #endif
76+
77+ /* UART3 Config */
78+ #ifdef USE_UART3
79+ #define UART_RX_PIN 9
80+ #define UART_TX_PIN 8
5981
60- #define AHB1_CLOCK_ER (*(volatile uint32_t *)(0x40023830))
61- #define GPIOD_AHB1_CLOCK_ER (1 << 3)
82+ #define UART3 (0x40004800)
83+ #define UART_SR (*(volatile uint32_t *)(UART3))
84+ #define UART_DR (*(volatile uint32_t *)(UART3 + 0x04))
85+ #define UART_BRR (*(volatile uint32_t *)(UART3 + 0x08))
86+ #define UART_CR1 (*(volatile uint32_t *)(UART3 + 0x0c))
87+ #define UART_CR2 (*(volatile uint32_t *)(UART3 + 0x10))
88+
89+ #define UART_CLOCK_ER (*(volatile uint32_t *)(0x40023840))
90+ #define UART_CLOCK_ER_VAL (1 << 18)
91+
92+ #define GPIO_CLOCK_ER (*(volatile uint32_t *)(0x40023830))
93+ #define GPIO_CLOCK_ER_VAL (1 << 3)
6294#define GPIOD_BASE 0x40020c00
63- #define GPIOD_MODE (*(volatile uint32_t *)(GPIOD_BASE + 0x00))
64- #define GPIOD_AFL (*(volatile uint32_t *)(GPIOD_BASE + 0x20))
65- #define GPIOD_AFH (*(volatile uint32_t *)(GPIOD_BASE + 0x24))
66- #define GPIO_MODE_AF (2)
95+ #define GPIO_MODE (*(volatile uint32_t *)(GPIOD_BASE + 0x00))
96+ #define GPIO_AFL (*(volatile uint32_t *)(GPIOD_BASE + 0x20))
97+ #define GPIO_AFH (*(volatile uint32_t *)(GPIOD_BASE + 0x24))
98+ #endif
99+
67100
68101static void uart_pins_setup (void )
69102{
70103 uint32_t reg ;
71- AHB1_CLOCK_ER |= GPIOD_AHB1_CLOCK_ER ;
104+ GPIO_CLOCK_ER |= GPIO_CLOCK_ER_VAL ;
72105 /* Set mode = AF */
73- reg = GPIOD_MODE & ~ (0x03 << (UART3_RX_PIN * 2 ));
74- GPIOD_MODE = reg | (2 << (UART3_RX_PIN * 2 ));
75- reg = GPIOD_MODE & ~ (0x03 << (UART3_TX_PIN * 2 ));
76- GPIOD_MODE = reg | (2 << (UART3_TX_PIN * 2 ));
77-
78- /* Alternate function: use high pins (8 and 9) */
79- reg = GPIOD_AFH & ~(0xf << ((UART3_TX_PIN - 8 ) * 4 ));
80- GPIOD_AFH = reg | (UART3_PIN_AF << ((UART3_TX_PIN - 8 ) * 4 ));
81- reg = GPIOD_AFH & ~(0xf << ((UART3_RX_PIN - 8 ) * 4 ));
82- GPIOD_AFH = reg | (UART3_PIN_AF << ((UART3_RX_PIN - 8 ) * 4 ));
106+ reg = GPIO_MODE & ~ (0x03 << (UART_RX_PIN * 2 ));
107+ GPIO_MODE = reg | (2 << (UART_RX_PIN * 2 ));
108+ reg = GPIO_MODE & ~ (0x03 << (UART_TX_PIN * 2 ));
109+ GPIO_MODE = reg | (2 << (UART_TX_PIN * 2 ));
110+
111+ /* The alternate function register is split across two 32bit
112+ * registers (AFL, AFH). AFL covers pins 0 through 7, and
113+ * AFH covers pins 8 through 15. The code below determines
114+ * which register to use at compile time based on the chosen
115+ * pin number
116+ */
117+
118+ #if UART_TX_PIN > 7
119+ reg = GPIO_AFH & ~(0xf << ((UART_TX_PIN - 8 ) * 4 ));
120+ GPIO_AFH = reg | (UART_PIN_AF << ((UART_TX_PIN - 8 ) * 4 ));
121+ #else
122+ reg = GPIO_AFL & ~(0xf << (UART_TX_PIN * 4 ));
123+ GPIO_AFL = reg | (UART_PIN_AF << (UART_TX_PIN * 4 ));
124+ #endif
125+
126+ #if UART_RX_PIN > 7
127+ reg = GPIO_AFH & ~(0xf << ((UART_RX_PIN - 8 ) * 4 ));
128+ GPIO_AFH = reg | (UART_PIN_AF << ((UART_RX_PIN - 8 ) * 4 ));
129+ #else
130+ reg = GPIO_AFL & ~(0xf << (UART_RX_PIN * 4 ));
131+ GPIO_AFL = reg | (UART_PIN_AF << (UART_RX_PIN * 4 ));
132+ #endif
133+
83134}
84135
85136int uart_tx (const uint8_t c )
86137{
87138 uint32_t reg ;
88139 do {
89- reg = UART3_SR ;
140+ reg = UART_SR ;
90141 } while ((reg & UART_SR_TX_EMPTY ) == 0 );
91- UART3_DR = c ;
142+ UART_DR = c ;
92143 return 1 ;
93144}
94145
95146int uart_rx (uint8_t * c )
96147{
97- volatile uint32_t reg = UART3_SR ;
148+ volatile uint32_t reg = UART_SR ;
98149 if ((reg & UART_SR_RX_NOTEMPTY ) != 0 ) {
99- reg = UART3_DR ;
150+ reg = UART_DR ;
100151 * c = (uint8_t )(reg & 0xff );
101152 return 1 ;
102153 }
@@ -109,42 +160,42 @@ int uart_init(uint32_t bitrate, uint8_t data, char parity, uint8_t stop)
109160 /* Enable pins and configure for AF7 */
110161 uart_pins_setup ();
111162 /* Turn on the device */
112- APB1_CLOCK_ER |= UART3_APB1_CLOCK_ER_VAL ;
113- UART3_CR1 &= ~(UART_CR1_UART_ENABLE );
163+ UART_CLOCK_ER |= UART_CLOCK_ER_VAL ;
164+ UART_CR1 &= ~(UART_CR1_UART_ENABLE );
114165
115166 /* Configure for TX + RX */
116- UART3_CR1 |= (UART_CR1_TX_ENABLE | UART_CR1_RX_ENABLE );
167+ UART_CR1 |= (UART_CR1_TX_ENABLE | UART_CR1_RX_ENABLE );
117168
118169 /* Configure clock */
119- UART3_BRR = CLOCK_SPEED / bitrate ;
170+ UART_BRR = CLOCK_SPEED / bitrate ;
120171
121172 /* Configure data bits */
122173 if (data == 8 )
123- UART3_CR1 &= ~UART_CR1_SYMBOL_LEN ;
174+ UART_CR1 &= ~UART_CR1_SYMBOL_LEN ;
124175 else
125- UART3_CR1 |= UART_CR1_SYMBOL_LEN ;
176+ UART_CR1 |= UART_CR1_SYMBOL_LEN ;
126177
127178 /* Configure parity */
128179 switch (parity ) {
129180 case 'O' :
130- UART3_CR1 |= UART_CR1_PARITY_ODD ;
181+ UART_CR1 |= UART_CR1_PARITY_ODD ;
131182 /* fall through to enable parity */
132183 /* FALL THROUGH */
133184 case 'E' :
134- UART3_CR1 |= UART_CR1_PARITY_ENABLED ;
185+ UART_CR1 |= UART_CR1_PARITY_ENABLED ;
135186 break ;
136187 default :
137- UART3_CR1 &= ~(UART_CR1_PARITY_ENABLED | UART_CR1_PARITY_ODD );
188+ UART_CR1 &= ~(UART_CR1_PARITY_ENABLED | UART_CR1_PARITY_ODD );
138189 }
139190 /* Set stop bits */
140- reg = UART3_CR2 & ~UART_CR2_STOPBITS ;
191+ reg = UART_CR2 & ~UART_CR2_STOPBITS ;
141192 if (stop > 1 )
142- UART3_CR2 = reg & (2 << 12 );
193+ UART_CR2 = reg & (2 << 12 );
143194 else
144- UART3_CR2 = reg ;
195+ UART_CR2 = reg ;
145196
146197 /* Turn on uart */
147- UART3_CR1 |= UART_CR1_UART_ENABLE ;
198+ UART_CR1 |= UART_CR1_UART_ENABLE ;
148199 return 0 ;
149200}
150201
0 commit comments