Merge remote-tracking branch 'mjb/master'
[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         ao_dma_set_transfer(ao_spi_dma_in_id,
147                             &SPI_BUF,
148                             &ao_spi_const,
149                             len,
150                             DMA_CFG0_WORDSIZE_8 |
151                             DMA_CFG0_TMODE_SINGLE |
152                             SPI_DMA_RX,
153                             DMA_CFG1_SRCINC_0 |
154                             DMA_CFG1_DESTINC_0 |
155                             DMA_CFG1_PRIORITY_NORMAL);
156         ao_dma_set_transfer(ao_spi_dma_out_id,
157                             block,
158                             &SPI_BUF,
159                             len,
160                             DMA_CFG0_WORDSIZE_8 |
161                             DMA_CFG0_TMODE_SINGLE |
162                             SPI_DMA_TX,
163                             DMA_CFG1_SRCINC_1 |
164                             DMA_CFG1_DESTINC_0 |
165                             DMA_CFG1_PRIORITY_NORMAL);
166
167         ao_dma_start(ao_spi_dma_in_id);
168         ao_dma_start(ao_spi_dma_out_id);
169         ao_dma_trigger(ao_spi_dma_out_id);
170 #if !AO_SPI_SLAVE
171         __critical while (!ao_spi_dma_in_done)
172                 ao_sleep(&ao_spi_dma_in_done);
173 #endif
174 }
175
176 #if AO_SPI_SLAVE
177 void
178 ao_spi_send_wait(void)
179 {
180         __critical while (!ao_spi_dma_in_done)
181                 ao_sleep(&ao_spi_dma_in_done);
182 }
183 #endif
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 #if !AO_SPI_SLAVE
208         ao_dma_set_transfer(ao_spi_dma_out_id,
209                             &ao_spi_const,
210                             &SPI_BUF,
211                             len,
212                             DMA_CFG0_WORDSIZE_8 |
213                             DMA_CFG0_TMODE_SINGLE |
214                             SPI_DMA_TX,
215                             DMA_CFG1_SRCINC_0 |
216                             DMA_CFG1_DESTINC_0 |
217                             DMA_CFG1_PRIORITY_NORMAL);
218 #endif
219
220         ao_dma_start(ao_spi_dma_in_id);
221 #if !AO_SPI_SLAVE
222         ao_dma_start(ao_spi_dma_out_id);
223         ao_dma_trigger(ao_spi_dma_out_id);
224         __critical while (!ao_spi_dma_in_done)
225                 ao_sleep(&ao_spi_dma_in_done);
226 #endif
227 }
228
229 #if AO_SPI_SLAVE
230 void
231 ao_spi_recv_wait(void)
232 {
233         __critical while (!ao_spi_dma_in_done)
234                 ao_sleep(&ao_spi_dma_in_done);
235 }
236 #endif
237
238 void
239 ao_spi_init(void)
240 {
241         /* Set up the USART pin assignment */
242         PERCFG = (PERCFG & ~SPI_CFG_MASK) | SPI_CFG;
243
244         /* Ensure that SPI USART takes precidence over the other USART
245          * for pins that they share
246          */
247 #ifdef SPI_PRI
248         P2SEL = (P2SEL & ~P2SEL_PRI3P1_MASK) | SPI_PRI;
249 #endif
250
251         /* Make the SPI pins be controlled by the USART peripheral */
252         SPI_SEL |= SPI_BITS | CSS;
253
254         /* Set up OUT DMA */
255         ao_spi_dma_out_id = ao_dma_alloc(&ao_spi_dma_out_done);
256
257         /* Set up IN DMA */
258         ao_spi_dma_in_id = ao_dma_alloc(&ao_spi_dma_in_done);
259
260         /* Set up the USART.
261          *
262          * SPI master/slave mode
263          */
264         SPI_CSR = (UxCSR_MODE_SPI | UxCSR_RE | UxCSR_DIRECTION);
265
266         /* Set the baud rate and signal parameters
267          *
268          * The cc1111 is limited to a 24/8 MHz SPI clock.
269          * Every peripheral I've ever seen goes faster than that,
270          * so set the clock to 3MHz (BAUD_E 17, BAUD_M 0)
271          */
272         SPI_BAUD = 0;
273         SPI_GCR = (UxGCR_CPOL_NEGATIVE |
274                    UxGCR_CPHA_FIRST_EDGE |
275                    UxGCR_ORDER_MSB |
276                    (17 << UxGCR_BAUD_E_SHIFT));
277 }