├── .gitignore ├── CMakeLists.txt ├── FreeRTOSConfig.h ├── LICENSE ├── Makefile ├── README.md ├── btblecontroller ├── CMakeLists.txt ├── btble_inc │ ├── btble_lib_api.h │ └── hci_onchip.h └── lib │ ├── libbtblecontroller_bl616_ble1m0s1sbredr1.a │ └── libbtblecontroller_bl808_ble1m0s1bredr0.a ├── btstack_config.h ├── btstack_port.c ├── flash_prog_cfg.ini ├── libqcc743_on_bl616.c ├── main.c └── proj.conf /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | codecs 3 | *.ps1 4 | dump_dma.py -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required (VERSION 3.22.1) 2 | 3 | include(proj.conf) 4 | 5 | find_package(bouffalo_sdk REQUIRED HINTS $ENV{BL_SDK_BASE}) 6 | 7 | sdk_set_main_file(main.c) 8 | sdk_add_include_directories(.) 9 | sdk_add_include_directories(./btblecontroller/btble_inc) 10 | sdk_add_static_library(btblecontroller/lib/libbtblecontroller_bl616_ble1m0s1sbredr1.a) 11 | project(btstack_test) 12 | 13 | SET(BTSTACK_ROOT ${CMAKE_CURRENT_BINARY_DIR}/../btstack) 14 | sdk_add_include_directories(${BTSTACK_ROOT}/3rd-party/micro-ecc) 15 | sdk_add_include_directories(${BTSTACK_ROOT}/3rd-party/bluedroid/decoder/include) 16 | sdk_add_include_directories(${BTSTACK_ROOT}/3rd-party/bluedroid/encoder/include) 17 | sdk_add_include_directories(${BTSTACK_ROOT}/3rd-party/lc3-google/include) 18 | sdk_add_include_directories(${BTSTACK_ROOT}/3rd-party/md5) 19 | sdk_add_include_directories(${BTSTACK_ROOT}/3rd-party/hxcmod-player) 20 | sdk_add_include_directories(${BTSTACK_ROOT}/3rd-party/hxcmod-player/mod) 21 | sdk_add_include_directories(${BTSTACK_ROOT}/3rd-party/rijndael) 22 | sdk_add_include_directories(${BTSTACK_ROOT}/3rd-party/yxml) 23 | sdk_add_include_directories(${BTSTACK_ROOT}/3rd-party/tinydir) 24 | sdk_add_include_directories(${BTSTACK_ROOT}/src) 25 | sdk_add_include_directories(${BTSTACK_ROOT}/platform/embedded) 26 | sdk_add_include_directories(${BTSTACK_ROOT}/platform/freertos) 27 | 28 | file(GLOB SOURCES_SRC "${BTSTACK_ROOT}/src/*.c" "${BTSTACK_ROOT}/example/sco_demo_util.c") 29 | file(GLOB SOURCES_BLE "${BTSTACK_ROOT}/src/ble/*.c") 30 | file(GLOB SOURCES_BLUEDROID "${BTSTACK_ROOT}/3rd-party/bluedroid/encoder/srce/*.c" "${BTSTACK_ROOT}/3rd-party/bluedroid/decoder/srce/*.c") 31 | file(GLOB SOURCES_CLASSIC "${BTSTACK_ROOT}/src/classic/*.c") 32 | file(GLOB SOURCES_MESH "${BTSTACK_ROOT}/src/mesh/*.c") 33 | file(GLOB SOURCES_GATT "${BTSTACK_ROOT}/src/ble/gatt-service/*.c") 34 | file(GLOB SOURCES_UECC "${BTSTACK_ROOT}/3rd-party/micro-ecc/uECC.c")#ecc 35 | file(GLOB SOURCES_RIJNDAEL "${BTSTACK_ROOT}/3rd-party/rijndael/rijndael.c")#btstack_crypto.c aes 36 | file(GLOB SOURCES_LC3_GOOGLE "${BTSTACK_ROOT}/3rd-party/lc3-google/src/*.c") 37 | file(GLOB SOURCES_FREERTOS_PORT "${BTSTACK_ROOT}/platform/freertos/btstack_run_loop_freertos.c") 38 | file(GLOB SOURCES_HCI_STDOUT "${BTSTACK_ROOT}/platform/embedded/hci_dump_embedded_stdout.c") 39 | file(GLOB SOURCES_BLE_OFF "${BTSTACK_ROOT}/src/ble/le_device_db_memory.c") 40 | list(REMOVE_ITEM SOURCES_BLE ${SOURCES_BLE_OFF}) 41 | 42 | set(SOURCES 43 | ${SOURCES_BLE} 44 | ${SOURCES_BLUEDROID} 45 | ${SOURCES_CLASSIC} 46 | ${SOURCES_GATT} 47 | ${SOURCES_MESH} 48 | ${SOURCES_RIJNDAEL} 49 | ${SOURCES_SRC} 50 | ${SOURCES_UECC} 51 | ${SOURCES_FREERTOS_PORT} 52 | ${SOURCES_HCI_STDOUT} 53 | ) 54 | list(SORT SOURCES) 55 | 56 | 57 | set(EXAMPLE "a2dp_sink_demo") 58 | find_package (Python REQUIRED COMPONENTS Interpreter) 59 | add_custom_command( 60 | OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE}.h 61 | DEPENDS ${BTSTACK_ROOT}/example/${EXAMPLE}.gatt 62 | COMMAND ${Python_EXECUTABLE} ${BTSTACK_ROOT}/tool/compile_gatt.py ${BTSTACK_ROOT}/example/${EXAMPLE}.gatt ${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE}.h 63 | VERBATIM 64 | ) 65 | #add_custom_command 可能不会运行,所以我将这个指令输出,第一次编译会输出这个指令,第二次编译之前就可以执行这个指令 66 | #add_custom_command may not run, so I will output this instruction. The first compilation will output this instruction, and it can be executed before the second compilation 67 | message("${Python_EXECUTABLE} ${BTSTACK_ROOT}/tool/compile_gatt.py ${BTSTACK_ROOT}/example/${EXAMPLE}.gatt ${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE}.h") 68 | 69 | sdk_add_include_directories(${CMAKE_CURRENT_BINARY_DIR}/) 70 | 71 | target_sources(app PRIVATE 72 | ${SOURCES} 73 | btstack_port.c 74 | #选择一个例子 75 | #Choose an example 76 | ${BTSTACK_ROOT}/example/${EXAMPLE}.c 77 | ) -------------------------------------------------------------------------------- /FreeRTOSConfig.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS Kernel V10.2.1 3 | * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://www.FreeRTOS.org 23 | * http://aws.amazon.com/freertos 24 | * 25 | * 1 tab == 4 spaces! 26 | */ 27 | 28 | #ifndef FREERTOS_CONFIG_H 29 | #define FREERTOS_CONFIG_H 30 | 31 | /*----------------------------------------------------------- 32 | * Application specific definitions. 33 | * 34 | * These definitions should be adjusted for your particular hardware and 35 | * application requirements. 36 | * 37 | * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE 38 | * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE. 39 | * 40 | * See http://www.freertos.org/a00110.html. 41 | *----------------------------------------------------------*/ 42 | #include "stdio.h" 43 | 44 | #ifdef BL702 45 | #define configMTIME_BASE_ADDRESS (0x02000000UL + 0xBFF8UL) 46 | #define configMTIMECMP_BASE_ADDRESS (0x02000000UL + 0x4000UL) 47 | #else 48 | #define configMTIME_BASE_ADDRESS (0xE0000000UL + 0xBFF8UL) 49 | #define configMTIMECMP_BASE_ADDRESS (0xE0000000UL + 0x4000UL) 50 | #endif 51 | 52 | #define configUSE_TASK_NOTIFICATIONS 1 53 | #define configSUPPORT_STATIC_ALLOCATION 1 54 | #define configUSE_PREEMPTION 1 55 | #define configUSE_IDLE_HOOK 0 56 | #define configUSE_TICK_HOOK 0 57 | #define configCPU_CLOCK_HZ ((uint32_t)(1 * 1000 * 1000)) 58 | #define configTICK_RATE_HZ ((TickType_t)1000) 59 | #define configMAX_PRIORITIES (32) 60 | #define configMINIMAL_STACK_SIZE ((unsigned short)128) /* Only needs to be this high as some demo tasks also use this constant. In production only the idle task would use this. */ 61 | //#define configTOTAL_HEAP_SIZE ((size_t)24 * 1024) 62 | #define configMAX_TASK_NAME_LEN (16) 63 | #define configUSE_TRACE_FACILITY 1 64 | #define configUSE_STATS_FORMATTING_FUNCTIONS 1 65 | #define configUSE_16_BIT_TICKS 0 66 | #define configIDLE_SHOULD_YIELD 0 67 | #define configUSE_MUTEXES 1 68 | #define configQUEUE_REGISTRY_SIZE 8 69 | #define configCHECK_FOR_STACK_OVERFLOW 2 70 | #define configUSE_RECURSIVE_MUTEXES 1 71 | #define configUSE_MALLOC_FAILED_HOOK 1 72 | #define configUSE_APPLICATION_TASK_TAG 1 73 | #define configUSE_COUNTING_SEMAPHORES 1 74 | #define configGENERATE_RUN_TIME_STATS 0 75 | #define configUSE_PORT_OPTIMISED_TASK_SELECTION 1 76 | #define configUSE_TICKLESS_IDLE 0 77 | #define configUSE_POSIX_ERRNO 1 78 | 79 | /* Co-routine definitions. */ 80 | #define configUSE_CO_ROUTINES 0 81 | #define configMAX_CO_ROUTINE_PRIORITIES (2) 82 | 83 | /* Software timer definitions. */ 84 | #define configUSE_TIMERS 1 85 | #define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 1) 86 | #define configTIMER_QUEUE_LENGTH 4 87 | #define configTIMER_TASK_STACK_DEPTH (1024) 88 | 89 | /* Task priorities. Allow these to be overridden. */ 90 | #ifndef uartPRIMARY_PRIORITY 91 | #define uartPRIMARY_PRIORITY (configMAX_PRIORITIES - 3) 92 | #endif 93 | 94 | /* Set the following definitions to 1 to include the API function, or zero 95 | to exclude the API function. */ 96 | #define INCLUDE_vTaskPrioritySet 1 97 | #define INCLUDE_uxTaskPriorityGet 1 98 | #define INCLUDE_vTaskDelete 1 99 | #define INCLUDE_vTaskCleanUpResources 1 100 | #define INCLUDE_vTaskSuspend 1 101 | #define INCLUDE_vTaskDelayUntil 1 102 | #define INCLUDE_vTaskDelay 1 103 | #define INCLUDE_eTaskGetState 1 104 | #define INCLUDE_xTimerPendFunctionCall 1 105 | #define INCLUDE_xTaskAbortDelay 1 106 | #define INCLUDE_xTaskGetHandle 1 107 | #define INCLUDE_xSemaphoreGetMutexHolder 1 108 | 109 | /* Normal assert() semantics without relying on the provision of an assert.h 110 | header file. */ 111 | void vApplicationMallocFailedHook(void); 112 | void vAssertCalled(void); 113 | #define configASSERT(x) \ 114 | if ((x) == 0) { \ 115 | printf("file [%s]\r\n", __FILE__); \ 116 | printf("func [%s]\r\n", __FUNCTION__); \ 117 | printf("line [%d]\r\n", __LINE__); \ 118 | printf("%s\r\n", (const char *)(#x)); \ 119 | vAssertCalled(); \ 120 | } 121 | 122 | #if (configUSE_TICKLESS_IDLE != 0) 123 | void vApplicationSleep(uint32_t xExpectedIdleTime); 124 | #define portSUPPRESS_TICKS_AND_SLEEP(xExpectedIdleTime) vApplicationSleep(xExpectedIdleTime) 125 | #endif 126 | 127 | // #define portUSING_MPU_WRAPPERS 128 | 129 | #endif /* FREERTOS_CONFIG_H */ 130 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 O2C14 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | SDK_DEMO_PATH ?= . 2 | BL_SDK_BASE ?= $(SDK_DEMO_PATH)/../.. 3 | 4 | export BL_SDK_BASE 5 | 6 | CHIP ?= bl616 7 | BOARD ?= bl616dk 8 | CROSS_COMPILE ?= riscv64-unknown-elf- 9 | 10 | # add custom cmake definition 11 | #cmake_definition+=-Dxxx=sss 12 | 13 | 14 | include $(BL_SDK_BASE)/project.build 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # btstack_test 2 | 准备环境: 3 | ``` 4 | git clone https://github.com/bouffalolab/bouffalo_sdk.git 5 | cd ./bouffalo_sdk/examples 6 | git clone https://github.com/O2C14/btstack_test.git 7 | cd ./btstack_test 8 | rm -rf ./btstack 9 | git clone https://github.com/bluekitchen/btstack.git -b v1.6.1 10 | ``` 11 | 然后就可以像其他例子那样编译烧录 12 | 13 | 若需要改变demo可以在CMakeLists.txt修改`set(EXAMPLE "a2dp_sink_demo")` 14 | 15 | 使用 `btstack erase` 来忘记已连接的设备 -------------------------------------------------------------------------------- /btblecontroller/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | sdk_add_include_directories(btble_inc) 2 | sdk_add_include_directories(btblecontroller_port) 3 | 4 | # use default port built by lib 5 | #target_sources(app PRIVATE btblecontroller_port/btblecontroller_port_freertos_os.c) 6 | #target_sources(app PRIVATE btblecontroller_port/btblecontroller_port.c) 7 | #target_sources(app PRIVATE btblecontroller_port/btblecontroller_port_uart.c) 8 | 9 | sdk_add_static_library(lib/libbtblecontroller_${CHIP}_${PRIV_CONFIG_GEN_BLE}.a) -------------------------------------------------------------------------------- /btblecontroller/btble_inc/btble_lib_api.h: -------------------------------------------------------------------------------- 1 | #ifndef BLE_LIB_API_H_ 2 | #define BLE_LIB_API_H_ 3 | 4 | #include 5 | #include 6 | 7 | 8 | #if defined(CONFIG_BLE_MFG) 9 | #define MAX_SWITCHING_PATTERN_LEN (0x4B) 10 | struct hci_le_rx_test_v2_cmd 11 | { 12 | uint8_t rx_channel; 13 | uint8_t phy; 14 | uint8_t mod_idx; 15 | }; 16 | 17 | struct hci_le_tx_test_v4_cmd 18 | { 19 | uint8_t tx_channel; 20 | uint8_t test_data_len; 21 | uint8_t pkt_payl; 22 | uint8_t phy; 23 | uint8_t cte_len; 24 | uint8_t cte_type; 25 | uint8_t switching_pattern_len; 26 | uint8_t antenna_id[MAX_SWITCHING_PATTERN_LEN]; 27 | int8_t tx_pwr_lvl; 28 | }; 29 | #endif 30 | 31 | #if defined(CONFIG_BT_MFG) 32 | struct hci_vs_rx_test_cmd 33 | { 34 | uint8_t rx_channel; 35 | uint8_t pkt_type; 36 | }; 37 | struct hci_vs_tx_test_cmd 38 | { 39 | uint8_t tx_channel; 40 | uint16_t test_data_len; 41 | uint8_t pkt_payl; 42 | uint8_t pkt_type; 43 | int8_t tx_pwr_lvl; 44 | }; 45 | #endif 46 | 47 | void btble_controller_set_task_stack_size(uint16_t stack_size); 48 | void btble_controller_init(uint8_t task_priority); 49 | void ble_controller_deinit(void); 50 | #if defined(CFG_NUTTX) 51 | void btblecontroller_main(void *arg); 52 | uint8_t btblecontroller_change_scan_itl_win(uint16_t interval, uint16_t window); 53 | #endif 54 | 55 | extern int32_t btble_controller_sleep(int32_t max_sleep_cycles); 56 | extern void btble_controller_sleep_restore(); 57 | #if defined(CFG_BT_RESET) 58 | void ble_controller_reset(void); 59 | #endif 60 | 61 | // return sleep duration, in unit of 1/32768s 62 | // if 0, means not allow sleep 63 | // if -1, means allow sleep, but there is no end of sleep interrupt (ble core deep sleep is not enabled) 64 | bool ble_controller_sleep_is_ongoing(void); 65 | 66 | char *btble_controller_get_lib_ver(void); 67 | 68 | void btble_controller_remaining_mem(uint8_t** addr, int* size); 69 | void btble_controller_set_cs2(uint8_t enable); // cs2 is enabled by default 70 | #if defined(BL702L) 71 | void btble_controller_sleep_init(void); 72 | typedef int (*btble_before_sleep_cb_t)(void); 73 | typedef void (*btble_after_sleep_cb_t)(void); 74 | typedef void (*btble_sleep_aborted_cb_t)(void); 75 | int8_t btble_controller_get_tx_pwr(void); 76 | void btble_set_before_sleep_callback(btble_before_sleep_cb_t cb); 77 | void btble_set_after_sleep_callback(btble_after_sleep_cb_t cb); 78 | #if !defined(CONFIG_BLE_MFG) 79 | /* 80 | If ble sleep preparation is aborted before sleep, this callback will be trigerred. Please be noticed, 81 | this callback is triggerd after before_sleep_callback. 82 | e.g. Application disables something before sleep, application needs to enable these when sleep is aborted. 83 | */ 84 | void btble_set_sleep_aborted_callback(btble_sleep_aborted_cb_t cb); 85 | #endif 86 | #endif 87 | 88 | #if defined (CONFIG_BLE_MFG) || defined (CONFIG_BT_MFG) 89 | int bt_mfg_cli_register(void); 90 | int reset_cmd_handler(void); 91 | #if defined (CONFIG_BLE_MFG) 92 | int hci_le_tx_test_v2_cmd_handler(struct hci_le_tx_test_v2_cmd const *param, uint16_t opcode, bool from_hci); 93 | int hci_le_tx_test_v4_cmd_handler(struct hci_le_tx_test_v4_cmd const *param, uint16_t opcode,bool from_hci); 94 | int hci_le_rx_test_v2_cmd_handler(struct hci_le_rx_test_v2_cmd const *param, uint16_t opcode, bool from_hci); 95 | int hci_le_test_end_cmd_handler(void const *param, uint16_t opcode, bool from_hci); 96 | bool ble_check_test_ongoing(void); 97 | #endif 98 | #if defined (CONFIG_BT_MFG) 99 | int hci_vs_rx_test_cmd_handler(struct hci_vs_rx_test_cmd const *param, uint16_t opcode, bool from_hci); 100 | int hci_vs_tx_test_cmd_handler(struct hci_vs_tx_test_cmd const *param, uint16_t opcode, bool from_hci); 101 | int hci_vs_test_end_cmd_handler(void const *param, uint16_t opcode, bool from_hci); 102 | #endif 103 | #endif 104 | #endif 105 | -------------------------------------------------------------------------------- /btblecontroller/btble_inc/hci_onchip.h: -------------------------------------------------------------------------------- 1 | #ifndef HCI_ONCHIP_H_ 2 | #define HCI_ONCHIP_H_ 3 | 4 | enum{ 5 | BT_HCI_CMD, 6 | BT_HCI_ACL_DATA, 7 | BT_HCI_CMD_CMP_EVT, 8 | BT_HCI_CMD_STAT_EVT, 9 | BT_HCI_LE_EVT, 10 | BT_HCI_EVT, 11 | BT_HCI_SYNC_DATA, 12 | BT_HCI_DBG_EVT, 13 | }; 14 | 15 | typedef struct{ 16 | uint16_t opcode; 17 | uint8_t *params; 18 | uint8_t param_len; 19 | #if defined(CONFIG_BT_MFG_HCI_CMD) 20 | bool from_hci; 21 | #endif 22 | }bl_hci_cmd_struct; 23 | 24 | typedef struct { 25 | /// connection handle 26 | uint16_t conhdl; 27 | /// broadcast and packet boundary flag 28 | uint8_t pb_bc_flag; 29 | /// length of the data 30 | uint16_t len; 31 | uint8_t* buffer; 32 | }bl_hci_acl_data_tx; 33 | 34 | typedef struct{ 35 | union{ 36 | bl_hci_cmd_struct hci_cmd; 37 | bl_hci_acl_data_tx acl_data; 38 | }p; 39 | }hci_pkt_struct; 40 | 41 | typedef void (*bt_hci_recv_cb)(uint8_t pkt_type, uint16_t src_id, uint8_t *param, uint8_t param_len); 42 | 43 | uint8_t bt_onchiphci_interface_init(bt_hci_recv_cb cb); 44 | int8_t bt_onchiphci_send(uint8_t pkt_type, uint16_t dest_id, hci_pkt_struct *pkt); 45 | uint16_t bt_onchiphci_hanlde_rx_acl(void *param, uint8_t *host_buf_data); 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /btblecontroller/lib/libbtblecontroller_bl616_ble1m0s1sbredr1.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/O2C14/btstack_test/11fe08bd7ecaecf614d185115144f9f227a33f8a/btblecontroller/lib/libbtblecontroller_bl616_ble1m0s1sbredr1.a -------------------------------------------------------------------------------- /btblecontroller/lib/libbtblecontroller_bl808_ble1m0s1bredr0.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/O2C14/btstack_test/11fe08bd7ecaecf614d185115144f9f227a33f8a/btblecontroller/lib/libbtblecontroller_bl808_ble1m0s1bredr0.a -------------------------------------------------------------------------------- /btstack_config.h: -------------------------------------------------------------------------------- 1 | // 2 | // btstack_config.h for generic POSIX H4 port 3 | // 4 | // Documentation: https://bluekitchen-gmbh.com/btstack/#how_to/ 5 | // 6 | 7 | #ifndef BTSTACK_CONFIG_H 8 | #define BTSTACK_CONFIG_H 9 | 10 | // Port related features 11 | #define HAVE_ASSERT 12 | #define HAVE_BTSTACK_STDIN 13 | #define HAVE_EMBEDDED_TIME_MS 14 | #define HAVE_FREERTOS_TASK_NOTIFICATIONS 15 | #define HAVE_MALLOC 16 | 17 | 18 | // BTstack features that can be enabled 19 | #define ENABLE_ATT_DELAYED_RESPONSE 20 | #define ENABLE_AVRCP_COVER_ART 21 | #define ENABLE_BLE 22 | #define ENABLE_CLASSIC 23 | #define ENABLE_CROSS_TRANSPORT_KEY_DERIVATION 24 | #define ENABLE_GOEP_L2CAP 25 | #define ENABLE_HFP_WIDE_BAND_SPEECH 26 | #define ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 27 | #define ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE 28 | #define ENABLE_LE_CENTRAL 29 | #define ENABLE_LE_DATA_LENGTH_EXTENSION 30 | #define ENABLE_LE_PERIPHERAL 31 | #define ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 32 | #define ENABLE_LE_SECURE_CONNECTIONS 33 | #define ENABLE_LOG_ERROR 34 | #define ENABLE_LOG_INFO 35 | #define ENABLE_MICRO_ECC_FOR_LE_SECURE_CONNECTIONS 36 | #define ENABLE_PRINTF_HEXDUMP 37 | #define HCI_DUMP_STDOUT_MAX_SIZE_ACL 100 38 | #define ENABLE_SCO_OVER_HCI 39 | #define ENABLE_SDP_DES_DUMP 40 | #define ENABLE_SOFTWARE_AES128 41 | 42 | // BTstack configuration. buffers, sizes, ... 43 | #define HCI_ACL_PAYLOAD_SIZE (1691 + 4) 44 | #define HCI_INCOMING_PRE_BUFFER_SIZE 14 // sizeof benep heade, avoid memcpy 45 | 46 | #define NVM_NUM_DEVICE_DB_ENTRIES 16 47 | #define NVM_NUM_LINK_KEYS 16 48 | 49 | // Mesh Configuration 50 | #define ENABLE_MESH 51 | #define ENABLE_MESH_ADV_BEARER 52 | #define ENABLE_MESH_GATT_BEARER 53 | #define ENABLE_MESH_PB_ADV 54 | #define ENABLE_MESH_PB_GATT 55 | #define ENABLE_MESH_PROVISIONER 56 | #define ENABLE_MESH_PROXY_SERVER 57 | 58 | #define MAX_NR_MESH_SUBNETS 2 59 | #define MAX_NR_MESH_TRANSPORT_KEYS 16 60 | #define MAX_NR_MESH_VIRTUAL_ADDRESSES 16 61 | 62 | // allow for one NetKey update 63 | #define MAX_NR_MESH_NETWORK_KEYS (MAX_NR_MESH_SUBNETS+1) 64 | 65 | #endif 66 | 67 | -------------------------------------------------------------------------------- /btstack_port.c: -------------------------------------------------------------------------------- 1 | #include "bflb_mtimer.h" 2 | #define BTSTACK_FILE__ "btstack_port.c" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | /******************************************* 10 | * transport implementation 11 | ******************************************/ 12 | 13 | // #define USE_SRAM_FLASH_BANK_EMU 14 | 15 | #include "btstack.h" 16 | 17 | #include "btstack_config.h" 18 | #include "btstack_event.h" 19 | #include "btstack_memory.h" 20 | #include "btstack_run_loop.h" 21 | #include "btstack_run_loop_freertos.h" 22 | #include "btstack_tlv_flash_bank.h" 23 | 24 | #include "hci.h" 25 | #include "hci_dump_embedded_stdout.h" 26 | #include "hci_dump.h" 27 | #include "hal_time_ms.h" 28 | #include "btstack_debug.h" 29 | #include "btstack_stdin.h" 30 | 31 | #include "FreeRTOSConfig.h" 32 | #include "FreeRTOS.h" 33 | #include "task.h" 34 | #include "semphr.h" 35 | #include "queue.h" 36 | 37 | #include 38 | #include "hci_onchip.h" 39 | #include "bluetooth.h" 40 | #include 41 | #include "bl616_glb.h" 42 | #include "btble_lib_api.h" 43 | #include "easyflash.h" 44 | #include "btstack_tlv.h" 45 | #include "ble/le_device_db_tlv.h" 46 | #include "classic/btstack_link_key_db_tlv.h" 47 | static void (*transport_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); 48 | 49 | struct rx_msg_struct { 50 | uint8_t pkt_type; 51 | uint16_t src_id; 52 | uint8_t *param; 53 | uint8_t param_len; 54 | } __packed; 55 | static QueueHandle_t msg_queue; 56 | 57 | /** 58 | * CONFIG_BT_RX_BUF_COUNT: number of buffer for incoming ACL packages or HCI 59 | * events,range 2 to 255 60 | */ 61 | 62 | #define CONFIG_BT_RX_BUF_COUNT 4 63 | #define DATA_MSG_CNT 5 64 | 65 | #define CONFIG_ACL_RX_BUF_LEN 1024 66 | #define CONFIG_EVT_RX_BUF_LEN (255 + 2 + 3) 67 | 68 | #define CONFIG_BT_HCI_RESERVE 1 69 | #define CONFIG_BT_RX_BUF_RSV_COUNT (1) 70 | #if (CONFIG_BT_RX_BUF_RSV_COUNT >= CONFIG_BT_RX_BUF_COUNT) 71 | #error "CONFIG_BT_RX_BUF_RSV_COUNT config error" 72 | #endif 73 | 74 | #if defined(BFLB_BLE_NOTIFY_ADV_DISCARDED) 75 | extern void ble_controller_notify_adv_discarded(uint8_t *adv_bd_addr, uint8_t adv_type); 76 | #endif 77 | 78 | static __ALIGNED(4) uint8_t acl_rx_pool[CONFIG_BT_HCI_RESERVE + CONFIG_BT_RX_BUF_COUNT][CONFIG_ACL_RX_BUF_LEN]; 79 | static btstack_memory_pool_t acl_rx_pool_handle; 80 | static __ALIGNED(4) uint8_t evt_rx_pool[CONFIG_BT_HCI_RESERVE + CONFIG_BT_RX_BUF_COUNT][CONFIG_EVT_RX_BUF_LEN]; 81 | static btstack_memory_pool_t evt_rx_pool_handle; 82 | #define BT_HCI_EVT_CC_PARAM_OFFSET 0x05 83 | #define BT_HCI_CCEVT_HDR_PARLEN 0x03 84 | #define BT_HCI_CSEVT_LEN 0x06 85 | #define BT_HCI_CSVT_PARLEN 0x04 86 | #define BT_HCI_EVT_LE_PARAM_OFFSET 0x02 87 | 88 | #define BT_HCI_EVT_CMD_COMPLETE 0x0e 89 | #define BT_HCI_EVT_CMD_STATUS 0x0f 90 | 91 | #define BT_HCI_EVT_LE_META_EVENT 0x3e 92 | #define BT_HCI_EVT_LE_ADVERTISING_REPORT 0x02 93 | #define BT_HCI_EVT_NUM_COMPLETED_PACKETS 0x13 94 | 95 | static uint8_t hci_acl_can_send_now; 96 | static void transport_notify_packet_send(void) 97 | { 98 | // notify upper stack that it might be possible to send again 99 | uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0 }; 100 | transport_packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event)); 101 | } 102 | static void transport_notify_ready(void) 103 | { 104 | // notify upper stack that it transport is ready 105 | uint8_t event[] = { HCI_EVENT_TRANSPORT_READY, 0 }; 106 | transport_packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event)); 107 | } 108 | static void transport_send_hardware_error(uint8_t error_code) 109 | { 110 | uint8_t event[] = { HCI_EVENT_HARDWARE_ERROR, 1, error_code }; 111 | transport_packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event)); 112 | } 113 | 114 | static void bl_packet_to_host(uint8_t pkt_type, uint16_t src_id, uint8_t *param, uint8_t param_len, const uint8_t *buf) 115 | { 116 | uint16_t tlt_len; 117 | bool prio = true; 118 | uint8_t nb_h2c_cmd_pkts = 0x01; 119 | uint8_t *buf_data = buf; 120 | uint8_t to_hci_data_type = 0; 121 | //bt_buf_set_rx_adv(buf, false); 122 | 123 | switch (pkt_type) { 124 | case BT_HCI_CMD_CMP_EVT: { 125 | tlt_len = BT_HCI_EVT_CC_PARAM_OFFSET + param_len; 126 | *buf_data++ = BT_HCI_EVT_CMD_COMPLETE; 127 | *buf_data++ = BT_HCI_CCEVT_HDR_PARLEN + param_len; 128 | *buf_data++ = nb_h2c_cmd_pkts; 129 | *buf_data++ = ((uint8_t *)&src_id)[0]; 130 | *buf_data++ = ((uint8_t *)&src_id)[1]; 131 | 132 | memcpy(buf_data, param, param_len); 133 | to_hci_data_type = HCI_EVENT_PACKET; 134 | break; 135 | } 136 | case BT_HCI_CMD_STAT_EVT: { 137 | tlt_len = BT_HCI_CSEVT_LEN; 138 | *buf_data++ = BT_HCI_EVT_CMD_STATUS; 139 | *buf_data++ = BT_HCI_CSVT_PARLEN; 140 | *buf_data++ = *(uint8_t *)param; //STATUS 141 | *buf_data++ = nb_h2c_cmd_pkts; 142 | *buf_data++ = ((uint8_t *)&src_id)[0]; 143 | *buf_data++ = ((uint8_t *)&src_id)[1]; 144 | to_hci_data_type = HCI_EVENT_PACKET; 145 | 146 | break; 147 | } 148 | case BT_HCI_LE_EVT: { 149 | prio = false; 150 | if (param[0] == BT_HCI_EVT_LE_ADVERTISING_REPORT) { 151 | //bt_buf_set_rx_adv(buf, true); 152 | } 153 | tlt_len = BT_HCI_EVT_LE_PARAM_OFFSET + param_len; 154 | *buf_data++ = BT_HCI_EVT_LE_META_EVENT; 155 | *buf_data++ = param_len; 156 | memcpy(buf_data, param, param_len); 157 | to_hci_data_type = HCI_EVENT_PACKET; 158 | break; 159 | } 160 | case BT_HCI_EVT: { 161 | if (src_id != BT_HCI_EVT_NUM_COMPLETED_PACKETS) { 162 | prio = false; 163 | } 164 | tlt_len = BT_HCI_EVT_LE_PARAM_OFFSET + param_len; 165 | *buf_data++ = src_id; 166 | *buf_data++ = param_len; 167 | memcpy(buf_data, param, param_len); 168 | to_hci_data_type = HCI_EVENT_PACKET; 169 | break; 170 | } 171 | case BT_HCI_ACL_DATA: { 172 | prio = false; 173 | tlt_len = bt_onchiphci_hanlde_rx_acl(param, buf_data); 174 | if (tlt_len > CONFIG_ACL_RX_BUF_LEN) { 175 | printf("acl pkg is too big\r\n"); 176 | } 177 | to_hci_data_type = HCI_ACL_DATA_PACKET; 178 | break; 179 | } 180 | default: { 181 | return; 182 | } 183 | } 184 | transport_packet_handler(to_hci_data_type, buf, tlt_len); 185 | return; 186 | } 187 | 188 | static void bl_onchiphci_rx_packet_handler(uint8_t pkt_type, uint16_t src_id, uint8_t *param, uint8_t param_len) 189 | { 190 | struct net_buf *buf = NULL; 191 | struct rx_msg_struct rx_msg = { 192 | .pkt_type = pkt_type, 193 | .src_id = src_id, 194 | .param_len = param_len, 195 | }; 196 | if (param_len) { 197 | switch (pkt_type) { 198 | case BT_HCI_CMD_CMP_EVT: 199 | case BT_HCI_CMD_STAT_EVT: 200 | case BT_HCI_LE_EVT: 201 | case BT_HCI_EVT: { 202 | taskENTER_CRITICAL(); 203 | rx_msg.param = btstack_memory_pool_get(evt_rx_pool_handle); 204 | taskEXIT_CRITICAL(); 205 | break; 206 | } 207 | case BT_HCI_ACL_DATA: { 208 | taskENTER_CRITICAL(); 209 | rx_msg.param = btstack_memory_pool_get(acl_rx_pool_handle); 210 | taskEXIT_CRITICAL(); 211 | break; 212 | } 213 | default: { 214 | return; 215 | } 216 | } 217 | } 218 | memcpy(rx_msg.param, param, param_len); 219 | static BaseType_t yield = pdFALSE; 220 | xQueueSendFromISR(msg_queue, &rx_msg, &yield); 221 | btstack_run_loop_poll_data_sources_from_irq(); 222 | portYIELD_FROM_ISR(yield); 223 | } 224 | 225 | uint32_t hal_time_ms(void) 226 | { 227 | return (uint32_t)bflb_mtimer_get_time_ms(); 228 | } 229 | 230 | // data source for integration with BTstack Runloop 231 | static btstack_data_source_t transport_data_source; 232 | 233 | static void transport_deliver_hci_packets(void) 234 | { 235 | void *tmp_buf = NULL; 236 | struct rx_msg_struct msg; 237 | 238 | while (xQueueReceive(msg_queue, &msg, 0) == pdTRUE) { 239 | if (msg.param) { 240 | if (msg.pkt_type == BT_HCI_ACL_DATA) { 241 | taskENTER_CRITICAL(); 242 | tmp_buf = btstack_memory_pool_get(&acl_rx_pool_handle); 243 | taskEXIT_CRITICAL(); 244 | bl_packet_to_host(msg.pkt_type, msg.src_id, msg.param, msg.param_len, tmp_buf); 245 | taskENTER_CRITICAL(); 246 | btstack_memory_pool_free(&acl_rx_pool_handle, tmp_buf); 247 | btstack_memory_pool_free(&acl_rx_pool_handle, msg.param); 248 | taskEXIT_CRITICAL(); 249 | tmp_buf = NULL; 250 | } else { 251 | taskENTER_CRITICAL(); 252 | tmp_buf = btstack_memory_pool_get(&evt_rx_pool_handle); 253 | taskEXIT_CRITICAL(); 254 | bl_packet_to_host(msg.pkt_type, msg.src_id, msg.param, msg.param_len, tmp_buf); 255 | taskENTER_CRITICAL(); 256 | btstack_memory_pool_free(&evt_rx_pool_handle, tmp_buf); 257 | btstack_memory_pool_free(&evt_rx_pool_handle, msg.param); 258 | taskEXIT_CRITICAL(); 259 | } 260 | msg.param = NULL; 261 | } 262 | } 263 | } 264 | static void transport_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type) 265 | { 266 | switch (callback_type) { 267 | case DATA_SOURCE_CALLBACK_POLL: 268 | transport_notify_ready(); 269 | transport_deliver_hci_packets(); 270 | break; 271 | default: 272 | break; 273 | } 274 | } 275 | 276 | /** 277 | * init transport 278 | * @param transport_config 279 | */ 280 | static void transport_init(const void *transport_config) 281 | { 282 | log_info("transport_init"); 283 | btble_controller_init(configMAX_PRIORITIES - 1); 284 | 285 | btstack_memory_pool_create(&acl_rx_pool_handle, 286 | acl_rx_pool, 287 | CONFIG_BT_HCI_RESERVE + CONFIG_BT_RX_BUF_COUNT, 288 | CONFIG_ACL_RX_BUF_LEN); 289 | btstack_memory_pool_create(&evt_rx_pool_handle, 290 | evt_rx_pool, 291 | CONFIG_BT_HCI_RESERVE + CONFIG_BT_RX_BUF_COUNT, 292 | CONFIG_EVT_RX_BUF_LEN); 293 | msg_queue = xQueueCreate(DATA_MSG_CNT, sizeof(struct rx_msg_struct)); 294 | 295 | bt_onchiphci_interface_init(&bl_onchiphci_rx_packet_handler); 296 | 297 | 298 | hci_acl_can_send_now = 1; 299 | // set up polling data_source 300 | btstack_run_loop_set_data_source_handler(&transport_data_source, &transport_process); 301 | btstack_run_loop_enable_data_source_callbacks(&transport_data_source, DATA_SOURCE_CALLBACK_POLL); 302 | btstack_run_loop_add_data_source(&transport_data_source); 303 | } 304 | 305 | /** 306 | * open transport connection 307 | */ 308 | static int transport_open(void) 309 | { 310 | log_info("transport_open"); 311 | return 0; 312 | } 313 | 314 | /** 315 | * close transport connection 316 | */ 317 | static int transport_close(void) 318 | { 319 | log_info("transport_close"); 320 | 321 | 322 | struct rx_msg_struct msg; 323 | 324 | while (1) { 325 | if (xQueueReceive(msg_queue, &msg, 0)) { 326 | if (msg.param) { 327 | vPortFree(msg.param); 328 | } 329 | } else { 330 | break; 331 | } 332 | } 333 | vQueueDelete(msg_queue); 334 | msg_queue = NULL; 335 | 336 | 337 | return 0; 338 | } 339 | 340 | /** 341 | * register packet handler for HCI packets: ACL and Events 342 | */ 343 | static void transport_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)) 344 | { 345 | log_info("transport_register_packet_handler"); 346 | transport_packet_handler = handler; 347 | } 348 | 349 | /** 350 | * support async transport layers, e.g. IRQ driven without buffers 351 | */ 352 | static int transport_can_send_packet_now(uint8_t packet_type) 353 | { 354 | switch (packet_type) { 355 | case HCI_COMMAND_DATA_PACKET: 356 | return 1; 357 | 358 | case HCI_ACL_DATA_PACKET: 359 | return hci_acl_can_send_now; 360 | } 361 | return 1; 362 | } 363 | struct bt_hci_cmd_hdr { 364 | uint16_t opcode; 365 | uint8_t param_len; 366 | } __packed; 367 | struct bt_hci_acl_hdr { 368 | uint16_t handle; 369 | uint16_t len; 370 | } __packed; 371 | #define bt_acl_handle(h) ((h) & 0x0fff) 372 | #define bt_acl_flags(h) ((h) >> 12) 373 | 374 | static int transport_send_packet(uint8_t packet_type, uint8_t *packet, int size) 375 | { 376 | uint8_t pkt_type; 377 | uint16_t dest_id = 0x00; 378 | hci_pkt_struct pkt; 379 | if (size > CONFIG_ACL_RX_BUF_LEN) { 380 | printf("acl pkg is too big\r\n"); 381 | } 382 | switch (packet_type) { 383 | case HCI_COMMAND_DATA_PACKET: 384 | pkt_type = BT_HCI_CMD; 385 | if (size < sizeof(struct bt_hci_cmd_hdr)) { 386 | break; 387 | } 388 | struct bt_hci_cmd_hdr *chdr = packet; 389 | if (size < chdr->param_len) { 390 | break; 391 | } 392 | switch (chdr->opcode) { 393 | //ble refer to hci_cmd_desc_tab_le, for the ones of which dest_ll is BLE_CTRL 394 | case HCI_OPCODE_HCI_LE_CONNECTION_UPDATE: 395 | case HCI_OPCODE_HCI_LE_READ_CHANNEL_MAP: 396 | case HCI_OPCODE_HCI_LE_READ_REMOTE_USED_FEATURES: 397 | case HCI_OPCODE_HCI_LE_START_ENCRYPTION: 398 | case HCI_OPCODE_HCI_LE_LONG_TERM_KEY_REQUEST_REPLY: 399 | case HCI_OPCODE_HCI_LE_LONG_TERM_KEY_NEGATIVE_REPLY: 400 | case HCI_OPCODE_HCI_LE_REMOTE_CONNECTION_PARAMETER_REQUEST_REPLY: 401 | case HCI_OPCODE_HCI_LE_REMOTE_CONNECTION_PARAMETER_REQUEST_NEGATIVE_REPLY: 402 | case HCI_OPCODE_HCI_LE_SET_DATA_LENGTH: 403 | case HCI_OPCODE_HCI_LE_READ_PHY: 404 | case HCI_OPCODE_HCI_LE_SET_PHY: 405 | //bredr identify link id, according to dest_id 406 | case HCI_OPCODE_HCI_READ_REMOTE_SUPPORTED_FEATURES_COMMAND: 407 | case HCI_OPCODE_HCI_READ_REMOTE_EXTENDED_FEATURES_COMMAND: 408 | case HCI_OPCODE_HCI_READ_ENCRYPTION_KEY_SIZE: { 409 | //dest_id is connectin handle 410 | dest_id = *(uint8_t *)(packet + sizeof(struct bt_hci_cmd_hdr)); 411 | } 412 | default: 413 | break; 414 | } 415 | 416 | pkt.p.hci_cmd.opcode = chdr->opcode; 417 | pkt.p.hci_cmd.param_len = chdr->param_len; 418 | pkt.p.hci_cmd.params = packet + sizeof(struct bt_hci_cmd_hdr); 419 | break; 420 | case HCI_ACL_DATA_PACKET: 421 | pkt_type = BT_HCI_ACL_DATA; 422 | 423 | if (size < sizeof(struct bt_hci_acl_hdr)) { 424 | break; 425 | } 426 | struct bt_hci_acl_hdr *acl = packet; 427 | //connhandle +l2cap field 428 | uint16_t connhdl_l2cf, tlt_len; 429 | tlt_len = acl->len; 430 | connhdl_l2cf = acl->handle; 431 | if (size - sizeof(struct bt_hci_acl_hdr) < tlt_len) { 432 | break; 433 | } 434 | //get connection_handle 435 | dest_id = bt_acl_handle(connhdl_l2cf); 436 | pkt.p.acl_data.conhdl = dest_id; 437 | pkt.p.acl_data.pb_bc_flag = bt_acl_flags(connhdl_l2cf); 438 | pkt.p.acl_data.len = tlt_len; 439 | pkt.p.acl_data.buffer = packet + sizeof(struct bt_hci_acl_hdr); 440 | hci_acl_can_send_now = 0; 441 | break; 442 | default: 443 | transport_send_hardware_error(0x01); // invalid HCI packet 444 | return 0; 445 | } 446 | bt_onchiphci_send(pkt_type, dest_id, &pkt); 447 | btstack_run_loop_poll_data_sources_from_irq(); 448 | hci_acl_can_send_now = 1; 449 | transport_notify_packet_send(); 450 | return 0; 451 | } 452 | 453 | static const hci_transport_t transport = { 454 | "BouffaloBT", 455 | &transport_init, 456 | &transport_open, 457 | &transport_close, 458 | &transport_register_packet_handler, 459 | &transport_can_send_packet_now, 460 | &transport_send_packet, 461 | NULL, // set baud rate 462 | NULL, // reset link 463 | NULL, // set SCO config 464 | }; 465 | 466 | static const hci_transport_t *transport_get_instance(void) 467 | { 468 | return &transport; 469 | } 470 | 471 | static btstack_packet_callback_registration_t hci_event_callback_registration; 472 | 473 | static void local_version_information_handler(uint8_t *packet) 474 | { 475 | printf("Local version information:\n"); 476 | uint16_t hci_version = packet[6]; 477 | uint16_t hci_revision = little_endian_read_16(packet, 7); 478 | uint16_t lmp_version = packet[9]; 479 | uint16_t manufacturer = little_endian_read_16(packet, 10); 480 | uint16_t lmp_subversion = little_endian_read_16(packet, 12); 481 | printf("- HCI Version %#04x\n", hci_version); 482 | printf("- HCI Revision %#04x\n", hci_revision); 483 | printf("- LMP Version %#04x\n", lmp_version); 484 | printf("- LMP Subversion %#04x\n", lmp_subversion); 485 | printf("- Manufacturer %#04x\n", manufacturer); 486 | } 487 | static const btstack_tlv_t btstack_tlv_impl; 488 | static bd_addr_t local_addr = { 0 }; 489 | static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) 490 | { 491 | const uint8_t *params; 492 | if (packet_type != HCI_EVENT_PACKET) 493 | return; 494 | switch (hci_event_packet_get_type(packet)) { 495 | case BTSTACK_EVENT_STATE: 496 | switch (btstack_event_state_get_state(packet)) { 497 | case HCI_STATE_WORKING: 498 | printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); 499 | // setup global tlv 500 | btstack_tlv_set_instance(&btstack_tlv_impl, NULL); 501 | 502 | hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(&btstack_tlv_impl, NULL)); 503 | // setup LE Device DB using TLV 504 | le_device_db_tlv_configure(&btstack_tlv_impl, NULL); 505 | break; 506 | case HCI_STATE_OFF: 507 | printf("Good bye, see you.\n"); 508 | break; 509 | default: 510 | break; 511 | } 512 | break; 513 | case HCI_EVENT_COMMAND_COMPLETE: 514 | switch (hci_event_command_complete_get_command_opcode(packet)) { 515 | case HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION: 516 | local_version_information_handler(packet); 517 | break; 518 | case HCI_OPCODE_HCI_READ_BD_ADDR: 519 | params = hci_event_command_complete_get_return_parameters(packet); 520 | if (params[0] != 0) 521 | break; 522 | if (size < 12) 523 | break; 524 | reverse_48(¶ms[1], local_addr); 525 | break; 526 | default: 527 | break; 528 | } 529 | break; 530 | default: 531 | break; 532 | } 533 | } 534 | 535 | static void (*btstack_stdin_handler)(char c) = NULL; 536 | 537 | void btstack_stdin_setup(void (*stdin_handler)(char c)) 538 | { 539 | if (!btstack_stdin_handler) { 540 | btstack_stdin_handler = stdin_handler; 541 | } 542 | } 543 | void btstack_stdin_reset(void) 544 | { 545 | 546 | } 547 | static void settings_erase(); 548 | void btstack_cmd(int args, char **argv) 549 | { 550 | if (args < 2) { 551 | return; 552 | } 553 | 554 | if (strlen(argv[1]) > 1) { 555 | if (strcmp(argv[1], "erase") == 0) { 556 | settings_erase(); 557 | return; 558 | } 559 | } 560 | 561 | if (!btstack_stdin_handler) { 562 | return; 563 | } 564 | 565 | btstack_stdin_handler(argv[1][0]); 566 | } 567 | SHELL_CMD_EXPORT_ALIAS(btstack_cmd, btstack, btstack); 568 | 569 | bool ef_ready_flag = false; 570 | static int bt_check_if_ef_ready() 571 | { 572 | int err = 0; 573 | 574 | if (!ef_ready_flag) { 575 | err = easyflash_init(); 576 | if (!err) { 577 | ef_ready_flag = true; 578 | } else { 579 | printf("easyflash init fail :(\n"); 580 | } 581 | } 582 | 583 | return err; 584 | } 585 | static int bt_settings_set_bin(void *context, uint32_t tag, const uint8_t *data, uint32_t data_size) 586 | { 587 | int err; 588 | 589 | err = bt_check_if_ef_ready(); 590 | if (err) 591 | return err; 592 | char key[9] = { 0 }; 593 | sprintf(key, "%x", tag); 594 | key[8] = 0; 595 | err = ef_set_env_blob(key, data, data_size); 596 | 597 | return err; 598 | } 599 | 600 | static int bt_settings_get_bin(void *context, uint32_t tag, uint8_t *buffer, uint32_t buffer_size) 601 | { 602 | int err; 603 | size_t rlen; 604 | 605 | err = bt_check_if_ef_ready(); 606 | if (err) 607 | return err; 608 | char key[9] = { 0 }; 609 | sprintf(key, "%x", tag); 610 | key[8] = 0; 611 | rlen = ef_get_env_blob(key, buffer, buffer_size, NULL); 612 | 613 | return rlen; 614 | } 615 | 616 | static void settings_delete(void *context, uint32_t tag) 617 | { 618 | char key[9] = { 0 }; 619 | sprintf(key, "%x", tag); 620 | key[8] = 0; 621 | ef_del_env(key); 622 | return; 623 | } 624 | static void settings_erase() 625 | { 626 | //like bflb_mtd_erase_all 627 | if (ef_port_erase(0, 32768) == 0) { 628 | printf("erase success\n"); 629 | } 630 | } 631 | static const btstack_tlv_t btstack_tlv_impl = { 632 | .get_tag = &bt_settings_get_bin, 633 | .store_tag = &bt_settings_set_bin, 634 | .delete_tag = &settings_delete, 635 | }; 636 | extern int btstack_main(int argc, const char *argv[]); 637 | void port_thread(void *args) 638 | { 639 | bt_check_if_ef_ready(); 640 | 641 | hci_dump_init(hci_dump_embedded_stdout_get_instance()); 642 | 643 | /// GET STARTED with BTstack /// 644 | btstack_memory_init(); 645 | btstack_run_loop_init(btstack_run_loop_freertos_get_instance()); 646 | 647 | // init HCI 648 | hci_init(transport_get_instance(), NULL); 649 | 650 | // inform about BTstack state 651 | hci_event_callback_registration.callback = &packet_handler; 652 | hci_add_event_handler(&hci_event_callback_registration); 653 | 654 | btstack_main(0, NULL); 655 | 656 | //gap_set_security_level(LEVEL_2); 657 | 658 | log_info("btstack executing run loop..."); 659 | btstack_run_loop_execute(); 660 | } 661 | -------------------------------------------------------------------------------- /flash_prog_cfg.ini: -------------------------------------------------------------------------------- 1 | [cfg] 2 | # 0: no erase, 1:programmed section erase, 2: chip erase 3 | erase = 1 4 | # skip mode set first para is skip addr, second para is skip len, multi-segment region with ; separated 5 | skip_mode = 0x0, 0x0 6 | # 0: not use isp mode, #1: isp mode 7 | boot2_isp_mode = 0 8 | 9 | [boot2] 10 | filedir = ./build/build_out/boot2_*.bin 11 | address = 0x000000 12 | 13 | [partition] 14 | filedir = ./build/build_out/partition*.bin 15 | address = 0xE000 16 | 17 | [FW] 18 | filedir = ./build/build_out/btstack_test_$(CHIPNAME).bin 19 | address = @partition 20 | 21 | -------------------------------------------------------------------------------- /libqcc743_on_bl616.c: -------------------------------------------------------------------------------- 1 | #include "bflb_efuse.h" 2 | #include "bflb_irq.h" 3 | void qcc74x_irq_clear_pending(int irq){ 4 | bflb_irq_clear_pending(irq); 5 | } 6 | int qcc74x_irq_attach(int irq, irq_callback isr, void *arg){ 7 | return bflb_irq_attach(irq, isr, arg); 8 | } 9 | void qcc74x_irq_enable(int irq){ 10 | bflb_irq_enable(irq); 11 | } 12 | void qcc74x_irq_disable(int irq){ 13 | bflb_irq_disable(irq); 14 | } 15 | void qcc74x_efuse_get_device_info(bflb_efuse_device_info_type *device_info){ 16 | bflb_efuse_get_device_info(device_info); 17 | } 18 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | #include "bflb_mtimer.h" 2 | #include "board.h" 3 | #include "bflb_gpio.h" 4 | #define DBG_TAG "MAIN" 5 | #include "log.h" 6 | #include "FreeRTOSConfig.h" 7 | #include "FreeRTOS.h" 8 | #include "rfparam_adapter.h" 9 | #include "task.h" 10 | #include "bl616_glb.h" 11 | #include "bflb_mtd.h" 12 | 13 | 14 | 15 | static int btblecontroller_em_config(void) 16 | { 17 | extern uint8_t __LD_CONFIG_EM_SEL; 18 | volatile uint32_t em_size; 19 | 20 | em_size = (uint32_t)&__LD_CONFIG_EM_SEL; 21 | 22 | if (em_size == 0) { 23 | GLB_Set_EM_Sel(GLB_WRAM160KB_EM0KB); 24 | } else if (em_size == 32 * 1024) { 25 | GLB_Set_EM_Sel(GLB_WRAM128KB_EM32KB); 26 | } else if (em_size == 64 * 1024) { 27 | GLB_Set_EM_Sel(GLB_WRAM96KB_EM64KB); 28 | } else { 29 | GLB_Set_EM_Sel(GLB_WRAM96KB_EM64KB); 30 | } 31 | 32 | return 0; 33 | } 34 | 35 | TaskHandle_t hbtstack_task; 36 | /** 37 | * @brief The application entry point. 38 | * @retval int 39 | */ 40 | static struct bflb_device_s *gpio = NULL; 41 | static struct bflb_device_s *uart0; 42 | #define PIN_JTAG_TDO GPIO_PIN_14 43 | #define PIN_JTAG_TDI GPIO_PIN_19 44 | 45 | 46 | void port_thread(void *args); 47 | int main(void) 48 | { 49 | /* Reset of all peripherals, initializes the Systick. */ 50 | 51 | board_init(); 52 | gpio = bflb_device_get_by_name("gpio"); 53 | uart0 = bflb_device_get_by_name("uart0"); 54 | shell_init_with_task(uart0); 55 | 56 | /* 57 | 58 | bflb_gpio_init(gpio, PIN_JTAG_TDO, 59 | GPIO_FUNC_JTAG | GPIO_ALTERNATE | GPIO_FLOAT | GPIO_SMT_EN | 60 | GPIO_DRV_1); 61 | bflb_gpio_init(gpio, PIN_JTAG_TDI, 62 | GPIO_FUNC_JTAG | GPIO_ALTERNATE | GPIO_FLOAT | GPIO_SMT_EN | 63 | GPIO_DRV_1); 64 | */ 65 | 66 | /* Set ble controller EM Size */ 67 | btblecontroller_em_config(); 68 | 69 | /* Init rf */ 70 | if (0 != rfparam_init(0, NULL, 0)) { 71 | printf("PHY RF init failed!\r\n"); 72 | return 0; 73 | } 74 | 75 | /* For bt status save */ 76 | bflb_mtd_init(); 77 | easyflash_init(); 78 | 79 | 80 | //xTaskCreate(port_thread, "btstack_thread", 2048, NULL, configMAX_PRIORITIES - 4, &hbtstack_task); 81 | xTaskCreate(port_thread, "btstack_thread", 2048, NULL, 1, &hbtstack_task); 82 | 83 | vTaskStartScheduler(); 84 | //port_thread(NULL); 85 | /* We should never get here as control is now taken by the scheduler */ 86 | while (1); 87 | } -------------------------------------------------------------------------------- /proj.conf: -------------------------------------------------------------------------------- 1 | # Components 2 | set(CONFIG_VLIBC 0) 3 | set(CONFIG_BFLOG 0) 4 | set(CONFIG_FREERTOS 1) 5 | set(CONFIG_POSIX 1) 6 | 7 | set(CONFIG_TLSF 1) 8 | set(CONFIG_SHELL 1) 9 | set(CONFIG_RF 1) 10 | #set(CONFIG_LINK_IN_PSRAM 1) 11 | set(CONFIG_PSRAM 1) 12 | 13 | 14 | set(CONFIG_VSNPRINTF_FLOAT 1) 15 | set(CONFIG_VSNPRINTF_FLOAT_EX 1) 16 | set(CONFIG_VSNPRINTF_LONG_LONG 1) 17 | 18 | # Config 19 | ## mbedtls 20 | set(CONFIG_MBEDTLS_AES_USE_HW 0) 21 | set(CONFIG_MBEDTLS_BIGNUM_USE_HW 0) 22 | set(CONFIG_MBEDTLS_ECC_USE_HW 0) 23 | set(CONFIG_MBEDTLS_SHA1_USE_HW 0) 24 | set(CONFIG_MBEDTLS_SHA256_USE_HW 0) 25 | set(CONFIG_MBEDTLS_SHA512_USE_HW 0) 26 | 27 | # other 28 | set(CONFIG_COREDUMP 0) 29 | set(CONFIG_CODEC_USE_I2S_TX 1) 30 | set(CONFIG_CODEC_USE_I2S_RX 0) 31 | set(CONFIG_MSP_USE_STATIC_RAM 0) 32 | 33 | set(CONFIG_BFLB_MTD 1) # For bt status save 34 | set(CONFIG_PARTITION 1) # For bt status save 35 | set(CONFIG_EASYFLASH4 1) # For bt status save --------------------------------------------------------------------------------