Skip to content

Commit

Permalink
enhance(ble): impl more ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Nov 2, 2023
1 parent ba85a19 commit c40eb30
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/embassy-ble-broadcaster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions src/ble/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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" {

Expand All @@ -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();

Expand Down
14 changes: 14 additions & 0 deletions src/ble/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Check warning on line 6 in src/ble/mod.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `println`

pub mod ffi;
Expand Down Expand Up @@ -67,9 +68,22 @@ impl Default for Config {
}
}

unsafe extern "C" fn hal_tmos_task(task_id: u8, events: u16) -> u16 {

Check warning on line 71 in src/ble/mod.rs

View workflow job for this annotation

GitHub Actions / build

function `hal_tmos_task` is never used
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;
Expand Down

0 comments on commit c40eb30

Please sign in to comment.