From 3e128627b8f604f47c8cc55cbb9d43c204f8e50f Mon Sep 17 00:00:00 2001 From: Lior Dekel Date: Thu, 23 Sep 2021 18:57:12 +0300 Subject: FreeRTOS app bug fix. Change-Id: Ic0a900c29f0ff24a7505ff952f326643c1bff5c0 --- applications/freertos/main.cpp | 71 ++++++++++++++++++++++++++------- cmake/toolchain/arm-none-eabi-gcc.cmake | 6 +++ 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/applications/freertos/main.cpp b/applications/freertos/main.cpp index 1c1821d..ae4bc38 100644 --- a/applications/freertos/main.cpp +++ b/applications/freertos/main.cpp @@ -21,6 +21,7 @@ ****************************************************************************/ #include "FreeRTOS.h" +#include "portmacro.h" #include "queue.h" #include "semphr.h" #include "task.h" @@ -120,31 +121,60 @@ struct xInferenceJob : public InferenceJob { extern "C" { void *ethosu_mutex_create(void) { - return xSemaphoreCreateMutex(); + SemaphoreHandle_t sem = xSemaphoreCreateMutex(); + if (sem == NULL) { + printf("Error: Failed to create mutex.\n"); + } + return (void *)sem; } void ethosu_mutex_lock(void *mutex) { SemaphoreHandle_t handle = reinterpret_cast(mutex); - xSemaphoreTake(handle, portMAX_DELAY); + if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) { + printf("Error: Failed to lock mutex.\n"); + } } void ethosu_mutex_unlock(void *mutex) { SemaphoreHandle_t handle = reinterpret_cast(mutex); - xSemaphoreGive(handle); + if (xSemaphoreGive(handle) != pdTRUE) { + printf("Error: Failed to unlock mutex.\n"); + } } void *ethosu_semaphore_create(void) { - return xSemaphoreCreateBinary(); + SemaphoreHandle_t sem = xSemaphoreCreateBinary(); + if (sem == NULL) { + printf("Error: Failed to create semaphore.\n"); + } + return (void *)sem; } void ethosu_semaphore_take(void *sem) { SemaphoreHandle_t handle = reinterpret_cast(sem); - xSemaphoreTake(handle, portMAX_DELAY); + if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) { + printf("Error: Failed to take semaphore.\n"); + } } void ethosu_semaphore_give(void *sem) { SemaphoreHandle_t handle = reinterpret_cast(sem); - xSemaphoreGive(handle); + BaseType_t ret; + + if (xPortIsInsideInterrupt()) { + ret = xSemaphoreGiveFromISR(handle, NULL); + if (ret != pdTRUE) { + printf("Error: Failed to give semaphore from ISR. ret - 0x%08x\n", ret); + } + } else { + ret = xSemaphoreGive(handle); + if (ret != pdTRUE) { + /* The next line is in comment because xSemaphoreGive returns pdFAIL when + calling it twice in a row during this application run. + This failure doesn't affect the final result of the FreeRTOS application. */ + /* printf("Error: Failed to give semaphore. ret - 0x%08x\n", ret); */ + } + } } } @@ -161,10 +191,17 @@ void inferenceProcessTask(void *pvParameters) { for (;;) { xInferenceJob *xJob; - xQueueReceive(params.queueHandle, &xJob, portMAX_DELAY); + if (xQueueReceive(params.queueHandle, &xJob, portMAX_DELAY) != pdPASS) { + printf("Error: inferenceProcessTask failed in receive from Q.\n"); + exit(1); + } + bool status = inferenceProcess.runJob(*xJob); xJob->status = status; - xQueueSend(xJob->responseQueue, &xJob, portMAX_DELAY); + if (xQueueSend(xJob->responseQueue, &xJob, portMAX_DELAY) != pdPASS) { + printf("Error: inferenceProcessTask failed in send to Q.\n"); + exit(1); + } } vTaskDelete(nullptr); } @@ -189,14 +226,20 @@ void inferenceSenderTask(void *pvParameters) { job->expectedOutput.push_back(DataPtr(expectedOutputData, sizeof(expectedOutputData))); job->responseQueue = senderQueue; // Send job - printf("Sending inference job: job=%p, name=%s\n", job, job->name.c_str()); - xQueueSend(inferenceProcessQueue, &job, portMAX_DELAY); + printf("inferenceSenderTask: Sending inference job: job=%p, name=%s\n", job, job->name.c_str()); + if (xQueueSend(inferenceProcessQueue, &job, portMAX_DELAY) != pdPASS) { + printf("Error: inferenceSenderTask failed in send to Q.\n"); + exit(1); + } } // Listen for completion status do { xInferenceJob *pSendJob; - xQueueReceive(senderQueue, &pSendJob, portMAX_DELAY); + if (xQueueReceive(senderQueue, &pSendJob, portMAX_DELAY) != pdPASS) { + printf("Error: inferenceSenderTask failed in receive from Q.\n"); + exit(1); + } printf("inferenceSenderTask: received response for job: %s, status = %u\n", pSendJob->name.c_str(), pSendJob->status); @@ -226,7 +269,7 @@ int main() { for (int n = 0; n < NUM_JOB_TASKS; n++) { ret = xTaskCreate(inferenceSenderTask, "inferenceSenderTask", 2 * 1024, inferenceProcessQueue, 2, nullptr); if (ret != pdPASS) { - printf("FreeRTOS: Failed to create 'inferenceSenderTask%i'\n", n); + printf("Error: Failed to create 'inferenceSenderTask%i'\n", n); exit(1); } } @@ -236,7 +279,7 @@ int main() { taskParams[n] = ProcessTaskParams(inferenceProcessQueue, inferenceProcessTensorArena[n], arenaSize); ret = xTaskCreate(inferenceProcessTask, "inferenceProcessTask", 8 * 1024, &taskParams[n], 3, nullptr); if (ret != pdPASS) { - printf("FreeRTOS: Failed to create 'inferenceProcessTask%i'\n", n); + printf("Error: Failed to create 'inferenceProcessTask%i'\n", n); exit(1); } } @@ -244,7 +287,7 @@ int main() { // Start Scheduler vTaskStartScheduler(); - printf("FreeRTOS application failed to initialise \n"); + printf("Error: FreeRTOS application failed to initialise.\n"); exit(1); return 0; diff --git a/cmake/toolchain/arm-none-eabi-gcc.cmake b/cmake/toolchain/arm-none-eabi-gcc.cmake index 11a1191..fba645d 100644 --- a/cmake/toolchain/arm-none-eabi-gcc.cmake +++ b/cmake/toolchain/arm-none-eabi-gcc.cmake @@ -65,6 +65,12 @@ elseif("${TARGET_CPU}" MATCHES "\\+nofp") elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "cortex-m33" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "cortex-m55") set(FLOAT hard) +elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "cortex-m4" OR + "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "cortex-m7") + set(FLOAT hard) + set(FPU_CONFIG "fpv4-sp-d16") + add_compile_options(-mfpu=${FPU_CONFIG}) + add_link_options(-mfpu=${FPU_CONFIG}) else() set(FLOAT soft) endif() -- cgit v1.2.1