This commit is contained in:
shchen 2020-12-23 16:31:47 +08:00
parent 3a6fef76a2
commit a327efd414
10 changed files with 1445 additions and 0 deletions

View File

@ -0,0 +1,40 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#
PROJECT_NAME := sdk_app_bledemo
PROJECT_PATH := $(abspath .)
PROJECT_BOARD := evb
export PROJECT_PATH PROJECT_BOARD
#CONFIG_TOOLPREFIX :=
-include ./proj_config.mk
ifeq ($(origin BL60X_SDK_PATH), undefined)
BL60X_SDK_PATH_GUESS ?= $(shell pwd)
BL60X_SDK_PATH ?= $(BL60X_SDK_PATH_GUESS)/../..
$(info ****** Please SET BL60X_SDK_PATH ******)
$(info ****** Trying SDK PATH [$(BL60X_SDK_PATH)])
endif
COMPONENTS_NETWORK := sntp dns_server
COMPONENTS_BLSYS := bltime blfdt blmtd blota bloop loopadc looprt loopset
COMPONENTS_VFS := romfs
COMPONENTS_BLE := blecontroller blestack
INCLUDE_COMPONENTS += freertos_riscv_ram bl602 bl602_std bl602_wifi bl602_wifidrv hal_drv lwip lwip_dhcpd mbedtls vfs yloop utils cli aws-iot httpc netutils blog blog_testc blsync_ble cjson
INCLUDE_COMPONENTS += easyflash4
INCLUDE_COMPONENTS += $(COMPONENTS_NETWORK)
INCLUDE_COMPONENTS += $(COMPONENTS_BLSYS)
INCLUDE_COMPONENTS += $(COMPONENTS_VFS)
INCLUDE_COMPONENTS += $(PROJECT_NAME)
ifeq ($(CONFIG_BT),1)
INCLUDE_COMPONENTS += $(COMPONENTS_BLE)
ifeq ($(CONFIG_BT_MESH),1)
INCLUDE_COMPONENTS += blemesh
endif
endif
include $(BL60X_SDK_PATH)/make_scripts_riscv/project.mk

View File

@ -0,0 +1,52 @@
# BLE Demo user guide
This demo code button pin is IO12. Please add the following setting to corresponding dts file this node feature is " button" and mode is " multipress". this node will trigger three event , customer can set "press time" setting.
The central device will connect peripheral device when two devices is close to 5 cm to 10 cm ( Rssi < -20 ).
```c
gpio2 {
status = "okay";
pin = <12>;
feature = "button";
active = "Hi";
mode = "multipress";
button {
debounce = <10>;
short_press_ms {
start = <100>;
end = <2000>;
kevent = <2>;
};
long_press_ms {
start = <3000>;
end = <5000>;
kevent = <3>;
};
longlong_press_ms {
start = <10000>;
kevent = <4>;
};
trig_level = "Lo";
};
hbn_use = "disable";
};
```
## 1. Peripheral Function
Short press button : Start Bluetooth Advertising.
Longlong press button : Send notification when device state is connected.
## 2. Central Function
Long press button : Start Bluetooth Scanning.
Longlong press button : Write data without response when device state is connected.

View File

@ -0,0 +1,3 @@
#!/bin/sh
make CONFIG_CHIP_NAME=BL602 CONFIG_LINK_ROM=1 -j
exit $?

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -0,0 +1,56 @@
#
#compiler flag config domain
#
#CONFIG_TOOLPREFIX :=
#CONFIG_OPTIMIZATION_LEVEL_RELEASE := 1
#CONFIG_M4_SOFTFP := 1
#
#board config domain
#
CONFIG_BOARD_FLASH_SIZE := 2
#firmware config domain
#
#set CONFIG_ENABLE_ACP to 1 to enable ACP, set to 0 or comment this line to disable
#CONFIG_ENABLE_ACP:=1
CONFIG_BL_IOT_FW_AP:=1
CONFIG_BL_IOT_FW_AMPDU:=0
CONFIG_BL_IOT_FW_AMSDU:=0
CONFIG_BL_IOT_FW_P2P:=0
CONFIG_ENABLE_PSM_RAM:=1
#CONFIG_ENABLE_CAMERA:=1
CONFIG_ENABLE_BLSYNC:=1
#CONFIG_ENABLE_VFS_SPI:=1
CONFIG_ENABLE_VFS_ROMFS:=1
# set easyflash env psm size, only support 4K、8K、16K options
CONFIG_ENABLE_PSM_EF_SIZE:=16K
CONFIG_FREERTOS_TICKLESS_MODE:=0
CONFIG_BT:=1
CONFIG_BT_PERIPHERAL:=1
CONFIG_BT_CENTRAL:=1
CONFIG_BT_OBSERVER:=1
CONFIG_BT_CONN:=1
CONFIG_BLE_TP_SERVER:=1
CONFIG_BT_SMP:=1
CONFIG_BT_FIXED_PASSKEY:=1
CONFIG_BT_SETTINGS:=1
#CONFIG_BT_MESH := 1
CONFIG_BLE_STACK_DBG_PRINT := 1
CONFIG_BT_STACK_PTS := 0
ifeq ($(CONFIG_BT_MESH),1)
CONFIG_BT_MESH_PB_ADV := 1
CONFIG_BT_MESH_PB_GATT := 1
CONFIG_BT_MESH_FRIEND := 1
CONFIG_BT_MESH_LOW_POWER := 1
CONFIG_BT_MESH_PROXY := 1
CONFIG_BT_MESH_GATT_PROXY := 1
endif
#blog enable components format :=blog_testc cli vfs helper
LOG_ENABLED_COMPONENTS:=blog_testc hal_drv loopset looprt bloop

