summaryrefslogtreecommitdiffhomepage
path: root/digital/avr/common/io.h
blob: cf706e9cc9392a130b9e2982c802e235257d73d9 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#ifndef io_h
#define io_h
/* io.h */
/* avr.modules - AVR modules. {{{
 *
 * Copyright (C) 2005 Nicolas Schodet
 *
 * Robot APB Team/Efrei 2006.
 *        Web: http://assos.efrei.fr/robot/
 *      Email: robot AT efrei DOT fr
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */

#ifndef HOST

/* Avr part. */
#include <avr/interrupt.h>
#if !defined (__AVR_LIBC_VERSION__) || __AVR_LIBC_VERSION__ < 10400UL
#  include <avr/signal.h>
   /* No INTERRUPT as it is dangerous. */
#  undef INTERRUPT
   /* SIGNAL is now ISR. */
#  define ISR SIGNAL
#endif
/* Some AVR includes define HOST! */
#ifdef HOST
# undef HOST
#endif

/** Saved interrupts state. */
typedef uint8_t intr_flags_t;

/** Lock interrupts, and return saved state.  Warning: this is not a memory
 * barrier you still need to use volatile access where needed.
 *
 * Please try to find ways not to lock interrupts, this is dangerous! */
extern inline intr_flags_t
intr_lock (void)
{
    intr_flags_t flags = SREG;
    cli ();
    return flags;
}

/** Restore interrupts saved state. */
extern inline void
intr_restore (intr_flags_t flags)
{
    SREG = flags;
}

#else /* HOST */

/* Same as on AVR. */
#define _BV(x) (1<<(x))
#define bit_is_set(sfr, bit) ((sfr) & _BV (bit))
#define bit_is_clear(sfr, bit) (!((sfr) & _BV (bit)))
/* No interrupt support on host. */
#define sei()
#define cli()

typedef int intr_flags_t;

extern inline intr_flags_t
intr_lock (void) { return 0; }

extern inline void
intr_restore (intr_flags_t flags) { }

#endif /* HOST */

/** Use these macros to define port and bit number combination.
 * For example:
 *
 *   #define MY_IO B, 5
 *   IO_PORT (MY_IO) => PORTB
 *   IO_DDR (MY_IO) => DDRB
 *   IO_PIN (MY_IO) => PINB
 *   IO_BV (MY_IO) => _BV (5)
 *   IO_N (MY_IO) => (5)
 *
 *   IO_GET (MY_IO) => (PINB & _BV (5))
 *   IO_SET (MY_IO) => PORTB |= _BV (5)
 *   IO_CLR (MY_IO) => PORTB &= ~_BV (5)
 *   IO_OUTPUT (MY_IO) => DDRB |= _BV (5)
 *   IO_INPUT (MY_IO) => DDRB &= ~_BV (5)
 */
#define IO_PORT(io) IO_PORT_ (io)
#define IO_PORT_(p, n) PORT ## p
#define IO_DDR(io) IO_DDR_ (io)
#define IO_DDR_(p, n) DDR ## p
#define IO_PIN(io) IO_PIN_ (io)
#define IO_PIN_(p, n) PIN ## p
#define IO_BV(io) IO_BV_ (io)
#define IO_BV_(p, n) _BV (n)
#define IO_N(io) IO_N_ (io)
#define IO_N_(p, n) (n)

#define IO_GET(io) (IO_PIN_ (io) & IO_BV_ (io))
#define IO_SET(io) IO_PORT_ (io) |= IO_BV_ (io)
#define IO_CLR(io) IO_PORT_ (io) &= ~IO_BV_ (io)
#define IO_OUTPUT(io) IO_DDR_ (io) |= IO_BV_ (io)
#define IO_INPUT(io) IO_DDR_ (io) &= ~IO_BV_ (io)

#endif /* io_h */