Skip to content

Commit 8d49bf6

Browse files
committed
implement features-based board configuration
1 parent 3117dd7 commit 8d49bf6

8 files changed

Lines changed: 175 additions & 128 deletions

File tree

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@ heapless = "0.8.0"
3939
embuild = "0.33.0"
4040

4141
[patch.crates-io]
42-
embedded-svc = { git = "ssh://git@github.com/PixelWeatherProject/embedded-svc.git", rev = "29afd95" }
42+
embedded-svc = { git = "ssh://git@github.com/PixelWeatherProject/embedded-svc.git", rev = "29afd95" }
43+
44+
[features]
45+
lilygo-t7s3 = []
46+
xiao-s3 = []
47+
arduino-nano-esp32 = []

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ The default battery voltage measurement configuration has a measured inaccuracy
109109

110110
If you just want to build the image, use the following command (for example):
111111
```sh
112-
cargo espflash save-image -T partitions.csv --frozen --locked --release --chip esp32s3 --merge image.bin
112+
cargo espflash save-image --features $BOARD -T partitions.csv --frozen --locked --release --chip esp32s3 --merge image.bin
113113
```
114114

115115
To directly flash the firmware, use the command below. **Remember to change the serial port for your machine.**
116116
```sh
117-
cargo espflash flash -T partitions.csv --frozen --locked --release -c esp32s3 --noverify --erase-data-parts otadata -B 921600 -p /dev/ttyXXXX
117+
cargo espflash flash --features $BOARD -T partitions.csv --frozen --locked --release -c esp32s3 --noverify --erase-data-parts otadata -B 921600 -p /dev/ttyXXXX
118118
```
119119

120120
If you notice weird/buggy bevaiour, you can erase the entire flash like so:
@@ -131,6 +131,7 @@ cargo espflash erase-flash -c esp32s3 -p /dev/ttyACM0 -B 921600
131131
Depending on which ESP32S3 development board you're using, you may need to add additional arguments to the two example commands above (especially `flash`).
132132
- `-c esp32s3`
133133
- `-s 16mb` / `-s 8mb` - For 16MB and 8MB of flash respectively.
134+
- `--features` - Check section below.
134135

135136
To build a debug image (or flash it) remove the `--release` flag from the above commands.
136137

@@ -146,7 +147,7 @@ To build a debug image (or flash it) remove the `--release` flag from the above
146147
No changes are needed.
147148

148149
### GPIO Pins
149-
The default pin configuration of PWOS is designed for this board. No changes are needed.
150+
The default pin configuration of PWOS is designed for this board. No changes are needed. You may also leave out the `--features` flag for `espflash`.
150151

151152
- On-board LED: `GPIO_17`
152153
- I2C SDA: `GPIO_5`
@@ -155,9 +156,9 @@ To build a debug image (or flash it) remove the `--release` flag from the above
155156

156157
### `espflash` commands
157158
- For saving as image:
158-
- `cargo espflash save-image --frozen --locked --release -T partitions.csv -s 16mb --chip esp32s3 image.bin`
159+
- `cargo espflash save-image --features lilygo-t7s3 --frozen --locked --release -T partitions.csv -s 16mb --chip esp32s3 image.bin`
159160
- For flashing:
160-
- `cargo espflash flash --frozen --locked --release -T partitions.csv -s 16mb -c esp32s3 -B 921600 -p /dev/ttyXXXX -M --no-verify --erase-data-parts ota`
161+
- `cargo espflash flash --features lilygo-t7s3 --frozen --locked --release -T partitions.csv -s 16mb -c esp32s3 -B 921600 -p /dev/ttyXXXX -M --no-verify --erase-data-parts ota`
161162
</details>
162163

