|
1 | 1 | # stm32f1-hal |
2 | | -This crate is the Hardware Abstraction Layer (HAL) for the STM32 F1 series of MCUs. |
3 | 2 |
|
4 | | -Because the design of [stm32f1xx-hal](https://github.com/stm32-rs/stm32f1xx-hal) is unsuitable for my needs and [stm32-hal](https://github.com/David-OConnor/stm32-hal) doesn't support the F1 series, I decided to write a new crate. |
5 | | -Many codes come from [stm32f1xx-hal](https://github.com/stm32-rs/stm32f1xx-hal). |
| 3 | +[](https://crates.io/crates/stm32f1-hal) |
| 4 | +[](https://docs.rs/stm32f1-hal) |
| 5 | +[](https://github.com/mcu-rust/stm32f1-hal/actions) |
| 6 | +[](./LICENSE) |
| 7 | +[](https://crates.io/crates/stm32f1-hal) |
6 | 8 |
|
7 | | -## Design |
8 | | -Below are the design principles. |
9 | | -1. Readability is the most important. |
10 | | - - We only write code a few times, but we read it countless times. Moreover, understanding the code is a necessary condition for maintaining it. |
11 | | -2. Concise is not equal to simple. |
12 | | - - Fewer lines of code do not necessarily mean easier to read and understand. |
| 9 | +**stm32f1-hal** is a Rust Hardware Abstraction Layer (HAL) for **STM32F1 microcontrollers** (STM32F103, STM32F107, etc.). It implements selected [embedded-hal](https://github.com/rust-embedded/embedded-hal) traits to provide a clear, idiomatic interface for embedded development on STM32F1. Many parts are adapted from [stm32f1xx-hal](https://github.com/stm32-rs/stm32f1xx-hal), with a focus on readability and maintainability. |
13 | 10 |
|
14 | | -Therefore, if a module is quite complex, I would not use a `macro` + `generic` approach, as it is too difficult to read. |
| 11 | +## 🎯 Motivation |
| 12 | +Existing crates didn’t fully meet my needs: |
| 13 | +- **[stm32f1xx-hal](https://github.com/stm32-rs/stm32f1xx-hal)**’s design didn’t align with my workflow. |
| 14 | +- **[stm32-hal](https://github.com/David-OConnor/stm32-hal)** lacks support for the STM32F1 series. |
15 | 15 |
|
16 | | -Instead, I use [sync-code](https://crates.io/crates/sync-code) to manage duplicate code across peripherals and a script to generate code for GPIO alternate function remapping. |
| 16 | +To address this gap, I created **[stm32f1-hal](https://github.com/mcu-rust/stm32f1-hal)**. |
| 17 | +While parts of the implementation are adapted from [stm32f1xx-hal](https://github.com/stm32-rs/stm32f1xx-hal), the focus here is on clarity, readability, and usability. |
17 | 18 |
|
18 | | -## Usage |
| 19 | +## 📖 Design Philosophy |
| 20 | +- **Readability is the most important.** |
| 21 | + We only write code a few times, but we read it countless times. Clear understanding is essential for long-term maintenance. |
| 22 | + |
| 23 | +- **Concise is not equal to simple.** |
| 24 | + Fewer lines of code do not necessarily mean easier to read or understand. |
| 25 | + |
| 26 | +- **Prefer [sync-code](https://crates.io/crates/sync-code) over complex macros + generics.** |
| 27 | + In complex modules, combining macros with generics often makes the code harder to follow and maintain. |
| 28 | + Instead, I use [sync-code](https://crates.io/crates/sync-code), a preprocessing tool that replaces annotated comments with reusable code blocks, keeping peripheral code consistent and readable. |
| 29 | + |
| 30 | +- In addition, a script is used to generate code for GPIO alternate function remapping. |
| 31 | + |
| 32 | +## 📦 Usage |
19 | 33 | ```shell |
20 | 34 | cargo add stm32f1-hal |
21 | 35 | ``` |
22 | 36 |
|
23 | | -See [example](examples/f103c8/src/main.rs). |
| 37 | +```rust |
| 38 | +use stm32f1_hal as hal; |
| 39 | +use hal::pac; |
| 40 | +use hal::prelude::*; |
| 41 | + |
| 42 | +fn main() { |
| 43 | + let dp = pac::Peripherals::take().unwrap(); |
| 44 | + let mut flash = dp.FLASH.constrain(); |
| 45 | + let mut rcc = dp.RCC.constrain(); |
| 46 | + |
| 47 | + let clocks = rcc.cfgr.freeze(&mut flash.acr); |
| 48 | + let mut gpioa = dp.GPIOA.split(&mut rcc.apb2); |
24 | 49 |
|
| 50 | + let mut led = gpioa.pa5.into_push_pull_output(&mut gpioa.crl); |
| 51 | + |
| 52 | + loop { |
| 53 | + led.set_high(); |
| 54 | + // delay... |
| 55 | + led.set_low(); |
| 56 | + // delay... |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | +See [example](examples/f103c8/src/main.rs). |
25 | 61 | See [crate](https://crates.io/crates/stm32f1-hal). |
26 | 62 |
|
27 | | -## Note |
28 | | -This project is still in its early stages, with only a few features completed. |
| 63 | +## 🧩 Supported Devices |
| 64 | +- All F1 series devices |
| 65 | + |
| 66 | +## 🗺 Roadmap |
| 67 | +This project is still in its early stages, with only a few features implemented so far. Contributions and feedback are welcome to help expand support for more peripherals and features. |
| 68 | +- [x] GPIO |
| 69 | +- [x] UART |
| 70 | +- [ ] I2C |
| 71 | +- [ ] ADC |
| 72 | +- [ ] DMA |
| 73 | +- [ ] More features |
| 74 | + |
| 75 | +## 🛠 Contributing |
| 76 | +- Open issues for bugs or feature requests. |
| 77 | +- Submit PRs with improvements or new peripheral support (I2C, ADC, DMA, etc.). |
| 78 | + |
| 79 | +## 🔖 Keywords |
| 80 | +**stm32 · stm32f1 · rust · embedded-hal · hal · microcontroller · embedded development** |
0 commit comments