Developer World
Spresense SDK Library v3.3.0-375c679
chateau_osal.h
1/****************************************************************************
2 * modules/include/memutils/os_utils/chateau_osal.h
3 *
4 * Copyright 2018, 2023 Sony Semiconductor Solutions Corporation
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 * 3. Neither the name of Sony Semiconductor Solutions Corporation nor
17 * the names of its contributors may be used to endorse or promote
18 * products derived from this software without specific prior written
19 * permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 ****************************************************************************/
35
36#ifndef CHATEAU_OSAL_H_INCLUDED
37#define CHATEAU_OSAL_H_INCLUDED
38
39#include "memutils/common_utils/common_macro.h"
40#include "memutils/common_utils/common_assert.h"
41
42/* TODO(Ohashi): Chateau の下記ヘッダに置き換えたい */
43/* #include "chateau_macro.h"
44 #include "chateau_assert.h"
45 #include "chateau_types.h" */
46
47#if defined(_ITRON4)
48#if defined(_MERLOT)
49#include "kernel.h"
50#else /* _MERLOT */
51#include "itron.h"
52#include "kernel_itron.h"
53#include "kernel_SailOS.h"
54#endif /* _MERLOT */
55
56#define TIME_POLLING TMO_POL
57#define TIME_FOREVER (unsigned)TMO_FEVR
58
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; /* ITRONは、Task ID = Taskハンドル */
63typedef PRI Chateau_task_pri_t;
64
65/* Task */
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) {
69 T_CTSK cfg;
70 cfg.tskatr = TA_HLNG | TA_ACT;
71 cfg.exinf = (VP_INT)arg;
72 cfg.task = (FP)func;
73 cfg.itskpri = pri;
74 cfg.stksz = stack_size;
75 cfg.stk = NULL;
76 ID tid = acre_tsk(&cfg);
77 F_ASSERT(tid > 0);
78 return tid;
79}
80
81static INLINE Chateau_task_id_t Chateau_GetTaskId() {
82 Chateau_task_id_t tid = 0;
83 (void)get_tid(&tid); /* 割込み禁止状態で呼ぶとエラー */
84 return tid;
85}
86
87static INLINE Chateau_task_handle_t Chateau_GetTaskHandle() {
88 return Chateau_GetTaskId();
89}
90
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;
93}
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())
101
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)
110
111/* Semaphore */
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;
124}
125
126/* Cyclic */
127#define Chateau_StartCyclicHandler(h) F_ASSERT(sta_cyc(h) == E_OK)
128#define Chateau_StopCyclicHandler(h) F_ASSERT(stp_cyc(h) == E_OK)
129
130#elif defined(_POSIX)
131#include <sys/types.h>
132#include <unistd.h>
133#include <sched.h>
134#include <semaphore.h>
135#include <time.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)
142
143#define Chateau_GetInterruptMask() (0)
144#define Chateau_IsTaskContext() (getpid() != 0)
145
146#define Chateau_LockInterrupt(pContext) \
147 do { \
148 up_irq_disable(); \
149 sched_lock(); \
150 (void)pContext; \
151 } while(0)
152#define Chateau_LockInterruptIsr(pContext) \
153 do { \
154 up_irq_disable(); \
155 sched_lock(); \
156 (void)pContext; \
157 } while(0)
158#define Chateau_UnlockInterrupt(pContext) \
159 do { \
160 sched_unlock(); \
161 up_irq_enable(); \
162 (void)pContext; \
163 } while(0)
164#define Chateau_UnlockInterruptIsr(pContext) \
165 do { \
166 sched_unlock(); \
167 up_irq_enable(); \
168 (void)pContext; \
169 } while(0)
170
171/* 取り敢えず仮実装 */
172#define TIME_FOREVER (unsigned)TMO_FEVR
173typedef sem_t Chateau_sem_handle_t;
174#define Chateau_CreateSemaphore(pH, ini, max) \
175 do { \
176 int ret = sem_init(pH, ini, max); \
177 F_ASSERT(ret == 0); \
178 } while (0)
179#define Chateau_DeleteSemaphore(h) \
180 do { \
181 int ret = sem_destroy(&h); \
182 F_ASSERT(ret == 0); \
183 } while (0)
184#define Chateau_SignalSemaphore(h) \
185 do { \
186 int ret = sem_post(&h); \
187 F_ASSERT(ret == 0); \
188 } while (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)
193
194#elif defined(NO_OS)
195/* 取り敢えず仮実装 */
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)
207#else
208#error "Unkown OS"
209#endif
210#endif /* CHATEAU_OSAL_H_INCLUDED */