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