#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <string>
#include <sstream>
#include <iomanip>
#include "FreeRTOS.h"
#include <esp_log.h>
#include "sdkconfig.h"
static const char* LOG_TAG = "FreeRTOS";
void FreeRTOS::sleep(uint32_t ms) {
::vTaskDelay(ms/portTICK_PERIOD_MS);
}
void FreeRTOS::startTask(void task(void*), std::string taskName, void *param, int stackSize) {
::xTaskCreate(task, taskName.data(), stackSize, param, 5, NULL);
}
void FreeRTOS::deleteTask(TaskHandle_t pTask) {
::vTaskDelete(pTask);
}
uint32_t FreeRTOS::getTimeSinceStart() {
return (uint32_t)(xTaskGetTickCount()*portTICK_PERIOD_MS);
}
uint32_t FreeRTOS::Semaphore::wait(std::string owner) {
ESP_LOGV(LOG_TAG, "Semaphore waiting: %s for %s", toString().c_str(), owner.c_str());
xSemaphoreTake(m_semaphore, portMAX_DELAY);
m_owner = owner;
xSemaphoreGive(m_semaphore);
ESP_LOGV(LOG_TAG, "Semaphore released: %s", toString().c_str());
m_owner = "<N/A>";
return m_value;
}
FreeRTOS::Semaphore::Semaphore(std::string name) {
m_semaphore = xSemaphoreCreateMutex();
m_name = name;
m_owner = "<N/A>";
m_value = 0;
}
FreeRTOS::Semaphore::~Semaphore() {
vSemaphoreDelete(m_semaphore);
}
void FreeRTOS::Semaphore::give() {
xSemaphoreGive(m_semaphore);
ESP_LOGV(LOG_TAG, "Semaphore giving: %s", toString().c_str());
m_owner = "<N/A>";
}
void FreeRTOS::Semaphore::give(uint32_t value) {
m_value = value;
give();
}
void FreeRTOS::Semaphore::giveFromISR() {
BaseType_t higherPriorityTaskWoken;
xSemaphoreGiveFromISR(m_semaphore, &higherPriorityTaskWoken);
}
void FreeRTOS::Semaphore::take(std::string owner)
{
ESP_LOGV(LOG_TAG, "Semaphore taking: %s for %s", toString().c_str(), owner.c_str());
xSemaphoreTake(m_semaphore, portMAX_DELAY);
m_owner = owner;
ESP_LOGV(LOG_TAG, "Semaphore taken: %s", toString().c_str());
}
void FreeRTOS::Semaphore::take(uint32_t timeoutMs, std::string owner) {
ESP_LOGV(LOG_TAG, "Semaphore taking: %s for %s", toString().c_str(), owner.c_str());
m_owner = owner;
xSemaphoreTake(m_semaphore, timeoutMs/portTICK_PERIOD_MS);
ESP_LOGV(LOG_TAG, "Semaphore taken: %s", toString().c_str());
}
std::string FreeRTOS::Semaphore::toString() {
std::stringstream stringStream;
stringStream << "name: "<< m_name << " (0x" << std::hex << std::setfill('0') << (uint32_t)m_semaphore << "), owner: " << m_owner;
return stringStream.str();
}
void FreeRTOS::Semaphore::setName(std::string name) {
m_name = name;
}