BLE Task needs more stack!

This commit is contained in:
Joe Kearney 2025-08-14 20:21:39 -05:00
parent 3ce181656f
commit 8b655d8be7
6 changed files with 344 additions and 12 deletions

View file

@ -22,11 +22,17 @@ TaskHandle_t COMM_BLE_Task_Handle;
/* Private Variables */
static const char *TAG = "BLE";
static const TickType_t BLE_Task_Delay = BLE_TASK_PERIOD_IN_ms / portTICK_PERIOD_MS;
static COMM_BLE_StateID_T Current_State = COMM_BLE_DEFAULT;
static COMM_BLE_StateID_T Next_State = COMM_BLE_INITIALIZING;
static bool State_Changed = false;
static TickType_t Time_At_State_Entry_In_Ticks;
static bool Is_Quiet = false;
static bool Was_Advertising = false;
static TimerHandle_t BLEUnquietTimer = NULL;
static StaticTimer_t xBLEUnquietTimerBuffer;
//! Immediate Alert Service alert level value.
volatile uint8_t COMM_BLE_IASAlertLevel = 0;
@ -288,6 +294,69 @@ void COMM_BLE_Task(void * pvParameters)
#endif // TRACE_BLE
}
}
switch (command.ID)
{
case COMM_BLE_STOP_SCANNING:
{
cy_en_ble_api_result_t apiResult = Cy_BLE_GAPC_StopScan();
if (apiResult != CY_BLE_SUCCESS)
{
#ifdef TRACE_BLE
COMM_Console_Print_String("[BLE] Cy_BLE_GAPC_StopScan API Error: 0x");
COMM_Console_Print_UInt32AsHex(apiResult);
COMM_Console_Print_String("\n");
#endif // TRACE_BLE
}
apiResult = Cy_BLE_GAPP_StopAdvertisement();
if (apiResult != CY_BLE_SUCCESS)
{
#ifdef TRACE_BLE
COMM_Console_Print_String("[BLE] Cy_BLE_GAPP_StopAdvertisement API Error: 0x");
COMM_Console_Print_UInt32AsHex(apiResult);
COMM_Console_Print_String("\n");
#endif // TRACE_BLE
}
COMM_BLE_RequestState(COMM_BLE_ADVERTISING_AS_BROADCASTER);
}
break;
case COMM_BLE_STOP_ADVERTISING:
{
cy_en_ble_api_result_t apiResult = Cy_BLE_GAPC_StopScan();
if (apiResult != CY_BLE_SUCCESS)
{
#ifdef TRACE_BLE
COMM_Console_Print_String("[BLE] Cy_BLE_GAPC_StopScan API Error: 0x");
COMM_Console_Print_UInt32AsHex(apiResult);
COMM_Console_Print_String("\n");
#endif // TRACE_BLE
}
apiResult = Cy_BLE_GAPP_StopAdvertisement();
if (apiResult != CY_BLE_SUCCESS)
{
#ifdef TRACE_BLE
COMM_Console_Print_String("[BLE] Cy_BLE_GAPP_StopAdvertisement API Error: 0x");
COMM_Console_Print_UInt32AsHex(apiResult);
COMM_Console_Print_String("\n");
#endif // TRACE_BLE
}
COMM_BLE_RequestState(COMM_BLE_SCANNING_FOR_KTAG_PACKETS);
}
break;
default:
// All other commands are ignored in this state.
break;
}
}
break;
}
@ -342,8 +411,17 @@ SystemKResult_T BLE_GetMyAddress(uint8_t * BD_ADDR)
SystemKResult_T BLE_ScanAndAdvertise(void)
{
COMM_BLE_Command_T command = { .ID = COMM_BLE_SCAN_AND_ADVERTISE, .Data = (void *)0x00 };
xQueueSend(COMM_BLE_CommandQueue, &command, 0);
if (Is_Quiet == false)
{
COMM_BLE_Command_T command = { .ID = COMM_BLE_SCAN_AND_ADVERTISE, .Data = (void *)0x00 };
xQueueSend(COMM_BLE_CommandQueue, &command, 0);
}
else
{
// If we are quiet, we don't want to advertise yet,
// but we will remember that we were asked to advertise.
Was_Advertising = true;
}
return SYSTEMK_RESULT_SUCCESS;
}
@ -364,6 +442,84 @@ SystemKResult_T BLE_StopScanning(void)
return SYSTEMK_RESULT_SUCCESS;
}
static void Unquiet_Timer_Callback(TimerHandle_t xTimer)
{
KLOG_INFO(TAG, "Unquiet timer expired after %lu ms.", (xTimerGetPeriod(xTimer) / portTICK_PERIOD_MS));
BLE_Unquiet();
}
SystemKResult_T BLE_Quiet(uint32_t duration_ms)
{
SystemKResult_T result = SYSTEMK_RESULT_NOT_READY;
if (Is_Quiet == true)
{
// Already quiet; no action needed.
return SYSTEMK_RESULT_SUCCESS;
}
else
{
Is_Quiet = true;
if ((Current_State == COMM_BLE_SCANNING_AND_ADVERTISING) ||
(Current_State == COMM_BLE_ADVERTISING_AS_BROADCASTER))
{
result = BLE_StopAdvertising();
Was_Advertising = true;
}
if (duration_ms > 0)
{
// Set a timer to unquiet after the specified duration.
BLEUnquietTimer = xTimerCreateStatic(
"BLEUnquietTimer",
pdMS_TO_TICKS(duration_ms),
pdFALSE,
(void *)0,
Unquiet_Timer_Callback,
&xBLEUnquietTimerBuffer);
if (BLEUnquietTimer == NULL)
{
KLOG_ERROR(TAG, "Couldn't create the BLEUnquietTimer!");
}
else
{
if (xTimerStart(BLEUnquietTimer, 0) != pdPASS)
{
KLOG_ERROR(TAG, "Couldn't start the BLEUnquietTimer!");
}
}
}
result = SYSTEMK_RESULT_SUCCESS;
}
return result;
}
SystemKResult_T BLE_Unquiet(void)
{
SystemKResult_T result = SYSTEMK_RESULT_NOT_READY;
if (Is_Quiet == false)
{
// Already unquiet; no action needed.
return SYSTEMK_RESULT_SUCCESS;
}
else
{
Is_Quiet = false;
if (Was_Advertising == true)
{
BLE_ScanAndAdvertise();
Was_Advertising = false;
}
result = SYSTEMK_RESULT_SUCCESS;
}
return result;
}
void COMM_BLE_RequestState(COMM_BLE_StateID_T state)
{
Next_State = state;
@ -396,6 +552,8 @@ SystemKResult_T BLE_SetAdvertisingData(BLE_AdvertisingData_T * data)
COMM_Console_Print_String("[BLE] Cy_BLE_GAPP_UpdateAdvScanData Error: 0x");
COMM_Console_Print_UInt32AsHex(result);
COMM_Console_Print_String("\n");
vTaskDelay(100);
#endif // TRACE_BLE
}
}
@ -612,8 +770,8 @@ static void BLE_EventHandler(uint32_t event, void * eventParam)
COMM_Console_Print_String(" KTag 'Instigate Game' packet found!");
break;
case BLE_PACKET_TYPE_JOIN_NOW:
COMM_Console_Print_String(" KTag 'Join Now' packet found!");
case BLE_PACKET_TYPE_EVENT:
COMM_Console_Print_String(" KTag 'Event' packet found!");
break;
case BLE_PACKET_TYPE_TAG:
@ -628,6 +786,14 @@ static void BLE_EventHandler(uint32_t event, void * eventParam)
COMM_Console_Print_String(" KTag 'Status' packet found!");
break;
case BLE_PACKET_TYPE_PARAMETERS:
COMM_Console_Print_String(" KTag 'Parameters' packet found!");
break;
case BLE_PACKET_TYPE_HELLO:
COMM_Console_Print_String(" KTag 'Hello' packet found!");
break;
default:
COMM_Console_Print_String(" Unknown KTag packet found!");
break;

