diff --git a/Cargo.toml b/Cargo.toml index ed91325..4c8631d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ readme = "README.md" license = "MIT/Apache-2.0" [features] -default = ["ble"] +default = [] embassy = ["dep:embassy-time"] ble = [] @@ -74,6 +74,27 @@ required-features = ["embassy"] name = "embassy-ble-broadcaster" required-features = ["embassy", "ble"] +[[example]] +name = "embassy-ble-scanner" +required-features = ["embassy", "ble"] + +[[example]] +name = "ble-peripheral-simple" +required-features = ["embassy", "ble"] + +[[example]] +name = "ble-peripheral" +required-features = ["embassy", "ble"] + +# [[example]] +# name = "embassy-ble-sensor" +# required-features = ["embassy", "ble"] +# +# [[example]] +# name = "embassy-ble-sync-adv" +# required-features = ["embassy", "ble"] + + [profile.release] # panic = "unwind" panic = "abort" diff --git a/examples/ble-peripheral-simple.rs b/examples/ble-peripheral-simple.rs index 6b0e4af..0272e83 100644 --- a/examples/ble-peripheral-simple.rs +++ b/examples/ble-peripheral-simple.rs @@ -280,7 +280,8 @@ async fn read_rssi(conn_handle: u16) { unsafe { let r = GAPRole_ReadRssiCmd(conn_handle); if r.is_err() { - println!("!! GAPRole_ReadRssiCmd error: {:?}", r); + // normally it's already disconnected, quit + // println!("!! GAPRole_ReadRssiCmd error: {:?}", r); return; } } @@ -346,15 +347,15 @@ async fn peripheral(spawner: Spawner, task_id: u8, mut subscriber: ble::EventSub unsafe extern "C" fn on_rssi_read(conn_handle: u16, rssi: i8) { println!("RSSI -{} dB Conn {:x}", -rssi, conn_handle); } - unsafe extern "C" fn on_param_update(connHandle: u16, connInterval: u16, connSlaveLatency: u16, connTimeout: u16) { + unsafe extern "C" fn on_param_update(conn_handle: u16, interval: u16, slave_latency: u16, timeout: u16) { println!( "on_param_update Conn handle: {} inverval: {} timeout: {}", - connHandle, connInterval, connTimeout + conn_handle, interval, timeout ); } unsafe { - static bond_mgr_cb: gapBondCBs_t = gapBondCBs_t { + static BOND_MGR_CB: gapBondCBs_t = gapBondCBs_t { passcodeCB: None, pairStateCB: None, oobCB: None, @@ -362,13 +363,13 @@ async fn peripheral(spawner: Spawner, task_id: u8, mut subscriber: ble::EventSub // peripheralStateNotificationCB - static app_cb: gapRolesCBs_t = gapRolesCBs_t { + static APP_CB: gapRolesCBs_t = gapRolesCBs_t { pfnStateChange: Some(on_state_change), pfnRssiRead: Some(on_rssi_read), pfnParamUpdate: Some(on_param_update), }; // Start the Device - GAPRole_PeripheralStartDevice(task_id, &bond_mgr_cb, &app_cb); + GAPRole_PeripheralStartDevice(task_id, &BOND_MGR_CB, &APP_CB); } loop { @@ -470,13 +471,15 @@ async fn main(spawner: Spawner) -> ! { println!("System Clocks: {}", hal::sysctl::clocks().hclk); println!("ChipID: 0x{:02x}", hal::signature::get_chip_id()); println!("RTC datetime: {}", rtc.now()); + println!("MemFree: {}K", hal::stack_free() / 1024); spawner.spawn(blink(p.PA8.degrade())).unwrap(); // BLE part println!("BLE Lib Version: {}", ble::lib_version()); - let (task_id, sub) = hal::ble::init(Default::default()).unwrap(); + let mut ble_config = ble::Config::default(); + let (task_id, sub) = hal::ble::init(ble_config).unwrap(); println!("BLE task id: {}", task_id); unsafe { @@ -498,12 +501,11 @@ async fn main(spawner: Spawner) -> ! { #[highcode] #[embassy_executor::task] async fn tmos_mainloop() { + let mut ticker = Ticker::every(Duration::from_micros(300)); loop { - Timer::after(Duration::from_micros(300)).await; + ticker.next().await; unsafe { - hal::interrupt::SysTick::pend(); TMOS_SystemProcess(); - hal::interrupt::SysTick::unpend(); } } } diff --git a/examples/embassy-ble-broadcaster.rs b/examples/embassy-ble-broadcaster.rs index 9859597..8e58bab 100644 --- a/examples/embassy-ble-broadcaster.rs +++ b/examples/embassy-ble-broadcaster.rs @@ -4,6 +4,7 @@ use ch32v_rt::highcode; use ch58x_hal as hal; +use ch58x_hal::ble::gap::*; use embassy_executor::Spawner; use embassy_time::{Delay, Duration, Instant, Timer}; use hal::ble::ffi::*; diff --git a/examples/embassy-ble-scanner.rs b/examples/embassy-ble-scanner.rs index 4d5d52f..a35f67b 100644 --- a/examples/embassy-ble-scanner.rs +++ b/examples/embassy-ble-scanner.rs @@ -8,6 +8,7 @@ use embassy_executor::Spawner; use embassy_sync::channel::Channel; use embassy_time::{Delay, Duration, Instant, Timer}; use hal::ble::ffi::*; +use hal::ble::gap::*; use hal::ble::{get_raw_temperature, MacAddress}; use hal::gpio::{AnyPin, Input, Level, Output, OutputDrive, Pin, Pull}; use hal::interrupt::Interrupt; diff --git a/examples/uart-echo.rs b/examples/uart-echo.rs index c463494..0f56e24 100644 --- a/examples/uart-echo.rs +++ b/examples/uart-echo.rs @@ -3,7 +3,7 @@ use ch32v_rt::highcode; use ch58x_hal as hal; -use embedded_hal_1::delay::DelayUs; + use hal::gpio::{Level, Output, OutputDrive}; use hal::peripherals; use hal::rtc::Rtc; diff --git a/src/ble/ffi.rs b/src/ble/ffi.rs index 8f9f47e..3ddb0f7 100644 --- a/src/ble/ffi.rs +++ b/src/ble/ffi.rs @@ -508,11 +508,11 @@ pub const GAP_PHY_VAL_LE_2M: u16 = 2; pub const GAP_PHY_VAL_LE_CODED: u16 = 3; // GAP_ADVERTISEMENT_TYPE_DEFINES GAP Scan PHY VAL TYPE(GAP_PHY_BIT_TYPE) -pub const GAP_PHY_BIT_LE_1M: u32 = 1; -pub const GAP_PHY_BIT_LE_2M: u32 = 2; -pub const GAP_PHY_BIT_LE_CODED: u32 = 4; -pub const GAP_PHY_BIT_ALL: u32 = 7; -pub const GAP_PHY_BIT_LE_CODED_S2: u32 = 8; +pub const GAP_PHY_BIT_LE_1M: u16 = 1; +pub const GAP_PHY_BIT_LE_2M: u16 = 2; +pub const GAP_PHY_BIT_LE_CODED: u16 = 4; +pub const GAP_PHY_BIT_ALL: u16 = 7; +pub const GAP_PHY_BIT_LE_CODED_S2: u16 = 8; // PHY_OPTIONS preferred coding when transmitting on the LE Coded PHY pub const GAP_PHY_OPTIONS_NOPRE: u32 = 0; diff --git a/src/ble/gatt_uuid.rs b/src/ble/gatt_uuid.rs index 4ff6060..c9b5ed1 100644 --- a/src/ble/gatt_uuid.rs +++ b/src/ble/gatt_uuid.rs @@ -47,53 +47,38 @@ pub const LE_GATT_SEC_LEVELS_UUID: u16 = 0x2BF5; // LE GATT Security Levels // UUID defined extern "C" { + // [u8; 2] // GATT Services + /// 1800 pub static gapServiceUUID: [u8; 0usize]; - + /// 1801 pub static gattServiceUUID: [u8; 0usize]; // GATT Attribute Types pub static primaryServiceUUID: [u8; 0usize]; - pub static secondaryServiceUUID: [u8; 0usize]; - pub static includeUUID: [u8; 0usize]; - pub static characterUUID: [u8; 0usize]; // GATT Characteristic Descriptors pub static charExtPropsUUID: [u8; 0usize]; - pub static charUserDescUUID: [u8; 0usize]; - pub static clientCharCfgUUID: [u8; 0usize]; - pub static servCharCfgUUID: [u8; 0usize]; - pub static charFormatUUID: [u8; 0usize]; - pub static charAggFormatUUID: [u8; 0usize]; - pub static validRangeUUID: [u8; 0usize]; - pub static extReportRefUUID: [u8; 0usize]; - pub static reportRefUUID: [u8; 0usize]; // GATT Characteristic Types pub static deviceNameUUID: [u8; 0usize]; - pub static appearanceUUID: [u8; 0usize]; - pub static periPrivacyFlagUUID: [u8; 0usize]; - pub static reconnectAddrUUID: [u8; 0usize]; - pub static periConnParamUUID: [u8; 0usize]; - pub static serviceChangedUUID: [u8; 0usize]; - pub static centAddrResUUID: [u8; 0usize]; } diff --git a/src/ble/mod.rs b/src/ble/mod.rs index 1a477c6..239576b 100644 --- a/src/ble/mod.rs +++ b/src/ble/mod.rs @@ -76,7 +76,7 @@ impl Config { impl Default for Config { fn default() -> Self { - let mac_addr = MacAddress::from_raw([0x11, 0x22, 0x33, 0x44, 0x55, 0x66]); + let mac_addr = MacAddress::from_msb([0x11, 0x22, 0x33, 0x44, 0x55, 0x66]); Config { mac_addr } } } @@ -87,7 +87,7 @@ unsafe extern "C" fn hal_tmos_task(task_id: u8, events: u16) -> u16 { if !msg.is_null() { let event = TmosEvent(msg as _); CHANNEL.publish_immediate(event); - + // Dealloc in Drop // let _ = tmos_msg_deallocate(msg); } return events ^ SYS_EVENT_MSG; diff --git a/src/lib.rs b/src/lib.rs index 89b8fcb..f3e1740 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ pub mod dma; pub mod gpio; pub mod i2c; // pub mod lcd; +#[cfg(feature = "ble")] pub mod ble; pub mod rtc; pub mod signature;