altoslib: fix computation of TeleGPS battery voltage
[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; 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 #if !HAS_SPI_0 && !HAS_SPI_1
24 #define HAS_SPI_0       1
25 #define SPI_0_ALT_2     1
26 #endif
27
28 #if HAS_SPI_0 && HAS_SPI_1
29 #define MULTI_SPI       1
30 #define N_SPI           2
31 #else
32 #define MULTI_SPI       0
33 #define N_SPI           1
34 #endif
35
36 extern __xdata uint8_t  ao_spi_mutex[N_SPI];
37
38 #if MULTI_SPI
39 #define ao_spi_get(bus) ao_mutex_get(&ao_spi_mutex[bus])
40 #define ao_spi_put(bus) ao_mutex_put(&ao_spi_mutex[bus])
41 #else
42 #define ao_spi_get(bus) ao_mutex_get(&ao_spi_mutex[0])
43 #define ao_spi_put(bus) ao_mutex_put(&ao_spi_mutex[0])
44 #endif
45
46 #define AO_SPI_SPEED_FAST       17
47 #define AO_SPI_SPEED_200kHz     13
48
49 #if MULTI_SPI
50 #define ao_spi_set_speed(bus,speed) (*(bus ? &U1GCR : &U0GCR) =(UxGCR_CPOL_NEGATIVE | \
51                                                                 UxGCR_CPHA_FIRST_EDGE | \
52                                                                 UxGCR_ORDER_MSB | \
53                                                                 ((speed) << UxGCR_BAUD_E_SHIFT)))
54 #else
55 #define ao_spi_set_speed(bus,speed) (U0GCR = (UxGCR_CPOL_NEGATIVE |     \
56                                               UxGCR_CPHA_FIRST_EDGE |   \
57                                               UxGCR_ORDER_MSB |         \
58                                               ((speed) << UxGCR_BAUD_E_SHIFT)))
59 #endif
60
61 #define ao_spi_get_slave(bus) do {                      \
62                 ao_spi_get(bus);                        \
63                 ao_spi_set_speed(bus,AO_SPI_SPEED_FAST);        \
64         } while (0)
65
66 #define ao_spi_put_slave(bus) do {              \
67                 ao_spi_put(bus);                \
68         } while (0)
69
70 #define ao_spi_get_mask(reg,mask,bus,speed) do {        \
71                 ao_spi_get(bus);                        \
72                 ao_spi_set_speed(bus,speed);            \
73                 (reg) &= ~(mask);                       \
74         } while (0)
75
76 #define ao_spi_put_mask(reg,mask,bus) do {              \
77         (reg) |= (mask); \
78         ao_spi_put(bus); \
79         } while (0)
80
81
82 #define ao_spi_get_bit(reg,bit,pin,bus,speed) do {      \
83                 ao_spi_get(bus);                        \
84                 ao_spi_set_speed(bus,speed);            \
85                 pin = 0;                                \
86         } while (0)
87
88 #define ao_spi_put_bit(reg,bit,pin,bus) do {    \
89                 pin = 1;                        \
90                 ao_spi_put(bus);                \
91         } while (0)
92
93
94 /*
95  * The SPI mutex must be held to call either of these
96  * functions -- this mutex covers the entire SPI operation,
97  * from chip select low to chip select high
98  */
99
100 #if MULTI_SPI
101 void
102 ao_spi_send(void __xdata *block, uint16_t len, uint8_t bus) __reentrant;
103
104 void
105 ao_spi_recv(void __xdata *block, uint16_t len, uint8_t bus) __reentrant;
106 #else
107 void
108 ao_spi_send_bus(void __xdata *block, uint16_t len) __reentrant;
109
110 void
111 ao_spi_recv_bus(void __xdata *block, uint16_t len) __reentrant;
112
113 #define ao_spi_send(block, len, bus) ao_spi_send_bus(block, len)
114 #define ao_spi_recv(block, len, bus) ao_spi_recv_bus(block, len)
115 #endif
116
117 #if AO_SPI_SLAVE
118 void
119 ao_spi_send_wait(void);
120
121 void
122 ao_spi_recv_wait(void);
123 #endif
124
125 void
126 ao_spi_init(void);
127
128 #define token_paster(x,y)       x ## y
129 #define token_paster3(x,y,z)    x ## y ## z
130 #define token_evaluator(x,y)    token_paster(x,y)
131 #define token_evaluator3(x,y,z) token_paster3(x,y,z)
132
133 #define ao_spi_init_cs(port, mask) do {                 \
134                 port |= mask;                           \
135                 token_evaluator(port,DIR) |= mask;      \
136                 token_evaluator(port,SEL) &= ~mask;     \
137         } while (0)
138
139 #define cc1111_enable_output(port,dir,sel,pin,bit,v) do {       \
140                 pin = v;                                        \
141                 dir |= (1 << bit);                              \
142                 sel &= ~(1 << bit);                             \
143         } while (0)
144
145 #define disable_unreachable     _Pragma("disable_warning 126")
146
147 #define ao_enable_output(port,bit,pin,v) cc1111_enable_output(port,token_evaluator(port,DIR), token_evaluator(port,SEL), pin, bit, v)
148 #define ao_gpio_set(port, bit, pin, v) ((pin) = (v))
149 #define ao_gpio_get(port, bit, pin) (pin)
150 #define ao_gpio_get_bits(port) (port)
151 #define ao_gpio_set_bits(port, bits) ((port) |= bits)
152 #define ao_gpio_clr_bits(port, bits) ((port) &= ~bits)