Fix typo.
[Rockbox.git] / firmware / export / kernel.h
blob8d7ca951846d057593f1613739d7ddda4e288d87
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Björn Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #ifndef _KERNEL_H_
20 #define _KERNEL_H_
22 #include <stdbool.h>
23 #include <inttypes.h>
24 #include "config.h"
26 /* wrap-safe macros for tick comparison */
27 #define TIME_AFTER(a,b) ((long)(b) - (long)(a) < 0)
28 #define TIME_BEFORE(a,b) TIME_AFTER(b,a)
30 #define HZ 100 /* number of ticks per second */
32 #define MAX_NUM_TICK_TASKS 8
34 #define QUEUE_LENGTH 16 /* MUST be a power of 2 */
35 #define QUEUE_LENGTH_MASK (QUEUE_LENGTH - 1)
37 /* System defined message ID's - |sign bit = 1|class|id| */
38 /* Event class list */
39 #define SYS_EVENT_CLS_QUEUE 0
40 #define SYS_EVENT_CLS_USB 1
41 #define SYS_EVENT_CLS_POWER 2
42 #define SYS_EVENT_CLS_FILESYS 3
43 #define SYS_EVENT_CLS_PLUG 4
44 #define SYS_EVENT_CLS_MISC 5
45 /* make sure SYS_EVENT_CLS_BITS has enough range */
47 /* Bit 31->|S|c...c|i...i| */
48 #define SYS_EVENT ((long)(int)(1 << 31))
49 #define SYS_EVENT_CLS_BITS (3)
50 #define SYS_EVENT_CLS_SHIFT (31-SYS_EVENT_CLS_BITS)
51 #define SYS_EVENT_CLS_MASK (((1l << SYS_EVENT_CLS_BITS)-1) << SYS_EVENT_SHIFT)
52 #define MAKE_SYS_EVENT(cls, id) (SYS_EVENT | ((long)(cls) << SYS_EVENT_CLS_SHIFT) | (long)(id))
53 /* Macros for extracting codes */
54 #define SYS_EVENT_CLS(e) (((e) & SYS_EVENT_CLS_MASK) >> SYS_EVENT_SHIFT)
55 #define SYS_EVENT_ID(e) ((e) & ~(SYS_EVENT|SYS_EVENT_CLS_MASK))
57 #define SYS_TIMEOUT MAKE_SYS_EVENT(SYS_EVENT_CLS_QUEUE, 0)
58 #define SYS_USB_CONNECTED MAKE_SYS_EVENT(SYS_EVENT_CLS_USB, 0)
59 #define SYS_USB_CONNECTED_ACK MAKE_SYS_EVENT(SYS_EVENT_CLS_USB, 1)
60 #define SYS_USB_DISCONNECTED MAKE_SYS_EVENT(SYS_EVENT_CLS_USB, 2)
61 #define SYS_USB_DISCONNECTED_ACK MAKE_SYS_EVENT(SYS_EVENT_CLS_USB, 3)
62 #define SYS_POWEROFF MAKE_SYS_EVENT(SYS_EVENT_CLS_POWER, 0)
63 #define SYS_CHARGER_CONNECTED MAKE_SYS_EVENT(SYS_EVENT_CLS_POWER, 1)
64 #define SYS_CHARGER_DISCONNECTED MAKE_SYS_EVENT(SYS_EVENT_CLS_POWER, 2)
65 #define SYS_FS_CHANGED MAKE_SYS_EVENT(SYS_EVENT_CLS_FILESYS, 0)
66 #define SYS_HOTSWAP_INSERTED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 0)
67 #define SYS_HOTSWAP_EXTRACTED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 1)
68 #define SYS_PHONE_PLUGGED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 2)
69 #define SYS_PHONE_UNPLUGGED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 3)
70 #define SYS_REMOTE_PLUGGED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 4)
71 #define SYS_REMOTE_UNPLUGGED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 5)
72 #define SYS_SCREENDUMP MAKE_SYS_EVENT(SYS_EVENT_CLS_MISC, 0)
74 struct event
76 long id;
77 intptr_t data;
80 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
81 struct queue_sender_list
83 /* If non-NULL, there is a thread waiting for the corresponding event */
84 /* Must be statically allocated to put in non-cached ram. */
85 struct thread_entry *senders[QUEUE_LENGTH];
86 /* Send info for last message dequeued or NULL if replied or not sent */
87 struct thread_entry *curr_sender;
89 #endif /* HAVE_EXTENDED_MESSAGING_AND_NAME */
91 struct event_queue
93 struct event events[QUEUE_LENGTH];
94 struct thread_entry *thread;
95 unsigned int read;
96 unsigned int write;
97 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
98 struct queue_sender_list *send;
99 #endif
102 struct mutex
104 uint32_t locked;
105 struct thread_entry *thread;
108 /* global tick variable */
109 #if defined(CPU_PP) && defined(BOOTLOADER)
110 /* We don't enable interrupts in the iPod bootloader, so we need to fake
111 the current_tick variable */
112 #define current_tick (signed)(USEC_TIMER/10000)
113 #elif (CONFIG_CPU == IMX31L) && defined(BOOTLOADER)
114 #define current_tick (signed)((0xFFFFFFFF - EPITCNT1)/10000)
115 #else
116 extern volatile long current_tick;
117 #endif
119 #ifdef SIMULATOR
120 #define sleep(x) sim_sleep(x)
121 #endif
123 /* kernel functions */
124 extern void kernel_init(void);
125 extern void yield(void);
126 extern void sleep(int ticks);
127 int tick_add_task(void (*f)(void));
128 int tick_remove_task(void (*f)(void));
130 struct timeout;
132 /* timeout callback type
133 * tmo - pointer to struct timeout associated with event
135 typedef bool (* timeout_cb_type)(struct timeout *tmo);
137 struct timeout
139 /* for use by callback/internal - read/write */
140 timeout_cb_type callback;/* callback - returning false cancels */
141 int ticks; /* timeout period in ticks */
142 intptr_t data; /* data passed to callback */
143 /* internal use - read-only */
144 const struct timeout * const next; /* next timeout in list */
145 const long expires; /* expiration tick */
148 void timeout_register(struct timeout *tmo, timeout_cb_type callback,
149 int ticks, intptr_t data);
150 void timeout_cancel(struct timeout *tmo);
152 extern void queue_init(struct event_queue *q, bool register_queue);
153 extern void queue_delete(struct event_queue *q);
154 extern void queue_wait(struct event_queue *q, struct event *ev);
155 extern void queue_wait_w_tmo(struct event_queue *q, struct event *ev, int ticks);
156 extern void queue_post(struct event_queue *q, long id, intptr_t data);
157 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
158 extern void queue_enable_queue_send(struct event_queue *q, struct queue_sender_list *send);
159 extern intptr_t queue_send(struct event_queue *q, long id, intptr_t data);
160 extern void queue_reply(struct event_queue *q, intptr_t retval);
161 extern bool queue_in_queue_send(struct event_queue *q);
162 #endif /* HAVE_EXTENDED_MESSAGING_AND_NAME */
163 extern bool queue_empty(const struct event_queue* q);
164 extern void queue_clear(struct event_queue* q);
165 extern void queue_remove_from_head(struct event_queue *q, long id);
166 extern int queue_count(const struct event_queue *q);
167 extern int queue_broadcast(long id, intptr_t data);
169 extern void mutex_init(struct mutex *m);
170 static inline void spinlock_init(struct mutex *m)
171 { mutex_init(m); } /* Same thing for now */
172 extern void mutex_lock(struct mutex *m);
173 extern void mutex_unlock(struct mutex *m);
174 extern void spinlock_lock(struct mutex *m);
175 extern void spinlock_unlock(struct mutex *m);
176 extern void tick_start(unsigned int interval_in_ms);
178 #define IS_SYSEVENT(ev) ((ev & SYS_EVENT) == SYS_EVENT)
180 #endif