summaryrefslogtreecommitdiff
path: root/include/common.h
blob: dea638aa7d0261f808890992a4065a4d30317e85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
 *
 * SPDX-License-Identifier:	GPL-2.0
 */

#ifndef COMMON_H
#define COMMON_H

#include <stdio.h>
#include <stdint.h>

#ifdef __AVR__
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>

#define udelay(n)  _delay_us(n)

struct bits {
  uint8_t b0:1;
  uint8_t b1:1;
  uint8_t b2:1;
  uint8_t b3:1;
  uint8_t b4:1;
  uint8_t b5:1;
  uint8_t b6:1;
  uint8_t b7:1;
} __attribute__((__packed__));

#define SBIT(port,pin) ((*(volatile struct bits*)&port).b##pin)

//TODO:
//  Known to work: 4.8.4, 4.9.1
//  Known to fail: 4.8.3, 4.9.0
#define GCC_BUG_61443 1

#else
// TODO: stm32
#endif /* __AVR__ */

#ifdef __FLASH
#define FLASH __flash
#define MEMX __memx
#else
#define FLASH
#define MEMX
#endif

#define stringify(s)	tostring(s)
#define tostring(s)	#s

#define FSTR(X) ((const FLASH char[]) { X } )
#define ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))

#define MIN(a,b)    ({ typeof (a) _a = (a);                                    \
						typeof (b) _b = (b);                                   \
						_a < _b ? _a : _b; })
#define MAX(a,b)    ({ typeof (a) _a = (a);                                    \
						typeof (b) _b = (b);                                   \
						_a > _b ? _a : _b; })

#ifdef __AVR__
#define Stat GPIOR0
#else
extern volatile uint_least8_t Stat;
#endif /* __AVR__ */

#define S_10MS_TO		(1<<0)
#define S_MSG_PENDING	(1<<1)
#define S_CON_PENDING	(1<<2)

static inline
void my_puts(const char *s)
{
	fputs(s, stdout);
}

static inline
void my_puts_P(const char *s)
{
#ifdef __AVR__
	fputs_P(s, stdout);
#else
	fputs(s, stdout);
#endif /* __AVR__ */
}

#endif /* COMMON_H */