Skip to content

Commit

Permalink
refactor: rewrite using qingke-rt
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Jan 1, 2024
1 parent ad102fe commit 380af5c
Show file tree
Hide file tree
Showing 35 changed files with 361 additions and 586 deletions.
17 changes: 10 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ embassy = ["dep:embassy-time", "dep:embassy-sync"]
ble = []

[dependencies]
#ch58x = { version = "0.2.0", path = "../ch32-rs-nightlies/ch58x", features = [
# "ch58x",
#] }
ch58x = { version = "0.2.0", features = ["ch58x"] }
ch58x = { version = "0.3.0", features = ["ch58x", "rt"] }

critical-section = "1.1.2"
qingke = { version = "0.1.6", features = ["critical-section-impl"] }
# ch32v-rt = { version = "0.0.2", path = "../ch32v-rt" }
ch32v-rt = "0.0.2"
qingke = { version = "0.1.7", features = ["critical-section-impl"] }
qingke-rt = { version = "0.1.7", features = ["highcode"] }

# qingke = { version = "0.1.7", features = [
# "critical-section-impl",
# ], path = "../qingke" }
# qingke-rt = { version = "0.1.7", features = [
# "highcode",
# ], path = "../qingke/qingke-rt" }

fugit = "0.3.7"
nb = "1.1.0"
Expand Down
6 changes: 3 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ fn main() {
}

// Put the linker script somewhere the linker can find it.
fs::write(out_dir.join("link.x"), include_bytes!("link.x")).unwrap();
// fs::write(out_dir.join("link.x"), include_bytes!("link.x")).unwrap();

//fs::write(out_dir.join("memory.x"), include_bytes!("memory.x")).unwrap();
fs::write(out_dir.join("memory.x"), include_bytes!("memory.x")).unwrap();
println!("cargo:rustc-link-search={}", out_dir.display());

println!("cargo:rustc-link-search={}", out_dir.display());
//println!("cargo:rustc-link-search={}", out_dir.display());

println!("cargo:rerun-if-changed=memory.x");
println!("cargo:rerun-if-changed=build.rs");
Expand Down
6 changes: 3 additions & 3 deletions examples/adc_pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use hal::uart::UartTx;
use hal::{pac, peripherals, Peripherals};
use {ch58x_hal as hal, panic_halt as _};

