]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC15XX/spi_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC15XX / spi_api.c
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "mbed_assert.h"
17 #include <math.h>
18
19 #include "spi_api.h"
20 #include "cmsis.h"
21 #include "pinmap.h"
22 #include "mbed_error.h"
23
24 static const SWM_Map SWM_SPI_SSEL[] = {
25     {4, 0},
26     {5, 24},
27 };
28
29 static const SWM_Map SWM_SPI_SCLK[] = {
30     {3, 8},
31     {5, 0},
32 };
33
34 static const SWM_Map SWM_SPI_MOSI[] = {
35     {3, 16},
36     {5, 8},
37 };
38
39 static const SWM_Map SWM_SPI_MISO[] = {
40     {3, 24},
41     {5, 16},
42 };
43
44 // bit flags for used SPIs
45 static unsigned char spi_used = 0;
46 static int get_available_spi(PinName mosi, PinName miso, PinName sclk, PinName ssel)
47 {
48     if (spi_used == 0) {
49         return 0; // The first user
50     }
51
52     const SWM_Map *swm;
53     uint32_t regVal;
54
55     // Investigate if same pins as the used SPI0/1 - to be able to reuse it
56     for (int spi_n = 0; spi_n < 2; spi_n++) {
57         if (spi_used & (1<<spi_n)) {
58             if (sclk != NC) {
59                 swm = &SWM_SPI_SCLK[spi_n];
60                 regVal = LPC_SWM->PINASSIGN[swm->n] & (0xFF << swm->offset);
61                 if (regVal != (sclk << swm->offset)) {
62                     // Existing pin is not the same as the one we want
63                     continue;
64                 }
65             }
66
67             if (mosi != NC) {
68                 swm = &SWM_SPI_MOSI[spi_n];
69                 regVal = LPC_SWM->PINASSIGN[swm->n] & (0xFF << swm->offset);
70                 if (regVal != (mosi << swm->offset)) {
71                     // Existing pin is not the same as the one we want
72                     continue;
73                 }
74             }
75
76             if (miso != NC) {
77                 swm = &SWM_SPI_MISO[spi_n];
78                 regVal = LPC_SWM->PINASSIGN[swm->n] & (0xFF << swm->offset);
79                 if (regVal != (miso << swm->offset)) {
80                     // Existing pin is not the same as the one we want
81                     continue;
82                 }
83             }
84
85             if (ssel != NC) {
86                 swm = &SWM_SPI_SSEL[spi_n];
87                 regVal = LPC_SWM->PINASSIGN[swm->n] & (0xFF << swm->offset);
88                 if (regVal != (ssel << swm->offset)) {
89                     // Existing pin is not the same as the one we want
90                     continue;
91                 }
92             }
93
94             // The pins for the currently used SPIx are the same as the
95             // ones we want so we will reuse it
96             return spi_n;
97         }
98     }
99
100     // None of the existing SPIx pin setups match the pins we want
101     // so the last hope is to select one unused SPIx
102     if ((spi_used & 1) == 0) {
103         return 0;
104     } else if ((spi_used & 2) == 0) {
105         return 1;
106     }
107
108     // No matching setup and no free SPIx
109     return -1;
110 }
111
112 static inline void spi_disable(spi_t *obj);
113 static inline void spi_enable(spi_t *obj);
114
115 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
116 {
117     int spi_n = get_available_spi(mosi, miso, sclk, ssel);
118     if (spi_n == -1) {
119         error("No available SPI");
120     }
121
122     obj->spi_n = spi_n;
123     spi_used |= (1 << spi_n);
124
125     obj->spi = (spi_n) ? (LPC_SPI0_Type *)(LPC_SPI1_BASE) : (LPC_SPI0_Type *)(LPC_SPI0_BASE);
126
127     const SWM_Map *swm;
128     uint32_t regVal;
129
130     if (sclk != NC) {
131         swm = &SWM_SPI_SCLK[obj->spi_n];
132         regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
133         LPC_SWM->PINASSIGN[swm->n] = regVal |  (sclk   << swm->offset);
134     }
135
136     if (mosi != NC) {
137         swm = &SWM_SPI_MOSI[obj->spi_n];
138         regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
139         LPC_SWM->PINASSIGN[swm->n] = regVal |  (mosi   << swm->offset);
140     }
141
142     if (miso != NC) {
143         swm = &SWM_SPI_MISO[obj->spi_n];
144         regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
145         LPC_SWM->PINASSIGN[swm->n] = regVal |  (miso   << swm->offset);
146     }
147
148     if (ssel != NC) {
149         swm = &SWM_SPI_SSEL[obj->spi_n];
150         regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
151         LPC_SWM->PINASSIGN[swm->n] = regVal |  (ssel   << swm->offset);
152     }
153
154     // clear interrupts
155     obj->spi->INTENCLR = 0x3f;
156
157     // enable power and clocking
158     LPC_SYSCON->SYSAHBCLKCTRL1 |=  (0x1 << (obj->spi_n + 9));
159     LPC_SYSCON->PRESETCTRL1    |=  (0x1 << (obj->spi_n + 9));
160     LPC_SYSCON->PRESETCTRL1    &= ~(0x1 << (obj->spi_n + 9));
161
162     // set default format and frequency
163     if (ssel == NC) {
164         spi_format(obj, 8, 0, 0);  // 8 bits, mode 0, master
165     } else {
166         spi_format(obj, 8, 0, 1);  // 8 bits, mode 0, slave
167     }
168     spi_frequency(obj, 1000000);
169
170     // enable the spi channel
171     spi_enable(obj);
172 }
173
174 void spi_free(spi_t *obj)
175 {
176 }
177
178 void spi_format(spi_t *obj, int bits, int mode, int slave)
179 {
180     spi_disable(obj);
181     MBED_ASSERT((bits >= 1 && bits <= 16) && (mode >= 0 && mode <= 3));
182
183     int polarity = (mode & 0x2) ? 1 : 0;
184     int phase = (mode & 0x1) ? 1 : 0;
185
186     // set it up
187     int LEN = bits - 1;             // LEN  - Data Length
188     int CPOL = (polarity) ? 1 : 0;  // CPOL - Clock Polarity select
189     int CPHA = (phase) ? 1 : 0;     // CPHA - Clock Phase select
190
191     uint32_t tmp = obj->spi->CFG;
192     tmp &= ~((1 << 5) | (1 << 4) | (1 << 2));
193     tmp |= (CPOL << 5) | (CPHA << 4) | ((slave ? 0 : 1) << 2);
194     obj->spi->CFG = tmp;
195
196     // select frame length
197     tmp = obj->spi->TXCTL;
198     tmp &= ~(0xf << 24);
199     tmp |= (LEN << 24);
200     obj->spi->TXCTL = tmp;
201
202     spi_enable(obj);
203 }
204
205 void spi_frequency(spi_t *obj, int hz)
206 {
207     spi_disable(obj);
208
209     // rise DIV value if it cannot be divided
210     obj->spi->DIV = (SystemCoreClock + (hz - 1))/hz - 1;
211     obj->spi->DLY = 0;
212
213     spi_enable(obj);
214 }
215
216 static inline void spi_disable(spi_t *obj)
217 {
218     obj->spi->CFG &= ~(1 << 0);
219 }
220
221 static inline void spi_enable(spi_t *obj)
222 {
223     obj->spi->CFG |= (1 << 0);
224 }
225
226 static inline int spi_readable(spi_t *obj)
227 {
228     return obj->spi->STAT & (1 << 0);
229 }
230
231 static inline int spi_writeable(spi_t *obj)
232 {
233     return obj->spi->STAT & (1 << 1);
234 }
235
236 static inline void spi_write(spi_t *obj, int value)
237 {
238     while (!spi_writeable(obj));
239     // end of transfer
240     obj->spi->TXCTL |= (1 << 20);
241     obj->spi->TXDAT = (value & 0xffff);
242 }
243
244 static inline int spi_read(spi_t *obj)
245 {
246     while (!spi_readable(obj));
247     return obj->spi->RXDAT & 0xffff; // Only the lower 16 bits contain data
248 }
249
250 int spi_busy(spi_t *obj)
251 {
252     // checking RXOV(Receiver Overrun interrupt flag)
253     return obj->spi->STAT & (1 << 2);
254 }
255
256 int spi_master_write(spi_t *obj, int value)
257 {
258     spi_write(obj, value);
259     return spi_read(obj);
260 }
261
262 int spi_slave_receive(spi_t *obj)
263 {
264     return (spi_readable(obj) && !spi_busy(obj)) ? (1) : (0);
265 }
266
267 int spi_slave_read(spi_t *obj)
268 {
269     return obj->spi->RXDAT & 0xffff; // Only the lower 16 bits contain data
270 }
271
272 void spi_slave_write(spi_t *obj, int value)
273 {
274     while (spi_writeable(obj) == 0) ;
275     obj->spi->TXDAT = value;
276 }