ch58x-hal 0.0.2

HAL for the CH583/CH582/CH581 RISC-V BLE microcotrollers from WCH
Documentation
//! Joystick module - ADC
//!
/*
3 axes: X, Y (potentiometers) and Z (button)
Potentiometers (X and Y): 2 x 5Kohm
Button: normally open (Z)
Strip Connection: 2.54mm
Dimensions: 37 x 32 x 25 mm
Weight: 15g
*/

#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]

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

use embassy_executor::Spawner;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::channel::Channel;
use embassy_time::Timer;
use embedded_hal_1::delay::DelayNs;
use hal::adc::Adc;
use hal::gpio::{AnyPin, Input, Level, Output, OutputDrive, Pin, 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::{adc, pac, peripherals, println, Peripherals};
use {ch58x_hal as hal, panic_halt as _};

#[embassy_executor::main(entry = "qingke_rt::entry")]
async fn main(spawner: Spawner) -> ! {
    // 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 = hal::init(config);
    hal::embassy::init();

    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();
    unsafe {
        hal::set_default_serial(uart);
    }

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

    let mut adc_config = adc::Config::default();
    adc_config.pga_gain = adc::Gain::GAIN1_4; // -12dB, 1/4. -0.2V to 3.3V+0.2V
    let mut adc = Adc::new(p.ADC, adc_config);

    // conflicts with UartRx
    let mut px = p.PA4;
    let mut py = p.PA5;

    loop {
        led.toggle();

        let vx = adc.read_as_millivolts(&mut px);
        let vy = adc.read_as_millivolts(&mut py);

        println!("ADC: {} {} ", vx, vy);

        Timer::after_millis(500).await;
    }
}