altos/telelco-v2.0: Fix button names and pin mappings. Add drag LED.
[fw/altos] / src / avr / ao_arch_funcs.h
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 /*
20  * ao_spi.c
21  */
22
23 extern __xdata uint8_t  ao_spi_mutex;
24
25 #define ao_spi_get_mask(reg,mask,bus,speed) do {        \
26                 ao_mutex_get(&ao_spi_mutex);    \
27                 (reg) &= ~(mask);               \
28         } while (0)
29
30 #define ao_spi_put_mask(reg,mask,bus) do {      \
31                 (reg) |= (mask);                \
32                 ao_mutex_put(&ao_spi_mutex);    \
33         } while (0)
34
35 #define ao_spi_get_bit(reg,bit,pin,bus,speed) do {      \
36                 ao_mutex_get(&ao_spi_mutex);    \
37                 (pin) = 0;                      \
38         } while (0)
39
40 #define ao_spi_put_bit(reg,bit,pin,bus) do {    \
41                 (pin) = 1;                      \
42                 ao_mutex_put(&ao_spi_mutex);    \
43         } while (0)
44
45
46 #define ao_gpio_token_paster(x,y)               x ## y
47 #define ao_gpio_token_evaluator(x,y)    ao_gpio_token_paster(x,y)
48
49 #define ao_gpio_set(port, bit, pin, v) do {                             \
50                 if (v)                                                  \
51                         (ao_gpio_token_evaluator(PORT,port)) |= (1 << bit); \
52                 else                                                    \
53                         (ao_gpio_token_evaluator(PORT,port)) &= ~(1 << bit); \
54         } while (0)
55
56 /*
57  * The SPI mutex must be held to call either of these
58  * functions -- this mutex covers the entire SPI operation,
59  * from chip select low to chip select high
60  */
61
62 #define ao_enable_output(port, bit, pin, v) do {                        \
63                 ao_gpio_set(port, bit, pin, v);                         \
64                 ao_gpio_token_evaluator(DDR,port) |= (1 << bit);        \
65         } while (0)
66
67
68 void
69 ao_spi_send_bus(void __xdata *block, uint16_t len) __reentrant;
70
71 void
72 ao_spi_recv_bus(void __xdata *block, uint16_t len) __reentrant;
73
74 #define ao_spi_send(block, len, bus) ao_spi_send_bus(block, len)
75 #define ao_spi_recv(block, len, bus) ao_spi_recv_bus(block, len)
76
77 void
78 ao_spi_init(void);
79
80 #define ao_spi_init_cs(port, mask) do {         \
81                 SPI_CS_PORT |= (mask);          \
82                 SPI_CS_DIR |= (mask);           \
83         } while (0)