]> cloudbase.mooo.com Git - z180-stamp.git/blob - include/common.h
cmd_attach.c, cmd_boot.c, cmd_loadcpm3.c: use cmd_error()
[z180-stamp.git] / include / common.h
1 /*
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * SPDX-License-Identifier: GPL-2.0
5 */
6
7 #ifndef COMMON_H
8 #define COMMON_H
9
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <stdbool.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include "errnum.h"
16
17 #define GCC_VERSION (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
18
19 #define USED __attribute__((used))
20 #define UNUSED __attribute__((unused))
21
22 #ifdef __AVR__
23 #include <avr/io.h>
24 #include <avr/pgmspace.h>
25 #include <util/delay.h>
26
27 #define udelay(n) _delay_us(n)
28
29 struct bits {
30 uint8_t b0:1;
31 uint8_t b1:1;
32 uint8_t b2:1;
33 uint8_t b3:1;
34 uint8_t b4:1;
35 uint8_t b5:1;
36 uint8_t b6:1;
37 uint8_t b7:1;
38 } __attribute__((__packed__));
39
40 #define SBIT(port,pin) ((*(volatile struct bits*)&port).b##pin)
41
42 //GCC bug PR61443
43 // Known to work: 4.8.4, 4.9.1
44 // Known to fail: 4.8.3, 4.9.0
45
46 #if (GCC_VERSION < 40804) || (GCC_VERSION == 40900)
47 #define GCC_BUG_61443 1
48 #endif /* PR61443 */
49
50 #else
51 // TODO: stm32
52 #endif /* __AVR__ */
53
54 #ifdef __FLASH
55 #define FLASH __flash
56 #define MEMX __memx
57 #else
58 #define FLASH
59 #define MEMX
60 #endif
61
62 #define stringify(s) tostring(s)
63 #define tostring(s) #s
64
65 #define FSTR(X) ((const FLASH char[]) { X } )
66 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
67
68 #define MIN(a,b) ({ typeof (a) _a = (a); \
69 typeof (b) _b = (b); \
70 _a < _b ? _a : _b; })
71 #define MAX(a,b) ({ typeof (a) _a = (a); \
72 typeof (b) _b = (b); \
73 _a > _b ? _a : _b; })
74
75 #ifdef __AVR__
76 #define Stat GPIOR0
77 #else
78 extern volatile uint_least8_t Stat;
79 #endif /* __AVR__ */
80
81 #define S_10MS_TO (1<<0)
82 #define S_MSG_PENDING (1<<1)
83 #define S_CON_PENDING (1<<2)
84
85 static inline
86 int my_puts(const char *s)
87 {
88 return fputs(s, stdout);
89 }
90
91 static inline
92 int my_puts_P(const char *s)
93 {
94 #ifdef __AVR__
95 return fputs_P(s, stdout);
96 #else
97 return fputs(s, stdout);
98 #endif /* __AVR__ */
99 }
100
101 #endif /* COMMON_H */