Added global lock for BLE stack.

Signed-off-by: Yilin Sun <imi415@imi.moe>
This commit is contained in:
Yilin Sun 2023-10-28 13:55:02 +08:00
parent 35170da6ab
commit 31447e4249
Signed by: imi415
GPG Key ID: 17F01E106F9F5E0A
4 changed files with 53 additions and 15 deletions

View File

@ -19,8 +19,8 @@ extern uint32_t SystemCoreClock;
#define configIDLE_SHOULD_YIELD 1 #define configIDLE_SHOULD_YIELD 1
#define configUSE_TASK_NOTIFICATIONS 1 #define configUSE_TASK_NOTIFICATIONS 1
#define configTASK_NOTIFICATION_ARRAY_ENTRIES 3 #define configTASK_NOTIFICATION_ARRAY_ENTRIES 3
#define configUSE_MUTEXES 0 #define configUSE_MUTEXES 1
#define configUSE_RECURSIVE_MUTEXES 0 #define configUSE_RECURSIVE_MUTEXES 1
#define configUSE_COUNTING_SEMAPHORES 0 #define configUSE_COUNTING_SEMAPHORES 0
#define configUSE_ALTERNATIVE_API 0 /* Deprecated! */ #define configUSE_ALTERNATIVE_API 0 /* Deprecated! */
#define configQUEUE_REGISTRY_SIZE 10 #define configQUEUE_REGISTRY_SIZE 10

10
include/app_ble_support.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef APP_BLE_SUPPORT_H
#define APP_BLE_SUPPORT_H
#include <stdint.h>
int app_ble_init(void);
int app_ble_lock(uint32_t block_ms);
int app_ble_unlock(void);
#endif // APP_BLE_SUPPORT_H

View File

@ -15,7 +15,8 @@
/* Board */ /* Board */
#include "board.h" #include "board.h"
static TaskHandle_t s_ble_task_handle; static TaskHandle_t s_ble_task_handle;
static SemaphoreHandle_t s_ble_lock_handle;
static void app_ble_task(void *parameters); static void app_ble_task(void *parameters);
static void app_ble_msg_handler(struct ble_msg_t const *p_ble_msg); static void app_ble_msg_handler(struct ble_msg_t const *p_ble_msg);
@ -25,7 +26,7 @@ static void app_ble_adv_init(void);
static void app_ble_sec_init(void); static void app_ble_sec_init(void);
static void app_ble_prf_init(void); static void app_ble_prf_init(void);
void app_ble_init(void) { int app_ble_init(void) {
struct ns_stack_cfg_t app_handler = {0}; struct ns_stack_cfg_t app_handler = {0};
app_handler.ble_msg_handler = app_ble_msg_handler; app_handler.ble_msg_handler = app_ble_msg_handler;
app_handler.user_msg_handler = app_user_msg_handler; app_handler.user_msg_handler = app_user_msg_handler;
@ -41,14 +42,33 @@ void app_ble_init(void) {
// start advertising // start advertising
ns_ble_adv_start(); ns_ble_adv_start();
s_ble_lock_handle = xSemaphoreCreateMutex();
if (s_ble_lock_handle == NULL) {
return -1;
}
if (xTaskCreate(app_ble_task, "BLE", 1536, NULL, 5, &s_ble_task_handle) != pdPASS) { if (xTaskCreate(app_ble_task, "BLE", 1536, NULL, 5, &s_ble_task_handle) != pdPASS) {
for (;;) { return -2;
/* -- */
}
} }
NVIC_SetPriority(RESERVED_IRQn, 2); NVIC_SetPriority(RESERVED_IRQn, 2);
NVIC_EnableIRQ(RESERVED_IRQn); NVIC_EnableIRQ(RESERVED_IRQn);
return 0;
}
int app_ble_lock(uint32_t block_ms) {
if (xSemaphoreTake(s_ble_lock_handle, pdMS_TO_TICKS(block_ms)) != pdPASS) {
return -1;
}
return 0;
}
int app_ble_unlock(void) {
xSemaphoreGive(s_ble_lock_handle);
return 0;
} }
void RESERVED_IRQHandler(void) { void RESERVED_IRQHandler(void) {

View File

@ -11,7 +11,8 @@
/* Profiles */ /* Profiles */
#include "app_ble_profiles/app_ble_bass.h" #include "app_ble_profiles/app_ble_bass.h"
void app_ble_init(void); /* App */
#include "app_ble_support.h"
static void task_initialize(void *parameters); static void task_initialize(void *parameters);
@ -19,7 +20,9 @@ int main(void) {
board_led_init(0); board_led_init(0);
board_led_init(1); board_led_init(1);
app_ble_init(); if (app_ble_init() < 0) {
goto dead_loop;
}
if (xTaskCreate(task_initialize, "INIT", 256, NULL, 4, NULL) != pdTRUE) { if (xTaskCreate(task_initialize, "INIT", 256, NULL, 4, NULL) != pdTRUE) {
goto dead_loop; goto dead_loop;
@ -37,13 +40,18 @@ dead_loop:
static void task_initialize(void *parameters) { static void task_initialize(void *parameters) {
uint8_t battery_level = 0U; uint8_t battery_level = 0U;
for (;;) { for (;;) {
board_led_toggle(0); if (app_ble_lock(100) >= 0) {
app_ble_bass_update_level(battery_level); board_led_set(0, true);
app_ble_bass_update_level(battery_level);
battery_level++;
if (battery_level > 100) {
battery_level = 0;
}
battery_level++; board_led_set(0, false);
if (battery_level > 100) { app_ble_unlock();
battery_level = 0;
} }
vTaskDelay(pdMS_TO_TICKS(500));
vTaskDelay(pdMS_TO_TICKS(5000));
} }
} }