29 #if !defined(PIOS_INCLUDE_CHIBIOS)
30 #error "pios_thread.c requires PIOS_INCLUDE_CHIBIOS"
33 #if defined(PIOS_INCLUDE_CHIBIOS)
40 #define CVT_MS2ST(msec) (msec)
41 #define CVT_ST2MS(n) (n)
44 #define CVT_MS2ST(msec) ((systime_t)(((((uint32_t)(msec)) * ((uint64_t)CH_FREQUENCY) - 1UL) / 1000UL) + 1UL))
45 #define CVT_ST2MS(n) (((((n) - 1ULL) * 1000ULL) / ((uint64_t)CH_FREQUENCY)) + 1UL)
52 static uint32_t ceil_size(uint32_t size)
54 const uint32_t a =
sizeof(stkalign_t);
55 size = size + (a - size % a);
64 static uint8_t * align8_alloc(uint32_t size)
69 size = size +
sizeof(uintptr_t);
73 uint32_t
pad = ((uint32_t) wap) %
sizeof(stkalign_t);
91 thread->threadp = chThdSelf();
93 thread->threadp->p_name = namep;
107 chThdSetPriority(prio);
130 stack_bytes = ceil_size(stack_bytes);
131 uint8_t *wap = align8_alloc(stack_bytes);
138 thread->threadp = chThdCreateStatic(wap, stack_bytes, prio, (msg_t (*)(
void *))fp, argp);
139 if (thread->threadp == NULL)
147 thread->threadp->p_name = namep;
153 #if (CH_USE_WAITEXIT == TRUE)
169 chThdTerminate(threadp->threadp);
170 chThdWait(threadp->threadp);
174 #error "PIOS_Thread_Delete requires CH_USE_WAITEXIT to be defined TRUE"
186 return (uint32_t)CVT_ST2MS(chTimeNow());
199 chThdSleep(TIME_INFINITE);
201 chThdSleep(MS2ST(time_ms));
216 *previous_ms += increment_ms;
218 systime_t future = CVT_MS2ST(*previous_ms);
219 systime_t increment_st = CVT_MS2ST(increment_ms);
223 systime_t now = chTimeNow();
224 systime_t sleep_time = future - now;
226 if (sleep_time > increment_st) {
232 if ((now - future) > increment_st) {
236 *previous_ms = CVT_ST2MS(now);
238 }
else if (sleep_time > 0) {
241 chThdSleepS(sleep_time);
250 return CVT_MS2ST(increment_ms) <= chTimeElapsedSince(CVT_MS2ST(prev_systime));
263 #if CH_DBG_FILL_THREADS
264 uint32_t *stack = (uint32_t*)((
size_t)threadp->threadp +
sizeof(*threadp->threadp));
265 uint32_t *stklimit = stack;
267 ((CH_STACK_FILL_VALUE << 24) |
268 (CH_STACK_FILL_VALUE << 16) |
269 (CH_STACK_FILL_VALUE << 8) |
270 (CH_STACK_FILL_VALUE << 0)))
272 return (stack - stklimit) * 4;
291 uint32_t result = threadp->threadp->ticks_total;
292 threadp->threadp->ticks_total = 0;
uint32_t PIOS_Thread_Systime(void)
Main PiOS header to include all the compiled in PiOS options.
void PIOS_Thread_Scheduler_Suspend(void)
#define PIOS_THREAD_TIMEOUT_MAX
void * PIOS_malloc(size_t size)
void * PIOS_malloc_no_dma(size_t size)
#define CH_FREQUENCY
System tick frequency.
void PIOS_Thread_ChangePriority(enum pios_thread_prio_e prio)
struct pios_thread * PIOS_Thread_Create(void(*fp)(void *), const char *namep, size_t stack_bytes, void *argp, enum pios_thread_prio_e prio)
bool PIOS_Thread_Period_Elapsed(const uint32_t prev_systime, const uint32_t increment_ms)
Determine if a period has elapsed since a datum.
void PIOS_Thread_Sleep_Until(uint32_t *previous_ms, uint32_t increment_ms)
void PIOS_free(void *buf)
void PIOS_Thread_Sleep(uint32_t time_ms)
uint32_t PIOS_Thread_Get_Stack_Usage(struct pios_thread *threadp)
#define DONT_BUILD_IF(COND, MSG)
uint32_t PIOS_Thread_Get_Runtime(struct pios_thread *threadp)
void PIOS_Thread_Scheduler_Resume(void)
void PIOS_Thread_Delete(struct pios_thread *threadp)
struct pios_thread * PIOS_Thread_WrapCurrentThread(const char *namep)
Creates a handle for the current thread.