]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11U6X/spi_api.c
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC11U6X / 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 #if DEVICE_SPI
25
26 static const PinMap PinMap_SPI_SCLK[] = {
27 {P0_6 , SPI_0, 0x02},
28 {P1_29, SPI_0, 0x01},
29 {P2_7 , SPI_0, 0x01},
30 {P1_20, SPI_1, 0x02},
31 {P1_27, SPI_1, 0x04},
32 {NC , NC , 0}
33 };
34
35 static const PinMap PinMap_SPI_MOSI[] = {
36 {P0_9 , SPI_0, 0x01},
37 {P1_12, SPI_0, 0x01},
38 {P0_21, SPI_1, 0x02},
39 {P1_22, SPI_1, 0x01},
40 {NC , NC , 0}
41 };
42
43 static const PinMap PinMap_SPI_MISO[] = {
44 {P0_8 , SPI_0, 0x01},
45 {P1_16, SPI_0, 0x01},
46 {P0_22, SPI_1, 0x03},
47 {P1_21, SPI_1, 0x02},
48 {NC , NC , 0}
49 };
50
51 static const PinMap PinMap_SPI_SSEL[] = {
52 {P0_2 , SPI_0, 0x01},
53 {P1_15, SPI_0, 0x01},
54 {P0_23, SPI_1, 0x04},
55 {P1_23, SPI_1, 0x02},
56 {NC , NC , 0}
57 };
58
59 static inline int ssp_disable(spi_t *obj);
60 static inline int ssp_enable(spi_t *obj);
61
62 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) {
63 // determine the SPI to use
64 SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
65 SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
66 SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
67 SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
68 SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
69 SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
70
71 obj->spi = (LPC_SSP0_Type*)pinmap_merge(spi_data, spi_cntl);
72 MBED_ASSERT((int)obj->spi != NC);
73
74 // enable power and clocking
75 switch ((int)obj->spi) {
76 case SPI_0:
77 LPC_SYSCON->SYSAHBCLKCTRL |= 1 << 11;
78 LPC_SYSCON->SSP0CLKDIV = 0x01;
79 LPC_SYSCON->PRESETCTRL |= 1 << 0;
80 break;
81 case SPI_1:
82 LPC_SYSCON->SYSAHBCLKCTRL |= 1 << 18;
83 LPC_SYSCON->SSP1CLKDIV = 0x01;
84 LPC_SYSCON->PRESETCTRL |= 1 << 2;
85 break;
86 }
87
88 // set default format and frequency
89 if (ssel == NC) {
90 spi_format(obj, 8, 0, 0); // 8 bits, mode 0, master
91 } else {
92 spi_format(obj, 8, 0, 1); // 8 bits, mode 0, slave
93 }
94 spi_frequency(obj, 1000000);
95
96 // enable the ssp channel
97 ssp_enable(obj);
98
99 // pin out the spi pins
100 pinmap_pinout(mosi, PinMap_SPI_MOSI);
101 pinmap_pinout(miso, PinMap_SPI_MISO);
102 pinmap_pinout(sclk, PinMap_SPI_SCLK);
103 if (ssel != NC) {
104 pinmap_pinout(ssel, PinMap_SPI_SSEL);
105 }
106 }
107
108 void spi_free(spi_t *obj) {}
109
110 void spi_format(spi_t *obj, int bits, int mode, int slave) {
111 ssp_disable(obj);
112 MBED_ASSERT(((bits >= 4) && (bits <= 16)) || ((mode >= 0) && (mode <= 3)));
113
114 int polarity = (mode & 0x2) ? 1 : 0;
115 int phase = (mode & 0x1) ? 1 : 0;
116
117 // set it up
118 int DSS = bits - 1; // DSS (data select size)
119 int SPO = (polarity) ? 1 : 0; // SPO - clock out polarity
120 int SPH = (phase) ? 1 : 0; // SPH - clock out phase
121
122 int FRF = 0; // FRF (frame format) = SPI
123 uint32_t tmp = obj->spi->CR0;
124 tmp &= ~(0xFFFF);
125 tmp |= DSS << 0
126 | FRF << 4
127 | SPO << 6
128 | SPH << 7;
129 obj->spi->CR0 = tmp;
130
131 tmp = obj->spi->CR1;
132 tmp &= ~(0xD);
133 tmp |= 0 << 0 // LBM - loop back mode - off
134 | ((slave) ? 1 : 0) << 2 // MS - master slave mode, 1 = slave
135 | 0 << 3; // SOD - slave output disable - na
136 obj->spi->CR1 = tmp;
137
138 ssp_enable(obj);
139 }
140
141 void spi_frequency(spi_t *obj, int hz) {
142 ssp_disable(obj);
143
144 uint32_t PCLK = SystemCoreClock;
145
146 int prescaler;
147
148 for (prescaler = 2; prescaler <= 254; prescaler += 2) {
149 int prescale_hz = PCLK / prescaler;
150
151 // calculate the divider
152 int divider = floor(((float)prescale_hz / (float)hz) + 0.5f);
153
154 // check we can support the divider
155 if (divider < 256) {
156 // prescaler
157 obj->spi->CPSR = prescaler;
158
159 // divider
160 obj->spi->CR0 &= ~(0xFFFF << 8);
161 obj->spi->CR0 |= (divider - 1) << 8;
162 ssp_enable(obj);
163 return;
164 }
165 }
166 error("Couldn't setup requested SPI frequency");
167 }
168
169 static inline int ssp_disable(spi_t *obj) {
170 return obj->spi->CR1 &= ~(1 << 1);
171 }
172
173 static inline int ssp_enable(spi_t *obj) {
174 return obj->spi->CR1 |= (1 << 1);
175 }
176
177 static inline int ssp_readable(spi_t *obj) {
178 return obj->spi->SR & (1 << 2);
179 }
180
181 static inline int ssp_writeable(spi_t *obj) {
182 return obj->spi->SR & (1 << 1);
183 }
184
185 static inline void ssp_write(spi_t *obj, int value) {
186 while (!ssp_writeable(obj));
187 obj->spi->DR = value;
188 }
189
190 static inline int ssp_read(spi_t *obj) {
191 while (!ssp_readable(obj));
192 return obj->spi->DR;
193 }
194
195 static inline int ssp_busy(spi_t *obj) {
196 return (obj->spi->SR & (1 << 4)) ? (1) : (0);
197 }
198
199 int spi_master_write(spi_t *obj, int value) {
200 ssp_write(obj, value);
201 return ssp_read(obj);
202 }
203
204 int spi_slave_receive(spi_t *obj) {
205 return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0);
206 }
207
208 int spi_slave_read(spi_t *obj) {
209 return obj->spi->DR;
210 }
211
212 void spi_slave_write(spi_t *obj, int value) {
213 while (ssp_writeable(obj) == 0) ;
214 obj->spi->DR = value;
215 }
216
217 int spi_busy(spi_t *obj) {
218 return ssp_busy(obj);
219 }
220
221 #endif
Imprint / Impressum