Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deps: refactor I2C and SPI, update dependencies, and enhance CI for examples #4

Merged
merged 4 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions .github/workflows/Cargo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,17 @@ jobs:
- name: Rustfmt Check
uses: actions-rust-lang/rustfmt@v1

build-py32f030-hal:
name: Build
build:
name: Build examples
needs: fmt
runs-on: ubuntu-latest
strategy:
matrix:
TARGET: [thumbv6m-none-eabi]
TOOLCHAIN: [nightly]
EXAMPLES: [adc_block, # adc_block_interrupt_closure,
advanced_timer_block,
advanced_timer_block_2, bit_test, blinky, block_uart, clock, crc,
dma_mem2mem, embassy_adc, embassy_allpin, embassy_blinky, embassy_delay,
embassy_dma_mem2mem, embassy_exit, embassy_i2c, embassy_iwdg, embassy_pwm,
embassy_rtc, embassy_ssd1309, embassy_uart, flash, hello_world,
i2c_master_block, key, rtc_block, timer3_block, timer3_pwm_block, uart,
uart_defmt]
steps:
- uses: actions/checkout@v4
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
target: ${{ MATRIX.TARGET }}
toolchain: ${{ MATRIX.TOOLCHAIN }}
- name: Run build
run: cargo build --target ${{ MATRIX.TARGET }} --release --example ${{ MATRIX.EXAMPLES }}
target: thumbv6m-none-eabi
toolchain: nightly
- name: Build under default features
run: cargo build --examples --target thumbv6m-none-eabi
# - name: Build under no embassy feature
# run: cargo build --examples --no-default-features --target thumbv6m-none-eabi
102 changes: 61 additions & 41 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ repository = "https://github.com/hysonglet/py32f030-hal"
documentation = "https://docs.rs/py32f030-hal"
readme = "README.md"


# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Expand All @@ -25,56 +24,24 @@ embedded-io = "0.6.1"
embedded-io-async = { version = "0.6.1", option = true }
nb = "1"
void = { version = "1.0", default-features = false }
panic-halt = { version = "0.2.0", optional = true }
panic-probe = { version = "0.3", features = ["print-defmt"], optional = true }
defmt = { version = "0.3.2", optional = true }
defmt-rtt = { version = "0.4", optional = true }
cortex-m = { version = "0.7.7", features = [
"critical-section-single-core",
"inline-asm",
] }
cortex-m = { version = "0.7.7", features = ["critical-section-single-core", "inline-asm"] }
enumset = "1.1.3"
alloc-cortex-m = "0.4.2"
atomic-polyfill = "1.0.1"
PY32f030xx-pac = { version = "0.1.0", features = ["rt"] }
num-traits = { version = "0.2", default-features = false }
heapless = "0.7"
embassy-hal-internal = { version = "0.1.0", features = [
"cortex-m",
"prio-bits-3",
] }
embassy-hal-internal = { version = "0.1.0", features = ["cortex-m", "prio-bits-3"] }
embassy-time-driver = { version = "0.1.0", default-features = false, optional = true }
embassy-sync = { version = "0.5.0", optional = true }
embassy-executor = { version = "0.5.0", optional = true, features = [
"arch-cortex-m",
"executor-thread",
] }
embassy-time = { version = "0.3.0", features = [
"generic-queue-16",
], optional = true }
embassy-futures = { version = "0.1.0" }
# ssd1309 = {version = "0.4.0", optional = true}
# display-interface = "^ 0.4"
# embedded-graphics-core = { version = "^ 0.4", optional = true }
# embedded-graphics = {version ="0.8.1", optional = true}
display-interface = {version = "0.4.1", optional = true}
# oled_async = "0.1.0-alpha1"
embedded-graphics = "0.7.0"
ssd1309 = "0.3.0"
embedded-graphics-core = { version = "^ 0.3", optional = true }
display-interface-i2c = "^ 0.4"
embassy-time = { version = "0.3.0", features = ["generic-queue-16"], optional = true }
fugit = { version = "0.3.6" }
fugit-timer = "0.1.3"
drop-move = "0.1.0"
defmt-serial = "0.10.0"
static_cell = "1"
embedded-time = "0.12.1"

