src/cc1111: Add ao_gpio_set macro
[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_mask(reg,mask,bus,speed) do {        \
33                 ao_mutex_get(&ao_spi_mutex);            \
34                 ao_spi_set_speed(speed);                \
35                 (reg) &= ~(mask);                       \
36         } while (0)
37
38 #define ao_spi_put_mask(reg,mask,bus) do {              \
39         (reg) |= (mask); \
40         ao_mutex_put(&ao_spi_mutex); \
41         } while (0)
42
43
44 #define ao_spi_get_bit(reg,bit,pin,bus,speed) do {      \
45                 ao_mutex_get(&ao_spi_mutex);    \
46                 ao_spi_set_speed(speed);        \
47                 pin = 0;                        \
48         } while (0)
49
50 #define ao_spi_put_bit(reg,bit,pin,bus) do {    \
51                 pin = 1;                        \
52                 ao_mutex_put(&ao_spi_mutex);    \
53         } while (0)
54
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 void
63 ao_spi_send_bus(void __xdata *block, uint16_t len) __reentrant;
64
65 void
66 ao_spi_recv_bus(void __xdata *block, uint16_t len) __reentrant;
67
68 #define ao_spi_send(block, len, bus) ao_spi_send_bus(block, len)
69 #define ao_spi_recv(block, len, bus) ao_spi_recv_bus(block, len)
70
71 void
72 ao_spi_init(void);
73
74 #define ao_spi_init_cs(port, mask) do {         \
75                 SPI_CS_PORT |= mask;            \
76                 SPI_CS_DIR |= mask;             \
77                 SPI_CS_SEL &= ~mask;            \
78         } while (0)
79
80 #define cc1111_enable_output(port,dir,sel,pin,bit,v) do {       \
81                 pin = v;                                        \
82                 dir |= (1 << bit);                              \
83                 sel &= ~(1 << bit);                             \
84         } while (0)
85
86 #define disable_unreachable     _Pragma("disable_warning 126")
87
88 #define token_paster(x,y)       x ## y
89 #define token_evaluator(x,y)    token_paster(x,y)
90 #define ao_enable_output(port,bit,pin,v) cc1111_enable_output(port,token_evaluator(port,DIR), token_evaluator(port,SEL), pin, bit, v)
91 #define ao_gpio_set(port, bit, pin, v) ((pin) = (v))