Skip to content

Commit

Permalink
Merge pull request #7 from ZhuLingQing/os-timer
Browse files Browse the repository at this point in the history
update to recent from gf_aurora
  • Loading branch information
ZhuLingQing authored May 19, 2024
2 parents c9320eb + 9431b82 commit fd792a8
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 32 deletions.
Empty file modified bash_build.sh
100644 → 100755
Empty file.
2 changes: 2 additions & 0 deletions os-timer-port.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ void portTimerInit( void (*callback)());

void portTimerStop();

#define portTimerTicksPerUs() 1

long portTimerGetUs();

void portTimerStart(long end_us);
Expand Down
56 changes: 27 additions & 29 deletions os-timer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "os-timer.h"
#include <etl/multiset.h>
#include "pt-os.h"
#include "os-timer-port.h"
#include <etl/multiset.h>

static void OsTimerIsrHandler_();

Expand All @@ -15,7 +15,8 @@ class PtTimer
uint64_t period_us;
uint64_t end_us;
int id;
bool repeatable;

int repeatable;
} PtTimer_t;
struct tick_cmp
{
Expand All @@ -34,8 +35,9 @@ class PtTimer
return true;
}

void Init()
void Init(uint64_t ticksPerUs)
{
ticksPerUs_ = ticksPerUs;
timerObj_.clear();
}

Expand All @@ -49,22 +51,22 @@ class PtTimer
if (tim->repeatable)
{
timerObj_.insert(
{tim->callback, tim->param, tim->period_us, tim->end_us + tim->period_us, tim->id, true});
{tim->callback, tim->param, tim->period_us, tim->end_us + tim->period_us, tim->id, 1});
}
// remove the current timer.
timerObj_.erase(tim);

// if has one or more timer enable it.
tim = timerObj_.begin();
if (tim != timerObj_.end())
portTimerStart(tim->end_us);
if (tim != timerObj_.end()) portTimerStart(tim->end_us);
}

int Register(OsTimerCallback_t callback, void *param, uint64_t period_us, bool repeatable, int *id)
{
if (timerObj_.full()) return NO_RESOURCE;
if (timerObj_.full()) return OS_NO_RESOURCE;
portTimerStop();
timerObj_.insert({callback, param, period_us,
portTimerGetUs() + period_us, ++timerIdSeed_, repeatable});
timerObj_.insert(
{callback, param, period_us, portTimerGetUs() + period_us, ++timerIdSeed_, repeatable ? 1 : 0});
portTimerStart(timerObj_.begin()->end_us);
if (id) *id = timerIdSeed_;
return TASK_OP_SUCCESS;
Expand All @@ -78,7 +80,16 @@ class PtTimer
{
if (it->id == id)
{
timerObj_.erase(it);
if (it == timerObj_.begin())
{
// if 1st timer is killed, reload the next one.
portTimerStop();
timerObj_.erase(it);
it = timerObj_.begin();
if (it != timerObj_.end()) portTimerStart(it->end_us);
}
else
timerObj_.erase(it);
return TASK_OP_SUCCESS;
}
}
Expand All @@ -94,11 +105,6 @@ class PtTimer
while (timerTriggered == false) TaskYield();
return TASK_OP_SUCCESS;
}

inline void try_insert(OsTimerCallback_t callback, void *param, uint64_t period_us, uint64_t end_us, bool repeatable)
{

}
void EventCallback()
{
timerTrigged_ += 1;
Expand All @@ -115,15 +121,13 @@ class PtTimer
volatile int timerTrigged_;
int timerHandled_;
int timerIdSeed_;
uint64_t ticksPerUs_;
etl::multiset<PtTimer_t, osMaxTimers, tick_cmp> timerObj_;
};

static PtTimer kTimer_;

static void OsTimerIsrHandler_()
{
kTimer_.EventCallback();
}
static void OsTimerIsrHandler_() { kTimer_.EventCallback(); }

static TASK_DECLARE(OsTimerTask_(OsTaskId taskId, void *))
{
Expand All @@ -139,9 +143,9 @@ static TASK_DECLARE(OsTimerTask_(OsTaskId taskId, void *))

void OsTimerInit()
{
kTimer_.Init();
portTimerInit(OsTimerIsrHandler_);
portTimerStop();
kTimer_.Init(portTimerTicksPerUs());
RegisterTask("thrTmr", OsTimerTask_, nullptr);
}

Expand All @@ -152,12 +156,6 @@ int OsTimerRegister(OsTimerCallback_t callback, void *param, uint64_t period_us,

int OsTimerCount() { return kTimer_.Count(); }

int OsTimerKill(int id)
{
return kTimer_.Kill(id);
}
int OsTimerKill(int id) { return kTimer_.Kill(id); }

int OsTimerDelayUs(uint64_t delay_us)
{
return kTimer_.DelayUs(delay_us);
}
int OsTimerDelayUs(uint64_t delay_us) { return kTimer_.DelayUs(delay_us); }
5 changes: 5 additions & 0 deletions os-timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ extern "C"
\*****************************************************************************/
int OsTimerKill(int id);

/*****************************************************************************\
* @description : delay micro seconds
* @param [I]: delay_us
* @return error or success
\*****************************************************************************/
int OsTimerDelayUs(uint64_t delay_us);

#ifdef __cplusplus
Expand Down
7 changes: 6 additions & 1 deletion pt-os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,12 @@ int OsInit(void)
return TASK_OP_SUCCESS;
}

static TaskFunction idleTask = nullptr;

void OsStart(void)
{
if (idleTask) RegisterTask("Idle", idleTask, nullptr);
while (sOsControlBlock_.NumOfLivingTasks() > 0) sOsControlBlock_.Schedule();
}
}

void OsAddIdleTask(TaskFunction task) { idleTask = task; }
4 changes: 3 additions & 1 deletion pt-os.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extern "C"
#define TASK_OP_SUCCESS (0)
#define INVALID_TASK_ID (-1)
#define INVALID_TASK_STATUS (-2)
#define NO_RESOURCE (-3)
#define OS_NO_RESOURCE (-3)

typedef enum
{
Expand Down Expand Up @@ -65,6 +65,8 @@ extern "C"
// Start scheduling all the tasks, only return while all tasks are OsTaskExit or OsTaskNotExist.
void OsStart(void);

void OsAddIdleTask(TaskFunction task);

#if __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion pt-osConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// Number of maximum threads support
#define osMaxThreads (64)

// Number of maximum timer support
// Number of timer.
#define osMaxTimers (64)

#include <cassert>
Expand Down

0 comments on commit fd792a8

Please sign in to comment.