Skip to content
This repository was archived by the owner on Jun 23, 2024. It is now read-only.

Commit 2fc9829

Browse files
bors[bot]robamu
andauthored
Merge #12
12: Refactored GPIO module r=robamu a=robamu - The GPIO module uses type-level programming now - Implementation heavily based on the ATSAMD GPIO HAL: https://docs.rs/atsamd-hal/0.13.0/atsamd_hal/gpio/v2/index.html - Changes to API, but no passing of peripheral references necessary anymore. All examples and tests updated accordingly Co-authored-by: Robin Mueller <robin.mueller.m@gmail.com>
2 parents 47e64c2 + 63be6ed commit 2fc9829

File tree

15 files changed

+1851
-758
lines changed

15 files changed

+1851
-758
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1111

1212
## [0.2.0]
1313

14+
### Changed
15+
16+
- New GPIO implementation which uses type-level programming. Implementation heavily based on the
17+
ATSAMD GPIO HAL: https://docs.rs/atsamd-hal/0.13.0/atsamd_hal/gpio/v2/index.html
18+
- Changes to API, therefore minor version bump
19+
1420
### Added
1521

1622
- UART implementation

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "va108xx-hal"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
authors = ["Robin Mueller <robin.mueller.m@gmail.com>"]
55
edition = "2021"
66
description = "HAL for the Vorago VA108xx family of microcontrollers"
@@ -14,6 +14,7 @@ categories = ["embedded", "no-std", "hardware-support"]
1414
cortex-m = "0.7"
1515
cortex-m-rt = "0.7"
1616
nb = "1"
17+
paste = "1.0"
1718
embedded-hal = { features = ["unproven"], version = "0.2.6" }
1819
void = { version = "1.0", default-features = false }
1920
once_cell = { version = "1.8.0", default-features = false }

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ is contained within the
7777
embedded-hal = "0.2.6"
7878

