]> cloudbase.mooo.com Git - z180-stamp.git/blame_incremental - include/common.h
rewrite of cmd_cpu/do_cpu_freq
[z180-stamp.git] / include / common.h
... / ...
CommitLineData
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
29struct 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#define DEVICE_NAME __AVR_DEVICE_NAME__
51
52#else
53// TODO: stm32
54#endif /* __AVR__ */
55
56#ifdef __FLASH
57#define FLASH __flash
58#define MEMX __memx
59#else
60#define FLASH
61#define MEMX
62#endif
63
64#define stringify(s) tostring(s)
65#define tostring(s) #s
66
67#define FSTR(X) ((const FLASH char[]) { X } )
68#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
69
70#define MIN(a,b) ({ typeof (a) _a = (a); \
71 typeof (b) _b = (b); \
72 _a < _b ? _a : _b; })
73#define MAX(a,b) ({ typeof (a) _a = (a); \
74 typeof (b) _b = (b); \
75 _a > _b ? _a : _b; })
76
77#ifdef __AVR__
78#define Stat GPIOR0
79#else
80extern volatile uint_least8_t Stat;
81#endif /* __AVR__ */
82
83#define S_10MS_TO (1<<0)
84#define S_MSG_PENDING (1<<1)
85#define S_CON_PENDING (1<<3)
86#define S_RESET_POLARITY (1<<4)
87
88static inline
89int my_puts(const char *s)
90{
91 return fputs(s, stdout);
92}
93
94static inline
95int my_puts_P(const char *s)
96{
97#ifdef __AVR__
98 return fputs_P(s, stdout);
99#else
100 return fputs(s, stdout);
101#endif /* __AVR__ */
102}
103
104#endif /* COMMON_H */