Skip to content

Commit

Permalink
Merge pull request #33 from taks/esp32c6
Browse files Browse the repository at this point in the history
Support for esp32c6
  • Loading branch information
taks authored Nov 14, 2023
2 parents 5310ea6 + a93e009 commit 8cbef6b
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ runner = "espflash --monitor"
# For ESP-IDF 5 add `espidf_time64` and for earlier versions - remove this flag: /~https://github.com/esp-rs/rust/issues/110
rustflags = ["-C", "default-linker-libraries"]

[target.riscv32imac-esp-espidf]
linker = "ldproxy"
runner = "espflash flash --monitor"
rustflags = ["--cfg", "espidf_time64", "-C", "default-linker-libraries"]

[unstable]
build-std = ["std", "panic_abort"]
#build-std-features = ["panic_immediate_abort"] # Required for older ESP-IDF versions without a realpath implementation
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ jobs:
- target: riscv32imc-esp-espidf
idf-version: v5.1.1
rustflags: "-C default-linker-libraries --cfg espidf_time64"
- target: riscv32imac-esp-espidf
idf-version: v5.1.1
rustflags: "-C default-linker-libraries --cfg espidf_time64"
mcu: esp32c6
- target: xtensa-esp32-espidf
idf-version: release/v4.4
build-options: --example ble_*
Expand All @@ -45,6 +49,9 @@ jobs:
with:
default: true
ldproxy: true
- name: Setup | MCU
if: ${{ matrix.mcu }}
run: echo "MCU=${{ matrix.mcu }}" >> "$GITHUB_ENV"
- name: Build
env:
ESP_IDF_VERSION: ${{ matrix.idf-version }}
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ keywords = ["bluetooth", "ble", "esp32", "embedded", "async"]
categories = ["embedded", "hardware-support"]
documentation = "https://taks.github.io/esp32-nimble/esp32_nimble/index.html"

[patch.crates-io]
esp-idf-sys = { git = '/~https://github.com/taks/esp-idf-sys.git', branch = 'fix-compile-error-for-esp32c6-with-nimble' }
esp-idf-hal = { git = '/~https://github.com/esp-rs/esp-idf-hal.git', branch = 'master' }

[profile.release]
opt-level = "s"

Expand Down
7 changes: 4 additions & 3 deletions src/server/ble_characteristic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use core::{cell::UnsafeCell, ffi::c_void};

use alloc::{boxed::Box, sync::Arc, vec::Vec};
use bitflags::bitflags;
use esp_idf_sys::{ble_uuid_any_t, ble_uuid_cmp, os_mbuf_append};
use esp_idf_sys::{ble_uuid_any_t, ble_uuid_cmp};

