Skip to content

Commit c52980e

Browse files
committed
Correct baud calculation
1 parent b1f79f6 commit c52980e

File tree

9 files changed

+26
-31
lines changed

9 files changed

+26
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ static inline void uart_init(struct uart *uart, unsigned long baud) {
10311031
gpio_set_mode(rx, GPIO_MODE_AF);
10321032
gpio_set_af(rx, af);
10331033
uart->CR1 = 0; // Disable this UART
1034-
uart->BRR = FREQ / baud; // FREQ is a CPU frequency
1034+
uart->BRR = FREQ / baud; // FREQ is a UART bus frequency
10351035
uart->CR1 |= BIT(13) | BIT(2) | BIT(3); // Set UE, RE, TE
10361036
}
10371037
```

README_zh-CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ static inline void uart_init(struct uart *uart, unsigned long baud) {
807807
gpio_set_mode(rx, GPIO_MODE_AF);
808808
gpio_set_af(rx, af);
809809
uart->CR1 = 0; // Disable this UART
810-
uart->BRR = FREQ / baud; // FREQ is a CPU frequency
810+
uart->BRR = FREQ / baud; // FREQ is a UART bus frequency
811811
uart->CR1 |= BIT(13) | BIT(2) | BIT(3); // Set UE, RE, TE
812812
}
813813
```

step-3-uart/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static inline void uart_init(struct uart *uart, unsigned long baud) {
9090
gpio_set_mode(rx, GPIO_MODE_AF);
9191
gpio_set_af(rx, af);
9292
uart->CR1 = 0; // Disable this UART
93-
uart->BRR = FREQ / baud; // FREQ is a CPU frequency
93+
uart->BRR = FREQ / baud; // FREQ is a UART bus frequency
9494
uart->CR1 |= BIT(13) | BIT(2) | BIT(3); // Set UE, RE, TE
9595
}
9696

step-4-printf/mcu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static inline void uart_init(struct uart *uart, unsigned long baud) {
9494
gpio_set_mode(rx, GPIO_MODE_AF);
9595
gpio_set_af(rx, af);
9696
uart->CR1 = 0; // Disable this UART
97-
uart->BRR = FREQ / baud; // FREQ is a CPU frequency
97+
uart->BRR = FREQ / baud; // FREQ is a UART bus frequency
9898
uart->CR1 |= BIT(13) | BIT(2) | BIT(3); // Set UE, RE, TE
9999
}
100100

step-5-cmsis/mcu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static inline void uart_init(USART_TypeDef *uart, unsigned long baud) {
7474
gpio_set_mode(rx, GPIO_MODE_AF);
7575
gpio_set_af(rx, af);
7676
uart->CR1 = 0; // Disable this UART
77-
uart->BRR = FREQ / baud; // FREQ is a CPU frequency
77+
uart->BRR = FREQ / baud; // FREQ is a UART bus frequency
7878
uart->CR1 |= BIT(13) | BIT(2) | BIT(3); // Set UE, RE, TE
7979
}
8080

step-6-clock/main.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
#include "mcu.h"
55

66
static volatile uint32_t s_ticks;
7-
void SysTick_Handler(void) {
8-
s_ticks++;
9-
}
7+
void SysTick_Handler(void) { s_ticks++; }
108

