Files
esp-protocols/components/esp_modem/src/esp_modem_primitives_freertos.cpp

115 lines
2.5 KiB
C++
Raw Normal View History

2021-04-04 22:15:46 +02:00
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "cxx_include/esp_modem_primitives.hpp"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/semphr.h"
namespace esp_modem {
2021-06-01 10:21:51 +02:00
void Lock::unlock()
{
2021-04-04 22:15:46 +02:00
xSemaphoreGiveRecursive(m);
}
Lock::Lock(): m(nullptr)
{
m = xSemaphoreCreateRecursiveMutex();
throw_if_false(m != nullptr, "create signal event group failed");
}
2021-06-01 10:21:51 +02:00
Lock::~Lock()
{
2021-04-04 22:15:46 +02:00
vSemaphoreDelete(m);
}
2021-06-01 10:21:51 +02:00
void Lock::lock()
{
2021-04-04 22:15:46 +02:00
xSemaphoreTakeRecursive(m, portMAX_DELAY);
}
SignalGroup::SignalGroup(): event_group(nullptr)
2021-04-04 22:15:46 +02:00
{
event_group = xEventGroupCreate();
throw_if_false(event_group != nullptr, "create signal event group failed");
}
void SignalGroup::set(uint32_t bits)
2021-04-04 22:15:46 +02:00
{
xEventGroupSetBits(event_group, bits);
}
void SignalGroup::clear(uint32_t bits)
2021-04-04 22:15:46 +02:00
{
xEventGroupClearBits(event_group, bits);
}
bool SignalGroup::wait(uint32_t flags, uint32_t time_ms)
2021-04-04 22:15:46 +02:00
{
EventBits_t bits = xEventGroupWaitBits(event_group, flags, pdTRUE, pdTRUE, pdMS_TO_TICKS(time_ms));
return bits & flags;
}
bool SignalGroup::is_any(uint32_t flags)
2021-04-04 22:15:46 +02:00
{
return xEventGroupGetBits(event_group) & flags;
}
bool SignalGroup::wait_any(uint32_t flags, uint32_t time_ms)
2021-04-04 22:15:46 +02:00
{
EventBits_t bits = xEventGroupWaitBits(event_group, flags, pdFALSE, pdFALSE, pdMS_TO_TICKS(time_ms));
return bits & flags;
}
SignalGroup::~SignalGroup()
{
2021-06-01 10:21:51 +02:00
if (event_group) {
vEventGroupDelete(event_group);
2021-06-01 10:21:51 +02:00
}
}
Task::Task(size_t stack_size, size_t priority, void *task_param, TaskFunction_t task_function)
2021-06-01 10:21:51 +02:00
: task_handle(nullptr)
{
BaseType_t ret = xTaskCreate(task_function, "vfs_task", stack_size, task_param, priority, &task_handle);
throw_if_false(ret == pdTRUE, "create vfs task failed");
}
Task::~Task()
{
2021-06-01 10:21:51 +02:00
if (task_handle) {
vTaskDelete(task_handle);
2021-06-01 10:21:51 +02:00
}
}
void Task::Delete()
{
vTaskDelete(nullptr);
}
void Task::Relinquish()
2021-04-04 22:15:46 +02:00
{
vTaskDelay(1);
2021-04-04 22:15:46 +02:00
}
void Task::Delay(uint32_t ms)
{
vTaskDelay(pdMS_TO_TICKS(ms));
}
} // namespace esp_modem