Battery blinks if >BATTERY_LEVEL_DANGEROUS
[kugel-rb.git] / firmware / system.h
blob28c33d6c198ef6f6c5687f24f4e81b7103cb6185
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Alan Korr
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 ****************************************************************************/
20 #ifndef __SYSTEM_H__
21 #define __SYSTEM_H__
23 #include "sh7034.h"
24 #include "config.h"
26 #ifdef ARCHOS_RECORDER
27 # define FREQ 11059200
28 #else
29 # define FREQ 12000000 /* cycle time ~83.3ns */
30 #endif
31 #define BAUDRATE 9600
33 #ifndef NULL
34 #define NULL ((void*)0)
35 #endif
37 #ifndef MIN
38 #define MIN(a, b) (((a)<(b))?(a):(b))
39 #endif
41 #ifndef MAX
42 #define MAX(a, b) (((a)>(b))?(a):(b))
43 #endif
45 #ifdef LITTLE_ENDIAN
46 #define SWAB16(x) (x)
47 #define SWAB32(x) (x)
48 #endif
50 #define nop \
51 asm volatile ("nop")
53 #define __set_mask_constant(mask,address) \
54 asm \
55 ("or.b\t%0,@(r0,gbr)" \
56 : \
57 : /* %0 */ "I"((char)(mask)), \
58 /* %1 */ "z"(address-GBR))
60 #define __clear_mask_constant(mask,address) \
61 asm \
62 ("and.b\t%0,@(r0,gbr)" \
63 : \
64 : /* %0 */ "I"((char)~(mask)), \
65 /* %1 */ "z"(address-GBR))
67 #define __toggle_mask_constant(mask,address) \
68 asm \
69 ("xor.b\t%0,@(r0,gbr)" \
70 : \
71 : /* %0 */ "I"((char)(mask)), \
72 /* %1 */ "z"(address-GBR))
74 #define __test_mask_constant(mask,address) \
75 ({ \
76 int result; \
77 asm \
78 ("tst.b\t%1,@(r0,gbr)\n\tmovt\t%0" \
79 : "=r"(result) \
80 : "I"((char)(mask)),"z"(address-GBR)); \
81 result; \
84 #define __set_bit_constant(bit,address) \
85 asm \
86 ("or.b\t%0,@(r0,gbr)" \
87 : \
88 : /* %0 */ "I"((char)(1<<(bit))), \
89 /* %1 */ "z"(address-GBR))
91 #define __clear_bit_constant(bit,address) \
92 asm \
93 ("and.b\t%0,@(r0,gbr)" \
94 : \
95 : /* %0 */ "I"((char)~(1<<(bit))), \
96 /* %1 */ "z"(address-GBR))
98 #define __toggle_bit_constant(bit,address) \
99 asm \
100 ("xor.b\t%0,@(r0,gbr)" \
102 : /* %0 */ "I"((char)(1<<(bit))), \
103 /* %1 */ "z"(address-GBR))
105 #define __test_bit_constant(bit,address) \
106 ({ \
107 int result; \
108 asm \
109 ("tst.b\t%1,@(r0,gbr)\n\tmovt\t%0" \
110 : "=r"(result) \
111 : "I"((char)(1<<(bit))),"z"(address-GBR)); \
112 result; \
115 #define __set_mask(mask,address) /* FIXME */
116 #define __test_mask(mask,address) 0 /* FIXME */
117 #define __clear_mask(mask,address) /* FIXME */
118 #define __toggle_mask(mask,address) /* FIXME */
120 #define __set_bit(bit,address) /* FIXME */
121 #define __test_bit(bit,address) 0 /* FIXME */
122 #define __clear_bit(bit,address) /* FIXME */
123 #define __toggle_bit(bit,address) /* FIXME */
125 #define set_mask(mask,address) \
126 if (__builtin_constant_p (mask)) \
127 __set_mask_constant (mask,address); \
128 else \
129 __set_mask (mask,address)
131 #define clear_mask(mask,address) \
132 if (__builtin_constant_p (mask)) \
133 __clear_mask_constant (mask,address); \
134 else \
135 __clear_mask (mask,address)
137 #define toggle_mask(mask,address) \
138 if (__builtin_constant_p (mask)) \
139 __toggle_mask_constant (mask,address); \
140 else \
141 __toggle_mask (mask,address)
143 #define test_mask(mask,address) \
145 (__builtin_constant_p (mask)) \
146 ? (int)__test_mask_constant (mask,address) \
147 : (int)__test_mask (mask,address) \
151 #define set_bit(bit,address) \
152 if (__builtin_constant_p (bit)) \
153 __set_bit_constant (bit,address); \
154 else \
155 __set_bit (bit,address)
157 #define clear_bit(bit,address) \
158 if (__builtin_constant_p (bit)) \
159 __clear_bit_constant (bit,address); \
160 else \
161 __clear_bit (bit,address)
163 #define toggle_bit(bit,address) \
164 if (__builtin_constant_p (bit)) \
165 __toggle_bit_constant (bit,address); \
166 else \
167 __toggle_bit (bit,address)
169 #define test_bit(bit,address) \
171 (__builtin_constant_p (bit)) \
172 ? (int)__test_bit_constant (bit,address) \
173 : (int)__test_bit (bit,address) \
177 extern char __swap_bit[256];
179 #define swap_bit(byte) \
180 __swap_bit[byte]
182 #ifndef SIMULATOR
184 static inline short SWAB16(short value)
186 result[15..8] = value[ 7..0];
187 result[ 7..0] = value[15..8];
190 short result;
191 asm volatile ("swap.b\t%1,%0" : "=r"(result) : "r"(value));
192 return result;
195 static inline long SWAW32(long value)
197 result[31..16] = value[15.. 0];
198 result[15.. 0] = value[31..16];
201 long result;
202 asm volatile ("swap.w\t%1,%0" : "=r"(result) : "r"(value));
203 return result;
206 static inline long SWAB32(long value)
208 result[31..24] = value[ 7.. 0];
209 result[23..16] = value[15.. 8];
210 result[15.. 8] = value[23..16];
211 result[ 7.. 0] = value[31..24];
214 asm volatile ("swap.b\t%0,%0\n"
215 "swap.w\t%0,%0\n"
216 "swap.b\t%0,%0\n" : "+r"(value));
217 return value;
220 /* Test And Set - UNTESTED */
221 static inline int tas (volatile int *pointer)
223 int result;
224 asm volatile ("tas.b\t@%1;movt\t%0" : "=t"(result) : "r"((char *)pointer) : "memory");
225 return result;
228 static inline void sti (void)
230 asm volatile ("ldc\t%0,sr" : : "r"(0<<4));
233 static inline void cli (void)
235 asm volatile ("ldc\t%0,sr" : : "r"(15<<4));
238 /* Compare And Swap */
239 static inline int cas (volatile int *pointer,int requested_value,int new_value)
241 cli();
242 if (*pointer == requested_value)
244 *pointer = new_value;
245 sti ();
246 return 1;
248 sti ();
249 return 0;
252 static inline int cas2 (volatile int *pointer1,volatile int *pointer2,int requested_value1,int requested_value2,int new_value1,int new_value2)
254 cli();
255 if (*pointer1 == requested_value1 && *pointer2 == requested_value2)
257 *pointer1 = new_value1;
258 *pointer2 = new_value2;
259 sti ();
260 return 1;
262 sti ();
263 return 0;
266 #endif
268 extern void system_reboot (void);
269 extern void system_init(void);
271 #endif