]>
git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC43XX/spi_api.c
1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * Ported to NXP LPC43XX by Micromint USA <support@micromint.com>
18 #include "mbed_assert.h"
24 #include "mbed_error.h"
26 // SCU mode for SPI pins
27 #define SCU_PINIO_SPI SCU_PINIO_FAST
29 static const PinMap PinMap_SPI_SCLK
[] = {
30 {P1_19
, SPI_1
, (SCU_PINIO_SPI
| 1)},
31 {P3_0
, SPI_0
, (SCU_PINIO_SPI
| 4)},
32 {P3_3
, SPI_0
, (SCU_PINIO_SPI
| 2)},
33 {PF_0
, SPI_0
, (SCU_PINIO_SPI
| 0)},
34 {PF_4
, SPI_1
, (SCU_PINIO_SPI
| 0)},
38 static const PinMap PinMap_SPI_MOSI
[] = {
39 {P0_1
, SPI_1
, (SCU_PINIO_SPI
| 1)},
40 {P1_2
, SPI_0
, (SCU_PINIO_SPI
| 5)},
41 {P1_4
, SPI_1
, (SCU_PINIO_SPI
| 5)},
42 {P3_7
, SPI_0
, (SCU_PINIO_SPI
| 5)},
43 {P3_8
, SPI_0
, (SCU_PINIO_SPI
| 2)},
44 {P9_2
, SPI_0
, (SCU_PINIO_SPI
| 7)},
45 {PF_3
, SPI_0
, (SCU_PINIO_SPI
| 2)},
46 {PF_7
, SPI_1
, (SCU_PINIO_SPI
| 2)},
50 static const PinMap PinMap_SPI_MISO
[] = {
51 {P0_0
, SPI_1
, (SCU_PINIO_SPI
| 1)},
52 {P1_1
, SPI_0
, (SCU_PINIO_SPI
| 5)},
53 {P1_3
, SPI_1
, (SCU_PINIO_SPI
| 5)},
54 {P3_6
, SPI_0
, (SCU_PINIO_SPI
| 5)},
55 {P3_7
, SPI_0
, (SCU_PINIO_SPI
| 2)},
56 {P9_1
, SPI_0
, (SCU_PINIO_SPI
| 7)},
57 {PF_2
, SPI_0
, (SCU_PINIO_SPI
| 2)},
58 {PF_6
, SPI_1
, (SCU_PINIO_SPI
| 2)},
62 static const PinMap PinMap_SPI_SSEL
[] = {
63 {P1_0
, SPI_0
, (SCU_PINIO_SPI
| 5)},
64 {P1_5
, SPI_1
, (SCU_PINIO_SPI
| 5)},
65 {P1_20
, SPI_1
, (SCU_PINIO_SPI
| 2)},
66 {P3_6
, SPI_0
, (SCU_PINIO_SPI
| 2)},
67 {P3_8
, SPI_0
, (SCU_PINIO_SPI
| 5)},
68 {P9_0
, SPI_0
, (SCU_PINIO_SPI
| 7)},
69 {PF_1
, SPI_0
, (SCU_PINIO_SPI
| 2)},
70 {PF_5
, SPI_1
, (SCU_PINIO_SPI
| 2)},
74 static inline int ssp_disable(spi_t
*obj
);
75 static inline int ssp_enable(spi_t
*obj
);
77 void spi_init(spi_t
*obj
, PinName mosi
, PinName miso
, PinName sclk
, PinName ssel
) {
78 // determine the SPI to use
79 SPIName spi_mosi
= (SPIName
)pinmap_peripheral(mosi
, PinMap_SPI_MOSI
);
80 SPIName spi_miso
= (SPIName
)pinmap_peripheral(miso
, PinMap_SPI_MISO
);
81 SPIName spi_sclk
= (SPIName
)pinmap_peripheral(sclk
, PinMap_SPI_SCLK
);
82 SPIName spi_ssel
= (SPIName
)pinmap_peripheral(ssel
, PinMap_SPI_SSEL
);
83 SPIName spi_data
= (SPIName
)pinmap_merge(spi_mosi
, spi_miso
);
84 SPIName spi_cntl
= (SPIName
)pinmap_merge(spi_sclk
, spi_ssel
);
86 obj
->spi
= (LPC_SSP_T
*)pinmap_merge(spi_data
, spi_cntl
);
87 MBED_ASSERT((int)obj
->spi
!= NC
);
90 switch ((int)obj
->spi
) {
91 case SPI_0
: LPC_CGU
->BASE_CLK
[CLK_BASE_SSP0
] = (1 << 11) | (CLKIN_MAINPLL
<< 24); break;
92 case SPI_1
: LPC_CGU
->BASE_CLK
[CLK_BASE_SSP1
] = (1 << 11) | (CLKIN_MAINPLL
<< 24); break;
95 // set default format and frequency
97 spi_format(obj
, 8, 0, 0); // 8 bits, mode 0, master
99 spi_format(obj
, 8, 0, 1); // 8 bits, mode 0, slave
101 spi_frequency(obj
, 1000000);
103 // enable the ssp channel
106 // pin out the spi pins
107 pinmap_pinout(mosi
, PinMap_SPI_MOSI
);
108 pinmap_pinout(miso
, PinMap_SPI_MISO
);
109 pinmap_pinout(sclk
, PinMap_SPI_SCLK
);
111 pinmap_pinout(ssel
, PinMap_SPI_SSEL
);
115 void spi_free(spi_t
*obj
) {}
117 void spi_format(spi_t
*obj
, int bits
, int mode
, int slave
) {
118 MBED_ASSERT(((bits
>= 4) && (bits
<= 16)) || ((mode
>= 0) && (mode
<= 3)));
121 int polarity
= (mode
& 0x2) ? 1 : 0;
122 int phase
= (mode
& 0x1) ? 1 : 0;
125 int DSS
= bits
- 1; // DSS (data select size)
126 int SPO
= (polarity
) ? 1 : 0; // SPO - clock out polarity
127 int SPH
= (phase
) ? 1 : 0; // SPH - clock out phase
129 int FRF
= 0; // FRF (frame format) = SPI
130 uint32_t tmp
= obj
->spi
->CR0
;
140 tmp
|= 0 << 0 // LBM - loop back mode - off
141 | ((slave
) ? 1 : 0) << 2 // MS - master slave mode, 1 = slave
142 | 0 << 3; // SOD - slave output disable - na
147 void spi_frequency(spi_t
*obj
, int hz
) {
150 uint32_t PCLK
= SystemCoreClock
;
154 for (prescaler
= 2; prescaler
<= 254; prescaler
+= 2) {
155 int prescale_hz
= PCLK
/ prescaler
;
157 // calculate the divider
158 int divider
= floor(((float)prescale_hz
/ (float)hz
) + 0.5f
);
160 // check we can support the divider
163 obj
->spi
->CPSR
= prescaler
;
166 obj
->spi
->CR0
&= ~(0xFFFF << 8);
167 obj
->spi
->CR0
|= (divider
- 1) << 8;
172 error("Couldn't setup requested SPI frequency");
175 static inline int ssp_disable(spi_t
*obj
) {
176 return obj
->spi
->CR1
&= ~(1 << 1);
179 static inline int ssp_enable(spi_t
*obj
) {
180 return obj
->spi
->CR1
|= (1 << 1);
183 static inline int ssp_readable(spi_t
*obj
) {
184 return obj
->spi
->SR
& (1 << 2);
187 static inline int ssp_writeable(spi_t
*obj
) {
188 return obj
->spi
->SR
& (1 << 1);
191 static inline void ssp_write(spi_t
*obj
, int value
) {
192 while (!ssp_writeable(obj
));
193 obj
->spi
->DR
= value
;
196 static inline int ssp_read(spi_t
*obj
) {
197 while (!ssp_readable(obj
));
201 static inline int ssp_busy(spi_t
*obj
) {
202 return (obj
->spi
->SR
& (1 << 4)) ? (1) : (0);
205 int spi_master_write(spi_t
*obj
, int value
) {
206 ssp_write(obj
, value
);
207 return ssp_read(obj
);
210 int spi_slave_receive(spi_t
*obj
) {
211 return (ssp_readable(obj
) && !ssp_busy(obj
)) ? (1) : (0);
214 int spi_slave_read(spi_t
*obj
) {
218 void spi_slave_write(spi_t
*obj
, int value
) {
219 while (ssp_writeable(obj
) == 0) ;
220 obj
->spi
->DR
= value
;
223 int spi_busy(spi_t
*obj
) {
224 return ssp_busy(obj
);