Skip to content

Commit

Permalink
#451 sleep interface improved and first test cases added
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoVom committed Nov 23, 2016
1 parent 34581d0 commit fc2a91b
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 76 deletions.
2 changes: 1 addition & 1 deletion modules/base/inc/ciaaPOSIX_assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extern "C" {
while(1==1); \
}

/* UNITY_EXCLUDE_STDINT_H macro is used in Unit Test Enviroment */
/* CIAA_UNIT_TEST macro is used in Unit Test Enviroment */
#ifdef CIAA_UNIT_TEST
void ciaaPOSIX_assert(int expr);
#else
Expand Down
32 changes: 32 additions & 0 deletions modules/posix/inc/ciaaPOSIX_unistd_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ extern "C" {
#endif

/*==================[macros]=================================================*/
/* milisecond tick definition */
#define CIAAPOSIX_SLEEP_TICK_MS ((CIAAPOSIX_SLEEP_TICK)/1000)

/* sleep counts definition. They are 1000 counts per milisecond tick */
#define SLEEP_TIME_TO_COUNTS (1000 / CIAAPOSIX_SLEEP_TICK_MS)

/* maximum quantity of second supported */
#define MAX_SECONDS (MAX_COUNTS / SLEEP_TIME_TO_COUNTS)

/* maximum quantity of usecond supported */
#define MAX_USECONDS (UINT32_MAX - (CIAAPOSIX_SLEEP_TICK-2))

/* Position of the state bit that indicates if the task is sleeping or not*/
#define STATE_BIT 31

Expand Down Expand Up @@ -116,6 +128,26 @@ typedef struct ciaaPOSIX_counter_type
/*==================[external data declaration]==============================*/

/*==================[external functions declaration]=========================*/
/** \brief ciaaPOSIX_sleepAddTask
**
** Add our task to sleeping list and reload the global counter if required
**
** \param[in] toSleep counts of calls to ciaaPOSIX_sleepMainFunction
** to sleep the calling task
**
**
** \remarks shall be called with POSIXR ocupides since access global variables
**/
extern void ciaaPOSIX_sleepAddTask(uint32_t toSleep);

/** \brief ciaaPOSIX_sleepRemTask
**
** Remove our task of the sleeping ones.
**
** \returns rest time before expiration in counts of calls to
** ciaaPOSIX_sleepMainFunction.
**/
extern uint32_t ciaaPOSIX_sleepRemTask(void);

/*==================[cplusplus]==============================================*/
#ifdef __cplusplus
Expand Down
32 changes: 5 additions & 27 deletions modules/posix/inc/ciaaPOSIX_unistd_nonstd.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,43 +53,21 @@ extern "C" {
#endif

/*==================[macros]=================================================*/
#define CIAAPOSIX_MAINFUNCTION_PERIODUS 10000
#define CIAAPOSIX_SLEEP_TICK 10000

#define CIAAPOSIX_TIMEOUT 0xFFFFFFFFU
#define CIAAPOSIX_TIMEOUT 0xFFFFFFFFU

/*==================[typedef]================================================*/

/*==================[external data declaration]==============================*/

/*==================[external functions declaration]=========================*/
/** \brief ciaaPOSIX_sleepAlgorithm
/** \brief ciaaPOSIX sleep tick function
**
** Sleeps the current task and reload the global counter if required
**
** \param[in] toSleep counts of calls to ciaaPOSIX_sleepMainFunction
** to sleep the calling task
**
**
** \remarks shall be called with POSIXR ocupides since access global variables
**/
extern void ciaaPOSIX_sleepAlgorithm(uint32_t toSleep);

/** \brief ciaaPOSIX_sleepRemove
**
** Remove our task of the sleeping ones.
**
** \returns rest time before expiration in counts of calls to
** ciaaPOSIX_sleepMainFunction.
**/
extern uint32_t ciaaPOSIX_sleepRemove(void);

/** \brief ciaaPOSIX sleep main function
**
** Sleep main funciton, this function shall be called every
** CIAAPOSIX_MAINFUNCTION_PERIODUS micro seconds.
** this function shall be called every CIAAPOSIX_SLEEP_TICK micro seconds.
**
**/
extern void ciaaPOSIX_sleepMainFunction(void);
extern void ciaaPOSIX_sleepTick(void);

/*==================[cplusplus]==============================================*/
#ifdef __cplusplus
Expand Down
8 changes: 4 additions & 4 deletions modules/posix/src/ciaaPOSIX_semaphore.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#include "ciaaPOSIX_stdlib.h"
#include "ciaaPOSIX_assert.h"
#include "Os_Internal.h"
#include "ciaaPOSIX_unistd_nonstd.h"
#include "ciaaPOSIX_unistd_Internal.h"

/*==================[macros and definitions]=================================*/

Expand Down Expand Up @@ -121,11 +121,11 @@ extern uint32_t ciaaPOSIX_sem_waitfor(sem_t * const sem, uint32_t timeout)
timeout = 1000 * timeout;

/* calculate how many main function cycles shall be sleep */
toSleep = (timeout + (CIAAPOSIX_MAINFUNCTION_PERIODUS-1))
/ CIAAPOSIX_MAINFUNCTION_PERIODUS;
toSleep = (timeout + (CIAAPOSIX_SLEEP_TICK-1))
/ CIAAPOSIX_SLEEP_TICK;

/* call sleep algorithm */
ciaaPOSIX_sleepAlgorithm(toSleep);
ciaaPOSIX_sleepAddTask(toSleep);

ReleaseResource(POSIXR);
WaitEvent(POSIXE);
Expand Down
21 changes: 7 additions & 14 deletions modules/posix/src/ciaaPOSIX_unistd.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@
#include "Os_Internal.h"

/*==================[macros and definitions]=================================*/
#define CIAAPOSIX_MAINFUNCTION_PERIODMS ((CIAAPOSIX_MAINFUNCTION_PERIODUS)/1000)

#define SLEEP_TIME_TO_COUNTS (1000 / CIAAPOSIX_MAINFUNCTION_PERIODMS)

#define MAX_SECONDS (MAX_COUNTS / SLEEP_TIME_TO_COUNTS)

#define MAX_USECONDS (UINT32_MAX - (CIAAPOSIX_MAINFUNCTION_PERIODUS-2))

/*==================[internal data declaration]==============================*/
/** \brief Remaining counts to wake up the next task */
Expand Down Expand Up @@ -90,7 +83,7 @@ extern uint32_t ciaaPOSIX_sleep(uint32_t seconds)

GetResource(POSIXR);
/* sleep time processing*/
ciaaPOSIX_sleepAlgorithm(toSleep);
ciaaPOSIX_sleepAddTask(toSleep);
ReleaseResource(POSIXR);

WaitEvent(POSIXR);
Expand All @@ -115,11 +108,11 @@ extern int32_t ciaaPOSIX_usleep(useconds_t useconds)
if(MAX_USECONDS > useconds)
{
/* calculate how many main function cycles shall be sleep */
toSleep = (useconds + (CIAAPOSIX_MAINFUNCTION_PERIODUS-1))
/ CIAAPOSIX_MAINFUNCTION_PERIODUS;
toSleep = (useconds + (CIAAPOSIX_SLEEP_TICK-1))
/ CIAAPOSIX_SLEEP_TICK;

/* sleep time processing */
ciaaPOSIX_sleepAlgorithm(toSleep);
ciaaPOSIX_sleepAddTask(toSleep);

WaitEvent(POSIXR);
ClearEvent(POSIXR);
Expand All @@ -133,7 +126,7 @@ extern int32_t ciaaPOSIX_usleep(useconds_t useconds)
return ret;
}

extern void ciaaPOSIX_sleepAlgorithm(uint32_t toSleep)
extern void ciaaPOSIX_sleepAddTask(uint32_t toSleep)
{
TaskType taskID;

Expand Down Expand Up @@ -163,7 +156,7 @@ extern void ciaaPOSIX_sleepAlgorithm(uint32_t toSleep)
ciaaPOSIX_sleeps[taskID].counter = (toSleep + ciaaPOSIX_counter.counter);
}

extern uint32_t ciaaPOSIX_sleepRemove(void)
extern uint32_t ciaaPOSIX_sleepRemTask(void)
{
TaskType taskID;

Expand All @@ -175,7 +168,7 @@ extern uint32_t ciaaPOSIX_sleepRemove(void)
return ciaaPOSIX_sleeps[taskID].counter;
}

extern void ciaaPOSIX_sleepMainFunction(void)
extern void ciaaPOSIX_sleepTick(void)
{
uint32_t taskID;
uint32_t aux_distance = MAX_COUNTS;
Expand Down
Loading

0 comments on commit fc2a91b

Please sign in to comment.