View File

@ -0,0 +1,73 @@
/*
* Copyright (c) 2020 Bouffalolab.
*
* This file is part of
* *** Bouffalolab Software Dev Kit ***
* (see www.bouffalolab.com).
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __BLE_APP_H_
#define __BLE_APP_H_
#define NAME_LEN 30
#define CHAR_SIZE_MAX 512
#define BT_UUID_TEST BT_UUID_DECLARE_16(0xFFF0)
#define BT_UUID_TEST_RX BT_UUID_DECLARE_16(0xFFF1)
#define BT_UUID_TEST_TX BT_UUID_DECLARE_16(0xFFF2)
#define EV_BLE_TEST 0x0504
#define BLE_ADV_START 0x01
#define BLE_ADV_STOP 0x02
#define BLE_DEV_CONN 0x03
#define BLE_DEV_DISCONN 0x04
#define BLE_SCAN_START 0x05
#define BLE_SCAN_STOP 0x06
#define LED_RED_PIN (17)
#define LED_GREEN_PIN (14)
#define LED_BLUE_PIN (11)
enum app_ble_role
{
PERIPHERAL=1,
CENTRAL
};
void apps_ble_start (void);
void bleapps_adv_starting(void);
void bleapps_adv_stop(void);
void ble_bl_notify_task(void);
void ble_bl_disconnect(void);
void ble_appc_start(void);
void start_scan(void);
void ble_appc_stopscan(void);
void bl_gatt_write_without_rsp(void);
void ble_appc_disconnected(void);
#endif

View File

@ -0,0 +1,332 @@
/*
* Copyright (c) 2020 Bouffblfolab.
*
* This file is part of
* *** Bouffblfolab Software Dev Kit ***
* (see www.bouffblfolab.com).
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materiblfs provided with the distribution.
* 3. Neither the name of Bouffblfo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHblfL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTblf, SPECIblf, EXEMPLARY, OR CONSEQUENTIblf
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ble_app.h"
#include <FreeRTOS.h>
#include <task.h>
#include "bluetooth.h"
#include "ble_cli_cmds.h"
#include "hci_driver.h"
#include "ble_lib_api.h"
#include <errno.h>
#include <aos/kernel.h>
#include <aos/yloop.h>
#include "conn.h"
#include "gatt.h"
#include "hci_core.h"
#include "uuid.h"
#include "oad_api.h"
#include "hal_pwm.h"
#include "hal_gpio.h"
#include "bl_gpio.h"
#include <vfs.h>
static struct bt_conn *default_conn;
static struct bt_uuid_16 uuid = BT_UUID_INIT_16(0);
static struct bt_gatt_discover_params discover_params;
static struct bt_gatt_subscribe_params subscribe_params;
static u16_t bt_gatt_write_without_hanle=0;
static uint8_t notify_func(struct bt_conn *conn,
struct bt_gatt_subscribe_params *params,
const void *data, uint16_t length)
{
if(length!=0){
uint8_t *recv_buffer;
recv_buffer=pvPortMalloc(sizeof(uint8_t)*length);
memcpy(recv_buffer, data, length);
printf("ble notification=");
for (size_t i = 0; i < length; i++)
{
printf("0x%x ",recv_buffer[i]);
}
vPortFree(recv_buffer);
printf("\n");
}
return BT_GATT_ITER_CONTINUE;
}
void bl_gatt_write_without_rsp(void)
{
char data[10] = {0x00,0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
if(default_conn!=NULL){
bt_gatt_write_without_response(default_conn, bt_gatt_write_without_hanle, data, 10, 0);
}
}
void ble_appc_disconnected(void)
{
if(default_conn!=NULL)
{
printf("user disconnect\n");
bt_conn_disconnect(default_conn,BT_HCI_ERR_REMOTE_USER_TERM_CONN);
aos_post_event(EV_BLE_TEST,BLE_DEV_DISCONN,NULL);
}
}
void ble_appc_stopscan(void)
{
printf("user stop scan\n");
bt_le_scan_stop();
aos_post_event(EV_BLE_TEST,BLE_SCAN_STOP,NULL);
}
static uint8_t discover_func(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
struct bt_gatt_discover_params *params)
{
int err;
if (!attr) {
printf("Discover complete\n");
(void)memset(params, 0, sizeof(*params));
return BT_GATT_ITER_STOP;
}
printf("[ATTRIBUTE] handle %d\n", attr->handle);
if (!bt_uuid_cmp(discover_params.uuid, BT_UUID_TEST)) {
memcpy(&uuid, BT_UUID_TEST_RX, sizeof(uuid));
discover_params.uuid = &uuid.uuid;
discover_params.start_handle = attr->handle + 1;
discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
err = bt_gatt_discover(conn, &discover_params);
if (err) {
printf("Discover failed (err %d)\n", err);
}
} else if (!bt_uuid_cmp(discover_params.uuid,
BT_UUID_TEST_RX)) {
memcpy(&uuid, BT_UUID_GATT_CCC, sizeof(uuid));
discover_params.uuid = &uuid.uuid;
discover_params.start_handle = attr->handle + 2;
discover_params.type = BT_GATT_DISCOVER_DESCRIPTOR;
subscribe_params.value_handle = bt_gatt_attr_value_handle(attr);
err = bt_gatt_discover(conn, &discover_params);
if (err) {
printf("Discover failed (err %d)\n", err);
}
} else if(!bt_uuid_cmp(discover_params.uuid,
BT_UUID_GATT_CCC)) {
subscribe_params.notify = notify_func;
subscribe_params.value = BT_GATT_CCC_NOTIFY;
subscribe_params.ccc_handle = attr->handle;
err = bt_gatt_subscribe(conn, &subscribe_params);
if (err && err != -EALREADY) {
printf("Subscribe failed (err %d)\n", err);
} else {
printf("[SUBSCRIBED]\n");
bt_gatt_write_without_hanle=subscribe_params.ccc_handle+2;
}
return BT_GATT_ITER_STOP;
}
return BT_GATT_ITER_STOP;
}
static bool data_cb(struct bt_data *data, void *user_data)
{
char *name = user_data;
u8_t len;
switch (data->type) {
case BT_DATA_NAME_SHORTENED:
case BT_DATA_NAME_COMPLETE:
len = (data->data_len > NAME_LEN - 1)?(NAME_LEN - 1):(data->data_len);
memcpy(name, data->data, len);
return false;
default:
return true;
}
}
static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
struct net_buf_simple *ad)
{
char dev[BT_ADDR_LE_STR_LEN];
char name[NAME_LEN];
struct bt_le_conn_param param = {
.interval_min = BT_GAP_INIT_CONN_INT_MIN,
.interval_max = BT_GAP_INIT_CONN_INT_MAX,
.latency = 0,
.timeout = 400,
};
//struct bt_le_conn_param param;
bt_addr_le_to_str(addr, dev, sizeof(dev));
if (rssi < -20)
{
return;
}
/* We're only interested in connectable events */
if (type == BT_LE_ADV_IND) {
bt_le_scan_stop();
printf("device_found ok\r\n");
(void)memset(name, 0, sizeof(name));
bt_data_parse(ad, data_cb, name);
printf("[DEVICE]: %s, AD evt type %u, AD data len %u, RSSI %i NAME=%s\n",
dev, type, ad->len, rssi,name);
default_conn = bt_conn_create_le(addr, &param);
if(!default_conn){
printf("Connection failed\r\n");
}else{
printf("Connection pending\r\n");
}
}
}
void start_scan(void)
{
int err;
struct bt_le_scan_param scan_param;
scan_param.type=BT_LE_SCAN_TYPE_PASSIVE;
scan_param.filter_dup=0;
scan_param.interval=0x80;
scan_param.window=0x40;
err = bt_le_scan_start(&scan_param, device_found);
if (err) {
printf("Scanning failed to start (err %d)\n", err);
return;
}
aos_post_event(EV_BLE_TEST,BLE_SCAN_START,NULL);
printf("Scanning successfully started\n");
}
static void connected(struct bt_conn *conn, uint8_t conn_err)
{
char addr[BT_ADDR_LE_STR_LEN];
int err;
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
if (conn_err) {
printf("Failed to connect to %s (%u)\n", addr, conn_err);
bt_conn_unref(default_conn);
default_conn = NULL;
return;
}
printf("Connected: %s\n", addr);
aos_post_event(EV_BLE_TEST,BLE_DEV_CONN,NULL);
if (conn == default_conn) {
memcpy(&uuid, BT_UUID_TEST, sizeof(uuid));
discover_params.uuid = &uuid.uuid;
discover_params.func = discover_func;
discover_params.start_handle = 0x0001;
discover_params.end_handle = 0xffff;
discover_params.type = BT_GATT_DISCOVER_PRIMARY;
err = bt_gatt_discover(default_conn, &discover_params);
if (err) {
printf("Discover failed(err %d)\n", err);
return;
}
}
}
static void disconnected(struct bt_conn *conn, uint8_t reason)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
printf("Disconnected: %s (reason 0x%02x)\n", addr, reason);
if (default_conn != conn) {
return;
}
bt_conn_unref(default_conn);
default_conn = NULL;
aos_post_event(EV_BLE_TEST,BLE_DEV_DISCONN,NULL);
}
static struct bt_conn_cb conn_callbacks = {
.connected = connected,
.disconnected = disconnected,
};
static void bt_ready(void)
{
start_scan();
}
static void ble_app_init(int err)
{
if (err != 0) {
printf("BT FAILED started\n");
}else{
printf("BT SUCCESS started\n");
bt_ready();
}
}
void ble_appc_start(void)
{
int err;
ble_controller_init(configMAX_PRIORITIES - 1);
// Initiblfize BLE Host stack
hci_driver_init();
err = bt_enable(ble_app_init);
if (err) {
printf("Bluetooth init failed (err %d)\n", err);
return;
}
bt_conn_cb_register(&conn_callbacks);
}