119
int main(void) {
1210
uint16_t led = PIN('B', 7); // Blue LED
1311
clock_init(); // Run at 180Mhz
14-
systick_init(FREQ / 1000); // Tick every 1 ms
12+
systick_init(SYS_FREQUENCY / 1000); // Tick every 1 ms
1513
gpio_set_mode(led, GPIO_MODE_OUTPUT); // Set blue LED to output mode
1614
uart_init(UART3, 115200); // Initialise UART
1715
uint32_t timer = 0, period = 500; // Declare timer and 500ms period

step-6-clock/mcu.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
// 33.4: The AHB clock must be at least 25 MHz when Ethernet is used
2222
enum { APB1_PRE = 5 /* AHB clock / 4 */, APB2_PRE = 4 /* AHB clock / 2 */ };
2323
enum { PLL_HSI = 16, PLL_M = 8, PLL_N = 180, PLL_P = 2 }; // Run at 180 Mhz
24-
//#define PLL_FREQ PLL_HSI
25-
#define PLL_FREQ (PLL_HSI * PLL_N / PLL_M / PLL_P)
2624
#define FLASH_LATENCY 5
27-
#define FREQ (PLL_FREQ * 1000000)
25+
#define SYS_FREQUENCY ((PLL_HSI * PLL_N / PLL_M / PLL_P) * 1000000)
26+
#define APB2_FREQUENCY (SYS_FREQUENCY / (BIT(APB2_PRE - 3)))
27+
#define APB1_FREQUENCY (SYS_FREQUENCY / (BIT(APB1_PRE - 3)))
2828

2929
static inline void spin(volatile uint32_t count) {
3030
while (count--) asm("nop");
@@ -69,10 +69,11 @@ static inline void uart_init(USART_TypeDef *uart, unsigned long baud) {
6969
// https://www.st.com/resource/en/datasheet/stm32f429zi.pdf
7070
uint8_t af = 7; // Alternate function
7171
uint16_t rx = 0, tx = 0; // pins
72+
uint32_t freq = 0; // Bus frequency. UART1 is on APB2, rest on APB1
7273

73-
if (uart == UART1) RCC->APB2ENR |= BIT(4);
74-
if (uart == UART2) RCC->APB1ENR |= BIT(17);
75-
if (uart == UART3) RCC->APB1ENR |= BIT(18);
74+
if (uart == UART1) freq = APB2_FREQUENCY, RCC->APB2ENR |= BIT(4);
75+
if (uart == UART2) freq = APB1_FREQUENCY, RCC->APB1ENR |= BIT(17);
76+
if (uart == UART3) freq = APB1_FREQUENCY, RCC->APB1ENR |= BIT(18);
7677

7778
if (uart == UART1) tx = PIN('A', 9), rx = PIN('A', 10);
7879
if (uart == UART2) tx = PIN('A', 2), rx = PIN('A', 3);
@@ -83,7 +84,7 @@ static inline void uart_init(USART_TypeDef *uart, unsigned long baud) {
8384
gpio_set_mode(rx, GPIO_MODE_AF);
8485
gpio_set_af(rx, af);
8586
uart->CR1 = 0; // Disable this UART
86-
uart->BRR = FREQ / APB2_PRE / baud; // FREQ is a CPU frequency
87+
uart->BRR = freq / baud; // Set baud rate
8788
uart->CR1 |= BIT(13) | BIT(2) | BIT(3); // Set UE, RE, TE
8889
}
8990

step-7-webserver/nucleo-f429zi/main.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@
55
#include "mongoose.h"
66

77
static volatile uint32_t s_ticks;
8-
void SysTick_Handler(void) {
9-
s_ticks++;
10-
}
11-
12-
uint64_t mg_millis(void) { // Declare our own uptime function
13-
return s_ticks; // Return number of milliseconds since boot
14-
}
8+
void SysTick_Handler(void) { s_ticks++; } // IRQ handler
9+
uint64_t mg_millis(void) { return s_ticks; } // For Mongoose
1510

1611
int main(void) {
1712
uint16_t led = PIN('B', 7); // Blue LED
1813
clock_init(); // Run at 180Mhz
19-
systick_init(FREQ / 1000); // Tick every 1 ms
14+
systick_init(SYS_FREQUENCY / 1000); // Tick every 1 ms
2015
gpio_set_mode(led, GPIO_MODE_OUTPUT); // Set blue LED to output mode
2116
uart_init(UART3, 115200); // Initialise UART
2217
uint32_t timer = 0, period = 500; // Declare timer and 500ms period

step-7-webserver/nucleo-f429zi/mcu.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
// 33.4: The AHB clock must be at least 25 MHz when Ethernet is used
2323
enum { APB1_PRE = 5 /* AHB clock / 4 */, APB2_PRE = 4 /* AHB clock / 2 */ };
2424
enum { PLL_HSI = 16, PLL_M = 8, PLL_N = 180, PLL_P = 2 }; // Run at 180 Mhz
25-
//#define PLL_FREQ PLL_HSI
26-
#define PLL_FREQ (PLL_HSI * PLL_N / PLL_M / PLL_P)
2725
#define FLASH_LATENCY 5
28-
#define FREQ (PLL_FREQ * 1000000)
26+
#define SYS_FREQUENCY ((PLL_HSI * PLL_N / PLL_M / PLL_P) * 1000000)
27+
#define APB2_FREQUENCY (SYS_FREQUENCY / (BIT(APB2_PRE - 3)))
28+
#define APB1_FREQUENCY (SYS_FREQUENCY / (BIT(APB1_PRE - 3)))
2929

3030
static inline void spin(volatile uint32_t count) {
3131
while (count--) asm("nop");
@@ -86,10 +86,11 @@ static inline void uart_init(USART_TypeDef *uart, unsigned long baud) {
8686
// https://www.st.com/resource/en/datasheet/stm32f429zi.pdf
8787
uint8_t af = 7; // Alternate function
8888
uint16_t rx = 0, tx = 0; // pins
89+
uint32_t freq = 0; // Bus frequency. UART1 is on APB2, rest on APB1
8990

90-
if (uart == UART1) RCC->APB2ENR |= BIT(4);
91-
if (uart == UART2) RCC->APB1ENR |= BIT(17);
92-
if (uart == UART3) RCC->APB1ENR |= BIT(18);
91+
if (uart == UART1) freq = APB2_FREQUENCY, RCC->APB2ENR |= BIT(4);
92+
if (uart == UART2) freq = APB1_FREQUENCY, RCC->APB1ENR |= BIT(17);
93+
if (uart == UART3) freq = APB1_FREQUENCY, RCC->APB1ENR |= BIT(18);
9394

9495
if (uart == UART1) tx = PIN('A', 9), rx = PIN('A', 10);
9596
if (uart == UART2) tx = PIN('A', 2), rx = PIN('A', 3);
@@ -100,7 +101,7 @@ static inline void uart_init(USART_TypeDef *uart, unsigned long baud) {
100101
gpio_set_mode(rx, GPIO_MODE_AF);
101102
gpio_set_af(rx, af);
102103
uart->CR1 = 0; // Disable this UART
103-
uart->BRR = FREQ / APB2_PRE / baud; // FREQ is a CPU frequency
104+
uart->BRR = freq / baud; // Set baud rate
104105
uart->CR1 |= BIT(13) | BIT(2) | BIT(3); // Set UE, RE, TE
105106
}
106107

0 commit comments

Comments
 (0)