Skip to content

Commit 10a413c

Browse files
committed
修改一些错误
1 parent 9d79ad6 commit 10a413c

13 files changed

Lines changed: 194 additions & 29 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ embedded-hal = "1.0.0"
2121
embedded-hal-027 = { package = "embedded-hal", version = "0.2.7", features = ["unproven"] }
2222
embedded-hal-async = { version = "1.0" }
2323
embedded-io = "0.6.1"
24-
embedded-io-async = { version = "0.6.1", option = true }
24+
embedded-io-async = { version = "0.6.1"}
2525
nb = "1"
2626
void = { version = "1.0", default-features = false }
2727
cortex-m = { version = "0.7.7", features = ["critical-section-single-core", "inline-asm"] }
@@ -91,7 +91,7 @@ name = "embassy_dma_mem2mem"
9191
required-features = ["embassy"]
9292

9393
[[example]]
94-
name = "embassy_exit"
94+
name = "embassy_exti"
9595
required-features = ["embassy"]
9696

9797
[[example]]

README.md

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,126 @@
1-
21
## 💁 Overview
2+
33
This repository provides a driver layer for the Py32f030 chip. It currently supports most peripherals and provides many friendly interfaces. By calling the upper-level interfaces, the peripherals of the microcontroller can be easily put into operation.
44

