Skip to content

enhance(spi): implement spi for embedded-hal-async#151

Open
bdelwood wants to merge 1 commit intoch32-rs:mainfrom
bdelwood:main
Open

enhance(spi): implement spi for embedded-hal-async#151
bdelwood wants to merge 1 commit intoch32-rs:mainfrom
bdelwood:main

Conversation

@bdelwood
Copy link

Essentially this PR, but for SPI.

Tested on a ch32v003 with this small example and checked with a logic analyzer:

#![no_std]
#![no_main]

use ch32_hal as hal;

use embassy_executor::Spawner;
use embassy_time::{Delay, Timer};
use embedded_hal_async::spi::SpiDevice;
use embedded_hal_bus::spi::ExclusiveDevice;
use hal::gpio::{Level, Output};
use hal::prelude::*;
use hal::spi::Spi;
use panic_halt as _;
use qingke_rt;

#[embassy_executor::main(entry = "qingke_rt::entry")]
async fn main(_spawner: Spawner) {
    let config = hal::Config::default();
    let p = hal::init(config);

    let sck = p.PC5;
    let sda = p.PC6;
    let dma_spi = p.DMA1_CH3;

    let mut led: Output<'_> = Output::new(p.PC4, Level::Low, Default::default());
    let cs = Output::new(p.PC7, Level::High, Default::default());

    let mut spi_config = hal::spi::Config::default();
    spi_config.frequency = Hertz::mhz(1);

    let spi_bus = Spi::new_txonly::<0>(p.SPI1, sck, sda, dma_spi, spi_config);

    let mut spi = ExclusiveDevice::new(spi_bus, cs, Delay).unwrap();

    let buf: [u8; 2] = [0xDE, 0xAD];

    loop {
        spi.write(&buf).await.unwrap();

        Timer::after_millis(100).await;
        led.toggle();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant