From 1b0d15f4d9822406db423d941ebb4b8683a52fec Mon Sep 17 00:00:00 2001 From: Peter Harper Date: Fri, 12 Jun 2026 13:00:32 +0100 Subject: [PATCH] Add freetos version of ble_pointer --- bluetooth/ble_pointer/CMakeLists.txt | 33 ++++++++++ bluetooth/ble_pointer/ble_pointer.c | 4 +- .../ble_pointer/ble_pointer_freertos_main.c | 66 +++++++++++++++++++ .../ble_pointer/ble_pointer_nosys_main.c | 9 +++ 4 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 bluetooth/ble_pointer/ble_pointer_freertos_main.c create mode 100644 bluetooth/ble_pointer/ble_pointer_nosys_main.c diff --git a/bluetooth/ble_pointer/CMakeLists.txt b/bluetooth/ble_pointer/CMakeLists.txt index d36d13c0c..874c91b56 100644 --- a/bluetooth/ble_pointer/CMakeLists.txt +++ b/bluetooth/ble_pointer/CMakeLists.txt @@ -1,5 +1,6 @@ add_executable(ble_pointer ble_pointer.c + ble_pointer_nosys_main.c mpu6050_i2c_lib.c ) target_link_libraries(ble_pointer @@ -20,3 +21,35 @@ target_compile_definitions(ble_pointer PRIVATE pico_btstack_make_gatt_header(ble_pointer PRIVATE "${CMAKE_CURRENT_LIST_DIR}/ble_pointer.gatt") pico_add_extra_outputs(ble_pointer) +if (NOT FREERTOS_KERNEL_PATH AND NOT DEFINED ENV{FREERTOS_KERNEL_PATH}) + return() +endif() +include(../../freertos/FreeRTOS_Kernel_import.cmake) + +add_executable(ble_pointer_freertos + ble_pointer.c + ble_pointer_freertos_main.c + mpu6050_i2c_lib.c +) +target_link_libraries(ble_pointer_freertos + pico_stdlib + pico_btstack_ble + pico_btstack_cyw43 + pico_cyw43_arch_sys_freertos + hardware_i2c + FreeRTOS-Kernel-Heap4 +) +target_include_directories(ble_pointer_freertos PRIVATE + ${CMAKE_CURRENT_LIST_DIR} + ${CMAKE_CURRENT_LIST_DIR}/.. + ${CMAKE_CURRENT_LIST_DIR}/../config # for common btstack config + ${CMAKE_CURRENT_LIST_DIR}/../../freertos # for FreeRTOSConfig.h +) +target_compile_definitions(ble_pointer_freertos PRIVATE + #WANT_HCI_DUMP=1 + CYW43_LWIP=0 + configNUMBER_OF_CORES=1 # run freertos on one core, the other core is doing something else +) +pico_btstack_make_gatt_header(ble_pointer_freertos PRIVATE "${CMAKE_CURRENT_LIST_DIR}/ble_pointer.gatt") +pico_add_extra_outputs(ble_pointer_freertos) + diff --git a/bluetooth/ble_pointer/ble_pointer.c b/bluetooth/ble_pointer/ble_pointer.c index 775f02f51..12496bd0f 100644 --- a/bluetooth/ble_pointer/ble_pointer.c +++ b/bluetooth/ble_pointer/ble_pointer.c @@ -413,8 +413,7 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack } } -int main(void) { - stdio_init_all(); +void run_ble_pointer(void) { // Make the I2C pins available to picotool bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C)); @@ -470,5 +469,4 @@ int main(void) { sleep_ms(1000); } #endif - return 0; } diff --git a/bluetooth/ble_pointer/ble_pointer_freertos_main.c b/bluetooth/ble_pointer/ble_pointer_freertos_main.c new file mode 100644 index 000000000..2c9043449 --- /dev/null +++ b/bluetooth/ble_pointer/ble_pointer_freertos_main.c @@ -0,0 +1,66 @@ +#include + +#include "pico/stdio.h" +#include "pico/multicore.h" +#include "pico/flash.h" + +#include "FreeRTOS.h" +#include "task.h" + +#define START_CORE_1 0 + +extern void run_ble_pointer(void); + +#define MAIN_TASK_PRIORITY ( tskIDLE_PRIORITY + 2UL ) +#define MAIN_TASK_STACK_SIZE configMINIMAL_STACK_SIZE + +static void main_task_core0(__unused void *params) { + run_ble_pointer(); +} + +static void vLaunch0( void) { +#if 1 + // todo: is this right? + flash_safe_execute_core_init(); +#endif + + TaskHandle_t task; + static_assert(configSUPPORT_DYNAMIC_ALLOCATION, ""); + xTaskCreate(main_task_core0, "MainThread_core0", MAIN_TASK_STACK_SIZE, NULL, MAIN_TASK_PRIORITY, &task); +#if configNUMBER_OF_CORES > 1 + vTaskCoreAffinitySet(task, 1<<0); +#endif + + /* Start the tasks and timer running. */ + vTaskStartScheduler(); +} + +#if START_CORE_1 +static void main_task_core1(__unused void *params) { + printf("Hello from core1"); + while(true) { + sleep_ms(100); + } +} + +void vLaunch1( void) { + + TaskHandle_t task; + static_assert(configSUPPORT_DYNAMIC_ALLOCATION, ""); + xTaskCreate(main_task_core1, "MainThread_core1", MAIN_TASK_STACK_SIZE, NULL, MAIN_TASK_PRIORITY, &task); + vTaskCoreAffinitySet(task, 1<<1); + + /* Start the tasks and timer running. */ + vTaskStartScheduler(); +} +#endif + +int main(void) { + stdio_init_all(); + vLaunch0(); +#if START_CORE_1 + multicore_launch_core1(vLaunch1); +#endif + return 0; +} + diff --git a/bluetooth/ble_pointer/ble_pointer_nosys_main.c b/bluetooth/ble_pointer/ble_pointer_nosys_main.c new file mode 100644 index 000000000..d31711687 --- /dev/null +++ b/bluetooth/ble_pointer/ble_pointer_nosys_main.c @@ -0,0 +1,9 @@ +#include "pico/stdio.h" + +extern void run_ble_pointer(void); + +int main(void) { + stdio_init_all(); + run_ble_pointer(); + return 0; +}