altos: Fix BT link status pin for real TBT hardware
[fw/altos] / src / 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 __xdata uint8_t ao_spi_mutex;
21 __xdata uint8_t ao_spi_dma_in_done;
22 __xdata uint8_t ao_spi_dma_out_done;
23
24 uint8_t ao_spi_dma_out_id;
25 uint8_t ao_spi_dma_in_id;
26
27 static __xdata uint8_t ao_spi_const = 0xff;
28
29 /* Send bytes over SPI.
30  *
31  * This sets up two DMA engines, one writing the data and another reading
32  * bytes coming back.  We use the bytes coming back to tell when the transfer
33  * is complete, as the transmit register is double buffered and hence signals
34  * completion one byte before the transfer is actually complete
35  */
36 void
37 ao_spi_send(void __xdata *block, uint16_t len) __reentrant
38 {
39         ao_mutex_get(&ao_spi_mutex);
40         ao_dma_set_transfer(ao_spi_dma_in_id,
41                             &U0DBUFXADDR,
42                             &ao_spi_const,
43                             len,
44                             DMA_CFG0_WORDSIZE_8 |
45                             DMA_CFG0_TMODE_SINGLE |
46                             DMA_CFG0_TRIGGER_URX0,
47                             DMA_CFG1_SRCINC_0 |
48                             DMA_CFG1_DESTINC_0 |
49                             DMA_CFG1_PRIORITY_NORMAL);
50
51         ao_dma_set_transfer(ao_spi_dma_out_id,
52                             block,
53                             &U0DBUFXADDR,
54                             len,
55                             DMA_CFG0_WORDSIZE_8 |
56                             DMA_CFG0_TMODE_SINGLE |
57                             DMA_CFG0_TRIGGER_UTX0,
58                             DMA_CFG1_SRCINC_1 |
59                             DMA_CFG1_DESTINC_0 |
60                             DMA_CFG1_PRIORITY_NORMAL);
61
62         ao_dma_start(ao_spi_dma_in_id);
63         ao_dma_start(ao_spi_dma_out_id);
64         ao_dma_trigger(ao_spi_dma_out_id);
65         __critical while (!ao_spi_dma_in_done)
66                 ao_sleep(&ao_spi_dma_in_done);
67         ao_mutex_put(&ao_spi_mutex);
68 }
69
70 /* Receive bytes over SPI.
71  *
72  * This sets up tow DMA engines, one reading the data and another
73  * writing constant values to the SPI transmitter as that is what
74  * clocks the data coming in.
75  */
76 void
77 ao_spi_recv(void __xdata *block, uint16_t len) __reentrant
78 {
79         ao_mutex_get(&ao_spi_mutex);
80         ao_dma_set_transfer(ao_spi_dma_in_id,
81                             &U0DBUFXADDR,
82                             block,
83                             len,
84                             DMA_CFG0_WORDSIZE_8 |
85                             DMA_CFG0_TMODE_SINGLE |
86                             DMA_CFG0_TRIGGER_URX0,
87                             DMA_CFG1_SRCINC_0 |
88                             DMA_CFG1_DESTINC_1 |
89                             DMA_CFG1_PRIORITY_NORMAL);
90
91         ao_dma_set_transfer(ao_spi_dma_out_id,
92                             &ao_spi_const,
93                             &U0DBUFXADDR,
94                             len,
95                             DMA_CFG0_WORDSIZE_8 |
96                             DMA_CFG0_TMODE_SINGLE |
97                             DMA_CFG0_TRIGGER_UTX0,
98                             DMA_CFG1_SRCINC_0 |
99                             DMA_CFG1_DESTINC_0 |
100                             DMA_CFG1_PRIORITY_NORMAL);
101
102         ao_dma_start(ao_spi_dma_in_id);
103         ao_dma_start(ao_spi_dma_out_id);
104         ao_dma_trigger(ao_spi_dma_out_id);
105         __critical while (!ao_spi_dma_in_done)
106                 ao_sleep(&ao_spi_dma_in_done);
107         ao_mutex_put(&ao_spi_mutex);
108 }
109
110 /*
111  * Initialize USART0 for SPI using config alt 2
112  *
113  *      MO      P1_5
114  *      MI      P1_4
115  *      CLK     P1_3
116  *
117  * Chip select is the responsibility of the caller
118  */
119
120 void
121 ao_spi_init(void)
122 {
123         /* Set up the USART pin assignment */
124         PERCFG = (PERCFG & ~PERCFG_U0CFG_ALT_MASK) | PERCFG_U0CFG_ALT_2;
125
126         /* Ensure that USART0 takes precidence over USART1 for pins that
127          * they share
128          */
129         P2SEL = (P2SEL & ~P2SEL_PRI3P1_MASK) | P2SEL_PRI3P1_USART0;
130
131         /* Make the SPI pins be controlled by the USART peripheral */
132         P1SEL |= ((1 << 5) | (1 << 4) | (1 << 3));
133
134         /* Set up OUT DMA */
135         ao_spi_dma_out_id = ao_dma_alloc(&ao_spi_dma_out_done);
136
137         /* Set up IN DMA */
138         ao_spi_dma_in_id = ao_dma_alloc(&ao_spi_dma_in_done);
139
140         /* Set up the USART.
141          *
142          * SPI master mode
143          */
144         U0CSR = (UxCSR_MODE_SPI | UxCSR_RE | UxCSR_MASTER);
145
146         /* Set the baud rate and signal parameters
147          *
148          * The cc1111 is limited to a 24/8 MHz SPI clock.
149          * Every peripheral I've ever seen goes faster than that,
150          * so set the clock to 3MHz (BAUD_E 17, BAUD_M 0)
151          */
152         U0BAUD = 0;
153         U0GCR = (UxGCR_CPOL_NEGATIVE |
154                  UxGCR_CPHA_FIRST_EDGE |
155                  UxGCR_ORDER_MSB |
156                  (17 << UxGCR_BAUD_E_SHIFT));
157 }