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