Merge remote-tracking branch 'mjb/master'
[fw/altos] / src / cc1111 / 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_SPEED_FAST       17
25 #define AO_SPI_SPEED_200kHz     13
26
27 #define ao_spi_set_speed(speed) (U0GCR = (UxGCR_CPOL_NEGATIVE |         \
28                                           UxGCR_CPHA_FIRST_EDGE |       \
29                                           UxGCR_ORDER_MSB |             \
30                                           ((speed) << UxGCR_BAUD_E_SHIFT)))
31
32 #define ao_spi_get_slave(bus) do {                      \
33                 ao_mutex_get(&ao_spi_mutex);            \
34                 ao_spi_set_speed(AO_SPI_SPEED_FAST);    \
35         } while (0)
36
37 #define ao_spi_put_slave(bus) do {              \
38                 ao_mutex_put(&ao_spi_mutex);    \
39         } while (0)
40
41 #define ao_spi_get_mask(reg,mask,bus,speed) do {        \
42                 ao_mutex_get(&ao_spi_mutex);            \
43                 ao_spi_set_speed(speed);                \
44                 (reg) &= ~(mask);                       \
45         } while (0)
46
47 #define ao_spi_put_mask(reg,mask,bus) do {              \
48         (reg) |= (mask); \
49         ao_mutex_put(&ao_spi_mutex); \
50         } while (0)
51
52
53 #define ao_spi_get_bit(reg,bit,pin,bus,speed) do {      \
54                 ao_mutex_get(&ao_spi_mutex);    \
55                 ao_spi_set_speed(speed);        \
56                 pin = 0;                        \
57         } while (0)
58
59 #define ao_spi_put_bit(reg,bit,pin,bus) do {    \
60                 pin = 1;                        \
61                 ao_mutex_put(&ao_spi_mutex);    \
62         } while (0)
63
64
65 /*
66  * The SPI mutex must be held to call either of these
67  * functions -- this mutex covers the entire SPI operation,
68  * from chip select low to chip select high
69  */
70
71 void
72 ao_spi_send_bus(void __xdata *block, uint16_t len) __reentrant;
73
74 void
75 ao_spi_recv_bus(void __xdata *block, uint16_t len) __reentrant;
76
77 #define ao_spi_send(block, len, bus) ao_spi_send_bus(block, len)
78 #define ao_spi_recv(block, len, bus) ao_spi_recv_bus(block, len)
79
80 #if AO_SPI_SLAVE
81 void
82 ao_spi_send_wait(void);
83
84 void
85 ao_spi_recv_wait(void);
86 #endif
87
88 void
89 ao_spi_init(void);
90
91 #define ao_spi_init_cs(port, mask) do {         \
92                 SPI_CS_PORT |= mask;            \
93                 SPI_CS_DIR |= mask;             \
94                 SPI_CS_SEL &= ~mask;            \
95         } while (0)
96
97 #define cc1111_enable_output(port,dir,sel,pin,bit,v) do {       \
98                 pin = v;                                        \
99                 dir |= (1 << bit);                              \
100                 sel &= ~(1 << bit);                             \
101         } while (0)
102
103 #define disable_unreachable     _Pragma("disable_warning 126")
104
105 #define token_paster(x,y)       x ## y
106 #define token_evaluator(x,y)    token_paster(x,y)
107 #define ao_enable_output(port,bit,pin,v) cc1111_enable_output(port,token_evaluator(port,DIR), token_evaluator(port,SEL), pin, bit, v)
108 #define ao_gpio_set(port, bit, pin, v) ((pin) = (v))