Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BLE support for esp32s3 #12685

Closed
tommasoclini opened this issue Jul 13, 2024 · 16 comments
Closed

Add BLE support for esp32s3 #12685

tommasoclini opened this issue Jul 13, 2024 · 16 comments

Comments

@tommasoclini
Copy link

This is a feature request, is this already planned? When are we going to get support for BLE on esp32 s3?

@pkarashchenko
Copy link
Contributor

Lats time I was using BLE on ESP32 S3 it was working. What issues do you experience with BLE on ESP32 S3?

@pkarashchenko
Copy link
Contributor

@tommasoclini
Copy link
Author

I've tried a few bt commands, but bt advertise start didn't work, ioctl(SISIOCBTADVSTART) returned 5, or something like that.
I've asked because in the documentation it says that it is not supported, correct me if I'm wrong.
Do I need to run other commands before trying bt advertise?
Did I do something wrong?
I was using the blewifi example configuration

@pkarashchenko
Copy link
Contributor

I think that maybe you are mixing NuttX BLE stack support with ESP32 S3 BLE. I was using ESP32 S3 BLE with nimble stack and the BLE driver was capable to communicating and my application successfully paired with the device (my ESP32 S3 based device was a host).
I do not have much knowledge in NuttX BLE stack, so can't help much.

@acassis acassis assigned acassis and unassigned acassis Jul 13, 2024
@acassis
Copy link
Contributor

acassis commented Jul 13, 2024

@tommasoclini unfortunately the native NuttX stack is very primitive. It is a BLE Stack that Intel released many year ago, it was used on zephyr as well, but there Nordic helped to improve it and added more features.

@pkarashchenko the blewifi board profile doesn't have NIMBLE example, could you please submit your board config example using NIMBLE?

@tmedicci
Copy link
Contributor

Just to check it once again, did you already use ESP32-S3 with nimBLE, right @pkarashchenko ?

Can you provide us with a simple defconfig example (or at least point which configs/changes were necessary) to close this issue?

@maxikrie
Copy link
Contributor

maxikrie commented Oct 3, 2024

@pkarashchenko I would also be interested in a simple defconfig for ESP32 with nimble stack. Thanks!

@pkarashchenko
Copy link
Contributor

I will bake and publish some sample config early next week

@maxikrie
Copy link
Contributor

@pkarashchenko I am not sure if I missed it, did you publish it already? I assume you are employing the closed ble controller of espressif for this similar to softdevice of Nordic as in the nrf52840-dk:sdc_nimble example?

@pkarashchenko
Copy link
Contributor

@maxikrie you didn't. I had too many things flying around, so didn't managed to update on time. Here is my configuration

