36#ifndef CHATEAU_OSAL_H_INCLUDED
37#define CHATEAU_OSAL_H_INCLUDED
39#include "memutils/common_utils/common_macro.h"
40#include "memutils/common_utils/common_assert.h"
52#include "kernel_itron.h"
53#include "kernel_SailOS.h"
56#define TIME_POLLING TMO_POL
57#define TIME_FOREVER (unsigned)TMO_FEVR
59typedef ID Chateau_sem_handle_t;
60typedef void (*Chateau_task_func_t)(
void *arg);
61typedef ID Chateau_task_id_t;
62typedef Chateau_task_id_t Chateau_task_handle_t;
63typedef PRI Chateau_task_pri_t;
66static INLINE Chateau_task_handle_t Chateau_CreateTask(Chateau_task_func_t func,
67 void* arg,
size_t stack_size,
68 Chateau_task_pri_t pri) {
70 cfg.tskatr = TA_HLNG | TA_ACT;
71 cfg.exinf = (VP_INT)arg;
74 cfg.stksz = stack_size;
76 ID tid = acre_tsk(&cfg);
81static INLINE Chateau_task_id_t Chateau_GetTaskId() {
82 Chateau_task_id_t tid = 0;
87static INLINE Chateau_task_handle_t Chateau_GetTaskHandle() {
88 return Chateau_GetTaskId();
91static INLINE Chateau_task_pri_t Chateau_GetTaskPriority() {
92 Chateau_task_pri_t pri; F_ASSERT(get_pri(TSK_SELF, &pri) == E_OK);
return pri;
94#define Chateau_SetTaskPriority(pri) F_ASSERT(chg_pri(TSK_SELF, pri) == E_OK)
95#define Chateau_SuspendTask(h) F_ASSERT(sus_tsk(h) == E_OK)
96#define Chateau_ResumeTask(h) F_ASSERT(rsm_tsk(h) == E_OK)
97#define Chateau_SleepTask(ms) tslp_tsk(ms)
98#define Chateau_DelayTask(ms) dly_tsk(ms)
99#define Chateau_YieldTask() F_ASSERT(rot_rdq(TPRI_SELF) == E_OK)
100#define Chateau_IsTaskContext() (!sns_ctx())
102#define Chateau_GetSystemTime(n) do{ SYSTIM s = 0; get_tim(&s); (n) = (unsigned)s; }while(0)
103#define Chateau_GetInterruptMask() sns_loc()
104#define Chateau_LockInterrupt(pContext) loc_cpu()
105#define Chateau_LockInterruptIsr(pContext) iloc_cpu()
106#define Chateau_UnlockInterrupt(pContext) unl_cpu()
107#define Chateau_UnlockInterruptIsr(pContext) iunl_cpu()
108#define Chateau_EnableInterrupt(intno) ena_int(intno)
109#define Chateau_DisableInterrupt(intno) dis_int(intno)
112#define Chateau_CreateSemaphore(pH, ini, max) \
113 do{ T_CSEM _cfg_ = { TA_TFIFO, ini, max }; \
114 F_ASSERT((*pH = acre_sem(&_cfg_)) > 0); }while(0)
115#define Chateau_DeleteSemaphore(h) F_ASSERT(del_sem(h) == E_OK)
116#define Chateau_WaitSemaphore(h) F_ASSERT(wai_sem(h) == E_OK)
117#define Chateau_PollingWaitSemaphore(h) (pol_sem(h) == E_OK)
118#define Chateau_TimedWaitSemaphore(h, ms) (twai_sem(h, (ms)) == E_OK)
119#define Chateau_SignalSemaphore(h) (Chateau_IsTaskContext() ? Chateau_SignalSemaphoreTask(h) : Chateau_SignalSemaphoreIsr(h))
120#define Chateau_SignalSemaphoreTask(h) F_ASSERT(sig_sem(h) == E_OK)
121#define Chateau_SignalSemaphoreIsr(h) F_ASSERT(isig_sem(h) == E_OK)
122static INLINE
size_t Chateau_CountSemaphore(Chateau_sem_handle_t h) {
123 T_RSEM s; F_ASSERT(ref_sem(h, &s) == E_OK);
return s.semcnt;
127#define Chateau_StartCyclicHandler(h) F_ASSERT(sta_cyc(h) == E_OK)
128#define Chateau_StopCyclicHandler(h) F_ASSERT(stp_cyc(h) == E_OK)
131#include <sys/types.h>
134#include <semaphore.h>
136#include <nuttx/arch.h>
137#include <nuttx/semaphore.h>
138#include "memutils/os_utils/os_wrapper.h"
139#define Chateau_DelayTask(ms) (usleep((ms)*1000))
140#define Chateau_EnableInterrupt(irq) up_enable_irq(irq)
141#define Chateau_DisableInterrupt(irq) up_disable_irq(irq)
143#define Chateau_GetInterruptMask() (0)
144#define Chateau_IsTaskContext() (getpid() != 0)
146#define Chateau_LockInterrupt(pContext) \
152#define Chateau_LockInterruptIsr(pContext) \
158#define Chateau_UnlockInterrupt(pContext) \
164#define Chateau_UnlockInterruptIsr(pContext) \
172#define TIME_FOREVER (unsigned)TMO_FEVR
173typedef sem_t Chateau_sem_handle_t;
174#define Chateau_CreateSemaphore(pH, ini, max) \
176 int ret = sem_init(pH, ini, max); \
177 F_ASSERT(ret == 0); \
179#define Chateau_DeleteSemaphore(h) \
181 int ret = sem_destroy(&h); \
182 F_ASSERT(ret == 0); \
184#define Chateau_SignalSemaphore(h) \
186 int ret = sem_post(&h); \
187 F_ASSERT(ret == 0); \
189#define Chateau_SignalSemaphoreTask(h) Chateau_SignalSemaphore(h)
190#define Chateau_SignalSemaphoreIsr(h) Chateau_SignalSemaphore(h)
191#define Chateau_TimedWaitSemaphore(h, tm) (nxsem_timedwait_uninterruptible(&h, &tm) == 0)
192#define Chateau_WaitSemaphore(h) (nxsem_wait_uninterruptible(&h) == 0)
196typedef short Chateau_sem_handle_t;
197#include "arm_nosys.h"
198#define Chateau_LockInterruptIsr(pContext) loc_cpu()
199#define Chateau_UnlockInterruptIsr(pContext) unl_cpu()
200#define Chateau_IsTaskContext() 1
201#define Chateau_GetInterruptMask() sns_loc()
202#define Chateau_LockInterrupt(h) loc_cpu()
203#define Chateau_UnlockInterrupt(h) unl_cpu()
204#define Chateau_SleepTask(ms) \
205 do{ volatile unsigned _n_ = (ms) * CYCLE_PER_MS; while (_n_--) ; }while(0)
206#define Chateau_DelayTask(ms) Chateau_SleepTask(ms)