2019-09-05 13:17:47 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 Gunar Schorcht
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*
|
|
|
|
* FreeRTOS to RIOT-OS adaption module for source code compatibility
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DOXYGEN
|
|
|
|
|
2020-10-22 11:34:00 +02:00
|
|
|
#define ENABLE_DEBUG 0
|
2019-09-05 13:17:47 +02:00
|
|
|
#include "debug.h"
|
|
|
|
|
2020-10-21 15:56:42 +02:00
|
|
|
#include <assert.h>
|
2019-09-05 13:17:47 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "esp_common.h"
|
|
|
|
#include "esp_attr.h"
|
|
|
|
#include "log.h"
|
2021-12-09 11:43:20 +01:00
|
|
|
#include "ztimer.h"
|
|
|
|
#include "timex.h"
|
2019-09-05 13:17:47 +02:00
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/timers.h"
|
|
|
|
|
|
|
|
typedef struct {
|
2021-12-09 11:43:20 +01:00
|
|
|
ztimer_t ztimer; /* ztimer object */
|
2019-09-05 13:17:47 +02:00
|
|
|
const char* name; /* FreeRTOS timer name */
|
|
|
|
uint32_t period; /* in us */
|
|
|
|
bool autoreload; /* FreeRTOS timer reload indicator */
|
|
|
|
const void* timerid; /* FreeRTOS timer id */
|
|
|
|
TimerCallbackFunction_t cb; /* FreeRTOS callback function */
|
2021-12-09 11:43:20 +01:00
|
|
|
} freertos_ztimer_t;
|
2019-09-05 13:17:47 +02:00
|
|
|
|
2021-12-09 11:43:20 +01:00
|
|
|
static void IRAM_ATTR _ztimer_callback (void *arg)
|
2019-09-05 13:17:47 +02:00
|
|
|
{
|
|
|
|
assert(arg != NULL);
|
|
|
|
|
2021-12-09 11:43:20 +01:00
|
|
|
freertos_ztimer_t* timer = (freertos_ztimer_t*)arg;
|
2019-09-05 13:17:47 +02:00
|
|
|
|
|
|
|
if (timer->autoreload) {
|
2021-12-09 11:43:20 +01:00
|
|
|
ztimer_set(ZTIMER_MSEC, &timer->ztimer, timer->period);
|
2019-09-05 13:17:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (timer->cb) {
|
|
|
|
timer->cb(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TimerHandle_t xTimerCreate (const char * const pcTimerName,
|
|
|
|
const TickType_t xTimerPeriod,
|
|
|
|
const UBaseType_t uxAutoReload,
|
|
|
|
void * const pvTimerID,
|
|
|
|
TimerCallbackFunction_t pxCallbackFunction)
|
|
|
|
{
|
2021-12-09 11:43:20 +01:00
|
|
|
freertos_ztimer_t* timer = malloc(sizeof(freertos_ztimer_t));
|
2019-09-05 13:17:47 +02:00
|
|
|
if (timer == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FreeRTOS timer parameter */
|
|
|
|
timer->name = pcTimerName;
|
2021-12-09 11:43:20 +01:00
|
|
|
timer->period = xTimerPeriod * portTICK_PERIOD_MS;
|
2019-09-05 13:17:47 +02:00
|
|
|
timer->autoreload = uxAutoReload;
|
|
|
|
timer->timerid = pvTimerID;
|
|
|
|
timer->cb = pxCallbackFunction;
|
|
|
|
|
2021-12-09 11:43:20 +01:00
|
|
|
/* ztimer parameter */
|
|
|
|
timer->ztimer.callback = _ztimer_callback;
|
|
|
|
timer->ztimer.arg = timer;
|
2019-09-05 13:17:47 +02:00
|
|
|
|
2022-06-17 07:07:41 +02:00
|
|
|
DEBUG("%s %p %s %"PRIu32" %u\n",
|
|
|
|
__func__, timer, pcTimerName, xTimerPeriod, uxAutoReload);
|
2019-09-05 13:17:47 +02:00
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseType_t xTimerDelete(TimerHandle_t xTimer, TickType_t xBlockTime)
|
|
|
|
{
|
2022-06-17 07:07:41 +02:00
|
|
|
DEBUG("%s %p %"PRIu32"\n", __func__, xTimer, xBlockTime);
|
2019-09-05 13:17:47 +02:00
|
|
|
assert(xTimer != NULL);
|
|
|
|
|
2021-12-09 11:43:20 +01:00
|
|
|
freertos_ztimer_t* timer = (freertos_ztimer_t*)xTimer;
|
|
|
|
ztimer_remove(ZTIMER_MSEC, &timer->ztimer);
|
2019-09-05 13:17:47 +02:00
|
|
|
free(timer);
|
|
|
|
|
|
|
|
return pdTRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseType_t xTimerStart (TimerHandle_t xTimer, TickType_t xBlockTime)
|
|
|
|
{
|
2022-06-17 07:07:41 +02:00
|
|
|
DEBUG("%s %p %"PRIu32"\n", __func__, xTimer, xBlockTime);
|
2019-09-05 13:17:47 +02:00
|
|
|
assert(xTimer != NULL);
|
|
|
|
|
2021-12-09 11:43:20 +01:00
|
|
|
freertos_ztimer_t* timer = (freertos_ztimer_t*)xTimer;
|
|
|
|
ztimer_set(ZTIMER_MSEC, &timer->ztimer, timer->period);
|
2019-09-05 13:17:47 +02:00
|
|
|
|
|
|
|
return pdTRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseType_t xTimerStop (TimerHandle_t xTimer, TickType_t xBlockTime)
|
|
|
|
{
|
2022-06-17 07:07:41 +02:00
|
|
|
DEBUG("%s %p %"PRIu32"\n", __func__, xTimer, xBlockTime);
|
2019-09-05 13:17:47 +02:00
|
|
|
assert(xTimer != NULL);
|
|
|
|
|
2021-12-09 11:43:20 +01:00
|
|
|
freertos_ztimer_t* timer = (freertos_ztimer_t*)xTimer;
|
|
|
|
ztimer_remove(ZTIMER_MSEC, &timer->ztimer);
|
2019-09-05 13:17:47 +02:00
|
|
|
|
|
|
|
return pdTRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseType_t xTimerReset (TimerHandle_t xTimer, TickType_t xBlockTime)
|
|
|
|
{
|
2022-06-17 07:07:41 +02:00
|
|
|
DEBUG("%s %p %"PRIu32"\n", __func__, xTimer, xBlockTime);
|
2019-09-05 13:17:47 +02:00
|
|
|
assert(xTimer != NULL);
|
|
|
|
|
2021-12-09 11:43:20 +01:00
|
|
|
freertos_ztimer_t* timer = (freertos_ztimer_t*)xTimer;
|
|
|
|
ztimer_set(ZTIMER_MSEC, &timer->ztimer, xBlockTime * portTICK_PERIOD_MS * US_PER_MS);
|
2019-09-05 13:17:47 +02:00
|
|
|
|
|
|
|
return pdTRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *pvTimerGetTimerID(const TimerHandle_t xTimer)
|
|
|
|
{
|
|
|
|
assert(xTimer != NULL);
|
|
|
|
|
2021-12-09 11:43:20 +01:00
|
|
|
freertos_ztimer_t* timer = (freertos_ztimer_t*)xTimer;
|
2019-09-05 13:17:47 +02:00
|
|
|
return (void*)timer->timerid;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* DOXYGEN */
|