#
# This file is autogenerated: PLEASE DO NOT EDIT IT.
#
# You can use "make menuconfig" to make any modifications to the installed .config file.
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
# modifications.
#
# CONFIG_ARCH_LEDS is not set
# CONFIG_NIMBLE_PORTING_EXAMPLE is not set
# CONFIG_NSH_ARGCAT is not set
# CONFIG_NSH_CMDOPT_HEXDUMP is not set
# CONFIG_WIRELESS_BLUETOOTH_HOST is not set
CONFIG_ALLOW_BSD_COMPONENTS=y
CONFIG_ARCH="xtensa"
CONFIG_ARCH_BOARD="esp32s3-devkit"
CONFIG_ARCH_BOARD_COMMON=y
CONFIG_ARCH_BOARD_ESP32S3_DEVKIT=y
CONFIG_ARCH_CHIP="esp32s3"
CONFIG_ARCH_CHIP_ESP32S3=y
CONFIG_ARCH_CHIP_ESP32S3WROOM1=y
CONFIG_ARCH_INTERRUPTSTACK=2048
CONFIG_ARCH_STACKDUMP=y
CONFIG_ARCH_XTENSA=y
CONFIG_BLUETOOTH_TXCMD_STACKSIZE=4096
CONFIG_BLUETOOTH_TXCONN_STACKSIZE=4096
CONFIG_BOARDCTL_RESET=y
CONFIG_BOARD_LOOPSPERMSEC=16717
CONFIG_BUILTIN=y
CONFIG_DEBUG_ASSERTIONS=y
CONFIG_DEBUG_ERROR=y
CONFIG_DEBUG_FEATURES=y
CONFIG_DEBUG_INFO=y
CONFIG_DEBUG_WARN=y
CONFIG_DEFAULT_TASK_STACKSIZE=4096
CONFIG_DRIVERS_BLUETOOTH=y
CONFIG_DRIVERS_IEEE80211=y
CONFIG_DRIVERS_WIRELESS=y
CONFIG_ESP32S3_BLE=y
CONFIG_ESP32S3_RT_TIMER_TASK_STACK_SIZE=4096
CONFIG_ESP32S3_UART0=y
CONFIG_FS_LARGEFILE=y
CONFIG_FS_PROCFS=y
CONFIG_HAVE_CXX=y
CONFIG_HAVE_CXXINITIALIZE=y
CONFIG_HOST_MACOS=y
CONFIG_IDLETHREAD_STACKSIZE=3072
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_INIT_STACKSIZE=8192
CONFIG_INTELHEX_BINARY=y
CONFIG_NAME_MAX=48
CONFIG_NDEBUG=y
CONFIG_NETDEV_LATEINIT=y
CONFIG_NET_BLUETOOTH=y
CONFIG_NET_ETH_PKTSIZE=1514
CONFIG_NET_SOCKOPTS=y
CONFIG_NET_TCP=y
CONFIG_NIMBLE=y
CONFIG_NIMBLE_BLE_SM_SC_ONLY=y
CONFIG_NIMBLE_BLE_WHITELIST=y
CONFIG_NIMBLE_DEBUG_ERROR=y
CONFIG_NIMBLE_DEBUG_INFO=y
CONFIG_NIMBLE_DEBUG_WARN=y
CONFIG_NIMBLE_HS_FLOW_CTRL=y
CONFIG_NIMBLE_ROLE_CENTRAL=y
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_FILEIOSIZE=512
CONFIG_NSH_LINELEN=64
CONFIG_NSH_READLINE=y
CONFIG_POSIX_SPAWN_DEFAULT_STACKSIZE=4096
CONFIG_PREALLOC_MQ_MSGS=32
CONFIG_PREALLOC_TIMERS=4
CONFIG_PTHREAD_MUTEX_TYPES=y
CONFIG_RAM_SIZE=114688
CONFIG_RAM_START=0x20000000
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_HPWORKSTACKSIZE=4096
CONFIG_SCHED_LPWORKSTACKSIZE=4096
CONFIG_SCHED_WAITPID=y
CONFIG_SIG_EVTHREAD=y
CONFIG_SPINLOCK=y
CONFIG_START_DAY=6
CONFIG_START_MONTH=12
CONFIG_START_YEAR=2011
CONFIG_SYSLOG_BUFFER=y
CONFIG_SYSTEM_ARGTABLE3=y
CONFIG_SYSTEM_NSH=y
CONFIG_SYSTEM_NSH_STACKSIZE=4096
CONFIG_TIMER=y
CONFIG_TLS_TASK_NELEM=4
CONFIG_UART0_SERIAL_CONSOLE=y
CONFIG_WIRELESS=y
CONFIG_WIRELESS_BLUETOOTH=y

I'm having an app that implements nimBLE central device with the main that looks like:

/****************************************************************************
 * External Functions Prototypes
 ****************************************************************************/

void ble_hci_sock_ack_handler(FAR void *param);
void ble_hci_sock_init(void);
void ble_hci_sock_set_device(int dev);
void ble_store_ram_init(void);

/****************************************************************************
 * Private Functions Prototypes
 ****************************************************************************/
 
static FAR void *ble_hci_sock_task(FAR void *param);
static FAR void *ble_host_task(FAR void *param);

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: ble_hci_sock_task
 ****************************************************************************/

static FAR void *ble_hci_sock_task(FAR void *param)
{
  ble_hci_sock_ack_handler(param);
  return NULL;
}

/****************************************************************************
 * Name: ble_host_task
 ****************************************************************************/