7979
[dependencies.va108xx-hal]
80-
version = "0.1"
80+
version = "0.2"
8181
features = ["rt"]
8282
```
8383

examples/blinky.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,15 @@
99
use cortex_m_rt::entry;
1010
use embedded_hal::digital::v2::ToggleableOutputPin;
1111
use panic_halt as _;
12-
use va108xx_hal::{pac, prelude::*};
12+
use va108xx_hal::{gpio::PinsA, pac, prelude::*};
1313

1414
#[entry]
1515
fn main() -> ! {
1616
let mut dp = pac::Peripherals::take().unwrap();
17-
let porta = dp.PORTA.split(&mut dp.SYSCONFIG).unwrap();
18-
let mut led1 = porta
19-
.pa10
20-
.into_push_pull_output(&mut dp.IOCONFIG, &mut dp.PORTA);
21-
let mut led2 = porta
22-
.pa7
23-
.into_push_pull_output(&mut dp.IOCONFIG, &mut dp.PORTA);
24-
let mut led3 = porta
25-
.pa6
26-
.into_push_pull_output(&mut dp.IOCONFIG, &mut dp.PORTA);
17+
let porta = PinsA::new(&mut dp.SYSCONFIG, Some(dp.IOCONFIG), dp.PORTA);
18+
let mut led1 = porta.pa10.into_push_pull_output();
19+
let mut led2 = porta.pa7.into_push_pull_output();
20+
let mut led3 = porta.pa6.into_push_pull_output();
2721
for _ in 0..10 {
2822
led1.set_low().ok();
2923
led2.set_low().ok();

examples/tests.rs

Lines changed: 38 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
#![no_std]
77

88
use cortex_m_rt::entry;
9-
use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin, ToggleableOutputPin};
9+
use embedded_hal::digital::v2::{InputPin, OutputPin, ToggleableOutputPin};
1010
use panic_rtt_target as _;
1111
use rtt_target::{rprintln, rtt_init_print};
12-
use va108xx_hal::gpio::{porta, portb, PinState};
13-
use va108xx_hal::prelude::*;
12+
use va108xx_hal::gpio::{PinState, PinsA, PinsB};
1413

1514
#[allow(dead_code)]
1615
#[derive(Debug)]
@@ -20,6 +19,8 @@ enum TestCase {
2019
TestPullup,
2120
TestPulldown,
2221
TestMask,
22+
// Tie PORTB[22] to PORTB[23] for this test
23+
PortB,
2324
Perid,
2425
// Tie PA0 to an oscilloscope and configure pulse detection
2526
Pulse,
@@ -32,11 +33,9 @@ fn main() -> ! {
3233
rtt_init_print!();
3334
rprintln!("-- VA108xx Test Application --");
3435
let mut dp = va108xx::Peripherals::take().unwrap();
35-
let porta = dp.PORTA.split(&mut dp.SYSCONFIG).unwrap();
36-
let _portb = dp.PORTB.split(&mut dp.SYSCONFIG).unwrap();
37-
let mut led1 = porta
38-
.pa10
39-
.into_push_pull_output(&mut dp.IOCONFIG, &mut dp.PORTA);
36+
let pinsa = PinsA::new(&mut dp.SYSCONFIG, None, dp.PORTA);
37+
let pinsb = PinsB::new(&mut dp.SYSCONFIG, Some(dp.IOCONFIG), dp.PORTB);
38+
let mut led1 = pinsa.pa10.into_push_pull_output();
4039
let test_case = TestCase::Delay;
4140

4241
match test_case {
@@ -56,101 +55,80 @@ fn main() -> ! {
5655
match test_case {
5756
TestCase::TestBasic => {
5857
// Tie PORTA[0] to PORTA[1] for these tests!
59-
let mut out = porta
60-
.pa0
61-
.into_push_pull_output(&mut dp.IOCONFIG, &mut dp.PORTA)
62-
.enable_input(&mut dp.IOCONFIG, true);
63-
let input = porta
64-
.pa1
65-
.into_floating_input(&mut dp.IOCONFIG, &mut dp.PORTA);
58+
let mut out = pinsa.pa0.into_readable_push_pull_output();
59+
let input = pinsa.pa1.into_floating_input();
6660
out.set_high().unwrap();
67-
assert!(out.is_set_high().unwrap());
6861
assert!(input.is_high().unwrap());
6962
out.set_low().unwrap();
70-
assert!(out.is_set_low().unwrap());
7163
assert!(input.is_low().unwrap());
7264
}
7365
TestCase::TestPullup => {
7466
// Tie PORTA[0] to PORTA[1] for these tests!
75-
let input = porta
76-
.pa1
77-
.into_pull_up_input(&mut dp.IOCONFIG, &mut dp.PORTA);
67+
let input = pinsa.pa1.into_pull_up_input();
7868
assert!(input.is_high().unwrap());
79-
let mut out = porta
80-
.pa0
81-
.into_push_pull_output(&mut dp.IOCONFIG, &mut dp.PORTA);
69+
let mut out = pinsa.pa0.into_readable_push_pull_output();
8270
out.set_low().unwrap();
8371
assert!(input.is_low().unwrap());
8472
out.set_high().unwrap();
8573
assert!(input.is_high().unwrap());
86-
out.into_floating_input(&mut dp.IOCONFIG, &mut dp.PORTA);
74+
out.into_floating_input();
8775
assert!(input.is_high().unwrap());
8876
}
8977
TestCase::TestPulldown => {
9078
// Tie PORTA[0] to PORTA[1] for these tests!
91-
let input = porta
92-
.pa1
93-
.into_pull_down_input(&mut dp.IOCONFIG, &mut dp.PORTA);
79+
let input = pinsa.pa1.into_pull_down_input();
9480
assert!(input.is_low().unwrap());
95-
let mut out = porta
96-
.pa0
97-
.into_push_pull_output(&mut dp.IOCONFIG, &mut dp.PORTA);
81+
let mut out = pinsa.pa0.into_push_pull_output();
9882
out.set_low().unwrap();
9983
assert!(input.is_low().unwrap());
10084
out.set_high().unwrap();
10185
assert!(input.is_high().unwrap());
102-
out.into_floating_input(&mut dp.IOCONFIG, &mut dp.PORTA);
86+
out.into_floating_input();
10387
assert!(input.is_low().unwrap());
10488
}
10589
TestCase::TestMask => {
10690
// Tie PORTA[0] to PORTA[1] for these tests!
107-
let input = porta
108-
.pa1
109-
.into_pull_down_input(&mut dp.IOCONFIG, &mut dp.PORTA)
110-
.clear_datamask(&mut dp.PORTA);
111-
assert!(!input.datamask(&dp.PORTA));
112-
let out = porta
113-
.pa0
114-
.into_push_pull_output(&mut dp.IOCONFIG, &mut dp.PORTA)
115-
.clear_datamask(&mut dp.PORTA);
116-
assert!(input.is_low_masked(&mut dp.PORTA).is_err());
117-
assert!(out.set_high_masked(&mut dp.PORTA).is_err());
91+
let input = pinsa.pa1.into_pull_down_input().clear_datamask();
92+
assert!(!input.datamask());
93+
let mut out = pinsa.pa0.into_push_pull_output().clear_datamask();
94+
assert!(input.is_low_masked().is_err());
95+
assert!(out.set_high_masked().is_err());
96+
}
97+
TestCase::PortB => {
98+
// Tie PORTB[22] to PORTB[23] for these tests!
99+
let mut out = pinsb.pb22.into_readable_push_pull_output();
100+
let input = pinsb.pb23.into_floating_input();
101+
out.set_high().unwrap();
102+
assert!(input.is_high().unwrap());
103+
out.set_low().unwrap();
104+
assert!(input.is_low().unwrap());
118105
}
119106
TestCase::Perid => {
120-
assert_eq!(porta::get_perid(&dp.PORTA), 0x004007e1);
121-
assert_eq!(portb::get_perid(&dp.PORTB), 0x004007e1);
107+
assert_eq!(PinsA::get_perid(), 0x004007e1);
108+
assert_eq!(PinsB::get_perid(), 0x004007e1);
122109
}
123110
TestCase::Pulse => {
124-
let mut output_pulsed = porta
111+
let mut output_pulsed = pinsa
125112
.pa0
126-
.into_push_pull_output(&mut dp.IOCONFIG, &mut dp.PORTA)
127-
.pulse_mode(&mut dp.PORTA, true, PinState::Low);
113+
.into_push_pull_output()
114+
.pulse_mode(true, PinState::Low);
128115
rprintln!("Pulsing high 10 times..");
129116
output_pulsed.set_low().unwrap();
130117
for _ in 0..10 {
131118
output_pulsed.set_high().unwrap();
132119
cortex_m::asm::delay(25_000_000);
133120
}
134-
let mut output_pulsed = output_pulsed.pulse_mode(&mut dp.PORTA, true, PinState::High);
121+
let mut output_pulsed = output_pulsed.pulse_mode(true, PinState::High);
135122
rprintln!("Pulsing low 10 times..");
136123
for _ in 0..10 {
137124
output_pulsed.set_low().unwrap();
138125
cortex_m::asm::delay(25_000_000);
139126
}
140127
}
141128
TestCase::Delay => {
142-
let mut out_0 = porta
143-
.pa0
144-
.into_push_pull_output(&mut dp.IOCONFIG, &mut dp.PORTA)
145-
.delay(&mut dp.PORTA, true, false);
146-
let mut out_1 = porta
147-
.pa1
148-
.into_push_pull_output(&mut dp.IOCONFIG, &mut dp.PORTA)
149-
.delay(&mut dp.PORTA, false, true);
150-
let mut out_2 = porta
151-
.pa3
152-
.into_push_pull_output(&mut dp.IOCONFIG, &mut dp.PORTA)
153-
.delay(&mut dp.PORTA, true, true);
129+
let mut out_0 = pinsa.pa0.into_push_pull_output().delay(true, false);
130+
let mut out_1 = pinsa.pa1.into_push_pull_output().delay(false, true);
131+
let mut out_2 = pinsa.pa3.into_push_pull_output().delay(true, true);
154132
for _ in 0..20 {
155133
out_0.toggle().unwrap();
156134
out_1.toggle().unwrap();

examples/uart.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ use core::fmt::Write;
77
use cortex_m_rt::entry;
88
use panic_rtt_target as _;
99
use rtt_target::{rprintln, rtt_init_print};
10-
use va108xx_hal::{pac, prelude::*, uart};
10+
use va108xx_hal::{gpio::PinsB, pac, prelude::*, uart};
1111

1212
#[entry]
1313
fn main() -> ! {
1414
rtt_init_print!();
15-
rprintln!("-- VA108xx UART test application--");
15+
rprintln!("-- VA108xx UART example application--");
1616

1717
let mut dp = pac::Peripherals::take().unwrap();
1818

19-
let gpiob = dp.PORTB.split(&mut dp.SYSCONFIG).unwrap();
20-
let tx = gpiob.pb21.into_funsel_1(&mut dp.IOCONFIG);
21-
let rx = gpiob.pb20.into_funsel_1(&mut dp.IOCONFIG);
19+
let gpiob = PinsB::new(&mut dp.SYSCONFIG, Some(dp.IOCONFIG), dp.PORTB);
20+
let tx = gpiob.pb21.into_funsel_1();
21+
let rx = gpiob.pb20.into_funsel_1();
2222

2323
let uartb = uart::Uart::uartb(
2424
dp.UARTB,

0 commit comments

Comments
 (0)