]> mj.ucw.cz Git - home-hw.git/blob - ssr/Inc/util.h
3a29fd63c476627d721b7db94d0e22d4bcb274f3
[home-hw.git] / ssr / Inc / util.h
1 #include <stdint.h>
2 #include <stdbool.h>
3
4 typedef unsigned int uint;
5 typedef uint8_t byte;
6 typedef uint16_t u16;
7 typedef int16_t s16;
8 typedef uint32_t u32;
9 typedef int32_t s32;
10
11 #define MIN(x,y) ((x) < (y) ? (x) : (y))
12 #define MAX(x,y) ((x) > (y) ? (x) : (y))
13
14 static inline uint get_u16_le(byte *p)
15 {
16   return (p[1] << 8) | p[0];
17 }
18
19 static inline uint get_u16_be(byte *p)
20 {
21   return (p[0] << 8) | p[1];
22 }
23
24 static inline uint get_u32_le(byte *p)
25 {
26   return (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
27 }
28
29 static inline uint get_u32_be(byte *p)
30 {
31   return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
32 }
33
34 static inline void put_u16_le(byte *p, u16 x)
35 {
36   p[0] = x;
37   p[1] = x >> 8;
38 }
39
40 static inline void put_u16_be(byte *p, u16 x)
41 {
42   p[0] = x >> 8;
43   p[1] = x;
44 }
45
46 static inline void put_u32_be(byte *p, u32 x)
47 {
48   p[0] = x >> 24;
49   p[1] = (x >> 16) & 0xff;
50   p[2] = (x >> 8) & 0xff;
51   p[3] = x & 0xff;
52 }
53
54 static inline void put_u32_le(byte *p, u32 x)
55 {
56   p[3] = x >> 24;
57   p[2] = (x >> 16) & 0xff;
58   p[1] = (x >> 8) & 0xff;
59   p[0] = x & 0xff;
60 }
61
62 // debug.c
63
64 #undef DEBUG_SEMIHOSTING
65 // #define DEBUG_USART USART1
66 #undef DEBUG_USART
67
68 void debug_printf(const char *fmt, ...);
69 void debug_puts(const char *s);
70 void debug_putc(int c);