View File

@ -0,0 +1,354 @@
/*
* Copyright (c) 2020 Bouffblfolab.
*
* This file is part of
* *** Bouffblfolab Software Dev Kit ***
* (see www.bouffblfolab.com).
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materiblfs provided with the distribution.
* 3. Neither the name of Bouffblfo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHblfL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTblf, SPECIblf, EXEMPLARY, OR CONSEQUENTIblf
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ble_app.h"
#include <FreeRTOS.h>
#include <task.h>
#include "bluetooth.h"
#include "ble_cli_cmds.h"
#include "hci_driver.h"
#include "ble_lib_api.h"
#include <aos/kernel.h>
#include <aos/yloop.h>
#include "conn.h"
#include "gatt.h"
#include "hci_core.h"
#include "uuid.h"
#include "oad_api.h"
#include "hal_pwm.h"
#include "hal_gpio.h"
#include "bl_gpio.h"
#include <vfs.h>
#include <hal/soc/pwm.h>
#include <device/vfs_pwm.h>
static void ble_bl_ccc_cfg_changed(const struct bt_gatt_attr *attr, u16_t vblfue);
static int ble_blf_recv(struct bt_conn *conn,
const struct bt_gatt_attr *attr, const void *buf,
u16_t len, u16_t offset, u8_t flags);
static const struct bt_data ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA(BT_DATA_NAME_COMPLETE, "BL_602", 6),
BT_DATA(BT_DATA_MANUFACTURER_DATA, "BL_602", 6),
};
static struct bt_gatt_attr blattrs[]= {
BT_GATT_PRIMARY_SERVICE(BT_UUID_TEST),
BT_GATT_CHARACTERISTIC(BT_UUID_TEST_RX,
BT_GATT_CHRC_NOTIFY,
BT_GATT_PERM_READ,
NULL,
NULL,
NULL),
BT_GATT_CCC(ble_bl_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
BT_GATT_CHARACTERISTIC(BT_UUID_TEST_TX,
BT_GATT_CHRC_WRITE_WITHOUT_RESP,
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
NULL,
ble_blf_recv,
NULL)
};
static struct bt_conn *ble_bl_conn=NULL;
static bool notify_flag=false;
static void ble_bl_ccc_cfg_changed(const struct bt_gatt_attr *attr, u16_t vblfue)
{
if(vblfue == BT_GATT_CCC_NOTIFY) {
printf("enable notify.\n");
notify_flag=true;
} else {
printf("disable notify.\n");
notify_flag=false;
}
}
void ble_bl_notify_task(void)
{
char data[10] = {0x00,0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
if(ble_bl_conn!=NULL&&notify_flag==true){
printf("ble_bl_notify_task.\n");
bt_gatt_notify(ble_bl_conn, &blattrs[1],data,10);
}
}
void ble_bl_disconnect(void)
{
if(ble_bl_conn!=NULL)
{
printf("user disconnect\n");
bt_conn_disconnect(ble_bl_conn,BT_HCI_ERR_REMOTE_USER_TERM_CONN);
aos_post_event(EV_BLE_TEST,BLE_DEV_DISCONN,NULL);
}
}
static int ble_blf_recv(struct bt_conn *conn,
const struct bt_gatt_attr *attr, const void *buf,
u16_t len, u16_t offset, u8_t flags)
{
uint8_t *recv_buffer;
recv_buffer=pvPortMalloc(sizeof(uint8_t)*len);
memcpy(recv_buffer, buf, len);
printf("ble rx=%d\n",len);
for (size_t i = 0; i < len; i++)
{
printf("0x%x ",recv_buffer[i]);
}
vPortFree(recv_buffer);
printf("\n");
return 0;
}
struct bt_gatt_service ble_bl_server = BT_GATT_SERVICE(blattrs);
static void bt_ready(void)
{
bt_set_name("blf_602");
printf("Bluetooth initiblfized\n");
bleapps_adv_starting();
}
void bleapps_adv_starting(void)
{
int err;
printf("Bluetooth Advertising start\n");
err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
if (err) {
printf("Advertising failed to start (err %d)\n", err);
}else{
aos_post_event(EV_BLE_TEST,BLE_ADV_START,NULL);
}
}
void bleapps_adv_stop(void)
{
int err;
printf("Bluetooth Advertising stop\n");
err =bt_le_adv_stop();
if (err) {
printf("Advertising failed to stop (err %d)\n", err);
}else{
aos_post_event(EV_BLE_TEST,BLE_ADV_STOP,NULL);
}
}
static void ble_app_init(int err)
{
if (err != 0) {
printf("BT FAILED started\n");
}else{
printf("BT SUCCESS started\n");
bt_ready();
}
}
static void bl_connected(struct bt_conn *conn, uint8_t err)
{
struct bt_le_conn_param param;
param.interval_max=24;
param.interval_min=24;
param.latency=0;
param.timeout=600;
int update_err;
if (err) {
printf("Connection failed (err 0x%02x)\n", err);
} else {
printf("Connected\n");
ble_bl_conn=conn;
update_err = bt_conn_le_param_update(conn, &param);
if (err) {
printf("conn update failed (err %d)\r\n", err);
} else {
printf("conn update initiated\r\n");
}
#if 0
#if defined(CONFIG_BT_SMP)
bt_conn_set_security(conn, BT_SECURITY_L4);
#endif
#endif
aos_post_event(EV_BLE_TEST,BLE_DEV_CONN,NULL);
}
}
static void bl_disconnected(struct bt_conn *conn, uint8_t reason)
{
printf("Disconnected (reason 0x%02x)\n", reason);
aos_post_event(EV_BLE_TEST,BLE_DEV_DISCONN,NULL);
}
static struct bt_conn_cb conn_callbacks = {
.connected = bl_connected,
.disconnected = bl_disconnected,
};
static void ble_stack_start(void)
{
// Initiblfize BLE controller
ble_controller_init(configMAX_PRIORITIES - 1);
// Initiblfize BLE Host stack
hci_driver_init();
bt_enable(ble_app_init);
}
#if defined(CONFIG_BT_SMP)
static void auth_passkey_display(struct bt_conn *conn, unsigned int passkey)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
printf("passkey_str is: %06u\r\n", passkey);
}
static void auth_passkey_confirm(struct bt_conn *conn, unsigned int passkey)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
printf("Confirm passkey for %s: %06u\r\n", addr, passkey);
bt_conn_auth_passkey_confirm(conn);
}
static void auth_passkey_entry(struct bt_conn *conn)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
printf("Enter passkey for %s\r\n", addr);
}
static void auth_cancel(struct bt_conn *conn)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
printf("Pairing cancelled: %s\r\n", addr);
}
static void auth_pairing_confirm(struct bt_conn *conn)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
printf("Confirm pairing for %s\r\n", addr);
}
static void auth_pairing_complete(struct bt_conn *conn, bool bonded)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
printf("%s with %s\r\n", bonded ? "Bonded" : "Paired", addr);
}
static void auth_pairing_failed(struct bt_conn *conn, enum bt_security_err reason)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
printf("Pairing failed with %s\r\n", addr);
}
static struct bt_conn_auth_cb auth_cb_display = {
.passkey_display = auth_passkey_display,
.passkey_entry = auth_passkey_entry,
.passkey_confirm = auth_passkey_confirm,
.cancel = auth_cancel,
.pairing_confirm = auth_pairing_confirm,
.pairing_failed = auth_pairing_failed,
.pairing_complete = auth_pairing_complete,
};
#endif
void apps_ble_start(void)
{
int err;
ble_stack_start();
bt_conn_cb_register(&conn_callbacks);
err=bt_gatt_service_register(&ble_bl_server);
if (err==0) {
printf("bt_gatt_service_register ok\n");
} else {
printf("bt_gatt_service_register err\n");
}
#if defined(CONFIG_BT_SMP)
err = bt_conn_auth_cb_register(&auth_cb_display);
if(err){
printf("Auth callback has blfready been registered\r\n");
}else{
printf("Register auth callback successfully\r\n");
}
#endif
}

View File

@ -0,0 +1,26 @@
#
# "main" pseudo-component makefile.
#
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
include $(BL60X_SDK_PATH)/components/network/ble/ble_common.mk
ifeq ($(CONFIG_ENABLE_PSM_RAM),1)
CPPFLAGS += -DCONF_USER_ENABLE_PSRAM
endif
ifeq ($(CONFIG_ENABLE_CAMERA),1)
CPPFLAGS += -DCONF_USER_ENABLE_CAMERA
endif
ifeq ($(CONFIG_ENABLE_BLSYNC),1)
CPPFLAGS += -DCONF_USER_ENABLE_BLSYNC
endif
ifeq ($(CONFIG_ENABLE_VFS_SPI),1)
CPPFLAGS += -DCONF_USER_ENABLE_VFS_SPI
endif
ifeq ($(CONFIG_ENABLE_VFS_ROMFS),1)
CPPFLAGS += -DCONF_USER_ENABLE_VFS_ROMFS
endif

View File

@ -0,0 +1,509 @@
/*
* Copyright (c) 2020 Bouffalolab.
*
* This file is part of
* *** Bouffalolab Software Dev Kit ***
* (see www.bouffalolab.com).
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <FreeRTOS.h>
#include <task.h>
#include <timers.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <vfs.h>
#include <aos/kernel.h>
#include <aos/yloop.h>
#include <event_device.h>
#include <cli.h>
#include <bl602_glb.h>
#include <bl602_hbn.h>
#include "bl602_adc.h"
#include <bl_sys.h>
#include <bl_uart.h>
#include <bl_chip.h>
#include <bl_sec.h>
#include <bl_cks.h>
#include <bl_irq.h>
#include <bl_timer.h>
#include <bl_dma.h>
#include <bl_gpio.h>
#include <bl_gpio_cli.h>
#include <bl_wdt_cli.h>
#include <hal_uart.h>
#include <hal_sys.h>
#include <hal_gpio.h>
#include <hal_hbn.h>
#include <hal_boot2.h>
#include <hal_board.h>
#include <hal_button.h>
#include <looprt.h>
#include <loopset.h>
#include <bl_sys_time.h>
#include <bl_romfs.h>
#include <fdt.h>
#include "hal_pwm.h"
#include <vfs.h>
#include <hal/soc/pwm.h>
#include <device/vfs_pwm.h>
#include <utils_log.h>
#include <libfdt.h>
#include <blog.h>
#include "ble_lib_api.h"
#include "ble_app.h"
#include "hal_pds.h"
#include "bl_rtc.h"
#include "utils_string.h"
extern uint8_t _heap_start;
extern uint8_t _heap_size; // @suppress("Type cannot be resolved")
extern uint8_t _heap_wifi_start;
extern uint8_t _heap_wifi_size; // @suppress("Type cannot be resolved")
static HeapRegion_t xHeapRegions[] =
{
{ &_heap_start, (unsigned int) &_heap_size}, //set on runtime
{ &_heap_wifi_start, (unsigned int) &_heap_wifi_size },
{ NULL, 0 }, /* Terminates the array. */
{ NULL, 0 } /* Terminates the array. */
};
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName )
{
puts("Stack Overflow checked\r\n");
while (1) {
/*empty here*/
}
}
void vApplicationMallocFailedHook(void)
{
printf("Memory Allocate Failed. Current left size is %d bytes\r\n",
xPortGetFreeHeapSize()
);
while (1) {
/*empty here*/
}
}
void vApplicationIdleHook(void)
{
__asm volatile(
" wfi "
);
}
static void proc_hellow_entry(void *pvParameters)
{
vTaskDelay(500);
while (1) {
printf("%s: RISC-V rv32imafc\r\n", __func__);
vTaskDelay(10000);
}
vTaskDelete(NULL);
}
static int get_dts_addr(const char *name, uint32_t *start, uint32_t *off)
{
uint32_t addr = hal_board_get_factory_addr();
const void *fdt = (const void *)addr;
uint32_t offset;
if (!name || !start || !off) {
return -1;
}
offset = fdt_subnode_offset(fdt, 0, name);
if (offset <= 0) {
log_error("%s NULL.\r\n", name);
return -1;
}
*start = (uint32_t)fdt;
*off = offset;
return 0;
}
static enum app_ble_role app_role;
static void event_cb_key_event(input_event_t *event, void *private_data)
{
switch (event->code) {
case KEY_1:
{
printf("[KEY_1] [EVT] INIT DONE %lld\r\n", aos_now_ms());
printf("short press \r\n");
apps_ble_start();
app_role=PERIPHERAL;
}
break;
case KEY_2:
{
printf("[KEY_2] [EVT] INIT DONE %lld\r\n", aos_now_ms());
printf("long press \r\n");
ble_appc_start();
app_role=CENTRAL;
}
break;
case KEY_3:
{
printf("[KEY_3] [EVT] INIT DONE %lld\r\n", aos_now_ms());
printf("longlong press \r\n");
if(app_role==PERIPHERAL){
ble_bl_notify_task();
}else{
bl_gatt_write_without_rsp();
}
}
break;
default:
{
printf("[KEY] [EVT] Unknown code %u, %lld\r\n", event->code, aos_now_ms());
/*nothing*/
}
}
}
static void borad_rgb_init(void)
{
bl_gpio_enable_output(LED_RED_PIN,1,0);
bl_gpio_enable_output(LED_GREEN_PIN,1,0);
bl_gpio_enable_output(LED_BLUE_PIN,1,0);
bl_gpio_output_set(LED_RED_PIN,1);
bl_gpio_output_set(LED_GREEN_PIN,1);
bl_gpio_output_set(LED_BLUE_PIN,1);
}
static void borad_rgb_off(void)
{
bl_gpio_output_set(LED_RED_PIN,1);
bl_gpio_output_set(LED_GREEN_PIN,1);
bl_gpio_output_set(LED_BLUE_PIN,1);
}
static void event_cb_ble_event(input_event_t *event, void *private_data)
{
switch (event->code) {
case BLE_ADV_START:
{
borad_rgb_off();
printf("ble adv start \r\n");
bl_gpio_output_set(LED_BLUE_PIN,0);
}
break;
case BLE_ADV_STOP:
{
printf("ble adv stop \r\n");
borad_rgb_off();
}
break;
case BLE_DEV_CONN:
{
borad_rgb_off();
bl_gpio_output_set(LED_GREEN_PIN,0);
printf("ble dev connnected \r\n");
}
break;
case BLE_DEV_DISCONN:
{
borad_rgb_off();
bl_gpio_output_set(LED_RED_PIN,0);
printf("ble dev disconnnected\r\n");
vTaskDelay(5000);
if(app_role==PERIPHERAL){
bleapps_adv_starting();
}else{
start_scan();
}
}
break;
case BLE_SCAN_START:
{
borad_rgb_off();
bl_gpio_output_set(LED_BLUE_PIN,0);
printf("ble dev scanning\r\n");
}
break;
case BLE_SCAN_STOP:
{
borad_rgb_off();
printf("ble dev stop scan\r\n");
}
break;
default:
{
printf("[KEY] [EVT] Unknown code %u, %lld\r\n", event->code, aos_now_ms());
/*nothing*/
}
}
}
static void aos_loop_proc(void *pvParameters)
{
int fd_console;
uint32_t fdt = 0, offset = 0;
static StackType_t proc_stack_looprt[512];
static StaticTask_t proc_task_looprt;
/*Init bloop stuff*/
looprt_start(proc_stack_looprt, 512, &proc_task_looprt);
loopset_led_hook_on_looprt();
// easyflash_init();
vfs_init();
vfs_device_init();
/* uart */
#if 1
if (0 == get_dts_addr("uart", &fdt, &offset)) {
vfs_uart_init(fdt, offset);
}
#else
vfs_uart_init_simple_mode(0, 7, 16, 2 * 1000 * 1000, "/dev/ttyS0");
#endif
if (0 == get_dts_addr("gpio", &fdt, &offset)) {
fdt_button_module_init((const void *)fdt, (int)offset);
}
#if 0 /* pwm */
if (0 == get_dts_addr("pwm", &fdt, &offset)) {
vfs_pwm_init(fdt, offset);
}
#endif
aos_loop_init();
fd_console = aos_open("/dev/ttyS0", 0);
if (fd_console >= 0) {
printf("Init CLI with event Driven\r\n");
aos_cli_init(0);
aos_poll_read_fd(fd_console, aos_cli_event_cb_read_get(), (void*)0x12345678);
}
aos_register_event_filter(EV_KEY, event_cb_key_event, NULL);
aos_register_event_filter(EV_BLE_TEST, event_cb_ble_event, NULL);
//tsen_adc_init();
#if defined(CONFIG_BT_TL)
//uart's pinmux has been configured in vfs_uart_init(load uart1's pin info from devicetree)
uart_init(1);
ble_controller_init(configMAX_PRIORITIES - 1);
#endif
aos_loop_run();
puts("------------------------------------------\r\n");
puts("+++++++++Critical Exit From Loop++++++++++\r\n");
puts("******************************************\r\n");
vTaskDelete(NULL);
}
void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize)
{
/* If the buffers to be provided to the Idle task are declared inside this
function then they must be declared static - otherwise they will be allocated on
the stack and so not exists after this function exits. */
static StaticTask_t xIdleTaskTCB;
//static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
static StackType_t uxIdleTaskStack[512];
/* Pass out a pointer to the StaticTask_t structure in which the Idle task's
state will be stored. */
*ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
/* Pass out the array that will be used as the Idle task's stack. */
*ppxIdleTaskStackBuffer = uxIdleTaskStack;
/* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
Note that, as the array is necessarily of type StackType_t,
configMINIMAL_STACK_SIZE is specified in words, not bytes. */
//*pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
*pulIdleTaskStackSize = 512;//size 512 words is For ble pds mode, otherwise stack overflow of idle task will happen.
}
/* configSUPPORT_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the
application must provide an implementation of vApplicationGetTimerTaskMemory()
to provide the memory that is used by the Timer service task. */
void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize)
{
/* If the buffers to be provided to the Timer task are declared inside this
function then they must be declared static - otherwise they will be allocated on
the stack and so not exists after this function exits. */
static StaticTask_t xTimerTaskTCB;
static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
/* Pass out a pointer to the StaticTask_t structure in which the Timer
task's state will be stored. */
*ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
/* Pass out the array that will be used as the Timer task's stack. */
*ppxTimerTaskStackBuffer = uxTimerTaskStack;
/* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.
Note that, as the array is necessarily of type StackType_t,
configTIMER_TASK_STACK_DEPTH is specified in words, not bytes. */
*pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
}
void user_vAssertCalled(void) __attribute__ ((weak, alias ("vAssertCalled")));
void vAssertCalled(void)
{
volatile uint32_t ulSetTo1ToExitFunction = 0;
taskDISABLE_INTERRUPTS();
while( ulSetTo1ToExitFunction != 1 ) {
__asm volatile( "NOP" );
}
}
static void _dump_boot_info(void)
{
char chip_feature[40];
const char *banner;
puts("Booting BL602 Chip...\r\n");
/*Display Banner*/
if (0 == bl_chip_banner(&banner)) {
puts(banner);
}
puts("\r\n");
/*Chip Feature list*/
puts("\r\n");
puts("------------------------------------------------------------\r\n");
puts("RISC-V Core Feature:");
bl_chip_info(chip_feature);
puts(chip_feature);
puts("\r\n");
puts("Build Version: ");
puts(BL_SDK_VER); // @suppress("Symbol is not resolved")
puts("\r\n");
puts("Build Version: ");
puts(BL_SDK_VER); // @suppress("Symbol is not resolved")
puts("\r\n");
puts("PHY Version: ");
puts(BL_SDK_PHY_VER); // @suppress("Symbol is not resolved")
puts("\r\n");
puts("RF Version: ");
puts(BL_SDK_RF_VER); // @suppress("Symbol is not resolved")
puts("\r\n");
puts("Build Date: ");
puts(__DATE__);
puts("\r\n");
puts("Build Time: ");
puts(__TIME__);
puts("\r\n");
puts("------------------------------------------------------------\r\n");
}
static void system_init(void)
{
blog_init();
bl_irq_init();
bl_sec_init();
bl_sec_test();
bl_dma_init();
bl_rtc_init();
hal_boot2_init();
/* board config is set after system is init*/
hal_board_cfg(0);
}
static void system_thread_init()
{
}
void bfl_main()
{
static StackType_t proc_hellow_stack[512];
static StaticTask_t proc_hellow_task;
static StackType_t aos_loop_proc_stack[1024];
static StaticTask_t aos_loop_proc_task;
bl_sys_early_init();
/*Init UART In the first place*/
bl_uart_init(0, 16, 7, 255, 255, 2 * 1000 * 1000);
puts("Starting bl602 now....\r\n");
bl_sys_init();
_dump_boot_info();
borad_rgb_init();
vPortDefineHeapRegions(xHeapRegions);
printf("Heap %u@%p, %u@%p\r\n",
(unsigned int)&_heap_size, &_heap_start,
(unsigned int)&_heap_wifi_size, &_heap_wifi_start
);
system_init();
system_thread_init();
puts("[OS] Starting proc_hellow_entry task...\r\n");
xTaskCreateStatic(proc_hellow_entry, (char*)"hellow", 512, NULL, 15, proc_hellow_stack, &proc_hellow_task);
puts("[OS] Starting aos_loop_proc task...\r\n");
xTaskCreateStatic(aos_loop_proc, (char*)"event_loop", 1024, NULL, 15, aos_loop_proc_stack, &aos_loop_proc_task);
puts("[OS] Starting OS Scheduler...\r\n");
vTaskStartScheduler();
}