Skip to content

Commit

Permalink
ESP32C3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
rojer committed Aug 8, 2022
1 parent d623118 commit 1d737d2
Show file tree
Hide file tree
Showing 8 changed files with 402 additions and 362 deletions.
8 changes: 4 additions & 4 deletions include/esp32/esp32_bt.h → include/esp32xx/esp32xx_bt.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ extern "C" {
#define MGOS_BT_ADDR_LEN 6

void mgos_bt_addr_to_esp32(const struct mgos_bt_addr *in, ble_addr_t *out);
void esp32_bt_addr_to_mgos(const ble_addr_t *in, struct mgos_bt_addr *out);
const char *esp32_bt_addr_to_str(const ble_addr_t *addr, char *out);
void esp32xx_bt_addr_to_mgos(const ble_addr_t *in, struct mgos_bt_addr *out);
const char *esp32xx_bt_addr_to_str(const ble_addr_t *addr, char *out);

void mgos_bt_uuid_to_esp32(const struct mgos_bt_uuid *in, ble_uuid_any_t *out);
void esp32_bt_uuid_to_mgos(const ble_uuid_t *in, struct mgos_bt_uuid *out);
const char *esp32_bt_uuid_to_str(const ble_uuid_t *uuid, char *out);
void esp32xx_bt_uuid_to_mgos(const ble_uuid_t *in, struct mgos_bt_uuid *out);
const char *esp32xx_bt_uuid_to_str(const ble_uuid_t *uuid, char *out);

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "mgos_bt_gap.h"

#include "esp32_bt.h"
#include "esp32xx_bt.h"

#ifdef __cplusplus
extern "C" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@
extern "C" {
#endif

bool esp32_bt_gap_start_advertising(void);
bool esp32_bt_gatts_init(void);
bool esp32_bt_gatts_start(void);
void esp32_bt_restart(void);
bool esp32xx_bt_gap_start_advertising(void);
bool esp32xx_bt_gatts_init(void);
bool esp32xx_bt_gatts_start(void);
void esp32xx_bt_restart(void);

struct ble_gap_event;
int esp32_bt_gatts_event(const struct ble_gap_event *event, void *arg);
int esp32xx_bt_gatts_event(const struct ble_gap_event *event, void *arg);

extern uint8_t own_addr_type;

void esp32_bt_rlock(void);
void esp32_bt_runlock(void);
void esp32xx_bt_rlock(void);
void esp32xx_bt_runlock(void);

struct os_mbuf;
struct mg_str esp32_bt_mbuf_to_flat(const struct os_mbuf *om);
struct mg_str esp32xx_bt_mbuf_to_flat(const struct os_mbuf *om);

#ifdef __cplusplus
}
Expand Down
36 changes: 34 additions & 2 deletions mos.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
author: mongoose-os
description: Bluetooth support
type: lib
version: 1.0
version: 1.1.0

platforms: [ esp32 ]
platforms: [ esp32, esp32c3 ]

sources:
- src
Expand Down Expand Up @@ -31,6 +31,10 @@ config_schema:
conds:
- when: mos.platform == "esp32"
apply:
sources:
- src/esp32xx
includes:
- include/esp32xx
libs:
- origin: https://github.com/mongoose-os-libs/mbedtls
build_vars:
Expand All @@ -53,6 +57,34 @@ conds:
cdefs:
MYNEWT_VAL_BLE_HS_AUTO_START: 0

- when: mos.platform == "esp32c3"
apply:
sources:
- src/esp32xx
includes:
- include/esp32xx
libs:
- origin: https://github.com/mongoose-os-libs/mbedtls
build_vars:
ESP_IDF_EXTRA_COMPONENTS: "${build_vars.ESP_IDF_EXTRA_COMPONENTS} bt"
ESP_IDF_SDKCONFIG_OPTS: >
${build_vars.ESP_IDF_SDKCONFIG_OPTS}
CONFIG_BT_ENABLED=y
CONFIG_BT_NIMBLE_ENABLED=y
CONFIG_BT_NIMBLE_ROLE_BROADCASTER=y
CONFIG_BT_NIMBLE_ROLE_OBSERVER=y
CONFIG_BT_NIMBLE_ROLE_CENTRAL=y
CONFIG_BT_NIMBLE_ROLE_PERIPHERAL=y
CONFIG_BT_NIMBLE_MAX_CONNECTIONS=7
CONFIG_BT_NIMBLE_DEBUG=n
CONFIG_BT_NIMBLE_TASK_STACK_SIZE=6144
CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM=0
CONFIG_BT_NIMBLE_GAP_DEVICE_NAME_MAX_LEN=50
CONFIG_BT_NIMBLE_CRYPTO_STACK_MBEDTLS=y
CONFIG_BTDM_CTRL_HLI=n
cdefs:
MYNEWT_VAL_BLE_HS_AUTO_START: 0

tags:
- bt
- bluetooth
Expand Down
66 changes: 33 additions & 33 deletions src/esp32/esp32_bt.c → src/esp32xx/esp32xx_bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
* limitations under the License.
*/

#include "esp32_bt.h"
#include "esp32_bt_gap.h"
#include "esp32_bt_internal.h"
#include "esp32xx_bt.h"
#include "esp32xx_bt_gap.h"
#include "esp32xx_bt_internal.h"

#include <stdbool.h>
#include <stdlib.h>
Expand All @@ -44,13 +44,13 @@ static TaskHandle_t s_host_task_handle;
static SemaphoreHandle_t s_sem = NULL;
static struct mgos_rlock_type *s_lock = NULL;

enum esp32_bt_state {
enum esp32xx_bt_state {
ESP32_BT_STOPPED = 0,
ESP32_BT_STARTING = 1,
ESP32_BT_STARTED = 2,
ESP32_BT_STOPPING = 3,
};
static enum esp32_bt_state s_state = ESP32_BT_STOPPED;
static enum esp32xx_bt_state s_state = ESP32_BT_STOPPED;
struct ble_hs_stop_listener s_stop_listener;

void mgos_bt_addr_to_esp32(const struct mgos_bt_addr *in, ble_addr_t *out) {
Expand All @@ -73,7 +73,7 @@ void mgos_bt_addr_to_esp32(const struct mgos_bt_addr *in, ble_addr_t *out) {
out->val[5] = in->addr[0];
}

void esp32_bt_addr_to_mgos(const ble_addr_t *in, struct mgos_bt_addr *out) {
void esp32xx_bt_addr_to_mgos(const ble_addr_t *in, struct mgos_bt_addr *out) {
out->type = MGOS_BT_ADDR_TYPE_NONE;
switch (in->type) {
case BLE_ADDR_PUBLIC:
Expand All @@ -97,9 +97,9 @@ void esp32_bt_addr_to_mgos(const ble_addr_t *in, struct mgos_bt_addr *out) {
out->addr[5] = in->val[0];
}

const char *esp32_bt_addr_to_str(const ble_addr_t *addr, char *out) {
const char *esp32xx_bt_addr_to_str(const ble_addr_t *addr, char *out) {
struct mgos_bt_addr maddr;
esp32_bt_addr_to_mgos(addr, &maddr);
esp32xx_bt_addr_to_mgos(addr, &maddr);
return mgos_bt_addr_to_str(&maddr, MGOS_BT_ADDR_STRINGIFY_TYPE, out);
}

Expand All @@ -120,7 +120,7 @@ void mgos_bt_uuid_to_esp32(const struct mgos_bt_uuid *in, ble_uuid_any_t *out) {
}
}

void esp32_bt_uuid_to_mgos(const ble_uuid_t *in, struct mgos_bt_uuid *out) {
void esp32xx_bt_uuid_to_mgos(const ble_uuid_t *in, struct mgos_bt_uuid *out) {
out->len = in->type / 8;
switch (in->type) {
case BLE_UUID_TYPE_16:
Expand All @@ -135,9 +135,9 @@ void esp32_bt_uuid_to_mgos(const ble_uuid_t *in, struct mgos_bt_uuid *out) {
}
}

const char *esp32_bt_uuid_to_str(const ble_uuid_t *uuid, char *out) {
const char *esp32xx_bt_uuid_to_str(const ble_uuid_t *uuid, char *out) {
struct mgos_bt_uuid uuidm;
esp32_bt_uuid_to_mgos(uuid, &uuidm);
esp32xx_bt_uuid_to_mgos(uuid, &uuidm);
return mgos_bt_uuid_to_str(&uuidm, out);
}

Expand Down Expand Up @@ -179,15 +179,15 @@ bool mgos_bt_get_device_address(struct mgos_bt_addr *addr) {
default:
return false;
}
esp32_bt_addr_to_mgos(&baddr, addr);
esp32xx_bt_addr_to_mgos(&baddr, addr);
return true;
}

static void esp32_bt_reset(int reason) {
static void esp32xx_bt_reset(int reason) {
LOG(LL_ERROR, ("Resetting state; reason=%d", reason));
}

static void esp32_bt_synced(void) {
static void esp32xx_bt_synced(void) {
int rc;

s_state = ESP32_BT_STARTED;
Expand All @@ -213,12 +213,12 @@ static void esp32_bt_synced(void) {
("BLE Device Address: %s",
mgos_bt_addr_to_str(&addr, MGOS_BT_ADDR_STRINGIFY_TYPE, addr_str)));
}
esp32_bt_gap_start_advertising();
esp32xx_bt_gap_start_advertising();
}

// Handler callback that executes host events on the mgos task.
// For efficiency, we process multiple events at a time.
static void esp32_bt_mgos_handler(void *arg) {
static void esp32xx_bt_mgos_handler(void *arg) {
int nevs = 1;
struct ble_npl_event *ev, **ep, *evs[20] = {arg};
struct ble_npl_eventq *q = nimble_port_get_dflt_eventq();
Expand All @@ -236,7 +236,7 @@ static void esp32_bt_mgos_handler(void *arg) {
}
}

static void esp32_bt_host_task(void *param) {
static void esp32xx_bt_host_task(void *param) {
if (param == NULL) { // This is the dummy task.
nimble_port_freertos_deinit();
return;
Expand All @@ -246,24 +246,24 @@ static void esp32_bt_host_task(void *param) {
struct ble_npl_eventq *q = nimble_port_get_dflt_eventq();
while (1) {
ev = ble_npl_eventq_get(q, BLE_NPL_TIME_FOREVER);
while (!mgos_invoke_cb(esp32_bt_mgos_handler, ev, false /* from_isr */)) {
while (!mgos_invoke_cb(esp32xx_bt_mgos_handler, ev, false /* from_isr */)) {
}
// Wait for the mgos task callback to run and process the event.
xSemaphoreTake(s_sem, portMAX_DELAY);
}
}

void esp32_bt_rlock(void) {
void esp32xx_bt_rlock(void) {
mgos_rlock(s_lock);
}

void esp32_bt_runlock(void) {
void esp32xx_bt_runlock(void) {
mgos_runlock(s_lock);
}

extern void ble_store_config_init(void);

static bool esp32_bt_init(void) {
static bool esp32xx_bt_init(void) {
if (s_inited) {
return true;
}
Expand All @@ -287,8 +287,8 @@ static bool esp32_bt_init(void) {

nimble_port_init();

ble_hs_cfg.reset_cb = esp32_bt_reset;
ble_hs_cfg.sync_cb = esp32_bt_synced;
ble_hs_cfg.reset_cb = esp32xx_bt_reset;
ble_hs_cfg.sync_cb = esp32xx_bt_synced;
ble_hs_cfg.store_status_cb = ble_store_util_status_rr;

ble_hs_cfg.sm_sc = true;
Expand All @@ -307,16 +307,16 @@ static bool esp32_bt_init(void) {

// This creates the high-pri LL task and a host task.
// We don't use the latter because its priority is too high.
nimble_port_freertos_init(esp32_bt_host_task);
nimble_port_freertos_init(esp32xx_bt_host_task);
// Instead we create our own here with priority below the main mgos task.
xTaskCreatePinnedToCore(esp32_bt_host_task, "ble", 1024, (void *) 1,
xTaskCreatePinnedToCore(esp32xx_bt_host_task, "ble", 1024, (void *) 1,
MGOS_TASK_PRIORITY - 1, &s_host_task_handle,
NIMBLE_CORE);

// Default INFO level log is too spammy.
esp_log_level_set("NimBLE", ESP_LOG_WARN);

if (!esp32_bt_gatts_init()) {
if (!esp32xx_bt_gatts_init()) {
LOG(LL_ERROR, ("%s init failed", "GATTS"));
goto out;
}
Expand Down Expand Up @@ -345,10 +345,10 @@ static void ble_hs_stop_cb(int status, void *arg) {
bool mgos_bt_start(void) {
s_should_be_running = true;
if (s_state != ESP32_BT_STOPPED) return true;
if (!esp32_bt_init()) return false;
if (!esp32xx_bt_init()) return false;
s_state = ESP32_BT_STARTING;
mgos_event_trigger_schedule(MGOS_BT_EV_STARTING, NULL, 0);
if (!esp32_bt_gatts_start()) {
if (!esp32xx_bt_gatts_start()) {
LOG(LL_ERROR, ("%s start failed", "GATTS"));
return false;
}
Expand All @@ -364,18 +364,18 @@ bool mgos_bt_stop(void) {
return (ble_hs_stop(&s_stop_listener, ble_hs_stop_cb, NULL) == 0);
}

void esp32_bt_restart(void) {
void esp32xx_bt_restart(void) {
if (!s_should_be_running) return;
mgos_bt_stop();
s_should_be_running = true;
}

static void esp32_bt_start(void *arg) {
static void esp32xx_bt_start(void *arg) {
mgos_bt_start();
(void) arg;
}

struct mg_str esp32_bt_mbuf_to_flat(const struct os_mbuf *om) {
struct mg_str esp32xx_bt_mbuf_to_flat(const struct os_mbuf *om) {
struct mg_str res = MG_NULL_STR;
if (om == NULL) return res;
char *data = NULL;
Expand Down Expand Up @@ -418,8 +418,8 @@ bool mgos_bt_common_init(void) {
LOG(LL_ERROR, ("Random addresses are not supported, using public"));
mgos_sys_config_set_bt_random_address(false);
}
esp32_bt_init();
esp32xx_bt_init();
// Delay starting the stack until other libraries are initialized
// and services registered to avoid unnecessary restarts.
return mgos_invoke_cb(esp32_bt_start, NULL, false /* from_isr */);
return mgos_invoke_cb(esp32xx_bt_start, NULL, false /* from_isr */);
}
Loading

0 comments on commit 1d737d2

Please sign in to comment.