163164
<details>
@@ -170,17 +171,17 @@ To build a debug image (or flash it) remove the `--release` flag from the above
170171
```
171172

172173
### GPIO Pins
173-
The on-board LED is on a different pin. You'll need to set `LED_BUILTIN` in your sysconfig ([src/config/sys.rs](src/config/sys.rs)).
174+
The on-board LED is on a different pin.
174175
- On-board LED: `GPIO_48`
175176
- I2C SDA: `GPIO_5`
176177
- I2C SCL: `GPIO_8`
177178
- Battery measurement: `GPIO_2`
178179

179180
### `espflash` commands
180181
- For saving as image:
181-
- `cargo espflash save-image --frozen --locked --release -T partitions.csv -s 16mb --chip esp32s3 image.bin`
182+
- `cargo espflash save-image --features arduino-nano-esp32 --frozen --locked --release -T partitions.csv -s 16mb --chip esp32s3 image.bin`
182183
- For flashing:
183-
- `cargo espflash flash --frozen --locked --release -T partitions.csv -s 16mb -c esp32s3 -B 921600 -p /dev/ttyXXXX -M --no-verify --erase-data-parts ota`
184+
- `cargo espflash flash --features arduino-nano-esp32 --frozen --locked --release -T partitions.csv -s 16mb -c esp32s3 -B 921600 -p /dev/ttyXXXX -M --no-verify --erase-data-parts ota`
184185
</details>
185186

186187
<details>
@@ -194,7 +195,7 @@ To build a debug image (or flash it) remove the `--release` flag from the above
194195
No changes are needed.
195196

196197
### GPIO Pins
197-
The on-board LED is on a different pin. You'll need to set `LED_BUILTIN` in your sysconfig ([src/config/sys.rs](src/config/sys.rs)). Additionally, you'll also need to set `LED_BUILTIN_INVERT` to `true`, because the LED's negative terminal is connected to the GPIO pin.
198+
The on-board LED is on a different pin, and its negative terminal is connected to the GPIO pin, meaning it works with inverted logic.
198199

199200
- On-board LED: `GPIO_21`
200201
- I2C SDA: `GPIO_5`
@@ -203,9 +204,9 @@ To build a debug image (or flash it) remove the `--release` flag from the above
203204

204205
### `espflash` commands
205206
- For saving as image:
206-
- `cargo espflash save-image --frozen --locked --release -T partitions.csv -s 8mb --chip esp32s3 image.bin`
207+
- `cargo espflash save-image --features xiao-s3 --frozen --locked --release -T partitions.csv -s 8mb --chip esp32s3 image.bin`
207208
- For flashing:
208-
- `cargo espflash flash --frozen --locked --release -T partitions.csv -s 8mb -c esp32s3 -B 921600 -p /dev/ttyXXXX -M --no-verify --erase-data-parts ota`
209+
- `cargo espflash flash --features xiao-s3 --frozen --locked --release -T partitions.csv -s 8mb -c esp32s3 -B 921600 -p /dev/ttyXXXX -M --no-verify --erase-data-parts ota`
209210
</details>
210211

211212
## Build variants

src/config/sys.rs.example

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,4 @@ pub const WIFI_TIMEOUT: Duration = Duration::from_secs(10);
2626
pub const WIFI_COUNTRY_CODE: &str = "SK";
2727

2828
/// PWMP server configuration
29-
pub const PWMP_SERVER: &str = "123.456.789.000:55300";
30-
31-
/// Built-in LED pin number
32-
pub const LED_BUILTIN: u8 = 17;
33-
pub const LED_BUILTIN_INVERT: bool = true;
29+
pub const PWMP_SERVER: &str = "123.456.789.000:55300";

src/main.rs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,18 @@
22
#![feature(panic_payload_as_str)]
33

44
use crate::config::AppConfig;
5-
use config::LED_BUILTIN;
6-
use esp_idf_svc::{
7-
eventloop::EspSystemEventLoop,
8-
hal::{
9-
i2c::{config::Config, I2cDriver},
10-
peripherals::Peripherals,
11-
units::FromValueType,
12-
},
5+
use esp_idf_svc::hal::{
6+
gpio::IOPin,
7+
i2c::{config::Config, I2cDriver},
8+
units::FromValueType,
139
};
1410
use std::{panic::PanicHookInfo, time::Instant};
1511
use sysc::{
1612
battery::Battery,
17-
gpio,
1813
ledctl::BoardLed,
1914
logging::OsLogger,
2015
ota::Ota,
16+
periph::SystemPeripherals,
2117
power::{deep_sleep, fake_sleep},
2218
usbctl, OsError,
2319
};
@@ -65,15 +61,13 @@ fn main() {
6561
sysc::brownout::disable_brownout_detector();
6662
}
6763

68-
os_debug!("Initializing peripherals");
69-
let mut peripherals = Peripherals::take().expect("Failed to initialize peripherals");
70-
71-
os_debug!("Initializing System Event Loop");
72-
let sys_loop = EspSystemEventLoop::take().expect("SEL init error");
64+
os_debug!("Initializing system peripherals");
65+
let peripherals = SystemPeripherals::take();
7366

7467
os_debug!("Initializing system LED");
7568
let led = BoardLed::new(
76-
gpio::number_to_io_pin(LED_BUILTIN, &mut peripherals).expect("Invalid LED pin"),
69+
peripherals.onboard_led.pin.downgrade(),
70+
peripherals.onboard_led.invert,
7771
)
7872
.expect("Failed to set up onboard LED");
7973

@@ -102,14 +96,14 @@ fn main() {
10296
);
10397

10498
os_debug!("Initializing system Battery");
105-
let battery = Battery::new(peripherals.adc1, peripherals.pins.gpio2)
99+
let battery = Battery::new(peripherals.battery.adc, peripherals.battery.pin)
106100
.expect("Failed to initialize battery ADC");
107101

108102
os_debug!("Initializing I2C bus");
109103
let i2c = I2cDriver::new(
110-
peripherals.i2c1,
111-
peripherals.pins.gpio5,
112-
peripherals.pins.gpio8,
104+
peripherals.i2c.i2c,
105+
peripherals.i2c.sda,
106+
peripherals.i2c.scl,
113107
&Config::default().baudrate(400u32.kHz().into()),
114108
)
115109
.expect("Failed to initialize I2C");
@@ -123,8 +117,8 @@ fn main() {
123117
let fw_exit = firmware::fw_main(
124118
battery,
125119
i2c,
126-
peripherals.modem,
127-
sys_loop,
120+
peripherals.wifi.modem,
121+
peripherals.wifi.sys_loop,
128122
led,
129123
&mut ota,
130124
&mut appcfg,

src/sysc/gpio.rs

Lines changed: 0 additions & 80 deletions
This file was deleted.

src/sysc/ledctl.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use super::OsResult;
2-
use crate::config::LED_BUILTIN_INVERT;
32
use esp_idf_svc::hal::gpio::{AnyIOPin, Output, PinDriver};
43

54
type LedDriver = PinDriver<'static, AnyIOPin, Output>;
65

7-
pub struct BoardLed(LedDriver);
6+
pub struct BoardLed(LedDriver, bool);
87

98
impl BoardLed {
10-
pub fn new(pin: AnyIOPin) -> OsResult<Self> {
11-
let mut i = Self(PinDriver::output(pin)?);
9+
pub fn new(pin: AnyIOPin, invert: bool) -> OsResult<Self> {
10+
let mut i = Self(PinDriver::output(pin)?, invert);
1211
i.on();
1312

1413
Ok(i)
@@ -18,10 +17,10 @@ impl BoardLed {
1817
// They can be safely ignored. This also reduces the size of the firmware.
1918

2019
pub fn on(&mut self) {
21-
let _ = self.0.set_level((!LED_BUILTIN_INVERT).into());
20+
let _ = self.0.set_level((!self.1).into());
2221
}
2322

2423
pub fn off(&mut self) {
25-
let _ = self.0.set_level(LED_BUILTIN_INVERT.into());
24+
let _ = self.0.set_level(self.1.into());
2625
}
2726
}

src/sysc/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@ pub mod battery;
33
pub mod brownout;
44
mod error;
55
pub mod ext_drivers;
6-
pub mod gpio;
76
pub mod ledctl;
87
pub mod logging;
98
mod macros;
109
pub mod net;
1110
pub mod ota;
11+
pub mod periph;
1212
pub mod power;
1313
pub mod usbctl;
1414

15-
use std::{borrow::Cow, ffi::CStr, ptr::NonNull};
16-
1715
pub use error::{OsError, ReportableError};
1816
pub type OsResult<T> = ::std::result::Result<T, OsError>;
1917

18+
#[cfg(debug_assertions)]
2019
/// Try and get the ESP-IDF version.
21-
pub fn get_idf_version() -> Option<Cow<'static, str>> {
20+
pub fn get_idf_version() -> Option<std::borrow::Cow<'static, str>> {
21+
use std::{ffi::CStr, ptr::NonNull};
22+
2223
let version_str_ptr =
2324
NonNull::new(unsafe { esp_idf_svc::sys::esp_get_idf_version().cast_mut() })?;
2425
let version_str = unsafe { CStr::from_ptr(version_str_ptr.as_ptr().cast_const()) };

0 commit comments

Comments
 (0)