]>
Commit | Line | Data |
---|---|---|
35edb766 L |
1 | /* |
2 | * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de> | |
3 | * | |
a1595a8e | 4 | * SPDX-License-Identifier: GPL-2.0 |
35edb766 L |
5 | */ |
6 | ||
d684c216 L |
7 | #ifndef COMMON_H |
8 | #define COMMON_H | |
9 | ||
a1595a8e L |
10 | #include <stdio.h> |
11 | #include <stdint.h> | |
7eecbdac | 12 | #include <stdbool.h> |
6dbf5891 | 13 | #include <ctype.h> |
7eecbdac L |
14 | #include <string.h> |
15 | #include <stdlib.h> | |
5e8ac5e0 | 16 | #include "errnum.h" |
a1595a8e | 17 | |
587f9485 L |
18 | #define GCC_VERSION (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__) |
19 | ||
fcf1d5b3 L |
20 | #define USED __attribute__((used)) |
21 | #define UNUSED __attribute__((unused)) | |
22 | ||
d684c216 L |
23 | #ifdef __AVR__ |
24 | #include <avr/io.h> | |
889202c4 | 25 | #include <avr/pgmspace.h> |
8a7decea L |
26 | #include <util/delay.h> |
27 | ||
28 | #define udelay(n) _delay_us(n) | |
d684c216 | 29 | |
26331e24 L |
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 | ||
587f9485 | 43 | //GCC bug PR61443 |
d684c216 L |
44 | // Known to work: 4.8.4, 4.9.1 |
45 | // Known to fail: 4.8.3, 4.9.0 | |
587f9485 L |
46 | |
47 | #if (GCC_VERSION < 40804) || (GCC_VERSION == 40900) | |
d684c216 | 48 | #define GCC_BUG_61443 1 |
587f9485 | 49 | #endif /* PR61443 */ |
d684c216 | 50 | |
76c082ca L |
51 | #define DEVICE_NAME __AVR_DEVICE_NAME__ |
52 | ||
d684c216 L |
53 | #else |
54 | // TODO: stm32 | |
72f58822 | 55 | #endif /* __AVR__ */ |
d684c216 | 56 | |
d684c216 L |
57 | #ifdef __FLASH |
58 | #define FLASH __flash | |
fc30b18e | 59 | #define MEMX __memx |
d684c216 L |
60 | #else |
61 | #define FLASH | |
fc30b18e | 62 | #define MEMX |
26331e24 | 63 | #endif |
d684c216 L |
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 | ||
d264541a L |
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; }) | |
d684c216 L |
77 | |
78 | #ifdef __AVR__ | |
79 | #define Stat GPIOR0 | |
80 | #else | |
81 | extern volatile uint_least8_t Stat; | |
72f58822 | 82 | #endif /* __AVR__ */ |
d684c216 | 83 | |
7dda03f3 L |
84 | #define S_10MS_TO (1<<0) |
85 | #define S_MSG_PENDING (1<<1) | |
dbc1de70 L |
86 | #define S_CON_PENDING (1<<3) |
87 | #define S_RESET_POLARITY (1<<4) | |
d684c216 | 88 | |
26331e24 | 89 | static inline |
5e8ac5e0 | 90 | int my_puts(const char *s) |
d684c216 | 91 | { |
5e8ac5e0 | 92 | return fputs(s, stdout); |
d684c216 L |
93 | } |
94 | ||
26331e24 | 95 | static inline |
5e8ac5e0 | 96 | int my_puts_P(const char *s) |
d684c216 | 97 | { |
61b0cfe9 | 98 | #ifdef __AVR__ |
5e8ac5e0 | 99 | return fputs_P(s, stdout); |
72f58822 | 100 | #else |
5e8ac5e0 | 101 | return fputs(s, stdout); |
72f58822 | 102 | #endif /* __AVR__ */ |
61b0cfe9 L |
103 | } |
104 | ||
d684c216 | 105 | #endif /* COMMON_H */ |