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 #include "mbed_assert.h"
22 #include "mbed_error.h"
26 static const PinMap PinMap_SPI_SCLK[] = {
35 static const PinMap PinMap_SPI_MOSI[] = {
43 static const PinMap PinMap_SPI_MISO[] = {
51 static const PinMap PinMap_SPI_SSEL[] = {
59 static inline int ssp_disable(spi_t *obj);
60 static inline int ssp_enable(spi_t *obj);
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);
71 obj->spi = (LPC_SSP0_Type*)pinmap_merge(spi_data, spi_cntl);
72 MBED_ASSERT((int)obj->spi != NC);
74 // enable power and clocking
75 switch ((int)obj->spi) {
77 LPC_SYSCON->SYSAHBCLKCTRL |= 1 << 11;
78 LPC_SYSCON->SSP0CLKDIV = 0x01;
79 LPC_SYSCON->PRESETCTRL |= 1 << 0;
82 LPC_SYSCON->SYSAHBCLKCTRL |= 1 << 18;
83 LPC_SYSCON->SSP1CLKDIV = 0x01;
84 LPC_SYSCON->PRESETCTRL |= 1 << 2;
88 // set default format and frequency
90 spi_format(obj, 8, 0, 0); // 8 bits, mode 0, master
92 spi_format(obj, 8, 0, 1); // 8 bits, mode 0, slave
94 spi_frequency(obj, 1000000);
96 // enable the ssp channel
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);
104 pinmap_pinout(ssel, PinMap_SPI_SSEL);
108 void spi_free(spi_t *obj) {}
110 void spi_format(spi_t *obj, int bits, int mode, int slave) {
112 MBED_ASSERT(((bits >= 4) && (bits <= 16)) || ((mode >= 0) && (mode <= 3)));
114 int polarity = (mode & 0x2) ? 1 : 0;
115 int phase = (mode & 0x1) ? 1 : 0;
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
122 int FRF = 0; // FRF (frame format) = SPI
123 uint32_t tmp = obj->spi->CR0;
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
141 void spi_frequency(spi_t *obj, int hz) {
144 uint32_t PCLK = SystemCoreClock;
148 for (prescaler = 2; prescaler <= 254; prescaler += 2) {
149 int prescale_hz = PCLK / prescaler;
151 // calculate the divider
152 int divider = floor(((float)prescale_hz / (float)hz) + 0.5f);
154 // check we can support the divider
157 obj->spi->CPSR = prescaler;
160 obj->spi->CR0 &= ~(0xFFFF << 8);
161 obj->spi->CR0 |= (divider - 1) << 8;
166 error("Couldn't setup requested SPI frequency");
169 static inline int ssp_disable(spi_t *obj) {
170 return obj->spi->CR1 &= ~(1 << 1);
173 static inline int ssp_enable(spi_t *obj) {
174 return obj->spi->CR1 |= (1 << 1);
177 static inline int ssp_readable(spi_t *obj) {
178 return obj->spi->SR & (1 << 2);
181 static inline int ssp_writeable(spi_t *obj) {
182 return obj->spi->SR & (1 << 1);
185 static inline void ssp_write(spi_t *obj, int value) {
186 while (!ssp_writeable(obj));
187 obj->spi->DR = value;
190 static inline int ssp_read(spi_t *obj) {
191 while (!ssp_readable(obj));
195 static inline int ssp_busy(spi_t *obj) {
196 return (obj->spi->SR & (1 << 4)) ? (1) : (0);
199 int spi_master_write(spi_t *obj, int value) {
200 ssp_write(obj, value);
201 return ssp_read(obj);
204 int spi_slave_receive(spi_t *obj) {
205 return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0);
208 int spi_slave_read(spi_t *obj) {
212 void spi_slave_write(spi_t *obj, int value) {
213 while (ssp_writeable(obj) == 0) ;
214 obj->spi->DR = value;
217 int spi_busy(spi_t *obj) {
218 return ssp_busy(obj);