From 5f1d63851152ad175e6533bb62cc8d594d33fecf Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Sat, 30 Mar 2024 18:56:54 +0000 Subject: [PATCH] Fixes for PR 2041 (Continuous time updates) --- CMakeLists.txt | 2 ++ sim/components/ble/SimpleWeatherService.cpp | 2 +- sim/components/ble/SimpleWeatherService.h | 4 +-- sim/nrf_assert.h | 1 + sim/portmacro_cmsis.h | 1 + sim/queue.cpp | 26 ++++++++------- sim/queue.h | 16 ++++----- sim/semphr.cpp | 37 +++++++++++++++++++++ sim/semphr.h | 11 ++++++ 9 files changed, 77 insertions(+), 23 deletions(-) create mode 100644 sim/nrf_assert.h create mode 100644 sim/semphr.cpp create mode 100644 sim/semphr.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6923efa..5136220 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,6 +65,8 @@ add_library(sim-base STATIC sim/timers.cpp sim/queue.h sim/queue.cpp + sim/semphr.h + sim/semphr.cpp # src/FreeRTOS sim/portmacro_cmsis.h sim/portmacro_cmsis.cpp diff --git a/sim/components/ble/SimpleWeatherService.cpp b/sim/components/ble/SimpleWeatherService.cpp index 024545b..66e22fb 100644 --- a/sim/components/ble/SimpleWeatherService.cpp +++ b/sim/components/ble/SimpleWeatherService.cpp @@ -62,7 +62,7 @@ int WeatherCallback(uint16_t /*connHandle*/, uint16_t /*attrHandle*/, struct ble return static_cast(arg)->OnCommand(ctxt); } -SimpleWeatherService::SimpleWeatherService(const DateTime& dateTimeController) : dateTimeController(dateTimeController) { +SimpleWeatherService::SimpleWeatherService(DateTime& dateTimeController) : dateTimeController(dateTimeController) { } void SimpleWeatherService::Init() { diff --git a/sim/components/ble/SimpleWeatherService.h b/sim/components/ble/SimpleWeatherService.h index 027518d..1f123c1 100644 --- a/sim/components/ble/SimpleWeatherService.h +++ b/sim/components/ble/SimpleWeatherService.h @@ -24,7 +24,7 @@ namespace Controllers { class SimpleWeatherService { public: - explicit SimpleWeatherService(const DateTime& dateTimeController); + explicit SimpleWeatherService(DateTime& dateTimeController); void Init(); @@ -127,7 +127,7 @@ class SimpleWeatherService { uint16_t eventHandle {}; - const Pinetime::Controllers::DateTime& dateTimeController; + Pinetime::Controllers::DateTime& dateTimeController; std::optional currentWeather; std::optional forecast; diff --git a/sim/nrf_assert.h b/sim/nrf_assert.h new file mode 100644 index 0000000..ac5aa76 --- /dev/null +++ b/sim/nrf_assert.h @@ -0,0 +1 @@ +#define ASSERT(expr) assert(expr) diff --git a/sim/portmacro_cmsis.h b/sim/portmacro_cmsis.h index 774878c..bc29d3e 100644 --- a/sim/portmacro_cmsis.h +++ b/sim/portmacro_cmsis.h @@ -37,6 +37,7 @@ extern "C" { typedef uint32_t TickType_t; #define portMAX_DELAY ( TickType_t ) 0xffffffffUL +#define portNRF_RTC_MAXTICKS ((1U<<24)-1U) typedef long BaseType_t; typedef unsigned long UBaseType_t; diff --git a/sim/queue.cpp b/sim/queue.cpp index acc3b34..13e845e 100644 --- a/sim/queue.cpp +++ b/sim/queue.cpp @@ -4,43 +4,45 @@ QueueHandle_t xQueueCreate(const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize) { - QueueHandle_t xQueue; + Queue_t *xQueue = new Queue_t; if (uxItemSize != 1) { throw std::runtime_error("uxItemSize must be 1"); } - xQueue.queue.reserve(uxQueueLength); + xQueue->queue.reserve(uxQueueLength); return xQueue; } -BaseType_t xQueueSend(QueueHandle_t &xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait) +BaseType_t xQueueSend(QueueHandle_t xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait) { - std::lock_guard guard(xQueue.mutex); - xQueue.queue.push_back(*reinterpret_cast(pvItemToQueue)); + Queue_t* pxQueue = ( Queue_t * ) xQueue; + std::lock_guard guard(pxQueue->mutex); + pxQueue->queue.push_back(*reinterpret_cast(pvItemToQueue)); return true; } -BaseType_t xQueueSendFromISR(QueueHandle_t &xQueue, const void * const pvItemToQueue, BaseType_t *xHigherPriorityTaskWoken) +BaseType_t xQueueSendFromISR(QueueHandle_t xQueue, const void * const pvItemToQueue, BaseType_t *xHigherPriorityTaskWoken) { TickType_t xTicksToWait = 0; *xHigherPriorityTaskWoken = pdFALSE; return xQueueSend(xQueue, pvItemToQueue, 0.0); } -BaseType_t xQueueReceive(QueueHandle_t &xQueue, void * const pvBuffer, TickType_t xTicksToWait) +BaseType_t xQueueReceive(QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait) { - while (xQueue.queue.empty()) { + Queue_t* pxQueue = ( Queue_t * ) xQueue; + while (pxQueue->queue.empty()) { if (xTicksToWait <= 25) { return false; } SDL_Delay(25); xTicksToWait -= 25; } - if (xQueue.queue.empty()) { + if (pxQueue->queue.empty()) { return false; } - std::lock_guard guard(xQueue.mutex); + std::lock_guard guard(pxQueue->mutex); uint8_t *buf = reinterpret_cast(pvBuffer); - *buf = xQueue.queue.at(0); - xQueue.queue.erase(xQueue.queue.begin()); + *buf = pxQueue->queue.at(0); + pxQueue->queue.erase(pxQueue->queue.begin()); return true; } diff --git a/sim/queue.h b/sim/queue.h index caffd96..52d62d8 100644 --- a/sim/queue.h +++ b/sim/queue.h @@ -10,15 +10,15 @@ * returns an QueueHandle_t variable that can then be used as a parameter to * xQueueSend(), xQueueReceive(), etc. */ -//typedef void * QueueHandle_t; -struct QueueHandle_t { +typedef void * QueueHandle_t; +struct Queue_t { std::mutex mutex; std::vector queue; - QueueHandle_t() {} - QueueHandle_t(const QueueHandle_t &o) { + Queue_t() {} + Queue_t(const Queue_t &o) { queue=o.queue; } - QueueHandle_t &operator=(const QueueHandle_t &o) { + Queue_t &operator=(const Queue_t &o) { queue=o.queue; return *this; } @@ -26,6 +26,6 @@ struct QueueHandle_t { //using QueueHandle_t = std::vector; QueueHandle_t xQueueCreate(const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize); -BaseType_t xQueueSend(QueueHandle_t &xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait); -BaseType_t xQueueSendFromISR(QueueHandle_t &xQueue, const void * const pvItemToQueue, BaseType_t *xHigherPriorityTaskWoken); -BaseType_t xQueueReceive(QueueHandle_t &xQueue, void * const pvBuffer, TickType_t xTicksToWait ); +BaseType_t xQueueSend(QueueHandle_t xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait); +BaseType_t xQueueSendFromISR(QueueHandle_t xQueue, const void * const pvItemToQueue, BaseType_t *xHigherPriorityTaskWoken); +BaseType_t xQueueReceive(QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait ); diff --git a/sim/semphr.cpp b/sim/semphr.cpp new file mode 100644 index 0000000..a223a37 --- /dev/null +++ b/sim/semphr.cpp @@ -0,0 +1,37 @@ +#include "semphr.h" +#include +#include + +QueueHandle_t xSemaphoreCreateMutex() { + SemaphoreHandle_t xSemaphore = xQueueCreate(1, 1); + Queue_t *pxQueue = (Queue_t *)xSemaphore; + pxQueue->queue.push_back(0); + return xSemaphore; +}; + +BaseType_t xSemaphoreTake(SemaphoreHandle_t xSemaphore, + TickType_t xTicksToWait) { + Queue_t *pxQueue = (Queue_t *)xSemaphore; + while (!pxQueue->queue.empty()) { + if (xTicksToWait <= 25) { + return false; + } + SDL_Delay(25); + xTicksToWait -= 25; + } + std::lock_guard guard(pxQueue->mutex); + if (!pxQueue->queue.empty()) { + return false; + } + pxQueue->queue.push_back(0); + return true; +} +BaseType_t xSemaphoreGive(SemaphoreHandle_t xSemaphore) { + Queue_t *pxQueue = (Queue_t *)xSemaphore; + std::lock_guard guard(pxQueue->mutex); + if (pxQueue->queue.size() != 1) { + throw std::runtime_error("Mutex released without being held"); + } + pxQueue->queue.pop_back(); + return true; +} diff --git a/sim/semphr.h b/sim/semphr.h new file mode 100644 index 0000000..74975d7 --- /dev/null +++ b/sim/semphr.h @@ -0,0 +1,11 @@ +#pragma once + +#include "FreeRTOS.h" +#include "queue.h" + +typedef QueueHandle_t SemaphoreHandle_t; + +QueueHandle_t xSemaphoreCreateMutex(); + +BaseType_t xSemaphoreTake( SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait); +BaseType_t xSemaphoreGive( SemaphoreHandle_t xSemaphore);