Skip to content

Commit

Permalink
mcu.h -> hal.h
Browse files Browse the repository at this point in the history
  • Loading branch information
cpq committed Jun 17, 2023
1 parent 9ab1b04 commit 52e1a8a
Show file tree
Hide file tree
Showing 23 changed files with 30 additions and 30 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1117,17 +1117,17 @@ mechanism. We use newlib, so let's modify `_write()` syscall to print to the
UART3.
Before that, let's organise our source code in the following way:
- move all API definitions to the file `mcu.h`
- move all API definitions to the file `hal.h` (Harware Abstraction Layer)
- move startup code to `startup.c`
- create an empty file `syscalls.c` for newlib "syscalls"
- modify Makefile to add `syscalls.c` and `startup.c` to the build
After moving all API definitions to the `mcu.h`, our `main.c` file becomes
After moving all API definitions to the `hal.h`, our `main.c` file becomes
quite compact. Note that it does not have any mention of the low-level
registers, just a high level API functions that are easy to understand:
```c
#include "mcu.h"
#include "hal.h"
static volatile uint32_t s_ticks;
void SysTick_Handler(void) {
Expand Down Expand Up @@ -1157,7 +1157,7 @@ Great, now let's retarget printf to the UART3. In the empty syscalls.c,
copy/paste the following code:
```c
#include "mcu.h"
#include "hal.h"
int _write(int fd, char *ptr, int len) {
(void) fd, (void) ptr, (void) len;
Expand Down Expand Up @@ -1272,7 +1272,7 @@ Choose our firmware.elf file:
<img src="images/ozone3.png" width="50%" />
Leave the defaults on the next screen, click Finish, and we've got our
debugger loaded (note the mcu.h source code is picked up):
debugger loaded (note the hal.h source code is picked up):
![](images/ozone4.png)
Expand Down Expand Up @@ -1329,8 +1329,8 @@ The ST CMSIS package also provides startup files for all their MCUs. We
can use those instead of hand-writing the startup.c. The ST-provided startup
file calls `SystemInit()` function, so we define it in the `main.c`.
Now, let's replace our API functions in the `mcu.h` using CMSIS definitions,
and leave the rest of the firmware intact. From the `mcu.h`, remove all
Now, let's replace our API functions in the `hal.h` using CMSIS definitions,
and leave the rest of the firmware intact. From the `hal.h`, remove all
peripheral API and definitions, and leave only standard C inludes, vendor CMSIS
include, defines to PIN, BIT, FREQ, and `timer_expired()` helper function.
Expand All @@ -1343,7 +1343,7 @@ Let's start from `systick_init()`. ARM core CMSIS headers provide a
Next goes `gpio_set_mode()` function. The `stm32f429xx.h` header has
`GPIO_TypeDef` structure, identical to our `struct gpio`. Let's use it:
https://github.com/cpq/bare-metal-programming-guide/blob/785aa2ead0432fc67327781c82b9c41149fba158/step-5-cmsis/mcu.h#L24-L33
https://github.com/cpq/bare-metal-programming-guide/blob/785aa2ead0432fc67327781c82b9c41149fba158/step-5-cmsis/hal.h#L24-L33
The `gpio_set_af()` and `gpio_write()` functions is also trivial -
simply replace `struct gpio` with `GPIO_TypeDef`, and that's all.
Expand Down
14 changes: 7 additions & 7 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -894,15 +894,15 @@ newlib实现了一些标准C函数,特别是文件输入输出操作,并且
在那之前,我们先重新组织下源码结构:
- 把所有API定义放到 `mcu.h` 文件中
- 把所有API定义放到 `hal.h` 文件中
- 把启动代码放到 `startup.c` 文件中
- 为newlib的系统调用创建一个空文件 `syscalls.c`
- 修改Makefile,把 `syscalls.c` 和 `startup.c` 加到build中
将所有 API 定义移动到 `mcu.h` 后,`main.c` 文件变得相当紧凑。注意我们还没提到底层寄存器,高级API函数很容易理解:
将所有 API 定义移动到 `hal.h` 后,`main.c` 文件变得相当紧凑。注意我们还没提到底层寄存器,高级API函数很容易理解:
```c
#include "mcu.h"
#include "hal.h"
static volatile uint32_t s_ticks;
void SysTick_Handler(void) {
Expand Down Expand Up @@ -931,7 +931,7 @@ int main(void) {
现在我们把 `printf()` 重定向到串口3,在空的 `syscalls.c` 文件中拷入一下内容:
```c
#include "mcu.h"
#include "hal.h"
int _write(int fd, char *ptr, int len) {
(void) fd, (void) ptr, (void) len;
Expand Down Expand Up @@ -1027,7 +1027,7 @@ LED: 0, tick: 1000

<img src="images/ozone3.png" width="50%" />

接下来的步骤保持默认,点击“完成”,调试器已经载入(可以看到`mcu.h`源码被拾取):
接下来的步骤保持默认,点击“完成”,调试器已经载入(可以看到`hal.h`源码被拾取):

<img src="images/ozone3.png" width="50%" />

Expand Down Expand Up @@ -1060,7 +1060,7 @@ LED: 0, tick: 1000

CMSIS代表通用微控制器软件接口标准(Common Microcontroller Software Interface Standard),因此它是MCU制造商指定外设API的共同基础。 因为CMSIS是一种ARM标准,并且CMSIS头文件由MCU厂商提供,所以是权威的来源。因此,使用供应商头文件是首选方法,而不是手动编写定义。

在这一节,我们将使用供应商CMSIS头文件替换 `mcu.h` 中的API函数,并保持固件其它部分不变。
在这一节,我们将使用供应商CMSIS头文件替换 `hal.h` 中的API函数,并保持固件其它部分不变。

STM32 F4系列的CMSIS头文件在这个[仓库](https://github.com/STMicroelectronics/cmsis_device_f4),从那里将以下文件拷到我们的固件文件夹[step-5-cmsis](step-5-cmsis)

Expand All @@ -1074,7 +1074,7 @@ Those two files depend on a standard ARM CMSIS includes, download them too:
- [cmsis_compiler.h](https://raw.githubusercontent.com/STMicroelectronics/STM32CubeF4/master/Drivers/CMSIS/Core/Include/cmsis_compiler.h)
- [mpu_armv7.h](https://raw.githubusercontent.com/STMicroelectronics/STM32CubeF4/master/Drivers/CMSIS/Core/Include/mpu_armv7.h)

然后移除 `mcu.h` 中所有外设API和定义,只留下标准C包含、供应商CMSIS包含,引脚定义等:
然后移除 `hal.h` 中所有外设API和定义,只留下标准C包含、供应商CMSIS包含,引脚定义等:

```c
#pragma once
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion step-4-printf/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2022 Cesanta Software Limited
// All rights reserved

#include "mcu.h"
#include "hal.h"

static volatile uint32_t s_ticks;
void SysTick_Handler(void) {
Expand Down
2 changes: 1 addition & 1 deletion step-4-printf/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <sys/stat.h>

#include "mcu.h"
#include "hal.h"

int _fstat(int fd, struct stat *st) {
if (fd < 0) return -1;
Expand Down
2 changes: 1 addition & 1 deletion step-5-cmsis/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ endif

build: firmware.bin

firmware.elf: cmsis_core cmsis_f4 mcu.h link.ld Makefile $(SOURCES)
firmware.elf: cmsis_core cmsis_f4 hal.h link.ld Makefile $(SOURCES)
arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(CFLAGS_EXTRA) $(LDFLAGS) -o $@

firmware.bin: firmware.elf
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion step-5-cmsis/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2022 Cesanta Software Limited
// All rights reserved

#include "mcu.h"
#include "hal.h"

static volatile uint32_t s_ticks;
void SysTick_Handler(void) {
Expand Down
2 changes: 1 addition & 1 deletion step-5-cmsis/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <sys/stat.h>

#include "mcu.h"
#include "hal.h"

int _fstat(int fd, struct stat *st) {
if (fd < 0) return -1;
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion step-6-clock/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2022 Cesanta Software Limited
// All rights reserved

#include "mcu.h"
#include "hal.h"

static volatile uint32_t s_ticks;
void SysTick_Handler(void) { s_ticks++; }
Expand Down
2 changes: 1 addition & 1 deletion step-6-clock/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <sys/stat.h>

#include "mcu.h"
#include "hal.h"

int _fstat(int fd, struct stat *st) {
if (fd < 0) return -1;
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion step-7-webserver/ek-tm4c1294xl/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2022 Cesanta Software Limited
// All rights reserved

#include "mcu.h"
#include "hal.h"
#include "mongoose.h"

static volatile uint32_t s_ticks;
Expand Down
2 changes: 1 addition & 1 deletion step-7-webserver/ek-tm4c1294xl/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <sys/stat.h>

#include "mcu.h"
#include "hal.h"

int _fstat(int fd, struct stat *st) {
if (fd < 0) return -1;
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion step-7-webserver/nucleo-f429zi/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2022 Cesanta Software Limited
// All rights reserved

#include "mcu.h"
#include "hal.h"
#include "mongoose.h"

static volatile uint32_t s_ticks;
Expand Down
2 changes: 1 addition & 1 deletion step-7-webserver/nucleo-f429zi/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <sys/stat.h>

#include "mcu.h"
#include "hal.h"

int _fstat(int fd, struct stat *st) {
if (fd < 0) return -1;
Expand Down
2 changes: 1 addition & 1 deletion step-7-webserver/pico-w5500/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ $(BIN2UF2): tools/bin2uf2.c
$(CC) -W -Wall $< -o $@
endif

firmware.elf: $(SOURCES) mcu.h
firmware.elf: $(SOURCES) hal.h
arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@

firmware.bin: firmware.elf
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion step-7-webserver/pico-w5500/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2023 Cesanta Software Limited
// All rights reserved

#include "mcu.h"
#include "hal.h"
#include "mongoose.h"

enum { LED = 25 }; // LED pins
Expand Down
2 changes: 1 addition & 1 deletion step-7-webserver/pico-w5500/startup.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2022 Cesanta Software Limited
// All rights reserved

#include "mcu.h"
#include "hal.h"

// Startup code
__attribute__((naked, section(".boot"))) void _reset(void) {
Expand Down
2 changes: 1 addition & 1 deletion step-7-webserver/pico-w5500/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <sys/stat.h>

#include "mcu.h"
#include "hal.h"

int _fstat(int fd, struct stat *st) {
if (fd < 0) return -1;
Expand Down

0 comments on commit 52e1a8a

Please sign in to comment.