From c40eb301b3578dc523b87c027b1b410e922b1e97 Mon Sep 17 00:00:00 2001 From: Andelf Date: Fri, 3 Nov 2023 01:46:08 +0800 Subject: [PATCH] enhance(ble): impl more ffi --- examples/embassy-ble-broadcaster.rs | 2 +- src/ble/ffi.rs | 14 ++++++++++++++ src/ble/mod.rs | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/examples/embassy-ble-broadcaster.rs b/examples/embassy-ble-broadcaster.rs index 0c7146a..c6274c9 100644 --- a/examples/embassy-ble-broadcaster.rs +++ b/examples/embassy-ble-broadcaster.rs @@ -242,7 +242,7 @@ async fn main(spawner: Spawner) -> ! { // Broadcaster_Init(); unsafe { // Setup the GAP Broadcaster Role Profile - GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, 1, &1 as *const _ as _); + GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, 1, &true as *const _ as _); GAPRole_SetParameter(GAPROLE_ADV_EVENT_TYPE, 1, &0x03 as *const _ as _); GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, diff --git a/src/ble/ffi.rs b/src/ble/ffi.rs index f806a14..c54bdd5 100644 --- a/src/ble/ffi.rs +++ b/src/ble/ffi.rs @@ -42,6 +42,7 @@ use core::num::NonZeroU8; */ // UNSAFE: size_of is 1 +#[allow(improper_ctypes)] pub type bStatus_t = Result<(), NonZeroU8>; pub type tmosTaskID = u8; @@ -245,6 +246,13 @@ pub struct tmos_event_hdr_t { pub status: u8, } +/// A message is waiting event +pub const SYS_EVENT_MSG: u16 = 0x8000; + +/// Task ID isn't setup properly +pub const INVALID_TASK_ID: u8 = 0xFF; +pub const TASK_NO_TASK: u8 = 0xFF; + // TMOS extern "C" { @@ -263,6 +271,12 @@ extern "C" { #[doc = " @brief start a event after period of time\n\n @param taskID - task ID to set event for\n @param event - event to be notified with\n @param time - timeout value\n\n @return TRUE,FALSE."] pub fn tmos_start_task(taskID: tmosTaskID, event: tmosEvents, time: tmosTimer) -> BOOL; + #[doc = " @brief receive a msg\n\n @param taskID - task ID of task need to receive msg\n\n @return *uint8_t - message information or NULL if no message"] + pub fn tmos_msg_receive(taskID: tmosTaskID) -> *mut u8; + + #[doc = " @brief delete a msg\n\n @param *msg_ptr - point of msg\n\n @return SUCCESS."] + pub fn tmos_msg_deallocate(msg_ptr: *mut u8) -> bStatus_t; + #[doc = " @brief Process system\n\n @param None.\n\n @return None."] pub fn TMOS_SystemProcess(); diff --git a/src/ble/mod.rs b/src/ble/mod.rs index 5f06f8f..eef635b 100644 --- a/src/ble/mod.rs +++ b/src/ble/mod.rs @@ -2,6 +2,7 @@ use core::ffi::CStr; use core::mem::MaybeUninit; use core::num::NonZeroU8; +use self::ffi::{tmos_msg_deallocate, tmos_msg_receive, SYS_EVENT_MSG}; use crate::{pac, println}; pub mod ffi; @@ -67,9 +68,22 @@ impl Default for Config { } } +unsafe extern "C" fn hal_tmos_task(task_id: u8, events: u16) -> u16 { + if events & SYS_EVENT_MSG != 0 { + let msg = tmos_msg_receive(task_id); + if !msg.is_null() { + let _ = tmos_msg_deallocate(msg); + } + return events ^ SYS_EVENT_MSG; + } else { + unimplemented!() + } +} + /// Wrapper of BLEInit and HAL_Init pub fn init(config: Config) -> Result<(), NonZeroU8> { use ffi::{bleConfig_t, BLE_LibInit, BLE_RegInit, TMOS_TimerInit, LL_TX_POWEER_6_DBM}; + const BLE_TX_NUM_EVENT: u8 = 1; const BLE_BUFF_NUM: u8 = 5; const BLE_BUFF_MAX_LEN: u16 = 27;