static FAR void *ble_host_task(FAR void *param)
{
  nimble_host_task(param);
  return NULL;
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: nimble_main
 ****************************************************************************/

int main(int argc, FAR char *argv[])
{
  struct ble_npl_task s_task_hci;
  int                 ret        = 0;

  /* allow to specify custom hci */

  if (argc > 1)
    {
      ble_hci_sock_set_device(atoi(argv[1]));
    }

#ifndef CONFIG_NSH_ARCHINIT
  /* Perform architecture-specific initialization */

  boardctl(BOARDIOC_INIT, 0);
#endif

#ifndef CONFIG_NSH_NETINIT
  /* Bring up the network */

  netinit_bringup();
#endif

  nimble_port_init();
  ble_hci_sock_init();

  /* Initialize services */

  ble_svc_gap_init();
  ble_svc_gatt_init();
  ble_svc_ans_init();
  ble_svc_ias_init();
  ble_svc_lls_init();
  ble_svc_tps_init();

  /* XXX Need to have template for store */
  ble_store_ram_init();

  /* Create task which handles HCI socket */

  ret = ble_npl_task_init(&s_task_hci, "hci_sock", ble_hci_sock_task,
                          NULL, TASK_DEFAULT_PRIORITY, BLE_NPL_TIME_FOREVER,
                          TASK_DEFAULT_STACK, TASK_DEFAULT_STACK_SIZE);
  if (ret != 0)
    {
      printf("ERROR: starting hci task: %i\n", ret);
    }

  ble_host_task(NULL);

  return 0;
}

and

static void
app_ble_sync_cb(void)
{
  int rc;
  ble_addr_t addr;

  /* generate new non-resolvable private address */

  rc = ble_hs_id_gen_rnd(0, &addr);
  assert(rc == 0);

  /* set generated address */

  rc = ble_hs_id_set_rnd(addr.val);
  assert(rc == 0);

  rc = ble_hs_util_ensure_addr(0);
  assert(rc == 0);

  rc = ble_hs_id_infer_auto(0, &g_own_addr_type);
  assert(rc == 0);

  scan();
}

void nimble_host_task(void *param)
{
  int rc;

  ble_hs_cfg.sync_cb = app_ble_sync_cb;

  /* Initialize data structures to track connected peers. */
  rc = peer_init(MYNEWT_VAL(BLE_MAX_CONNECTIONS), 64, 64, 64);
  assert(rc == 0);

  rc = ble_svc_gap_device_name_set(gap_name);
  assert(rc == 0);

  nimble_port_run();
}

I'm not sure if I can share more parts of the application here, but I hope that this should be enough for you to move forward.
Also I haven't tried my defconfig with the latest nuttx master, so I'm not sure of defconfig can be used "as is".

@maxikrie
Copy link
Contributor

@pkarashchenko Awesome, thanks a lot!

How does your scan function look like or is it similar to the the mynewt-nimble blecent example?

@pkarashchenko
Copy link
Contributor

@maxikrie I think I can share scan

static void
scan(void)
{
  int rc;

  /* set scan parameters:
      - scan interval in 0.625ms units
      - scan window in 0.625ms units
      - filter policy - 0 if whitelisting not used
      - limited - should limited discovery be used
      - passive - should passive scan be used
      - filter duplicates - 1 enables filtering duplicated advertisements */
  const struct ble_gap_disc_params scan_params = {0, 0, 0, 0, 0, 1};

  /* performs discovery procedure */
  rc = ble_gap_disc(g_own_addr_type, BLE_HS_FOREVER, &scan_params,
                    gap_event, NULL);
  assert(rc == 0);
}

@tmedicci
Copy link
Contributor

Is there anything else pending for this issue?

@tommasoclini , can you close the issue, please?

@acassis
Copy link
Contributor

acassis commented Oct 28, 2024

@maxikrie @pkarashchenko I think we need some board example integrated into mainline, otherwise someone else will face same issue in the future

@maxikrie
Copy link
Contributor

@acassis @pkarashchenko @tommasoclini
I have created a pull request on the nuttx-apps repository, which adds nimble_bleprph and nimble_blecent working in companion. So far I have tested this only with two nrf52840-dk boards, but I will also try with the ESP32 in the coming days. These are the pull requests for apps (apache/nuttx-apps#2807) and board configs (#14566)

@tmedicci
Copy link
Contributor

Thanks @maxikrie , let's us know when you are able to test on ESP32 devices and we can add this test on our internal CI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants