Added K70f120 project, set default playback to DAC
[pcm-lib.git] / source / os_utils.h
blob0c62e9c2c620602c8b1137e542f0de5ec17404e3
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation.
5 * os_utils.h - system layer function declare
6 *
7 * <liu090@sina.com>
8 */
9 #ifndef OS_UTILS_H
10 #define OS_UTILS_H
12 #include <ctype.h>
13 #include <string.h>
14 #include <stdlib.h>
16 #include <mqx.h>
17 #include <bsp.h>
19 #if _DEBUG == 1
20 //#define OS_DEBUG
21 #endif
23 /* build */
24 typedef int ssize_t;
26 /* building ***************************************************/
27 /* os function */
28 #if MQX_USE_UNCACHED_MEM && PSP_HAS_DATA_CACHE
29 #define PCM_mem_alloc_uncached(n) _mem_alloc_system_uncached(n)
30 #define PCM_mem_alloc_uncached_zero(n) _mem_alloc_system_zero_uncached(n)
31 #define PCM_mem_alloc_uncached_align(n,a) _mem_alloc_align_uncached(n,a)
32 #else
33 #define PCM_mem_alloc_uncached(n) _mem_alloc_system(n)
34 #define PCM_mem_alloc_uncached_zero(n) _mem_alloc_system_zero(n)
35 #define PCM_mem_alloc_uncached_align(n,a) _mem_alloc_align(n,a)
36 #endif /* PSP_HAS_DATA_CACHE */
38 #define PCM_mem_alloc(n) _mem_alloc_system(n)
39 #define PCM_mem_alloc_zero(n) _mem_alloc_system_zero(n)
40 #define PCM_mem_alloc_align(n,a) _mem_alloc_align(n,a)
42 #define PCM_mem_free(ptr) _mem_free(ptr)
43 #define PCM_mem_zero(ptr,n) _mem_zero(ptr,n)
44 #define PCM_mem_copy(src,dst,n) _mem_copy(src,dst,n)
46 #define os_memcpy memcpy
47 #define os_memset memset
48 #define os_malloc malloc
49 #define os_free free
50 #define os_zalloc PCM_mem_alloc_zero
52 /* misc */
53 #define PCM_lock() sco_pcm_lock()
54 #define PCM_unlock() sco_pcm_unlock()
56 #define snd_pcm_stream_lock_irq(substream) \
57 do { \
58 PCM_lock(); \
59 /*_int_disable();*/ \
60 } while (0)
62 #define snd_pcm_stream_unlock_irq(substream) \
63 do { \
64 PCM_unlock(); \
65 /*_int_enable();*/ \
66 } while (0)
68 #define snd_pcm_stream_lock_irqsave(substream, flags) \
69 do { \
70 PCM_lock(); \
71 /*_int_disable();*/ \
72 } while (0)
74 #define snd_pcm_stream_unlock_irqrestore(runtime, flags) \
75 do { \
76 PCM_unlock(); \
77 /* _int_enable();*/ \
78 } while (0)
80 /* debug message */
81 #define __stringify_1(x...) #x
82 #define __stringify(x...) __stringify_1(x)
84 #define halt() do { for(;;); } while (0)
85 #define ASSERT(x) do { if (!(x)) { dbgPrintf("%s:%d ASSERT failed: %s\n", __FILE__, __LINE__, #x); halt(); } } while (0)
87 #if 0
88 #define snd_BUG_ON(cond) \
89 do { \
90 if(cond) printf(1, __FILE__, __LINE__, "BUG? (%s)\n", __stringify(cond)); \
91 } while (0)
92 #else
93 static inline int snd_BUG_ON(int cond)
95 if(cond) printf("BUG? in file %s:%d \n", __FILE__, __LINE__);
96 return cond;
98 #endif
100 /* If you are writing a driver, please use dev_dbg instead */
101 #if defined(OS_DEBUG)
102 #define pr_debug(fmt, ...) \
103 printf(pr_fmt(fmt), ##__VA_ARGS__)
104 #define pr_warning(fmt, ...) \
105 printf(pr_fmt(fmt), ##__VA_ARGS__)
106 #define pr_warn pr_warning
107 #define os_printf(fmt, ...) \
108 printf(pr_fmt(fmt), ##__VA_ARGS__)
109 #else
110 #define pr_debug(fmt, ...)
111 #define pr_warning(fmt, ...)
112 #define pr_warn
113 #define os_printf(fmt, ...)
114 #endif
116 #ifndef pr_fmt
117 #define pr_fmt(fmt) fmt
118 #endif
120 #define pr_err(fmt, ...) \
121 printf(pr_fmt(fmt), ##__VA_ARGS__)
123 #define USHRT_MAX ((u16)(~0U))
124 #define SHRT_MAX ((s16)(USHRT_MAX>>1))
125 #define SHRT_MIN ((s16)(-SHRT_MAX - 1))
126 #define INT_MAX ((int)(~0U>>1))
127 #define INT_MIN (-INT_MAX - 1)
128 #define UINT_MAX (~0U)
129 #define LONG_MAX ((long)(~0UL>>1))
130 #define LONG_MIN (-LONG_MAX - 1)
131 #define ULONG_MAX (~0UL)
132 #define LLONG_MAX ((long long)(~0ULL>>1))
133 #define LLONG_MIN (-LLONG_MAX - 1)
134 #define ULLONG_MAX (~0ULL)
136 #ifndef min
137 #define min(x, y) (x < y ? x : y)
138 #endif
139 #ifndef max
140 #define max(x, y) (x > y ? x : y)
141 #endif
143 #define ULONG_MAX (~0UL)
144 #define LONG_MAX ((long)(~0UL>>1))
145 /* building ***************************************************/
147 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) /*+ __must_be_array(arr)*/)
149 /* critial , mutex */
150 //#define OS_DEFINE_MUTEX
151 #define os_mutex_init
152 #define os_mutex_lock
153 #define os_mutex_unlock
155 /* IO */
156 #define writeb(v,a) (*(volatile unsigned char __force *)(a) = (v))
157 #define writew(v,a) (*(volatile unsigned short __force *)(a) = (v))
158 #define writel(v,a) (*(volatile unsigned int __force *)(a) = (v))
160 #define readb(a) (*(volatile unsigned char __force *)(a))
161 #define readw(a) (*(volatile unsigned short __force *)(a))
162 #define readl(a) (*(volatile unsigned int __force *)(a))
164 /* ticks implement move to soc-codec */
165 extern uint32_t HZ;
166 #define os_msleep sco_msleep
167 #define os_get_ticks_interval // sco_get_ticks_interval
169 static inline uint8_t ffs(unsigned int val)
171 int i;
172 for(i = 0; i < 32; i++) {
173 if(val & (1<< i))
174 break;
177 if(i < 32)
178 return i + 1;
179 else
180 return 0;
183 /* for sgtl5000 internal power */
184 static inline uint16_t clamp(uint16_t val,uint16_t min,uint16_t max)
186 uint16_t __val = (val);
187 uint16_t __min = (min);
188 uint16_t __max = (max);
189 __val = __val < __min ? __min: __val;
190 return (__val > __max ? __max: __val);
193 static inline unsigned int ld2(uint32_t v)
195 unsigned r = 0;
197 if (v >= 0x10000) {
198 v >>= 16;
199 r += 16;
201 if (v >= 0x100) {
202 v >>= 8;
203 r += 8;
205 if (v >= 0x10) {
206 v >>= 4;
207 r += 4;
209 if (v >= 4) {
210 v >>= 2;
211 r += 2;
213 if (v >= 2)
214 r++;
215 return r;
218 #endif /* OS_UTILS_H */