altos: Make cc1111 SPI pins configurable
[fw/altos] / src / cc1111 / ao_spi.c
1 /*
2  * Copyright © 2010 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 #include "ao.h"
19
20 /* Default pin usage for existing Altus Metrum devices */
21 #if !HAS_SPI_0 && !HAS_SPI_1
22 #define HAS_SPI_0       1
23 #define SPI_0_ALT_2     1
24 #endif
25
26 #ifndef SPI_CONST
27 #define SPI_CONST       0xff
28 #endif
29
30 /*
31  * USART0 SPI config alt 1
32  * 
33  *      MO      P0_3
34  *      MI      P0_2
35  *      CLK     P0_5
36  *      SS      P0_4
37  *
38  * USART0 SPI config alt 2
39  *
40  *      MO      P1_5
41  *      MI      P1_4
42  *      CLK     P1_3
43  *      CSS     P1_2
44  *
45  * USART1 SPI config alt 1
46  *
47  *      MO      P0_4
48  *      MI      P0_5
49  *      CLK     P0_3
50  *      SS      P0_2
51  *
52  * USART1 SPI config alt 2
53  *
54  *      MO      P1_6
55  *      MI      P1_7
56  *      CLK     P1_5
57  *      SS      P1_4
58  *
59  *
60  * Chip select is the responsibility of the caller in master mode
61  */
62
63 #if HAS_SPI_0
64 #define SPI_CSR         U0CSR
65 #define SPI_BUF         U0DBUFXADDR
66 #define SPI_BAUD        U0BAUD
67 #define SPI_GCR         U0GCR
68 #define SPI_CFG_MASK    PERCFG_U0CFG_ALT_MASK
69 #define SPI_DMA_TX      DMA_CFG0_TRIGGER_UTX0
70 #define SPI_DMA_RX      DMA_CFG0_TRIGGER_URX0
71
72 #if SPI_0_ALT_1
73 #define SPI_CFG         PERCFG_U0CFG_ALT_1
74 #define SPI_SEL         P0SEL
75 #define SPI_BITS        (1 << 3) | (1 << 2) | (1 << 5)
76 #define SPI_CSS_BIT     (1 << 4)
77 #endif
78
79 #if SPI_0_ALT_2
80 #define SPI_CFG         PERCFG_U0CFG_ALT_2
81 #define SPI_SEL         P1SEL
82 #define SPI_PRI         P2SEL_PRI3P1_USART0
83 #define SPI_BITS        (1 << 5) | (1 << 4) | (1 << 3)
84 #define SPI_CSS_BIT     (1 << 2)
85 #endif
86
87 #endif
88
89 #if HAS_SPI_1
90 #define SPI_CSR         U1CSR
91 #define SPI_BUF         U1DBUFXADDR
92 #define SPI_BAUD        U1BAUD
93 #define SPI_GCR         U1GCR
94 #define SPI_CFG_MASK    PERCFG_U1CFG_ALT_MASK
95 #define SPI_DMA_TX      DMA_CFG0_TRIGGER_UTX1
96 #define SPI_DMA_RX      DMA_CFG0_TRIGGER_URX1
97
98 #if SPI_1_ALT_1
99 #define SPI_CFG         PERCFG_U1CFG_ALT_1
100 #define SPI_SEL         P0SEL
101 #define SPI_BITS        (1 << 4) | (1 << 5) | (1 << 3)
102 #define SPI_CSS_BIT     (1 << 2)
103 #endif
104
105 #if SPI_1_ALT_2
106 #define SPI_CFG         PERCFG_U1CFG_ALT_2
107 #define SPI_SEL         P1SEL
108 #define SPI_PRI         P2SEL_PRI3P1_USART1
109 #define SPI_BITS        (1 << 6) | (1 << 7) | (1 << 5)
110 #define SPI_CSS_BIT     (1 << 4)
111 #endif
112
113 #endif
114
115 #if AO_SPI_SLAVE
116 #define CSS             SPI_CSS_BIT
117 #define UxCSR_DIRECTION UxCSR_SLAVE
118 #else
119 #define CSS             0
120 #define UxCSR_DIRECTION UxCSR_MASTER
121 #endif
122
123 /* Shared mutex to protect SPI bus, must cover the entire
124  * operation, from CS low to CS high. This means that any SPI
125  * user must protect the SPI bus with this mutex
126  */
127 __xdata uint8_t ao_spi_mutex;
128 __xdata uint8_t ao_spi_dma_in_done;
129 __xdata uint8_t ao_spi_dma_out_done;
130
131 uint8_t ao_spi_dma_out_id;
132 uint8_t ao_spi_dma_in_id;
133
134 static __xdata uint8_t ao_spi_const;
135
136 /* Send bytes over SPI.
137  *
138  * This sets up two DMA engines, one writing the data and another reading
139  * bytes coming back.  We use the bytes coming back to tell when the transfer
140  * is complete, as the transmit register is double buffered and hence signals
141  * completion one byte before the transfer is actually complete
142  */
143 void
144 ao_spi_send_bus(void __xdata *block, uint16_t len) __reentrant
145 {
146 #if !AO_SPI_SLAVE
147         ao_dma_set_transfer(ao_spi_dma_in_id,
148                             &SPI_BUF,
149                             &ao_spi_const,
150                             len,
151                             DMA_CFG0_WORDSIZE_8 |
152                             DMA_CFG0_TMODE_SINGLE |
153                             SPI_DMA_RX,
154                             DMA_CFG1_SRCINC_0 |
155                             DMA_CFG1_DESTINC_0 |
156                             DMA_CFG1_PRIORITY_NORMAL);
157 #endif
158         ao_dma_set_transfer(ao_spi_dma_out_id,
159                             block,
160                             &SPI_BUF,
161                             len,
162                             DMA_CFG0_WORDSIZE_8 |
163                             DMA_CFG0_TMODE_SINGLE |
164                             SPI_DMA_TX,
165                             DMA_CFG1_SRCINC_1 |
166                             DMA_CFG1_DESTINC_0 |
167                             DMA_CFG1_PRIORITY_NORMAL);
168
169 #if !AO_SPI_SLAVE
170         ao_dma_start(ao_spi_dma_in_id);
171 #endif
172         ao_dma_start(ao_spi_dma_out_id);
173         ao_dma_trigger(ao_spi_dma_out_id);
174 #if AO_SPI_SLAVE
175         __critical while (!ao_spi_dma_out_done)
176                            ao_sleep(&ao_spi_dma_out_done);
177 #else
178         __critical while (!ao_spi_dma_in_done)
179                 ao_sleep(&ao_spi_dma_in_done);
180 #endif
181 }
182
183
184
185 /* Receive bytes over SPI.
186  *
187  * This sets up tow DMA engines, one reading the data and another
188  * writing constant values to the SPI transmitter as that is what
189  * clocks the data coming in.
190  */
191 void
192 ao_spi_recv_bus(void __xdata *block, uint16_t len) __reentrant
193 {
194         ao_dma_set_transfer(ao_spi_dma_in_id,
195                             &SPI_BUF,
196                             block,
197                             len,
198                             DMA_CFG0_WORDSIZE_8 |
199                             DMA_CFG0_TMODE_SINGLE |
200                             SPI_DMA_RX,
201                             DMA_CFG1_SRCINC_0 |
202                             DMA_CFG1_DESTINC_1 |
203                             DMA_CFG1_PRIORITY_NORMAL);
204
205         ao_spi_const = SPI_CONST;
206
207         ao_dma_set_transfer(ao_spi_dma_out_id,
208                             &ao_spi_const,
209                             &SPI_BUF,
210                             len,
211                             DMA_CFG0_WORDSIZE_8 |
212                             DMA_CFG0_TMODE_SINGLE |
213                             SPI_DMA_TX,
214                             DMA_CFG1_SRCINC_0 |
215                             DMA_CFG1_DESTINC_0 |
216                             DMA_CFG1_PRIORITY_NORMAL);
217
218         ao_dma_start(ao_spi_dma_in_id);
219         ao_dma_start(ao_spi_dma_out_id);
220         ao_dma_trigger(ao_spi_dma_out_id);
221         __critical while (!ao_spi_dma_in_done)
222                 ao_sleep(&ao_spi_dma_in_done);
223 }
224
225
226 void
227 ao_spi_init(void)
228 {
229         /* Set up the USART pin assignment */
230         PERCFG = (PERCFG & ~SPI_CFG_MASK) | SPI_CFG;
231
232         /* Ensure that SPI USART takes precidence over the other USART
233          * for pins that they share
234          */
235 #ifdef SPI_PRI
236         P2SEL = (P2SEL & ~P2SEL_PRI3P1_MASK) | SPI_PRI;
237 #endif
238
239         /* Make the SPI pins be controlled by the USART peripheral */
240         SPI_SEL |= SPI_BITS | CSS;
241
242         /* Set up OUT DMA */
243         ao_spi_dma_out_id = ao_dma_alloc(&ao_spi_dma_out_done);
244
245         /* Set up IN DMA */
246         ao_spi_dma_in_id = ao_dma_alloc(&ao_spi_dma_in_done);
247
248         /* Set up the USART.
249          *
250          * SPI master/slave mode
251          */
252         SPI_CSR = (UxCSR_MODE_SPI | UxCSR_RE | UxCSR_DIRECTION);
253
254         /* Set the baud rate and signal parameters
255          *
256          * The cc1111 is limited to a 24/8 MHz SPI clock.
257          * Every peripheral I've ever seen goes faster than that,
258          * so set the clock to 3MHz (BAUD_E 17, BAUD_M 0)
259          */
260         SPI_BAUD = 0;
261         SPI_GCR = (UxGCR_CPOL_NEGATIVE |
262                    UxGCR_CPHA_FIRST_EDGE |
263                    UxGCR_ORDER_MSB |
264                    (17 << UxGCR_BAUD_E_SHIFT));
265 }