]> mj.ucw.cz Git - home-hw.git/blob - test-modbus/util.h
ModBus test
[home-hw.git] / test-modbus / 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 #define UNUSED __attribute__((unused))
15
16 static inline uint get_u16_le(byte *p)
17 {
18         return (p[1] << 8) | p[0];
19 }
20
21 static inline uint get_u16_be(byte *p)
22 {
23         return (p[0] << 8) | p[1];
24 }
25
26 static inline uint get_u32_le(byte *p)
27 {
28         return (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
29 }
30
31 static inline uint get_u32_be(byte *p)
32 {
33         return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
34 }
35
36 static inline void put_u16_le(byte *p, u16 x)
37 {
38         p[0] = x;
39         p[1] = x >> 8;
40 }
41
42 static inline void put_u16_be(byte *p, u16 x)
43 {
44         p[0] = x >> 8;
45         p[1] = x;
46 }
47
48 static inline void put_u32_be(byte *p, u32 x)
49 {
50         p[0] = x >> 24;
51         p[1] = (x >> 16) & 0xff;
52         p[2] = (x >> 8) & 0xff;
53         p[3] = x & 0xff;
54 }
55
56 static inline void put_u32_le(byte *p, u32 x)
57 {
58         p[3] = x >> 24;
59         p[2] = (x >> 16) & 0xff;
60         p[1] = (x >> 8) & 0xff;
61         p[0] = x & 0xff;
62 }
63
64 // debug.c
65
66 // #define DEBUG_SEMIHOSTING
67 #define DEBUG_USART USART1
68
69 void debug_printf(const char *fmt, ...);
70 void debug_puts(const char *s);
71 void debug_putc(int c);