55
## 💻 Development environment
6+
67
Installing the Rust embedded compilation environment for Py32F030 is very simple. You need to install the Rust compilation chain and some tools
8+
79
### [Rust](https://www.rust-lang.org/tools/install)
10+
11+
### [Hardware repository](https://github.com/hysonglet/Py32F030_CrabBoard_Hardware)
12+
813
#### Mac/Linux
14+
915
```bash
1016
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
1117
```
18+
1219
#### [Windows](https://static.rust-lang.org/rustup/dist/i686-pc-windows-gnu/rustup-init.exe)
20+
1321
The installation of Rust on Windows is a little bit complicated. You can read [this website](https://forge.rust-lang.org/infra/other-installation-methods.html) to learn how to install it.
1422

1523
### Rust nightly version
24+
1625
After installing rust, you need to switch to the night version to compile some embedded rust code
26+
1727
```bash
1828
rustup default nightly
1929
```
30+
2031
### Installing the Cortex-M0 Compiler Tools
32+
2133
```bash
2234
rustup target add thumbv6m-none-eabi
2335
```
2436

2537
### Check the environment
38+
2639
Execute the following command. If there is no error, it means that the rust compilation environment is installed normally.
40+
2741
```
2842
git clone https://github.com/hysonglet/py32f030-hal.git
2943
cd py32f030-hal
3044
cargo build
3145
```
3246

3347
### Check Rust version
48+
3449
```bash
3550
➜ py32f030-hal git:(main) ✗ rustup --version
3651
rustup 1.27.1 (54dd3d00f 2024-04-24)
3752
info: This is the version for the rustup toolchain manager, not the rustc compiler.
3853
info: The currently active `rustc` version is `rustc 1.82.0-nightly (6de928dce 2024-08-18)`
3954
```
4055

41-
### 安装 [Probe-rs](https://probe.rs/docs/getting-started/installation/#homebrew)
56+
### Install [Probe-rs](https://probe.rs/docs/getting-started/installation/#homebrew)
57+
4258
Probe-rs is an excellent firmware download and log debugging tool. For detailed of installation and how to use, please click [this link](https://probe.rs/docs/getting-started/installation/#using-install-scripts) to see more。
59+
4360
#### Mac/Linux
61+
4462
```bash
4563
curl --proto '=https' --tlsv1.2 -LsSf \
4664
https://github.com/probe-rs/probe-rs/releases/latest/download/probe-rs-tools-installer.sh \
4765
| sh
4866
```
67+
4968
#### Windows
69+
5070
```bash
5171
cargo install cargo-binstall
5272
cargo binstall probe-rs-tools
5373
```
5474

5575
## Other tools (not required for now)
76+
5677
### cargo tools
78+
5779
```bash
5880
cargo install cargo-get
5981
brew install llvm
6082
```
6183

6284
### Serial port burning tool
85+
6386
There are two tools here, you can choose a tool written in Python or a serial port tool written in Rust.
87+
6488
1. python sccript: puyaisp
89+
6590
```
6691
pip install puyaisp
6792
pip install pyusb pyserial hid
6893
```
94+
6995
burn:
96+
7097
```bash
7198
# Press the boot and RST buttons at the same time, then release RST first, then release Boot, and then execute the following command
7299
puyaisp -f blink.bin
73100
```
101+
74102
2. rust tools: [pyisp](https://github.com/hysonglet/pyisp.git)
75-
Use pyisp rust serial port tool to burn bin file,
76-
``` bash
103+
Use pyisp rust serial port tool to burn bin file,
104+
105+
```bash
77106
# One-time download
78107
pyisp -s tty.usbserial-130 -g -f test.bin
79108
# Repeated download
80109
pyisp -s COM4 -g -c -f test.bin
81110
```
82111

83-
84112
### Jlink connection operation and view log
113+
85114
Of course, it is not limited to jlink, stlink is also a good choice. Sometimes we use the following two methods to download and view the running logs
115+
86116
1. cargo run
117+
87118
```
88119
cargo r --example embassy_uart
89120
```
121+
90122
2. probe-rs run
123+
91124
```
92125
probe-rs run --chip PY32F030x8 target/thumbv6m-none-eabi/debug/examples/embassy_uart
93126
```
@@ -111,13 +144,15 @@ probe-rs run --chip PY32F030x8 target/thumbv6m-none-eabi/debug/examples/embassy_
111144
- [x] Flash
112145

113146
## TODO
147+
114148
- LPTimer
115149
- Clock -> 48M
116150
- spi
117151

118152
## Examples
119153

120154
### Run
155+
121156
```bash
122157
# run
123158
cargo run --example blinky
@@ -126,6 +161,7 @@ cargo build --release --example blinky
126161
```
127162

128163
### Example list
164+
129165
```bash
130166
py32f030-hal git:(main) ✗ cargo r --example
131167
error: "--example" takes one argument.
@@ -157,14 +193,23 @@ Available examples:
157193
```
158194

159195
## [Evaluation Board](https://github.com/hysonglet/Py32F030_CrabBoard_Hardware)
196+
160197
<img src="https://s.imgkb.xyz/i/abcdocker/2025/01/01/67752f84dc98a.png" alt="Py32_Rust_Dev 1.2" title="Py32_Rust_Dev 1.2" />
161198

162199
## Wechat blog
200+
163201
公众号:`Rust嵌入式`
164202
<img src="https://s.imgkb.xyz/i/abcdocker/2024/07/20/669bac54b9156.jpg" alt="Rust嵌入式" style="display: block; margin: 0 auto;">
165203

166204
## How to buy
205+
167206
We currently provide Taobao links, you can choose any one to buy
207+
168208
- [Red Board](https://item.taobao.com/item.htm?abbucket=3&id=870372823551&ns=1&pisk=g_4ZEUV7dq2BYJFzdz02YPC73Jgt-qW57rMjiSVm1ADiCScmuJ2f6ANcB-zqKSE16m69gCUUUET1BGFDuqgcFT_5P5EtkqXSqW-3hdhSwfbjsmvnWjiOd69CP5FtHCJcNkbW38cpMIxmn-0nxjcoof0mmWfEGbLmoAmDKHcoKqD0nmYntblJsEDDopfE6bHDSFmDtDcmahcgnqfE-bHnnqmm6bN46UkZjsltBejv0vmZE5DezWUEsrKT_v7cuyP-2YYSLEY08fVISNCBSak3fo4s-PXHh2V8ARcEzZRqTuPnSfyAyF3gZWqmbrSXwxEUtuiUvC1tTPPzuboC8pcr4y4sWkWyPYqLiro36T--OoV75oy1pED_4W2EVy9BP24aQPmErgRXHX4VLrEwnnoi9Xk5T6kW_3HU4z6G5nKxY2GEF_5eDnniDXk5TCxvDDlsTY1__&priceTId=2147847117405862249073402ec550&spm=a21n57.1.hoverItem.2&utparam=%7B%22aplus_abtest%22%3A%22869e3ae3cd2e9d2f08a139771bd78df4%22%7D&xxc=taobaoSearch&skuId=5702998681883)
169209
- [Red Board](https://item.taobao.com/item.htm?abbucket=3&id=873483284901&ns=1&pisk=gQSoE2iF-IGsgeEgVjt7WM_HxuaYe3tBP6np9HdUuIRj2eRLP6Yh9sTF23BRiB5OtpIRvMbjxO6C28tLF36WAHPT6lBhFTtQMED4mMxV3L9zv4oE43t2zxwYDlEOFtDJURB4XgCN0Cvo8D5ezER2CIJyTBoznEJ9LHoy80uqgIO2TH8eUKu2QLmEYDoznxJwdDleaek23LpeUH-FTtyDdIJEOWnk1g7P0S7J9OWpz8svEUANUQWReikJy46WiflGmOGksTuIYDSDEUSC7qQCPH7PesYOr5mkAT71Od5ra7vcqOjh7srxqHXV7MxV0SkvaN6cx3Izpu-Vq_jyo6kQHISHI9YOkJuMMwWfmEjuWv9f29SWWhrsaBQAIMvhAWZcsT5l7FSrZgWSuV5aQD94piuIRUJXnCCPHBlGLZOyb-2mWkLyhdAYn-0QcUJXn6w0nV39zK9-s&priceTId=2147847117405862249073402ec550&skuId=5873994718463&spm=a21n57.1.hoverItem.34&utparam=%7B%22aplus_abtest%22%3A%22ff785536f5ab6bb6f59d61effd2b2e31%22%7D&xxc=taobaoSearch)
170210
- [Stlink](https://item.taobao.com/item.htm?abbucket=3&id=870372823551&ns=1&pisk=gV8teqfMiXFtjPk-QFo3o97RRAlHhDAZIdR7otXgcpppG94m_NmVkKBp3OjG5O4AkIp2nKdq_s6XhKBDjD0k_C7VlYjxr4Aw22ctpdC1G6GfG_zjtGw91NP1lYDokkVCbAbXIvN6j2ZCa911GO6j9w1Vw1_fc1OI9s10Coa6hXdCLsq_lsasR61P1R_blPOCd_10hs611y_CLs_fhK_j9MGCDA63G6UvVwfF2CyxYzzyJ1IOHrXLkg8TrMBWV9aj-j5GXlA1praXRHf84RW-JJ6HGQ-fvE338ZRH2KKXdjafHH1Je_viRPQXApLRVBk87OKBQEQG4qFA93dBBFJjHlpvdQ-P5FM4SO-XOE6H5j4AF3APJ68mLP6vAnTGbZysB6L9wEKA4B8kyHJQETC0fXhL0oS1TR1XiKTZgwId9TcsWorVc_5dEXnY0oS6s6Bo1XE40MPC.&priceTId=2147847117405862249073402ec550&skuId=5716683933293&spm=a21n57.1.hoverItem.2&utparam=%7B%22aplus_abtest%22%3A%22869e3ae3cd2e9d2f08a139771bd78df4%22%7D&xxc=taobaoSearch)
211+
212+
## Appendix
213+
214+
[py32f030 datasheet](https://www.puyasemi.com/shujushouce.html?keywords=py32f030)
215+
[py32f030 reference manual](https://www.puyasemi.com/yonghushouce.html?keywords=py32f030)

examples/adc_block_interrupt_closure.rs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@
33

44
use core::cell::RefCell;
55
use cortex_m::interrupt::{self, Mutex};
6-
use hal::adc::{AdcChannel, AnyAdc, ChannelConfig, Config, SampleCycles, TrigleSignal};
6+
use hal::adc::{
7+
temperature, vrefence_internal, AdcChannel, AnyAdc, ChannelConfig, Config, ConversionMode,
8+
Event, SampleCycles, TrigleSignal,
9+
};
10+
use hal::clock::sys_core_clock;
11+
use hal::interrupt::BindInterrupt;
12+
use hal::mcu::peripherals::ADC;
13+
use hal::mode::Blocking;
714
use heapless::spsc::Queue;
8-
9-
use py32f030_hal::adc::{ConversionMode, Event};
10-
use py32f030_hal::clock::sys_core_clock;
11-
use py32f030_hal::interrupt::BindInterrupt;
12-
use py32f030_hal::{self as hal, mode::Blocking};
15+
use py32f030_hal as hal;
1316

1417
use {defmt_rtt as _, panic_probe as _};
1518

16-
static ADC_INSTANCE: Mutex<RefCell<Option<AnyAdc<hal::mcu::peripherals::ADC, Blocking>>>> =
17-
Mutex::new(RefCell::new(None));
19+
// 多线程中使用ADC,需要使用中断
20+
static ADC_INSTANCE: Mutex<RefCell<Option<AnyAdc<ADC, Blocking>>>> = Mutex::new(RefCell::new(None));
1821

1922
type AdcQueue = Queue<u16, 128>;
2023

@@ -33,8 +36,10 @@ fn main() -> ! {
3336
.wait(true) // 转换完成后等待读取完毕再开始转换
3437
.singal(TrigleSignal::Soft)
3538
.mode(ConversionMode::Continuous),
39+
// 按顺序读取温度和电压采集的值
3640
&[AdcChannel::Channel11, AdcChannel::Channel12],
37-
// &[AdcChannel::Channel11],
41+
// 读取温度值
42+
// &[AdcChannel::Channel11]],
3843
)
3944
.unwrap();
4045

@@ -43,22 +48,34 @@ fn main() -> ! {
4348
ADC_INSTANCE.borrow(cs).replace(Some(adc));
4449
let mut adc_bind = ADC_INSTANCE.borrow(cs).borrow_mut();
4550
let adc = adc_bind.as_mut().unwrap();
46-
let _ = adc.id().bind(&|cs| {
47-
let mut adc = ADC_INSTANCE.borrow(cs).borrow_mut();
48-
let mut queue = ADC_QUEUE.borrow(cs).borrow_mut();
49-
let _ = queue.enqueue(adc.as_mut().unwrap().read_once());
50-
});
51+
52+
adc.id()
53+
.bind(&|cs| {
54+
// 拿到ADC实例
55+
let mut adc_borrow = ADC_INSTANCE.borrow(cs).borrow_mut();
56+
// 获取队列的所有权
57+
let mut queue = ADC_QUEUE.borrow(cs).borrow_mut();
58+
let _ = queue.enqueue(adc_borrow.as_mut().unwrap().read_once());
59+
})
60+
.unwrap();
61+
5162
adc.id().enable_irq();
63+
// 开始转换
5264
adc.start();
65+
// 开启中断
5366
});
5467

5568
loop {
5669
cortex_m::asm::wfi();
57-
5870
interrupt::free(|cs| {
5971
let mut queue = ADC_QUEUE.borrow(cs).borrow_mut();
60-
while let Some(v) = queue.dequeue() {
61-
defmt::info!("adc value: {}", v);
72+
while queue.len() >= 2 {
73+
defmt::info!(
74+
"voltage: {}, temperature: {} redunt: {}",
75+
vrefence_internal(queue.dequeue().unwrap()),
76+
temperature(queue.dequeue().unwrap()),
77+
queue.len()
78+
);
6279
}
6380
})
6481
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
use core::cell::RefCell;
5+
use cortex_m::interrupt::{self, Mutex};
6+
use hal::adc::{
7+
AdcChannel, AnyAdc, ChannelConfig, Config, ConversionMode, Event, SampleCycles, TrigleSignal,
8+
};
9+
use hal::clock::sys_core_clock;
10+
use hal::gpio;
11+
use hal::interrupt::BindInterrupt;
12+
use hal::mcu::peripherals::ADC;
13+
use hal::mode::Blocking;
14+
use heapless::spsc::Queue;
15+
use py32f030_hal as hal;
16+
use py32f030_hal::adc::AnalogPin;
17+
18+
use {defmt_rtt as _, panic_probe as _};
19+
20+
// 多线程中使用ADC,需要使用中断
21+
static ADC_INSTANCE: Mutex<RefCell<Option<AnyAdc<ADC, Blocking>>>> = Mutex::new(RefCell::new(None));
22+
23+
type AdcQueue = Queue<u16, 128>;
24+
25+
static ADC_QUEUE: Mutex<RefCell<AdcQueue>> = Mutex::new(RefCell::new(AdcQueue::new()));
26+
27+
#[cortex_m_rt::entry]
28+
fn main() -> ! {
29+
let p = hal::init(Default::default());
30+
defmt::info!("{}", sys_core_clock());
31+
32+
let gpioa = p.GPIOA.split();
33+
// gpio::Analog::new(gpioa.PA0);
34+
let channel_pin = gpioa.PA3;
35+
channel_pin.as_anlog();
36+
37+
let mut adc: AnyAdc<_, Blocking> = AnyAdc::new(
38+
p.ADC,
39+
Config::default().sample(SampleCycles::Cycle_239_5),
40+
ChannelConfig::default()
41+
.over_write(false)
42+
.wait(true) // 转换完成后等待读取完毕再开始转换
43+
.singal(TrigleSignal::Soft)
44+
.mode(ConversionMode::Continuous),
45+
// 读
46+
&[channel_pin.channel()],
47+
)
48+
.unwrap();
49+
50+
let _ = interrupt::free(|cs| {
51+
adc.event_config(Event::EOC, true);
52+
ADC_INSTANCE.borrow(cs).replace(Some(adc));
53+
let mut adc_bind = ADC_INSTANCE.borrow(cs).borrow_mut();
54+
let adc = adc_bind.as_mut().unwrap();
55+
56+
adc.id()
57+
.bind(&|cs| {
58+
// 拿到ADC实例
59+
let mut adc_borrow = ADC_INSTANCE.borrow(cs).borrow_mut();
60+
// 获取队列的所有权
61+
let mut queue = ADC_QUEUE.borrow(cs).borrow_mut();
62+
let _ = queue.enqueue(adc_borrow.as_mut().unwrap().read_once());
63+
})
64+
.unwrap();
65+
66+
adc.id().enable_irq();
67+
// 开始转换
68+
adc.start();
69+
// 开启中断
70+
});
71+
72+
loop {
73+
cortex_m::asm::wfi();
74+
interrupt::free(|cs| {
75+
let mut queue = ADC_QUEUE.borrow(cs).borrow_mut();
76+
while queue.len() > 0 {
77+
defmt::info!(
78+
"adc: {}, redunt: {}",
79+
queue.dequeue().unwrap(),
80+
queue.len()
81+
);
82+
}
83+
})
84+
}
85+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
use embassy_executor::Spawner;
55
use embassy_time::Timer;
66
use hal::exti::ExtiInput;
7+
use hal::exti::Line;
78
use hal::gpio::{Pull, Speed};
9+
use hal::interrupt::BindInterrupt;
810
use hal::mode::Async;
911
use py32f030_hal::{self as hal, prelude::*};
1012
use {defmt::info, defmt_rtt as _, panic_probe as _};

src/adc/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub enum AdcChannel {
5050
/// PB1
5151
Channel9 = 9,
5252

53-
/// inner temperature
53+
/// inner Temperature sensor
5454
Channel11 = 11,
5555
/// inner ref voltage
5656
Channel12 = 12,

src/exti/future.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct ExtiInputFuture<'a> {
2525
impl<'a> ExtiInputFuture<'a> {
2626
pub fn new(port: GpioPort, pin: usize, edge: Edge) -> Self {
2727
let line: Line = pin.into();
28+
let _ = line.bind_default();
2829
// line 选择
2930
Exti::exit_channle_select(line, port.into());
3031

0 commit comments

Comments
 (0)