Skip to content

Commit dd4d96d

Browse files
committed
Various fixes
1 parent b58a868 commit dd4d96d

File tree

21 files changed

+63
-67
lines changed

21 files changed

+63
-67
lines changed

README.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Nucleo-F429ZI.
3131

3232
| Skeleton project | MCU datasheet | Board datasheet | Status |
3333
| --------- | ---------------- | ------------- | ------------ |
34-
| [STM32 Nucleo-F429ZI](step-7-webserver/nucleo-f429zi/) | [mcu datasheet](https://www.st.com/resource/en/reference_manual/dm00031020-stm32f405-415-stm32f407-417-stm32f427-437-and-stm32f429-439-advanced-arm-based-32-bit-mcus-stmicroelectronics.pdf) | [board datasheet](https//www.st.com/resource/en/user_manual/dm00244518-stm32-nucleo144-boards-mb1137-stmicroelectronics.pdf) | complete |
34+
| [STM32 Nucleo-F429ZI](step-7-webserver/nucleo-f429zi/) | [mcu datasheet](https://www.st.com/resource/en/reference_manual/dm00031020-stm32f405-415-stm32f407-417-stm32f427-437-and-stm32f429-439-advanced-arm-based-32-bit-mcus-stmicroelectronics.pdf) | [board datasheet](https://www.st.com/resource/en/user_manual/dm00244518-stm32-nucleo144-boards-mb1137-stmicroelectronics.pdf) | complete |
3535
| [TI EK-TM4C1294XL](step-7-webserver/ek-tm4c1294xl/) | [mcu datasheet](https://www.ti.com/lit/ds/symlink/tm4c1294ncpdt.pdf) | [board datasheet](https://www.ti.com/lit/ug/spmu365c/spmu365c.pdf) | complete |
3636
| [RP2040 Pico-W5500](step-7-webserver/pico-w5500/) | [mcu datasheet](https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf) | [board datasheet](https://docs.wiznet.io/Product/iEthernet/W5500/w5500-evb-pico) | complete |
3737

@@ -764,7 +764,7 @@ int main(void) {
764764
uint16_t led = PIN('B', 7); // Blue LED
765765
RCC->AHB1ENR |= BIT(PINBANK(led)); // Enable GPIO clock for LED
766766
gpio_set_mode(led, GPIO_MODE_OUTPUT); // Set blue LED to output mode
767-
for (;;) asm volatile("nop"); // Infinite loop
767+
for (;;) (void) 0; // Infinite loop
768768
return 0;
769769
}
770770
```
@@ -779,7 +779,7 @@ register (i.e. set pin low). Let's define an API function for that:
779779
```c
780780
static inline void gpio_write(uint16_t pin, bool val) {
781781
struct gpio *gpio = GPIO(PINBANK(pin));
782-
gpio->BSRR |= (1U << PINNO(pin)) << (val ? 0 : 16);
782+
gpio->BSRR = (1U << PINNO(pin)) << (val ? 0 : 16);
783783
}
784784
```
785785
@@ -789,7 +789,7 @@ a NOP instruction a given number of times:
789789
790790
```c
791791
static inline void spin(volatile uint32_t count) {
792-
while (count--) asm("nop");
792+
while (count--) (void) 0;
793793
}
794794
```
795795
@@ -932,7 +932,7 @@ Now we are ready to update our main loop and use a precise timer for LED blink.
932932
For example, let's use 250 milliseconds blinking interval:
933933
934934
```c
935-
uint32_t timer, period = 250; // Declare timer and 250ms period
935+
uint32_t timer, period = 500; // Declare timer and 500ms period
936936
for (;;) {
937937
if (timer_expired(&timer, period, s_ticks)) {
938938
static bool on; // This block is executed
@@ -1019,16 +1019,16 @@ Now we're ready to create a UART initialization API function:
10191019
#define FREQ 16000000 // CPU frequency, 16 Mhz
10201020
static inline void uart_init(struct uart *uart, unsigned long baud) {
10211021
// https://www.st.com/resource/en/datasheet/stm32f429zi.pdf
1022-
uint8_t af = 0; // Alternate function
1022+
uint8_t af = 7; // Alternate function
10231023
uint16_t rx = 0, tx = 0; // pins
10241024
10251025
if (uart == UART1) RCC->APB2ENR |= BIT(4);
10261026
if (uart == UART2) RCC->APB1ENR |= BIT(17);
10271027
if (uart == UART3) RCC->APB1ENR |= BIT(18);
10281028
1029-
if (uart == UART1) af = 4, tx = PIN('A', 9), rx = PIN('A', 10);
1030-
if (uart == UART2) af = 4, tx = PIN('A', 2), rx = PIN('A', 3);
1031-
if (uart == UART3) af = 7, tx = PIN('D', 8), rx = PIN('D', 9);
1029+
if (uart == UART1) tx = PIN('A', 9), rx = PIN('A', 10);
1030+
if (uart == UART2) tx = PIN('A', 2), rx = PIN('A', 3);
1031+
if (uart == UART3) tx = PIN('D', 8), rx = PIN('D', 9);
10321032
10331033
gpio_set_mode(tx, GPIO_MODE_AF);
10341034
gpio_set_af(tx, af);
@@ -1156,7 +1156,7 @@ int main(void) {
11561156
systick_init(16000000 / 1000); // Tick every 1 ms
11571157
gpio_set_mode(led, GPIO_MODE_OUTPUT); // Set blue LED to output mode
11581158
uart_init(UART3, 115200); // Initialise UART
1159-
uint32_t timer = 0, period = 250; // Declare timer and 250ms period
1159+
uint32_t timer = 0, period = 500; // Declare timer and 500ms period
11601160
for (;;) {
11611161
if (timer_expired(&timer, period, s_ticks)) {
11621162
static bool on; // This block is executed
@@ -1364,7 +1364,7 @@ standard C inludes, vendor CMSIS include, defines to PIN, BIT, FREQ, and
13641364
#define PINBANK(pin) (pin >> 8)
13651365
13661366
static inline void spin(volatile uint32_t count) {
1367-
while (count--) asm("nop");
1367+
while (count--) (void) 0;
13681368
}
13691369
13701370
static inline bool timer_expired(uint32_t *t, uint32_t prd, uint32_t now) {

README_zh-CN.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ int main(void) {
631631
```c
632632
static inline void gpio_write(uint16_t pin, bool val) {
633633
struct gpio *gpio = GPIO(PINBANK(pin));
634-
gpio->BSRR |= (1U << PINNO(pin)) << (val ? 0 : 16);
634+
gpio->BSRR = (1U << PINNO(pin)) << (val ? 0 : 16);
635635
}
636636
```
637637
@@ -722,10 +722,10 @@ bool timer_expired(uint32_t *t, uint64_t prd, uint64_t now) {
722722
}
723723
```
724724
725-
使用这个精确的定时器函数更新闪烁LED的主循环,例如,每250ms闪烁一次
725+
使用这个精确的定时器函数更新闪烁LED的主循环,例如,每500ms闪烁一次
726726
727727
```c
728-
uint32_t timer, period = 250; // Declare timer and 250ms period
728+
uint32_t timer = 0, period = 500; // Declare timer and 500ms period
729729
for (;;) {
730730
if (timer_expired(&timer, period, s_ticks)) {
731731
static bool on; // This block is executed
@@ -791,16 +791,16 @@ static inline void gpio_set_mode(uint16_t pin, uint8_t mode) {
791791
#define FREQ 16000000 // CPU frequency, 16 Mhz
792792
static inline void uart_init(struct uart *uart, unsigned long baud) {
793793
// https://www.st.com/resource/en/datasheet/stm32f429zi.pdf
794-
uint8_t af = 0; // Alternate function
794+
uint8_t af = 7; // Alternate function
795795
uint16_t rx = 0, tx = 0; // pins
796796
797797
if (uart == UART1) RCC->APB2ENR |= BIT(4);
798798
if (uart == UART2) RCC->APB1ENR |= BIT(17);
799799
if (uart == UART3) RCC->APB1ENR |= BIT(18);
800800
801-
if (uart == UART1) af = 4, tx = PIN('A', 9), rx = PIN('A', 10);
802-
if (uart == UART2) af = 4, tx = PIN('A', 2), rx = PIN('A', 3);
803-
if (uart == UART3) af = 7, tx = PIN('D', 8), rx = PIN('D', 9);
801+
if (uart == UART1) tx = PIN('A', 9), rx = PIN('A', 10);
802+
if (uart == UART2) tx = PIN('A', 2), rx = PIN('A', 3);
803+
if (uart == UART3) tx = PIN('D', 8), rx = PIN('D', 9);
804804
805805
gpio_set_mode(tx, GPIO_MODE_AF);
806806
gpio_set_af(tx, af);
@@ -914,7 +914,7 @@ int main(void) {
914914
systick_init(16000000 / 1000); // Tick every 1 ms
915915
gpio_set_mode(led, GPIO_MODE_OUTPUT); // Set blue LED to output mode
916916
uart_init(UART3, 115200); // Initialise UART
917-
uint32_t timer = 0, period = 250; // Declare timer and 250ms period
917+
uint32_t timer = 0, period = 500; // Declare timer and 500ms period
918918
for (;;) {
919919
if (timer_expired(&timer, period, s_ticks)) {
920920
static bool on; // This block is executed

step-0-minimal/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ else
1111
RM = rm -f
1212
endif
1313

14-
build: firmware.elf
14+
build: firmware.bin
1515

1616
firmware.elf: $(SOURCES)
1717
arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@

step-1-blinky/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ else
1111
RM = rm -f
1212
endif
1313

14-
build: firmware.elf
14+
build: firmware.bin
1515

1616
firmware.elf: $(SOURCES)
1717
arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@

step-1-blinky/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static inline void gpio_set_mode(uint16_t pin, uint8_t mode) {
3535

3636
static inline void gpio_write(uint16_t pin, bool val) {
3737
struct gpio *gpio = GPIO(PINBANK(pin));
38-
gpio->BSRR |= (1U << PINNO(pin)) << (val ? 0 : 16);
38+
gpio->BSRR = (1U << PINNO(pin)) << (val ? 0 : 16);
3939
}
4040

4141
static inline void spin(volatile uint32_t count) {

step-2-systick/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ else
1111
RM = rm -f
1212
endif
1313

14-
build: firmware.elf
14+
build: firmware.bin
1515

1616
firmware.elf: $(SOURCES)
1717
arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@

step-2-systick/main.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,15 @@ static inline void gpio_set_mode(uint16_t pin, uint8_t mode) {
4848

4949
static inline void gpio_write(uint16_t pin, bool val) {
5050
struct gpio *gpio = GPIO(PINBANK(pin));
51-
gpio->BSRR |= (1U << PINNO(pin)) << (val ? 0 : 16);
51+
gpio->BSRR = (1U << PINNO(pin)) << (val ? 0 : 16);
5252
}
5353

5454
static inline void spin(volatile uint32_t count) {
5555
while (count--) asm("nop");
5656
}
5757

5858
static volatile uint32_t s_ticks;
59-
void SysTick_Handler(void) {
60-
s_ticks++;
61-
}
59+
void SysTick_Handler(void) { s_ticks++; }
6260

6361
// t: expiration time, prd: period, now: current time. Return true if expired
6462
bool timer_expired(uint32_t *t, uint32_t prd, uint32_t now) {
@@ -74,7 +72,7 @@ int main(void) {
7472
RCC->AHB1ENR |= BIT(PINBANK(led)); // Enable GPIO clock for LED
7573
systick_init(16000000 / 1000); // Tick every 1 ms
7674
gpio_set_mode(led, GPIO_MODE_OUTPUT); // Set blue LED to output mode
77-
uint32_t timer, period = 250; // Declare timer and 250ms period
75+
uint32_t timer = 0, period = 500; // Declare timer and 500ms period
7876
for (;;) {
7977
if (timer_expired(&timer, period, s_ticks)) {
8078
static bool on; // This block is executed

step-3-uart/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ else
1111
RM = rm -f
1212
endif
1313

14-
build: firmware.elf
14+
build: firmware.bin
1515

1616
firmware.elf: $(SOURCES)
1717
arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@

step-3-uart/main.c

+7-9
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static inline void gpio_set_af(uint16_t pin, uint8_t af_num) {
6262

6363
static inline void gpio_write(uint16_t pin, bool val) {
6464
struct gpio *gpio = GPIO(PINBANK(pin));
65-
gpio->BSRR |= (1U << PINNO(pin)) << (val ? 0 : 16);
65+
gpio->BSRR = (1U << PINNO(pin)) << (val ? 0 : 16);
6666
}
6767

6868
struct uart {
@@ -74,16 +74,16 @@ struct uart {
7474

7575
static inline void uart_init(struct uart *uart, unsigned long baud) {
7676
// https://www.st.com/resource/en/datasheet/stm32f429zi.pdf
77-
uint8_t af = 0; // Alternate function
77+
uint8_t af = 7; // Alternate function
7878
uint16_t rx = 0, tx = 0; // pins
7979

8080
if (uart == UART1) RCC->APB2ENR |= BIT(4);
8181
if (uart == UART2) RCC->APB1ENR |= BIT(17);
8282
if (uart == UART3) RCC->APB1ENR |= BIT(18);
8383

84-
if (uart == UART1) af = 4, tx = PIN('A', 9), rx = PIN('A', 10);
85-
if (uart == UART2) af = 4, tx = PIN('A', 2), rx = PIN('A', 3);
86-
if (uart == UART3) af = 7, tx = PIN('D', 8), rx = PIN('D', 9);
84+
if (uart == UART1) tx = PIN('A', 9), rx = PIN('A', 10);
85+
if (uart == UART2) tx = PIN('A', 2), rx = PIN('A', 3);
86+
if (uart == UART3) tx = PIN('D', 8), rx = PIN('D', 9);
8787

8888
gpio_set_mode(tx, GPIO_MODE_AF);
8989
gpio_set_af(tx, af);
@@ -112,9 +112,7 @@ static inline uint8_t uart_read_byte(struct uart *uart) {
112112
}
113113

114114
static volatile uint32_t s_ticks;
115-
void SysTick_Handler(void) {
116-
s_ticks++;
117-
}
115+
void SysTick_Handler(void) { s_ticks++; }
118116

119117
// t: expiration time, prd: period, now: current time. Return true if expired
120118
bool timer_expired(uint32_t *t, uint32_t prd, uint32_t now) {
@@ -130,7 +128,7 @@ int main(void) {
130128
systick_init(16000000 / 1000); // Tick every 1 ms
131129
gpio_set_mode(led, GPIO_MODE_OUTPUT); // Set blue LED to output mode
132130
uart_init(UART3, 115200); // Initialise UART
133-
uint32_t timer, period = 250; // Declare timer and 250ms period
131+
uint32_t timer = 0, period = 500; // Declare timer and 500ms period
134132
for (;;) {
135133
if (timer_expired(&timer, period, s_ticks)) {
136134
static bool on; // This block is executed

step-4-printf/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ else
1111
RM = rm -f
1212
endif
1313

14-
build: firmware.elf
14+
build: firmware.bin
1515

1616
firmware.elf: $(SOURCES)
1717
arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@

step-4-printf/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ int main(void) {
1313
systick_init(FREQ / 1000); // Tick every 1 ms
1414
gpio_set_mode(led, GPIO_MODE_OUTPUT); // Set blue LED to output mode
1515
uart_init(UART3, 115200); // Initialise UART
16-
uint32_t timer = 0, period = 250; // Declare timer and 250ms period
16+
uint32_t timer = 0, period = 500; // Declare timer and 500ms period
1717
for (;;) {
1818
if (timer_expired(&timer, period, s_ticks)) {
1919
static bool on; // This block is executed

step-4-printf/mcu.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#define PINBANK(pin) (pin >> 8)
1717

1818
static inline void spin(volatile uint32_t count) {
19-
while (count--) asm("nop");
19+
while (count--) (void) 0;
2020
}
2121

2222
struct systick {
@@ -66,7 +66,7 @@ static inline void gpio_set_af(uint16_t pin, uint8_t af_num) {
6666

6767
static inline void gpio_write(uint16_t pin, bool val) {
6868
struct gpio *gpio = GPIO(PINBANK(pin));
69-
gpio->BSRR |= (1U << PINNO(pin)) << (val ? 0 : 16);
69+
gpio->BSRR = (1U << PINNO(pin)) << (val ? 0 : 16);
7070
}
7171

7272
struct uart {
@@ -78,16 +78,16 @@ struct uart {
7878

7979
static inline void uart_init(struct uart *uart, unsigned long baud) {
8080
// https://www.st.com/resource/en/datasheet/stm32f429zi.pdf
81-
uint8_t af = 0; // Alternate function
81+
uint8_t af = 7; // Alternate function
8282
uint16_t rx = 0, tx = 0; // pins
8383

8484
if (uart == UART1) RCC->APB2ENR |= BIT(4);
8585
if (uart == UART2) RCC->APB1ENR |= BIT(17);
8686
if (uart == UART3) RCC->APB1ENR |= BIT(18);
8787

88-
if (uart == UART1) af = 4, tx = PIN('A', 9), rx = PIN('A', 10);
89-
if (uart == UART2) af = 4, tx = PIN('A', 2), rx = PIN('A', 3);
90-
if (uart == UART3) af = 7, tx = PIN('D', 8), rx = PIN('D', 9);
88+
if (uart == UART1) tx = PIN('A', 9), rx = PIN('A', 10);
89+
if (uart == UART2) tx = PIN('A', 2), rx = PIN('A', 3);
90+
if (uart == UART3) tx = PIN('D', 8), rx = PIN('D', 9);
9191

9292
gpio_set_mode(tx, GPIO_MODE_AF);
9393
gpio_set_af(tx, af);

step-5-cmsis/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ else
1111
RM = rm -f
1212
endif
1313

14-
build: firmware.elf
14+
build: firmware.bin
1515

1616
firmware.elf: $(SOURCES)
1717
arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@

step-5-cmsis/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ int main(void) {
1313
systick_init(FREQ / 1000); // Tick every 1 ms
1414
gpio_set_mode(led, GPIO_MODE_OUTPUT); // Set blue LED to output mode
1515
uart_init(UART3, 115200); // Initialise UART
16-
uint32_t timer = 0, period = 250; // Declare timer and 250ms period
16+
uint32_t timer = 0, period = 500; // Declare timer and 500ms period
1717
for (;;) {
1818
if (timer_expired(&timer, period, s_ticks)) {
1919
static bool on; // This block is executed

step-5-cmsis/mcu.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static inline void gpio_set_af(uint16_t pin, uint8_t af_num) {
4949

5050
static inline void gpio_write(uint16_t pin, bool val) {
5151
GPIO_TypeDef *gpio = GPIO(PINBANK(pin));
52-
gpio->BSRR |= (1U << PINNO(pin)) << (val ? 0 : 16);
52+
gpio->BSRR = (1U << PINNO(pin)) << (val ? 0 : 16);
5353
}
5454

5555
#define UART1 USART1
@@ -58,16 +58,16 @@ static inline void gpio_write(uint16_t pin, bool val) {
5858

5959
static inline void uart_init(USART_TypeDef *uart, unsigned long baud) {
6060
// https://www.st.com/resource/en/datasheet/stm32f429zi.pdf
61-
uint8_t af = 0; // Alternate function
61+
uint8_t af = 7; // Alternate function
6262
uint16_t rx = 0, tx = 0; // pins
6363

6464
if (uart == UART1) RCC->APB2ENR |= BIT(4);
6565
if (uart == UART2) RCC->APB1ENR |= BIT(17);
6666
if (uart == UART3) RCC->APB1ENR |= BIT(18);
6767

68-
if (uart == UART1) af = 4, tx = PIN('A', 9), rx = PIN('A', 10);
69-
if (uart == UART2) af = 4, tx = PIN('A', 2), rx = PIN('A', 3);
70-
if (uart == UART3) af = 7, tx = PIN('D', 8), rx = PIN('D', 9);
68+
if (uart == UART1) tx = PIN('A', 9), rx = PIN('A', 10);
69+
if (uart == UART2) tx = PIN('A', 2), rx = PIN('A', 3);
70+
if (uart == UART3) tx = PIN('D', 8), rx = PIN('D', 9);
7171

7272
gpio_set_mode(tx, GPIO_MODE_AF);
7373
gpio_set_af(tx, af);

step-6-clock/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ else
1111
RM = rm -f
1212
endif
1313

14-
build: firmware.elf
14+
build: firmware.bin
1515

1616
firmware.elf: $(SOURCES)
1717
arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@

step-6-clock/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ int main(void) {
1414
systick_init(FREQ / 1000); // Tick every 1 ms
1515
gpio_set_mode(led, GPIO_MODE_OUTPUT); // Set blue LED to output mode
1616
uart_init(UART3, 115200); // Initialise UART
17-
uint32_t timer = 0, period = 250; // Declare timer and 250ms period
17+
uint32_t timer = 0, period = 500; // Declare timer and 500ms period
1818
for (;;) {
1919
if (timer_expired(&timer, period, s_ticks)) {
2020
static bool on; // This block is executed

0 commit comments

Comments
 (0)