Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions kernel/atom.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ typedef struct atom_tcb
ATOM_TIMER *suspend_timo_cb; /* Callback registered for suspension timeouts */
uint8_t terminated; /* TRUE if task is being terminated (run to completion) */

uint16_t events; /* Evend flags */
uint16_t waits; /* Wait flags */
/* Details used if thread stack-checking is required */
#ifdef ATOM_STACK_CHECKING
POINTER stack_bottom; /* Pointer to bottom of stack allocation */
Expand Down Expand Up @@ -108,6 +110,13 @@ extern uint8_t atomOSStarted;
/* Idle thread priority (lowest) */
#define IDLE_THREAD_PRIORITY 255

#ifndef EXPECTED_IDLE_TIME_BEFORE_SUSPEND
#define EXPECTED_IDLE_TIME_BEFORE_SUSPEND 2
#endif

#if EXPECTED_IDLE_TIME_BEFORE_SUSPEND < 2
#error EXPECTED_IDLE_TIME_BEFORE_SUSPEND must not be less than 2
#endif

/* Function prototypes */
extern uint8_t atomOSInit (void *idle_thread_stack_bottom, uint32_t idle_thread_stack_size, uint8_t idle_thread_stack_check);
Expand All @@ -133,6 +142,13 @@ extern void archThreadContextInit (ATOM_TCB *tcb_ptr, void *stack_top, void (*en
extern void archFirstThreadRestore(ATOM_TCB *new_tcb_ptr);

extern void atomTimerTick (void);
extern void archSystemTickTimerStop (void);
extern void archSystemTickTimerRestart (void);

extern uint32_t atomOsSuspend(void);
extern void atomOsResume (uint32_t sleep_ticks);

extern void userIdleHook(void);

#ifdef __cplusplus
}
Expand Down
89 changes: 88 additions & 1 deletion kernel/atomkernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,11 @@ uint8_t atomOSStarted = FALSE;
static ATOM_TCB *curr_tcb = NULL;

/** Storage for the idle thread's TCB */
#ifdef ATOM_STACK_CHECKING
ATOM_TCB idle_tcb;
#else
static ATOM_TCB idle_tcb;
#endif

/* Number of nested interrupts */
static int atomIntCnt = 0;
Expand Down Expand Up @@ -409,6 +413,8 @@ uint8_t atomThreadCreate (ATOM_TCB *tcb_ptr, uint8_t priority, void (*entry_poin
tcb_ptr->prev_tcb = NULL;
tcb_ptr->next_tcb = NULL;
tcb_ptr->suspend_timo_cb = NULL;
tcb_ptr->events = 0;
tcb_ptr->waits = 0;

/**
* Store the thread entry point and parameter in the TCB. This may
Expand Down Expand Up @@ -768,7 +774,7 @@ static void atomIdleThread (uint32_t param)
/* Loop forever */
while (1)
{
/** \todo Provide user idle hooks*/
userIdleHook();
}
}

Expand Down Expand Up @@ -1049,3 +1055,84 @@ ATOM_TCB *tcbDequeuePriority (ATOM_TCB **tcb_queue_ptr, uint8_t priority)

return (ret_ptr);
}


/**
* \b atomOsLock
*
* This is an internal function not for use by application code.
*
* Stop system timer to lock OS.
*
* @return None
*/
static void atomOsLock (void)
{
archSystemTickTimerStop();
atomOSStarted = FALSE;
}


/**
* \b atomOsUnlock
*
* This is an internal function not for use by application code.
*
* Restart system timer to unlock OS.
*
* @return None
*/
static void atomOsUnlock (void)
{
atomOSStarted = TRUE;
archSystemTickTimerRestart();
}


/**
* \b atomOsSuspend
*
* Suspend OS scheduler and return ticks count that OS can be suspended.
*
* @return Tick count that OS can be suspended
*/
uint32_t atomOsSuspend (void)
{
uint32_t delta = 0xffff;

atomOsLock();

delta = atomUserTimerWakeupTimeGet();

if (delta < EXPECTED_IDLE_TIME_BEFORE_SUSPEND)
{
delta = 0;
}

return delta;
}


/**
* \b atomOsResume
*
* Resume OS scheduler after suspend.
*
* @param[in] sleep_ticks ticks count that OS was suspended
*
* @return None
*/
void atomOsResume (uint32_t sleep_ticks)
{
uint32_t new_system_ticks, old_system_ticks;

old_system_ticks = atomTimeGet();

new_system_ticks = sleep_ticks + old_system_ticks;

atomUserTimerUpdate(sleep_ticks);
atomTimeSet(new_system_ticks);

atomOsUnlock();
}

Loading