use crate::{
utilities::{
as_mut_ptr, ble_npl_hw_enter_critical, ble_npl_hw_exit_critical, mutex::Mutex, BleUuid,
as_mut_ptr, ble_npl_hw_enter_critical, ble_npl_hw_exit_critical, mutex::Mutex, os_mbuf_append,
BleUuid,
},
AttValue, BLEDescriptor, BLEDevice, DescriptorProperties, OnWriteArgs, BLE2904,
};
Expand Down Expand Up @@ -275,7 +276,7 @@ impl BLECharacteristic {

ble_npl_hw_enter_critical();
let value = characteristic.value.value();
let rc = unsafe { os_mbuf_append(ctxt.om, value.as_ptr() as _, value.len() as _) };
let rc = os_mbuf_append(ctxt.om, value);
ble_npl_hw_exit_critical();
if rc == 0 {
0
Expand Down
8 changes: 5 additions & 3 deletions src/server/ble_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use core::{cell::UnsafeCell, ffi::c_void};

use alloc::{boxed::Box, vec::Vec};
use bitflags::bitflags;
use esp_idf_sys::{ble_uuid_any_t, ble_uuid_cmp, os_mbuf_append};
use esp_idf_sys::{ble_uuid_any_t, ble_uuid_cmp};

use crate::{
utilities::{ble_npl_hw_enter_critical, ble_npl_hw_exit_critical, mutex::Mutex, BleUuid},
utilities::{
ble_npl_hw_enter_critical, ble_npl_hw_exit_critical, mutex::Mutex, os_mbuf_append, BleUuid,
},
AttValue, OnWriteArgs,
};

Expand Down Expand Up @@ -105,7 +107,7 @@ impl BLEDescriptor {

ble_npl_hw_enter_critical();
let value = descriptor.value.value();
let rc = unsafe { os_mbuf_append(ctxt.om, value.as_ptr() as _, value.len() as _) };
let rc = os_mbuf_append(ctxt.om, value);
ble_npl_hw_exit_critical();
if rc == 0 {
0
Expand Down
22 changes: 9 additions & 13 deletions src/server/ble_ext_advertising.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use once_cell::sync::Lazy;
use alloc::vec;
use alloc::vec::Vec;

use crate::{ble, utilities::BleUuid, BLEAddress, BLEReturnCode, BLEServer};
use crate::{
ble,
utilities::{os_mbuf_append, os_msys_get_pkthdr, BleUuid},
BLEAddress, BLEReturnCode, BLEServer,
};

pub struct BLEExtAdvertisement {
payload: Vec<u8>,
Expand Down Expand Up @@ -248,15 +252,11 @@ impl BLEExtAdvertising {
self as *mut Self as _
))?;

let buf = esp_idf_sys::os_msys_get_pkthdr(adv.payload.len() as _, 0);
let buf = os_msys_get_pkthdr(adv.payload.len() as _, 0);
if buf.is_null() {
return BLEReturnCode::fail();
}
ble!(esp_idf_sys::os_mbuf_append(
buf,
adv.payload.as_ptr() as _,
adv.payload.len() as _
))?;
ble!(os_mbuf_append(buf, &adv.payload))?;

if (adv.params.scannable() != 0) && (adv.params.legacy_pdu() == 0) {
ble!(esp_idf_sys::ble_gap_ext_adv_rsp_set_data(inst_id, buf))?;
Expand All @@ -278,15 +278,11 @@ impl BLEExtAdvertising {
lsr: &BLEExtAdvertisement,
) -> Result<(), BLEReturnCode> {
unsafe {
let buf = esp_idf_sys::os_msys_get_pkthdr(lsr.payload.len() as _, 0);
let buf = os_msys_get_pkthdr(lsr.payload.len() as _, 0);
if buf.is_null() {
return BLEReturnCode::fail();
}
ble!(esp_idf_sys::os_mbuf_append(
buf,
lsr.payload.as_ptr() as _,
lsr.payload.len() as _
))?;
ble!(os_mbuf_append(buf, &lsr.payload))?;

ble!(esp_idf_sys::ble_gap_ext_adv_rsp_set_data(inst_id, buf))
}
Expand Down
3 changes: 3 additions & 0 deletions src/utilities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub(crate) use ble_functions::*;
mod nimble_npl_os;
pub(crate) use nimble_npl_os::*;

mod os_mbuf;
pub(crate) use os_mbuf::*;

#[inline]
#[allow(unused)]
pub(crate) unsafe fn extend_lifetime_mut<'a, 'b: 'a, T: ?Sized>(r: &'a mut T) -> &'b mut T {
Expand Down
28 changes: 28 additions & 0 deletions src/utilities/os_mbuf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use core::ffi::c_int;
use esp_idf_sys::os_mbuf;

/// Allocate a packet header structure from the MSYS pool. See os_msys_register() for a description of MSYS.
pub(crate) fn os_msys_get_pkthdr(dsize: u16, user_hdr_len: u16) -> *mut os_mbuf {
#[cfg(not(esp_idf_soc_esp_nimble_controller))]
unsafe {
esp_idf_sys::os_msys_get_pkthdr(dsize, user_hdr_len)
}

#[cfg(esp_idf_soc_esp_nimble_controller)]
unsafe {
esp_idf_sys::r_os_msys_get_pkthdr(dsize, user_hdr_len)
}
}

/// Append data onto a mbuf
pub(crate) fn os_mbuf_append(m: *mut os_mbuf, data: &[u8]) -> c_int {
#[cfg(not(esp_idf_soc_esp_nimble_controller))]
unsafe {
esp_idf_sys::os_mbuf_append(m, data.as_ptr() as _, data.len() as _)
}

#[cfg(esp_idf_soc_esp_nimble_controller)]
unsafe {
esp_idf_sys::r_os_mbuf_append(m, data.as_ptr() as _, data.len() as _)
}
}

0 comments on commit 8cbef6b

Please sign in to comment.