[features]
default = ["example", "embassy"] # "embassy"
example = ["dep:defmt", "dep:defmt-rtt", "dep:panic-probe", "dep:panic-halt"]
default = ["embassy"]
embassy = [
"dep:embassy-executor",
"dep:embassy-sync",
"dep:embassy-time",
"time-driver",
Expand All @@ -83,23 +50,76 @@ embassy = [
time-driver = ["dep:embassy-time-driver", "embassy-time-driver/tick-hz-1_000"]

[dev-dependencies]
defmt = "0.3.2"
defmt-rtt = "0.4"
defmt-serial = "0.10.0"
panic-halt = "0.2.0"
panic-probe = { version = "0.3", features = ["print-defmt"] }
ssd1309 = "0.3.0"
# ssd1309 = { version = "0.4.0", optional = true }
# display-interface = "^0.4"
display-interface = "0.4.1"
display-interface-i2c = "^0.4"
# oled_async = "0.1.0-alpha.1"
# embedded-graphics-core = { version = "^0.4", optional = true }
# embedded-graphics = { version ="0.8.1", optional = true }
embedded-graphics = "0.7.0"
embedded-graphics-core = "^0.3"
embassy-executor = { version = "0.5.0", features = ["arch-cortex-m", "executor-thread"] }
embassy-futures = "0.1.0"
alloc-cortex-m = "0.4.2"
static_cell = "1"

[[example]]
name = "blinky"
name = "embassy_adc"
required-features = ["embassy"]

[[example]]
name = "embassy_uart"
name = "embassy_allpin"
required-features = ["embassy"]

[[example]]
name = "embassy_blinky"
required-features = ["embassy"]

[[example]]
name = "embassy_delay"
required-features = ["embassy"]

[[example]]
name = "crc"
name = "embassy_dma_mem2mem"
required-features = ["embassy"]

[[example]]
name = "embassy_adc"
name = "embassy_exit"
required-features = ["embassy"]

[[example]]
name = "embassy_i2c"
required-features = ["embassy"]

[[example]]
name = "embassy_iwdg"
required-features = ["embassy"]

[[example]]
name = "embassy_pwm"
required-features = ["embassy"]

[[example]]
name = "embassy_rtc"
required-features = ["embassy"]

[[example]]
name = "embassy_ssd1309"
required-features = ["embassy"]

[[example]]
name = "embassy_uart"
required-features = ["embassy"]

[[example]]
name = "crc"
required-features = ["embassy"]

[profile.dev]
Expand Down
2 changes: 2 additions & 0 deletions examples/adc_block_interrupt_closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
// 使用闭包的方式在中断中调用闭包处理函数
// 兼顾友好型 api
static mut QUEUE: [u16; 16] = [0; 16];
// Ensure this example builds under compile configurations with embassy feature
#[cfg(not(feature = "embassy"))]
adc.on_interrupt(
Event::EOC.into(), /* EOC 中断 */
alloc::boxed::Box::new(move |adc| {
Expand Down Expand Up @@ -73,9 +75,9 @@

defmt::info!(
"adc {:?} sum: {} avrage: {}",
Debug2Format(unsafe { &QUEUE }),

Check warning on line 78 in examples/adc_block_interrupt_closure.rs

View workflow job for this annotation

GitHub Actions / Build examples

creating a shared reference to mutable static is discouraged
unsafe { QUEUE.iter().sum::<u16>() },

Check warning on line 79 in examples/adc_block_interrupt_closure.rs

View workflow job for this annotation

GitHub Actions / Build examples

creating a shared reference to mutable static is discouraged
unsafe { QUEUE.iter().sum::<u16>() / QUEUE.len() as u16 }

Check warning on line 80 in examples/adc_block_interrupt_closure.rs

View workflow job for this annotation

GitHub Actions / Build examples

creating a shared reference to mutable static is discouraged

Check warning on line 80 in examples/adc_block_interrupt_closure.rs

View workflow job for this annotation

GitHub Actions / Build examples

creating a shared reference to mutable static is discouraged
);
}
}
27 changes: 27 additions & 0 deletions src/i2c/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
mode::{Blocking, Mode},
};
use core::marker::PhantomData;
use embedded_hal::i2c::Operation;

/// Master 角色
pub struct Master<'d, T: Instance, M: Mode> {
Expand Down Expand Up @@ -118,6 +119,32 @@ impl<'d, T: Instance> Master<'d, T, Blocking> {
}

////////////////////////////////////////////////////////////////////////////////

impl<'d, T: Instance> embedded_hal::i2c::ErrorType for Master<'d, T, Blocking> {
type Error = Error;
}

impl<'d, T: Instance> embedded_hal::i2c::I2c for Master<'d, T, Blocking> {
#[inline]
fn transaction(
&mut self,
address: u8,
operations: &mut [embedded_hal::i2c::Operation<'_>],
) -> Result<(), Self::Error> {
for op in operations {
match op {
Operation::Write(buf) => {
self.write_block(address, buf)?;
}
Operation::Read(buf) => {
self.read_block(address, buf)?;
}
}
}
Ok(())
}
}

impl<'d, T: Instance> embedded_hal_027::blocking::i2c::Write for Master<'d, T, Blocking> {
type Error = Error;
fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
Expand Down
8 changes: 4 additions & 4 deletions src/spi/hal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,19 @@ pub(super) mod sealed {

/// Clock polarity
#[inline]
fn set_clock_polarity(polarity: ClockPolarity) {
fn set_clock_polarity(polarity: embedded_hal::spi::Polarity) {
// This bit should not be changed when communication is ongoing
Self::block()
.cr1
.modify(|_, w| w.cpol().bit(polarity == ClockPolarity::Hight))
.modify(|_, w| w.cpol().bit(polarity == Polarity::IdleHigh))
}

/// Clock phase
#[inline]
fn set_clock_phase(phase: ClockPhase) {
fn set_clock_phase(phase: embedded_hal::spi::Phase) {
Self::block()
.cr1
.modify(|_, w| w.cpha().bit(phase == ClockPhase::Hight))
.modify(|_, w| w.cpha().bit(phase == Phase::CaptureOnSecondTransition))
}

/// Slave fast mode enable
Expand Down
21 changes: 5 additions & 16 deletions src/spi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::mode::Mode;
use core::marker::PhantomData;
use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef};

use embedded_hal_027::spi;
use embedded_hal::spi::{Phase, Polarity};
use types::*;

pub use master::Master;
Expand Down Expand Up @@ -67,19 +67,8 @@ impl<'d, T: Instance, M: Mode> AnySpi<'d, T, M> {
nss: Option<PeripheralRef<'d, AnyPin>>,
config: Config,
) -> Result<Self, Error> {
let phase = if config.mode.phase == spi::Phase::CaptureOnFirstTransition {
ClockPhase::Low
} else {
ClockPhase::Hight
};
let polarity = if config.mode.polarity == spi::Polarity::IdleLow {
ClockPolarity::Low
} else {
ClockPolarity::Hight
};

T::set_clock_phase(phase);
T::set_clock_polarity(polarity);
T::set_clock_phase(config.mode.phase);
T::set_clock_polarity(config.mode.polarity);
T::set_frame_format(config.bit_order);
T::set_baud_rate_div(config.baud_rate_div);

Expand All @@ -105,7 +94,7 @@ impl<'d, T: Instance, M: Mode> AnySpi<'d, T, M> {

sck.set_instance_af(
Speed::VeryHigh,
if config.mode.polarity == spi::Polarity::IdleLow {
if config.mode.polarity == Polarity::IdleLow {
PinIoType::PullDown
} else {
PinIoType::PullUp
Expand Down Expand Up @@ -169,7 +158,7 @@ pub struct Rx<'d, T: Instance, M: Mode> {
}

pub struct Config {
pub mode: spi::Mode,
pub mode: embedded_hal::spi::Mode,
pub bit_order: BitOrder,
pub baud_rate_div: BaudRateDiv,
pub data_len: DataLength,
Expand Down
20 changes: 1 addition & 19 deletions src/spi/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,6 @@ pub enum Rule {
Master = 1,
}

/// Clock polarity
#[derive(PartialEq, Clone, Copy)]
pub enum ClockPolarity {
/// 0: CK to 0 when idle
Low = 0,
/// 1: CK to 1 when idle
Hight = 1,
}

/// Clock phase
#[derive(PartialEq, Clone, Copy)]
pub enum ClockPhase {
/// 0: The first clock transition is the first data capture edge
Low = 0,
/// 1: The second clock transition is the first data capture edge
Hight = 1,
}

/// Slave fast mode enable
#[derive(PartialEq)]
pub enum SlaveSpeedMode {
Expand All @@ -114,7 +96,7 @@ pub enum SlaveSpeedMode {
#[derive(PartialEq)]
pub enum DataLength {
/// 0: 8-bit data frame transmission
Lenght8 = 0,
Length8 = 0,
/// 1: 16-bit data frame transmission
Length16 = 1,
}
Loading