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