Skip to content

Commit

Permalink
For compatibility reasons I am decreasing 64bit tick resolution to 32…
Browse files Browse the repository at this point in the history
…bits resolution.
  • Loading branch information
solariun committed Nov 11, 2019
1 parent 4379bef commit b2ea03f
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 43 deletions.
34 changes: 18 additions & 16 deletions CorePartition/CorePartition.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ typedef struct


uint32_t nNice;
uint64_t nLastMomentun;
uint32_t nLastMomentun;

void* pLastStack;
uint8_t* pnStackPage;
Expand Down Expand Up @@ -93,17 +93,17 @@ bool CorePartition_SetStackOverflowHandler (void (*pStackOverflowHandler)(void))
}


uint64_t getDefaultCTime()
uint32_t getDefaultCTime()
{
static uint64_t nCounter=0;
static uint32_t nCounter=0;

return (++nCounter);
}

uint64_t (*getCTime)(void) = getDefaultCTime;
uint32_t (*getCTime)(void) = getDefaultCTime;


bool CorePartition_SetCurrentTimeInterface (uint64_t (*getCurrentTimeInterface)(void))
bool CorePartition_SetCurrentTimeInterface (uint32_t (*getCurrentTimeInterface)(void))
{
if (getCTime != getDefaultCTime || getCurrentTimeInterface == NULL)
return false;
Expand All @@ -113,17 +113,17 @@ bool CorePartition_SetCurrentTimeInterface (uint64_t (*getCurrentTimeInterface)(
return true;
}

void sleepDefaultCTime (uint64_t nSleepTime)
void sleepDefaultCTime (uint32_t nSleepTime)
{
nSleepTime = getCTime() + nSleepTime;

while (getCTime() <= nSleepTime);
}


void (*sleepCTime)(uint64_t nSleepTime) = sleepDefaultCTime;
void (*sleepCTime)(uint32_t nSleepTime) = sleepDefaultCTime;

bool CorePartition_SetSleepTimeInterface (void (*getSleepTimeInterface)(uint64_t nSleepTime))
bool CorePartition_SetSleepTimeInterface (void (*getSleepTimeInterface)(uint32_t nSleepTime))
{
if (sleepCTime != sleepDefaultCTime || getSleepTimeInterface == NULL)
return false;
Expand Down Expand Up @@ -207,13 +207,13 @@ inline static void RestoreStack(void)
memcpy(pCurrentThread->pLastStack, pCurrentThread->pnStackPage, pCurrentThread->nStackSize);
}

inline static void SleepBeforeTask (uint64_t nCurTime)
inline static void SleepBeforeTask (uint32_t nCurTime)
{
uint64_t nMin;
uint32_t nMin;
size_t nCThread=0;

#define __NEXTIME(TH) (pThreadLight [TH].nLastMomentun + pThreadLight [TH].nNice - 1)
#define __CALC(TH) (uint64_t) (__NEXTIME(TH) - nCurTime)
#define __CALC(TH) (uint32_t) (__NEXTIME(TH) - nCurTime)

nMin = __CALC(0);

Expand All @@ -233,19 +233,19 @@ inline static void SleepBeforeTask (uint64_t nCurTime)

inline static size_t Scheduler (void)
{
static uint64_t nCounter = 0;
static uint32_t nCounter = 0;

if (nCounter == 0) nCounter = getCTime ();

pThreadLight [nCurrentThread].nExecTime = (uint32_t) ((uint64_t)(getCTime () - pThreadLight [nCurrentThread].nLastMomentun));
pThreadLight [nCurrentThread].nExecTime = (uint32_t) ((uint32_t)(getCTime () - pThreadLight [nCurrentThread].nLastMomentun));

while (1)
{
nCounter = getCTime();

if (++nCurrentThread <= nMaxThreads)
{
if (nCounter >= ((uint64_t)(pThreadLight [nCurrentThread].nLastMomentun + pThreadLight [nCurrentThread].nNice)))
if (nCounter >= ((uint32_t)(pThreadLight [nCurrentThread].nLastMomentun + pThreadLight [nCurrentThread].nNice)))
{
pThreadLight [nCurrentThread].nLastMomentun = nCounter;

Expand All @@ -258,6 +258,8 @@ inline static size_t Scheduler (void)
SleepBeforeTask (getCTime ());
}
}

return 0;
}


Expand Down Expand Up @@ -405,14 +407,14 @@ uint32_t CorePartition_GetLastDutyCycleByID (size_t nID)
return pThreadLight [nID].nExecTime;
}

uint64_t CorePartition_GetLastMomentumByID (size_t nID)
uint32_t CorePartition_GetLastMomentumByID (size_t nID)
{
if (nID >= nMaxThreads) return 0;

return pThreadLight [nID].nLastMomentun;
}

uint64_t CorePartition_GetLastMomentum (void)
uint32_t CorePartition_GetLastMomentum (void)
{
return CorePartition_GetLastMomentumByID(nCurrentThread);
}
Expand Down
8 changes: 4 additions & 4 deletions CorePartition/CorePartition.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ extern "C"{

bool CorePartition_CreateThread (void(*pFunction)(void*), void* pValue, size_t nStackMaxSize, uint32_t nNice);

bool CorePartition_SetCurrentTimeInterface (uint64_t (*getCurrentTimeInterface)(void));
bool CorePartition_SetCurrentTimeInterface (uint32_t (*getCurrentTimeInterface)(void));

bool CorePartition_SetSleepTimeInterface (void (*getSleepTimeInterface)(uint64_t nSleepTime));
bool CorePartition_SetSleepTimeInterface (void (*getSleepTimeInterface)(uint32_t nSleepTime));

bool CorePartition_SetStackOverflowHandler (void (*pStackOverflowHandler)(void));

Expand All @@ -84,7 +84,7 @@ extern "C"{

void CorePartition_SetNice (uint32_t nNice);

uint64_t CorePartition_GetLastMomentum (void);
uint32_t CorePartition_GetLastMomentum (void);

uint32_t CorePartition_GetLastDutyCycle (void);

Expand All @@ -98,7 +98,7 @@ extern "C"{

int CorePartition_GetStatusByID (size_t nID);

uint64_t CorePartition_GetLastMomentumByID (size_t nID);
uint32_t CorePartition_GetLastMomentumByID (size_t nID);

uint32_t CorePartition_GetLastDutyCycleByID (size_t nID);

Expand Down
6 changes: 3 additions & 3 deletions CorePartition/Resources/BlinkThreads/BlinkThreads.ino
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ void Thread1 (void* pValue)



static uint64_t getTimeTick()
static uint32_t getTimeTick()
{
return (uint64_t) millis();
return (uint32_t) millis();
}

static void sleepTick (uint64_t nSleepTime)
static void sleepTick (uint32_t nSleepTime)
{
delayMicroseconds (nSleepTime * 1000);
}
Expand Down
6 changes: 3 additions & 3 deletions CorePartition/Resources/LowMemoryExample/LowMemoryExample.ino
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@ void __attribute__ ((noinline)) ShowRunningThreads ()
}


static uint64_t getTimeTick()
static uint32_t getTimeTick()
{
return (uint64_t) millis();
return (uint32_t) millis();
}

static void sleepTick (uint64_t nSleepTime)
static void sleepTick (uint32_t nSleepTime)
{
delay (nSleepTime);
//delayMicroseconds (nSleepTime * 1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ void TraficLightKernel (void* pValue)



static uint64_t getTimeTick()
static uint32_t getTimeTick()
{
return (uint64_t) millis();
return (uint32_t) millis();
}

static void sleepTick (uint64_t nSleepTime)
static void sleepTick (uint32_t nSleepTime)
{
delayMicroseconds ((nSleepTime + 1) * 1000);
}
Expand Down
6 changes: 3 additions & 3 deletions CorePartition/Resources/SmartTraficLight/SmartTraficLight.ino
Original file line number Diff line number Diff line change
Expand Up @@ -682,12 +682,12 @@ void TraficLightKernel (void* pValue)



static uint64_t getTimeTick()
static uint32_t getTimeTick()
{
return (uint64_t) millis();
return (uint32_t) millis();
}

static void sleepTick (uint64_t nSleepTime)
static void sleepTick (uint32_t nSleepTime)
{
delayMicroseconds ((nSleepTime + 1) * 1000);
}
Expand Down
6 changes: 3 additions & 3 deletions CorePartition/Resources/Thread/Thread.ino
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,12 @@ void Thread2 (void* pValue)



static uint64_t getTimeTick()
static uint32_t getTimeTick()
{
return (uint64_t) millis();
return (uint32_t) millis();
}

static void sleepTick (uint64_t nSleepTime)
static void sleepTick (uint32_t nSleepTime)
{
if (nSleepTime) delayMicroseconds (nSleepTime * 1000);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,12 +520,12 @@ void Thread5 (void* pValue)



static uint64_t getTimeTick()
static uint32_t getTimeTick()
{
return (uint64_t) millis();
return (uint32_t) millis();
}

static void sleepTick (uint64_t nSleepTime)
static void sleepTick (uint32_t nSleepTime)
{
delayMicroseconds (nSleepTime * 1000);
}
Expand Down
10 changes: 5 additions & 5 deletions CorePartition/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ void sleepUseconds(uint32_t nTime)
}


void Sleep (uint64_t nSleep)
void Sleep (uint32_t nSleep)
{
uint64_t nMomentum = getMiliseconds();
uint32_t nMomentum = getMiliseconds();

do {
//sleepUseconds (100000);
Expand Down Expand Up @@ -122,18 +122,18 @@ void Thread3 (void* pValue)



static void sleepMSTicks (uint64_t nSleepTime)
static void sleepMSTicks (uint32_t nSleepTime)
{
printf ("\n%s: sleeping [%llu]\n", __FUNCTION__, nSleepTime);
usleep ((useconds_t) nSleepTime * 1000);
}

static uint64_t getMsTicks(void)
static uint32_t getMsTicks(void)
{
struct timeval tp;
gettimeofday(&tp, NULL);

return tp.tv_sec * 1000 + tp.tv_usec / 1000; //get current timestamp in milliseconds
return (uint32_t) tp.tv_sec * 1000 + tp.tv_usec / 1000; //get current timestamp in milliseconds
}

static void StackOverflowHandler ()
Expand Down

0 comments on commit b2ea03f

Please sign in to comment.