View file

@ -22,7 +22,7 @@ extern "C" {
//#define TRACE_BLE
//#define VERBOSE_BLE_TRACE
#define COMM_BLE_TASK_STACK_SIZE_in_bytes 4096
#define COMM_BLE_TASK_STACK_SIZE_in_bytes 6144
typedef enum
{

@ -1 +1 @@
Subproject commit fb1a4ab07b990fef8f957ef7c5f806649e126d0f
Subproject commit bb915121e8e9c8b2679397f4554fa9bc2d836b67

View file

@ -22,11 +22,17 @@ TaskHandle_t COMM_BLE_Task_Handle;
/* Private Variables */
static const char *TAG = "BLE";
static const TickType_t BLE_Task_Delay = BLE_TASK_PERIOD_IN_ms / portTICK_PERIOD_MS;
static COMM_BLE_StateID_T Current_State = COMM_BLE_DEFAULT;
static COMM_BLE_StateID_T Next_State = COMM_BLE_INITIALIZING;
static bool State_Changed = false;
static TickType_t Time_At_State_Entry_In_Ticks;
static bool Is_Quiet = false;
static bool Was_Advertising = false;
static TimerHandle_t BLEUnquietTimer = NULL;
static StaticTimer_t xBLEUnquietTimerBuffer;
//! Immediate Alert Service alert level value.
volatile uint8_t COMM_BLE_IASAlertLevel = 0;
@ -288,6 +294,69 @@ void COMM_BLE_Task(void * pvParameters)
#endif // TRACE_BLE
}
}
switch (command.ID)
{
case COMM_BLE_STOP_SCANNING:
{
cy_en_ble_api_result_t apiResult = Cy_BLE_GAPC_StopScan();
if (apiResult != CY_BLE_SUCCESS)
{
#ifdef TRACE_BLE
COMM_Console_Print_String("[BLE] Cy_BLE_GAPC_StopScan API Error: 0x");
COMM_Console_Print_UInt32AsHex(apiResult);
COMM_Console_Print_String("\n");
#endif // TRACE_BLE
}
apiResult = Cy_BLE_GAPP_StopAdvertisement();
if (apiResult != CY_BLE_SUCCESS)
{
#ifdef TRACE_BLE
COMM_Console_Print_String("[BLE] Cy_BLE_GAPP_StopAdvertisement API Error: 0x");
COMM_Console_Print_UInt32AsHex(apiResult);
COMM_Console_Print_String("\n");
#endif // TRACE_BLE
}
COMM_BLE_RequestState(COMM_BLE_ADVERTISING_AS_BROADCASTER);
}
break;
case COMM_BLE_STOP_ADVERTISING:
{
cy_en_ble_api_result_t apiResult = Cy_BLE_GAPC_StopScan();
if (apiResult != CY_BLE_SUCCESS)
{
#ifdef TRACE_BLE
COMM_Console_Print_String("[BLE] Cy_BLE_GAPC_StopScan API Error: 0x");
COMM_Console_Print_UInt32AsHex(apiResult);
COMM_Console_Print_String("\n");
#endif // TRACE_BLE
}
apiResult = Cy_BLE_GAPP_StopAdvertisement();
if (apiResult != CY_BLE_SUCCESS)
{
#ifdef TRACE_BLE
COMM_Console_Print_String("[BLE] Cy_BLE_GAPP_StopAdvertisement API Error: 0x");
COMM_Console_Print_UInt32AsHex(apiResult);
COMM_Console_Print_String("\n");
#endif // TRACE_BLE
}
COMM_BLE_RequestState(COMM_BLE_SCANNING_FOR_KTAG_PACKETS);
}
break;
default:
// All other commands are ignored in this state.
break;
}
}
break;
}
@ -342,8 +411,17 @@ SystemKResult_T BLE_GetMyAddress(uint8_t * BD_ADDR)
SystemKResult_T BLE_ScanAndAdvertise(void)
{
COMM_BLE_Command_T command = { .ID = COMM_BLE_SCAN_AND_ADVERTISE, .Data = (void *)0x00 };
xQueueSend(COMM_BLE_CommandQueue, &command, 0);
if (Is_Quiet == false)
{
COMM_BLE_Command_T command = { .ID = COMM_BLE_SCAN_AND_ADVERTISE, .Data = (void *)0x00 };
xQueueSend(COMM_BLE_CommandQueue, &command, 0);
}
else
{
// If we are quiet, we don't want to advertise yet,
// but we will remember that we were asked to advertise.
Was_Advertising = true;
}
return SYSTEMK_RESULT_SUCCESS;
}
@ -364,6 +442,84 @@ SystemKResult_T BLE_StopScanning(void)
return SYSTEMK_RESULT_SUCCESS;
}
static void Unquiet_Timer_Callback(TimerHandle_t xTimer)
{
KLOG_INFO(TAG, "Unquiet timer expired after %lu ms.", (xTimerGetPeriod(xTimer) / portTICK_PERIOD_MS));
BLE_Unquiet();
}
SystemKResult_T BLE_Quiet(uint32_t duration_ms)
{
SystemKResult_T result = SYSTEMK_RESULT_NOT_READY;
if (Is_Quiet == true)
{
// Already quiet; no action needed.
return SYSTEMK_RESULT_SUCCESS;
}
else
{
Is_Quiet = true;
if ((Current_State == COMM_BLE_SCANNING_AND_ADVERTISING) ||
(Current_State == COMM_BLE_ADVERTISING_AS_BROADCASTER))
{
result = BLE_StopAdvertising();
Was_Advertising = true;
}
if (duration_ms > 0)
{
// Set a timer to unquiet after the specified duration.
BLEUnquietTimer = xTimerCreateStatic(
"BLEUnquietTimer",
pdMS_TO_TICKS(duration_ms),
pdFALSE,
(void *)0,
Unquiet_Timer_Callback,
&xBLEUnquietTimerBuffer);
if (BLEUnquietTimer == NULL)
{
KLOG_ERROR(TAG, "Couldn't create the BLEUnquietTimer!");
}
else
{
if (xTimerStart(BLEUnquietTimer, 0) != pdPASS)
{
KLOG_ERROR(TAG, "Couldn't start the BLEUnquietTimer!");
}
}
}
result = SYSTEMK_RESULT_SUCCESS;
}
return result;
}
SystemKResult_T BLE_Unquiet(void)
{
SystemKResult_T result = SYSTEMK_RESULT_NOT_READY;
if (Is_Quiet == false)
{
// Already unquiet; no action needed.
return SYSTEMK_RESULT_SUCCESS;
}
else
{
Is_Quiet = false;
if (Was_Advertising == true)
{
BLE_ScanAndAdvertise();
Was_Advertising = false;
}
result = SYSTEMK_RESULT_SUCCESS;
}
return result;
}
void COMM_BLE_RequestState(COMM_BLE_StateID_T state)
{
Next_State = state;
@ -396,6 +552,8 @@ SystemKResult_T BLE_SetAdvertisingData(BLE_AdvertisingData_T * data)
COMM_Console_Print_String("[BLE] Cy_BLE_GAPP_UpdateAdvScanData Error: 0x");
COMM_Console_Print_UInt32AsHex(result);
COMM_Console_Print_String("\n");
vTaskDelay(100);
#endif // TRACE_BLE
}
}
@ -612,8 +770,8 @@ static void BLE_EventHandler(uint32_t event, void * eventParam)
COMM_Console_Print_String(" KTag 'Instigate Game' packet found!");
break;
case BLE_PACKET_TYPE_JOIN_NOW:
COMM_Console_Print_String(" KTag 'Join Now' packet found!");
case BLE_PACKET_TYPE_EVENT:
COMM_Console_Print_String(" KTag 'Event' packet found!");
break;
case BLE_PACKET_TYPE_TAG:
@ -628,6 +786,14 @@ static void BLE_EventHandler(uint32_t event, void * eventParam)
COMM_Console_Print_String(" KTag 'Status' packet found!");
break;
case BLE_PACKET_TYPE_PARAMETERS:
COMM_Console_Print_String(" KTag 'Parameters' packet found!");
break;
case BLE_PACKET_TYPE_HELLO:
COMM_Console_Print_String(" KTag 'Hello' packet found!");
break;
default:
COMM_Console_Print_String(" Unknown KTag packet found!");
break;

View file

@ -22,7 +22,7 @@ extern "C" {
//#define TRACE_BLE
//#define VERBOSE_BLE_TRACE
#define COMM_BLE_TASK_STACK_SIZE_in_bytes 4096
#define COMM_BLE_TASK_STACK_SIZE_in_bytes 6144
typedef enum
{

@ -1 +1 @@
Subproject commit fb1a4ab07b990fef8f957ef7c5f806649e126d0f
Subproject commit bb915121e8e9c8b2679397f4554fa9bc2d836b67