#[ch32v_rt::entry]
#[qingke_rt::entry]
fn main() -> ! {
let mut config = hal::Config::default();
config.clock.use_pll_60mhz().enable_lse();
Expand Down Expand Up @@ -49,9 +49,9 @@ fn main() -> ! {
loop {
blue_led.toggle();

let data = adc.read(&mut pin);
let data = adc().read(&mut pin);
writeln!(serial, "adc raw data: {}", data).unwrap();
let vi = adc.read_as_millivolts(&mut pin);
let vi = adc().read_as_millivolts(&mut pin);
writeln!(serial, "Vbat voltage: {}mV", vi).unwrap();

writeln!(
Expand Down
6 changes: 3 additions & 3 deletions examples/adc_temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use hal::uart::UartTx;
use hal::{pac, peripherals, Peripherals};
use {ch58x_hal as hal, panic_halt as _};

#[ch32v_rt::entry]
#[qingke_rt::entry]
fn main() -> ! {
let mut config = hal::Config::default();
config.clock.use_pll_60mhz().enable_lse();
Expand Down Expand Up @@ -59,12 +59,12 @@ fn main() -> ! {

let now = rtc.now();

let raw_temp = adc.read(&mut temp_sensor);
let raw_temp = adc().read(&mut temp_sensor);
writeln!(serial, "ADC raw data: {}", raw_temp).unwrap();
let temp = adc_to_temperature_celsius(raw_temp);
writeln!(serial, "sensor temp: {}C", temp).unwrap();

let vi = adc.read_as_millivolts(&mut temp_sensor);
let vi = adc().read_as_millivolts(&mut temp_sensor);
writeln!(serial, "ADC voltage: {}mV", vi).unwrap();

writeln!(
Expand Down
4 changes: 2 additions & 2 deletions examples/ble-blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::mem::{size_of_val, MaybeUninit};
use core::sync::atomic::{AtomicBool, Ordering};
use core::{ptr, slice};

use ch32v_rt::highcode;
use qingke_rt::highcode;
use ch58x_hal as hal;
use embassy_executor::Spawner;
use embassy_futures::select::{select, Either};
Expand Down Expand Up @@ -680,7 +680,7 @@ async fn handle_tmos_event(event: &TmosEvent) {
}
}

#[embassy_executor::main(entry = "ch32v_rt::entry")]
#[embassy_executor::main(entry = "qingke_rt::entry")]
async fn main(spawner: Spawner) -> ! {
use hal::ble::ffi::*;

Expand Down
4 changes: 2 additions & 2 deletions examples/ble-broadcaster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![no_main]
#![feature(type_alias_impl_trait)]

use ch32v_rt::highcode;
use qingke_rt::highcode;
use ch58x_hal as hal;
use ch58x_hal::ble::gap::*;
use embassy_executor::Spawner;
Expand Down Expand Up @@ -126,7 +126,7 @@ async fn mainloop() -> ! {
}
}

#[embassy_executor::main(entry = "ch32v_rt::entry")]
#[embassy_executor::main(entry = "qingke_rt::entry")]
async fn main(spawner: Spawner) -> ! {
use hal::ble::ffi::*;

Expand Down
4 changes: 2 additions & 2 deletions examples/ble-led-button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use core::mem::size_of_val;
use core::sync::atomic::{AtomicBool, AtomicU16, Ordering};
use core::{ptr, slice};

use ch32v_rt::highcode;
use qingke_rt::highcode;
use ch58x_hal as hal;
use embassy_executor::Spawner;
use embassy_futures::select::{select, Either};
Expand Down Expand Up @@ -588,7 +588,7 @@ async fn button_task(pin: AnyPin) {
}
}

#[embassy_executor::main(entry = "ch32v_rt::entry")]
#[embassy_executor::main(entry = "qingke_rt::entry")]
async fn main(spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.clock.use_pll_60mhz().enable_lse();
Expand Down
4 changes: 2 additions & 2 deletions examples/ble-peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use core::ffi::c_void;
use core::mem::size_of_val;

use ch32v_rt::highcode;
use qingke_rt::highcode;
use ch58x_hal as hal;
use embassy_executor::Spawner;
use embassy_time::{Delay, Duration, Instant, Timer};
Expand Down Expand Up @@ -220,7 +220,7 @@ async fn peripheral(task_id: u8, subscriber: ble::EventSubscriber) {
}
}

#[embassy_executor::main(entry = "ch32v_rt::entry")]
#[embassy_executor::main(entry = "qingke_rt::entry")]
async fn main(spawner: Spawner) -> ! {
use hal::ble::ffi::*;

Expand Down
2 changes: 1 addition & 1 deletion examples/ble-scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async fn observer() {
}
}

#[embassy_executor::main(entry = "ch32v_rt::entry")]
#[embassy_executor::main(entry = "qingke_rt::entry")]
#[highcode]
async fn main(spawner: Spawner) -> ! {
use hal::ble::ffi::*;
Expand Down
78 changes: 10 additions & 68 deletions examples/blinky.rs
Original file line number Diff line number Diff line change
@@ -1,86 +1,28 @@
#![no_std]
#![no_main]

use core::arch::{asm, global_asm};
use core::fmt::Write;
use core::writeln;

use embedded_hal_1::delay::DelayNs;
use hal::gpio::{AnyPin, Input, Level, Output, OutputDrive, Pull};
use hal::interrupt::Interrupt;
use hal::isp::EEPROM_BLOCK_SIZE;
use hal::rtc::{DateTime, Rtc};
use hal::sysctl::Config;
use hal::uart::UartTx;
use hal::{pac, peripherals, Peripherals};
use hal::delay::CycleDelay;
use hal::gpio::{Level, Output, OutputDrive};
use {ch58x_hal as hal, panic_halt as _};

#[ch32v_rt::entry]
#[qingke_rt::entry]
fn main() -> ! {
// LED PA8
// hal::sysctl::Config::pll_60mhz().freeze();
///hal::sysctl::Config::pll_60mhz().use_lse().freeze();
let mut config = hal::Config::default();
config.clock.use_pll_60mhz().enable_lse();
config.enable_dcdc = true;
config.low_power = true;
config.clock.enable_lse();

//let p = Peripherals::take();
let p = hal::init(config);

let mut pa8 = Output::new(p.PA8, Level::Low, OutputDrive::_5mA);

let mut download_button = Input::new(p.PB22, Pull::Up);
let mut reset_button = Input::new(p.PB23, Pull::Up);

let mut uart = UartTx::new(p.UART1, p.PA9, Default::default()).unwrap();

writeln!(uart, "\n\n\nHello World!").unwrap();
writeln!(uart, "Clocks: {}", hal::sysctl::clocks().hclk).unwrap();
writeln!(uart, "ChipID: {:02x}", hal::signature::get_chip_id());

let mut rtc = Rtc;
let mut delay = CycleDelay;

//unsafe {
// hal::interrupt::RTC::enable();
//}
//rtc.enable_timing(hal::rtc::TimingMode::_2S);
//F let pfic = unsafe { &*pac::PFIC::PTR };
// unsafe { pfic.ienr1.write(|w| w.bits(1 << 28)) }; // enable rtc irq
let mut rtc = hal::rtc::Rtc;

/*
rtc.set_datatime(DateTime {
year: 2023,
month: 10,
day: 12,
hour: 18,
minute: 45,
second: 0,
});
*/

// let buf = hal::isp::read_eeprom(0x0, 500);
// writeln!(uart, "read flash: {:02x?}", buf).unwrap();

// ISP functions
// LED PA8
// let mut led = Output::new(p.PA8, Level::Low, OutputDrive::_5mA);
let mut led = Output::new(p.PB18, Level::Low, OutputDrive::_5mA);

loop {
unsafe {
pa8.toggle();

// writeln!(uart, "day {:?}", rtc.counter_day()).unwrap();
// writeln!(uart, "2s {:?}", rtc.counter_2s()).unwrap();

// writeln!(uart, "tick! {}", SysTick::now()).unwrap();
//delay.delay_ms(1000);
hal::delay_ms(1000);

writeln!(uart, "toggle led").unwrap();
led.toggle();

if reset_button.is_low() {
writeln!(uart, "button: {} {}", download_button.is_low(), reset_button.is_low()).unwrap();
}
}
delay.delay_ms(1000);
}
}
2 changes: 1 addition & 1 deletion examples/embassy-blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async fn blink(pin: AnyPin) {
}
}

#[embassy_executor::main(entry = "ch32v_rt::entry")]
#[embassy_executor::main(entry = "qingke_rt::entry")]
async fn main(spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.clock.use_pll_60mhz();
Expand Down
2 changes: 1 addition & 1 deletion examples/embassy-gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async fn reset_if_requested(pin: AnyPin) {
}
}

#[embassy_executor::main(entry = "ch32v_rt::entry")]
#[embassy_executor::main(entry = "qingke_rt::entry")]
async fn main(spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.clock.use_pll_60mhz().enable_lse();
Expand Down
2 changes: 1 addition & 1 deletion examples/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ macro_rules! println {
}
}

#[ch32v_rt::entry]
#[qingke_rt::entry]
fn main() -> ! {
// LED PA8

Expand Down
4 changes: 2 additions & 2 deletions examples/flashisp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![no_std]
#![no_main]

use ch32v_rt::highcode;
use qingke_rt::highcode;
use embedded_hal_1::delay::DelayNs;
use hal::gpio::{Input, Level, Output, OutputDrive, Pull};
// use hal::interrupt::Interrupt;
Expand All @@ -25,7 +25,7 @@ macro_rules! println {
}
}

#[ch32v_rt::entry]
#[qingke_rt::entry]
#[highcode]
fn main() -> ! {
// LED PA8
Expand Down
6 changes: 3 additions & 3 deletions examples/hardfault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ macro_rules! println {
}
}

#[ch32v_rt::interrupt]
#[qingke_rt::interrupt]
fn RTC() {
let mut rtc = Rtc;

Expand All @@ -39,7 +39,7 @@ fn RTC() {
println!("Current time: {} weekday={}", now, now.isoweekday());
// writeln!(uart, "mepc: {:08x}", riscv::register::mepc::read()).unwrap();
}
#[ch32v_rt::interrupt]
#[qingke_rt::interrupt]
fn HardFault() {
let pa8 = unsafe { hal::peripherals::PA8::steal() };
let mut led = Output::new(pa8, Level::Low, OutputDrive::_20mA);
Expand Down Expand Up @@ -68,7 +68,7 @@ fn HardFault() {
}
}

#[ch32v_rt::entry]
#[qingke_rt::entry]
fn main() -> ! {
let mut config = hal::Config::default();
config.clock.use_pll_60mhz();
Expand Down
26 changes: 13 additions & 13 deletions examples/i2c-mpu6050.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,40 +263,40 @@ impl<'d> MPU6050<'d> {

/// Initialize device for active mode read of acclerometer, gyroscope, and temperature
fn init(&mut self, config: &Config) -> Result<(), MPU6050Error> {
if self.read_byte(regs::WHO_AM_I_MPU6050)? != 0x68 {
if self().read_byte(regs::WHO_AM_I_MPU6050)? != 0x68 {
return Err(MPU6050Error::WrongDevice);
}

// get stable time source
self.write_byte(regs::PWR_MGMT_1, 0x01)?; // Set clock source to be PLL with x-axis gyroscope reference, bits 2:0 = 001
self().write_byte(regs::PWR_MGMT_1, 0x01)?; // Set clock source to be PLL with x-axis gyroscope reference, bits 2:0 = 001

// Configure Gyro and Accelerometer
// Disable FSYNC and set accelerometer and gyro bandwidth to 44 and 42 Hz, respectively;
// DLPF_CFG = bits 2:0 = 010; this sets the sample rate at 1 kHz for both
// Maximum delay time is 4.9 ms corresponding to just over 200 Hz sample rate
self.write_byte(regs::CONFIG, 0x03)?;
self().write_byte(regs::CONFIG, 0x03)?;

// Set sample rate = gyroscope output rate/(1 + SMPLRT_DIV)
self.write_byte(regs::SMPLRT_DIV, 0x04)?; // Use a 200 Hz rate; the same rate set in CONFIG above
self().write_byte(regs::SMPLRT_DIV, 0x04)?; // Use a 200 Hz rate; the same rate set in CONFIG above

// Set gyroscope full scale range
// Range selects FS_SEL and AFS_SEL are 0 - 3, so 2-bit values are left-shifted into positions 4:3
let c = self.read_byte(regs::GYRO_CONFIG)?;
self.write_byte(regs::GYRO_CONFIG, c & !0xE0)?; // Clear self-test bits [7:5]
self.write_byte(regs::GYRO_CONFIG, c & !0x18)?; // Clear AFS bits [4:3]
self.write_byte(regs::GYRO_CONFIG, c | (config.gyro_scale as u8) << 3)?; // Set full scale range for the gyro
self().write_byte(regs::GYRO_CONFIG, c & !0xE0)?; // Clear self-test bits [7:5]
self().write_byte(regs::GYRO_CONFIG, c & !0x18)?; // Clear AFS bits [4:3]
self().write_byte(regs::GYRO_CONFIG, c | (config.gyro_scale as u8) << 3)?; // Set full scale range for the gyro

// Set accelerometer configuration
let c = self.read_byte(regs::ACCEL_CONFIG)?;
self.write_byte(regs::ACCEL_CONFIG, c & !0xE0)?; // Clear self-test bits [7:5]
self.write_byte(regs::ACCEL_CONFIG, c & !0x18)?; // Clear AFS bits [4:3]
self.write_byte(regs::ACCEL_CONFIG, c | (config.accel_scale as u8) << 3)?; // Set full scale range for the accelerometer
self().write_byte(regs::ACCEL_CONFIG, c & !0xE0)?; // Clear self-test bits [7:5]
self().write_byte(regs::ACCEL_CONFIG, c & !0x18)?; // Clear AFS bits [4:3]
self().write_byte(regs::ACCEL_CONFIG, c | (config.accel_scale as u8) << 3)?; // Set full scale range for the accelerometer

// Configure Interrupts and Bypass Enable
// Set interrupt pin active high, push-pull, and clear on read of INT_STATUS, enable I2C_BYPASS_EN so additional chips
// can join the I2C bus and all can be controlled by the Arduino as master
self.write_byte(regs::INT_PIN_CFG, 0x22)?;
self.write_byte(regs::INT_ENABLE, 0x01)?; // Enable data ready (bit 0) interrupt
self().write_byte(regs::INT_PIN_CFG, 0x22)?;
self().write_byte(regs::INT_ENABLE, 0x01)?; // Enable data ready (bit 0) interrupt

Ok(())
}
Expand All @@ -312,7 +312,7 @@ impl<'d> MPU6050<'d> {
}
}

#[ch32v_rt::entry]
#[qingke_rt::entry]
fn main() -> ! {
let mut config = hal::Config::default();
config.clock.use_pll_60mhz();
Expand Down
Loading

0 comments on commit 380af5c

Please sign in to comment.