mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-19 17:35:54 +08:00
添加智能灯固件代码
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
# Bluetooth Examples for Host Controller Interface
|
||||
|
||||
Note: To use examples in this directory, you need to have Bluetooth enabled in configuration.
|
||||
|
||||
# Example Layout
|
||||
|
||||
This directory includes examples to demonstrate controller interactions by virtual HCI layer and UART.
|
||||
|
||||
## controller_hci_uart
|
||||
|
||||
Demonstrates interaction with controller through HCI over UART.
|
||||
|
||||
See the [README.md](./controller_hci_uart/README.md) file in the example [controller_hci_uart](./controller_hci_uart).
|
||||
|
||||
## controller_vhci_ble_adv
|
||||
|
||||
Demonstrates interaction with controller though virtual HCI layer. In this example, simple BLE advertising is done.
|
||||
|
||||
See the [README.md](./controller_vhci_ble_adv/README.md) file in the example [controller_vhci_ble_adv](./controller_vhci_ble_adv).
|
||||
@@ -0,0 +1,6 @@
|
||||
# The following lines of boilerplate have to be in your project's CMakeLists
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(controller_hci_uart)
|
||||
@@ -0,0 +1,8 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := controller_hci_uart
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
@@ -0,0 +1,10 @@
|
||||
| Supported Targets | ESP32 |
|
||||
| ----------------- | ----- |
|
||||
|
||||
ESP-IDF UART HCI Controller
|
||||
===========================
|
||||
|
||||
This is a btdm controller use UART as HCI IO. This require the UART device support RTS/CTS mandatory.
|
||||
It can do the configuration of UART number and UART baudrate by menuconfig.
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "controller_hci_uart_demo.c"
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -0,0 +1,4 @@
|
||||
#
|
||||
# "main" pseudo-component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_bt.h"
|
||||
#include "soc/uhci_periph.h"
|
||||
#include "driver/uart.h"
|
||||
#include "driver/periph_ctrl.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
static const char *tag = "CONTROLLER_UART_HCI";
|
||||
|
||||
static void uart_gpio_reset(void)
|
||||
{
|
||||
#if CONFIG_BT_HCI_UART_NO == 1
|
||||
periph_module_enable(PERIPH_UART1_MODULE);
|
||||
#elif CONFIG_BT_HCI_UART_NO == 2
|
||||
periph_module_enable(PERIPH_UART2_MODULE);
|
||||
#endif
|
||||
periph_module_enable(PERIPH_UHCI0_MODULE);
|
||||
|
||||
#ifdef CONFIG_BT_HCI_UART_NO
|
||||
ESP_LOGI(tag, "HCI UART%d Pin select: TX 5, RX, 18, CTS 23, RTS 19", CONFIG_BT_HCI_UART_NO);
|
||||
|
||||
uart_set_pin(CONFIG_BT_HCI_UART_NO, 5, 18, 19, 23);
|
||||
#endif
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
esp_err_t ret;
|
||||
|
||||
/* Initialize NVS — it is used to store PHY calibration data */
|
||||
ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK( ret );
|
||||
|
||||
|
||||
/* As the UART1/2 pin conflict with flash pin, so do matrix of the signal and pin */
|
||||
uart_gpio_reset();
|
||||
|
||||
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
|
||||
ret = esp_bt_controller_init(&bt_cfg);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(tag, "Bluetooth Controller initialize failed: %s", esp_err_to_name(ret));
|
||||
return;
|
||||
}
|
||||
|
||||
ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(tag, "Bluetooth Controller initialize failed: %s", esp_err_to_name(ret));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# Override some defaults so BT stack is enabled
|
||||
# in this example
|
||||
|
||||
#
|
||||
# BT config
|
||||
#
|
||||
CONFIG_BT_ENABLED=y
|
||||
CONFIG_BTDM_CTRL_MODE_BLE_ONLY=n
|
||||
CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n
|
||||
CONFIG_BTDM_CTRL_MODE_BTDM=y
|
||||
CONFIG_BTDM_CTRL_BLE_MAX_CONN=9
|
||||
CONFIG_BTDM_CTRL_BR_EDR_MAX_ACL_CONN=7
|
||||
CONFIG_BTDM_CTRL_BR_EDR_MAX_SYNC_CONN=3
|
||||
CONFIG_BT_BLUEDROID_ENABLED=n
|
||||
CONFIG_BT_CONTROLLER_ONLY=y
|
||||
CONFIG_BT_HCI_UART=y
|
||||
CONFIG_BT_HCI_UART_NO_DEFAULT=1
|
||||
CONFIG_BT_HCI_UART_BAUDRATE_DEFAULT=921600
|
||||
@@ -0,0 +1,6 @@
|
||||
# The following lines of boilerplate have to be in your project's
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(ble_adv)
|
||||
@@ -0,0 +1,8 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := ble_adv
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
@@ -0,0 +1,9 @@
|
||||
| Supported Targets | ESP32 |
|
||||
| ----------------- | ----- |
|
||||
|
||||
ESP-IDF VHCI ble_advertising app
|
||||
================================
|
||||
|
||||
This is a BLE advertising demo with virtual HCI interface. Send Reset/ADV_PARAM/ADV_DATA/ADV_ENABLE HCI command for BLE advertising.
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "app_bt.c"
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -0,0 +1,255 @@
|
||||
/*
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_bt.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
static const char *tag = "BLE_ADV";
|
||||
|
||||
#define HCI_H4_CMD_PREAMBLE_SIZE (4)
|
||||
|
||||
/* HCI Command opcode group field(OGF) */
|
||||
#define HCI_GRP_HOST_CONT_BASEBAND_CMDS (0x03 << 10) /* 0x0C00 */
|
||||
#define HCI_GRP_BLE_CMDS (0x08 << 10)
|
||||
|
||||
#define HCI_RESET (0x0003 | HCI_GRP_HOST_CONT_BASEBAND_CMDS)
|
||||
#define HCI_BLE_WRITE_ADV_ENABLE (0x000A | HCI_GRP_BLE_CMDS)
|
||||
#define HCI_BLE_WRITE_ADV_PARAMS (0x0006 | HCI_GRP_BLE_CMDS)
|
||||
#define HCI_BLE_WRITE_ADV_DATA (0x0008 | HCI_GRP_BLE_CMDS)
|
||||
|
||||
#define HCIC_PARAM_SIZE_WRITE_ADV_ENABLE (1)
|
||||
#define HCIC_PARAM_SIZE_BLE_WRITE_ADV_PARAMS (15)
|
||||
#define HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA (31)
|
||||
|
||||
#define BD_ADDR_LEN (6) /* Device address length */
|
||||
typedef uint8_t bd_addr_t[BD_ADDR_LEN]; /* Device address */
|
||||
|
||||
#define UINT16_TO_STREAM(p, u16) {*(p)++ = (uint8_t)(u16); *(p)++ = (uint8_t)((u16) >> 8);}
|
||||
#define UINT8_TO_STREAM(p, u8) {*(p)++ = (uint8_t)(u8);}
|
||||
#define BDADDR_TO_STREAM(p, a) {int ijk; for (ijk = 0; ijk < BD_ADDR_LEN; ijk++) *(p)++ = (uint8_t) a[BD_ADDR_LEN - 1 - ijk];}
|
||||
#define ARRAY_TO_STREAM(p, a, len) {int ijk; for (ijk = 0; ijk < len; ijk++) *(p)++ = (uint8_t) a[ijk];}
|
||||
|
||||
enum {
|
||||
H4_TYPE_COMMAND = 1,
|
||||
H4_TYPE_ACL = 2,
|
||||
H4_TYPE_SCO = 3,
|
||||
H4_TYPE_EVENT = 4
|
||||
};
|
||||
|
||||
static uint8_t hci_cmd_buf[128];
|
||||
|
||||
/*
|
||||
* @brief: BT controller callback function, used to notify the upper layer that
|
||||
* controller is ready to receive command
|
||||
*/
|
||||
static void controller_rcv_pkt_ready(void)
|
||||
{
|
||||
printf("controller rcv pkt ready\n");
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief: BT controller callback function, to transfer data packet to upper
|
||||
* controller is ready to receive command
|
||||
*/
|
||||
static int host_rcv_pkt(uint8_t *data, uint16_t len)
|
||||
{
|
||||
printf("host rcv pkt: ");
|
||||
for (uint16_t i = 0; i < len; i++) {
|
||||
printf("%02x", data[i]);
|
||||
}
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static esp_vhci_host_callback_t vhci_host_cb = {
|
||||
controller_rcv_pkt_ready,
|
||||
host_rcv_pkt
|
||||
};
|
||||
|
||||
static uint16_t make_cmd_reset(uint8_t *buf)
|
||||
{
|
||||
UINT8_TO_STREAM (buf, H4_TYPE_COMMAND);
|
||||
UINT16_TO_STREAM (buf, HCI_RESET);
|
||||
UINT8_TO_STREAM (buf, 0);
|
||||
return HCI_H4_CMD_PREAMBLE_SIZE;
|
||||
}
|
||||
|
||||
static uint16_t make_cmd_ble_set_adv_enable (uint8_t *buf, uint8_t adv_enable)
|
||||
{
|
||||
UINT8_TO_STREAM (buf, H4_TYPE_COMMAND);
|
||||
UINT16_TO_STREAM (buf, HCI_BLE_WRITE_ADV_ENABLE);
|
||||
UINT8_TO_STREAM (buf, HCIC_PARAM_SIZE_WRITE_ADV_ENABLE);
|
||||
UINT8_TO_STREAM (buf, adv_enable);
|
||||
return HCI_H4_CMD_PREAMBLE_SIZE + HCIC_PARAM_SIZE_WRITE_ADV_ENABLE;
|
||||
}
|
||||
|
||||
static uint16_t make_cmd_ble_set_adv_param (uint8_t *buf, uint16_t adv_int_min, uint16_t adv_int_max,
|
||||
uint8_t adv_type, uint8_t addr_type_own,
|
||||
uint8_t addr_type_dir, bd_addr_t direct_bda,
|
||||
uint8_t channel_map, uint8_t adv_filter_policy)
|
||||
{
|
||||
UINT8_TO_STREAM (buf, H4_TYPE_COMMAND);
|
||||
UINT16_TO_STREAM (buf, HCI_BLE_WRITE_ADV_PARAMS);
|
||||
UINT8_TO_STREAM (buf, HCIC_PARAM_SIZE_BLE_WRITE_ADV_PARAMS );
|
||||
|
||||
UINT16_TO_STREAM (buf, adv_int_min);
|
||||
UINT16_TO_STREAM (buf, adv_int_max);
|
||||
UINT8_TO_STREAM (buf, adv_type);
|
||||
UINT8_TO_STREAM (buf, addr_type_own);
|
||||
UINT8_TO_STREAM (buf, addr_type_dir);
|
||||
BDADDR_TO_STREAM (buf, direct_bda);
|
||||
UINT8_TO_STREAM (buf, channel_map);
|
||||
UINT8_TO_STREAM (buf, adv_filter_policy);
|
||||
return HCI_H4_CMD_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_WRITE_ADV_PARAMS;
|
||||
}
|
||||
|
||||
|
||||
static uint16_t make_cmd_ble_set_adv_data(uint8_t *buf, uint8_t data_len, uint8_t *p_data)
|
||||
{
|
||||
UINT8_TO_STREAM (buf, H4_TYPE_COMMAND);
|
||||
UINT16_TO_STREAM (buf, HCI_BLE_WRITE_ADV_DATA);
|
||||
UINT8_TO_STREAM (buf, HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA + 1);
|
||||
|
||||
memset(buf, 0, HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA);
|
||||
|
||||
if (p_data != NULL && data_len > 0) {
|
||||
if (data_len > HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA) {
|
||||
data_len = HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA;
|
||||
}
|
||||
|
||||
UINT8_TO_STREAM (buf, data_len);
|
||||
|
||||
ARRAY_TO_STREAM (buf, p_data, data_len);
|
||||
}
|
||||
return HCI_H4_CMD_PREAMBLE_SIZE + HCIC_PARAM_SIZE_BLE_WRITE_ADV_DATA + 1;
|
||||
}
|
||||
|
||||
static void hci_cmd_send_reset(void)
|
||||
{
|
||||
uint16_t sz = make_cmd_reset (hci_cmd_buf);
|
||||
esp_vhci_host_send_packet(hci_cmd_buf, sz);
|
||||
}
|
||||
|
||||
static void hci_cmd_send_ble_adv_start(void)
|
||||
{
|
||||
uint16_t sz = make_cmd_ble_set_adv_enable (hci_cmd_buf, 1);
|
||||
esp_vhci_host_send_packet(hci_cmd_buf, sz);
|
||||
}
|
||||
|
||||
static void hci_cmd_send_ble_set_adv_param(void)
|
||||
{
|
||||
uint16_t adv_intv_min = 256; // 160ms
|
||||
uint16_t adv_intv_max = 256; // 160ms
|
||||
uint8_t adv_type = 0; // connectable undirected advertising (ADV_IND)
|
||||
uint8_t own_addr_type = 0; // Public Device Address
|
||||
uint8_t peer_addr_type = 0; // Public Device Address
|
||||
uint8_t peer_addr[6] = {0x80, 0x81, 0x82, 0x83, 0x84, 0x85};
|
||||
uint8_t adv_chn_map = 0x07; // 37, 38, 39
|
||||
uint8_t adv_filter_policy = 0; // Process All Conn and Scan
|
||||
|
||||
uint16_t sz = make_cmd_ble_set_adv_param(hci_cmd_buf,
|
||||
adv_intv_min,
|
||||
adv_intv_max,
|
||||
adv_type,
|
||||
own_addr_type,
|
||||
peer_addr_type,
|
||||
peer_addr,
|
||||
adv_chn_map,
|
||||
adv_filter_policy);
|
||||
esp_vhci_host_send_packet(hci_cmd_buf, sz);
|
||||
}
|
||||
|
||||
static void hci_cmd_send_ble_set_adv_data(void)
|
||||
{
|
||||
char *adv_name = "ESP-BLE-HELLO";
|
||||
uint8_t name_len = (uint8_t)strlen(adv_name);
|
||||
uint8_t adv_data[31] = {0x02, 0x01, 0x06, 0x0, 0x09};
|
||||
uint8_t adv_data_len;
|
||||
|
||||
adv_data[3] = name_len + 1;
|
||||
for (int i = 0; i < name_len; i++) {
|
||||
adv_data[5 + i] = (uint8_t)adv_name[i];
|
||||
}
|
||||
adv_data_len = 5 + name_len;
|
||||
|
||||
uint16_t sz = make_cmd_ble_set_adv_data(hci_cmd_buf, adv_data_len, (uint8_t *)adv_data);
|
||||
esp_vhci_host_send_packet(hci_cmd_buf, sz);
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief: send HCI commands to perform BLE advertising;
|
||||
*/
|
||||
void bleAdvtTask(void *pvParameters)
|
||||
{
|
||||
int cmd_cnt = 0;
|
||||
bool send_avail = false;
|
||||
esp_vhci_host_register_callback(&vhci_host_cb);
|
||||
printf("BLE advt task start\n");
|
||||
while (1) {
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
send_avail = esp_vhci_host_check_send_available();
|
||||
if (send_avail) {
|
||||
switch (cmd_cnt) {
|
||||
case 0: hci_cmd_send_reset(); ++cmd_cnt; break;
|
||||
case 1: hci_cmd_send_ble_set_adv_param(); ++cmd_cnt; break;
|
||||
case 2: hci_cmd_send_ble_set_adv_data(); ++cmd_cnt; break;
|
||||
case 3: hci_cmd_send_ble_adv_start(); ++cmd_cnt; break;
|
||||
}
|
||||
}
|
||||
printf("BLE Advertise, flag_send_avail: %d, cmd_sent: %d\n", send_avail, cmd_cnt);
|
||||
}
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
/* Initialize NVS — it is used to store PHY calibration data */
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK( ret );
|
||||
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
|
||||
|
||||
ret = esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
|
||||
if (ret) {
|
||||
ESP_LOGI(tag, "Bluetooth controller release classic bt memory failed: %s", esp_err_to_name(ret));
|
||||
return;
|
||||
}
|
||||
|
||||
if ((ret = esp_bt_controller_init(&bt_cfg)) != ESP_OK) {
|
||||
ESP_LOGI(tag, "Bluetooth controller initialize failed: %s", esp_err_to_name(ret));
|
||||
return;
|
||||
}
|
||||
|
||||
if ((ret = esp_bt_controller_enable(ESP_BT_MODE_BLE)) != ESP_OK) {
|
||||
ESP_LOGI(tag, "Bluetooth controller enable failed: %s", esp_err_to_name(ret));
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* If call mem release here, also work. Input ESP_BT_MODE_CLASSIC_BT, the function will
|
||||
* release the memory of classic bt mode.
|
||||
* esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* If call mem release here, also work. Input ESP_BT_MODE_BTDM, the function will calculate
|
||||
* that the BLE mode is already used, so it will release of only classic bt mode.
|
||||
* esp_bt_controller_mem_release(ESP_BT_MODE_BTDM);
|
||||
*/
|
||||
|
||||
xTaskCreatePinnedToCore(&bleAdvtTask, "bleAdvtTask", 2048, NULL, 5, NULL, 0);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
#
|
||||
# "main" pseudo-component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
||||
@@ -0,0 +1,12 @@
|
||||
# Override some defaults so BT stack is enabled
|
||||
# in this example
|
||||
|
||||
#
|
||||
# BT config
|
||||
#
|
||||
CONFIG_BT_ENABLED=y
|
||||
CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y
|
||||
CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n
|
||||
CONFIG_BTDM_CTRL_MODE_BTDM=n
|
||||
CONFIG_BT_BLUEDROID_ENABLED=n
|
||||
CONFIG_BT_CONTROLLER_ONLY=y
|
||||
Reference in New Issue
Block a user