mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
bogdanm
Date:
Mon Aug 05 14:12:34 2013 +0300
Revision:
13:0645d8841f51
Child:
15:4892fe388435
Update mbed sources to revision 64

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 13:0645d8841f51 1 /* mbed Microcontroller Library
bogdanm 13:0645d8841f51 2 * Copyright (c) 2006-2013 ARM Limited
bogdanm 13:0645d8841f51 3 *
bogdanm 13:0645d8841f51 4 * Licensed under the Apache License, Version 2.0 (the "License");
bogdanm 13:0645d8841f51 5 * you may not use this file except in compliance with the License.
bogdanm 13:0645d8841f51 6 * You may obtain a copy of the License at
bogdanm 13:0645d8841f51 7 *
bogdanm 13:0645d8841f51 8 * http://www.apache.org/licenses/LICENSE-2.0
bogdanm 13:0645d8841f51 9 *
bogdanm 13:0645d8841f51 10 * Unless required by applicable law or agreed to in writing, software
bogdanm 13:0645d8841f51 11 * distributed under the License is distributed on an "AS IS" BASIS,
bogdanm 13:0645d8841f51 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bogdanm 13:0645d8841f51 13 * See the License for the specific language governing permissions and
bogdanm 13:0645d8841f51 14 * limitations under the License.
bogdanm 13:0645d8841f51 15 */
bogdanm 13:0645d8841f51 16 #include "spi_api.h"
bogdanm 13:0645d8841f51 17
bogdanm 13:0645d8841f51 18 #include <math.h>
bogdanm 13:0645d8841f51 19
bogdanm 13:0645d8841f51 20 #include "cmsis.h"
bogdanm 13:0645d8841f51 21 #include "pinmap.h"
bogdanm 13:0645d8841f51 22 #include "error.h"
bogdanm 13:0645d8841f51 23
bogdanm 13:0645d8841f51 24 static const PinMap PinMap_SPI_SCLK[] = {
bogdanm 13:0645d8841f51 25 {PTA15, SPI_0, 2},
bogdanm 13:0645d8841f51 26 {PTB11, SPI_1, 2},
bogdanm 13:0645d8841f51 27 {PTC5, SPI_0, 2},
bogdanm 13:0645d8841f51 28 {PTD1, SPI_0, 2},
bogdanm 13:0645d8841f51 29 {PTD5, SPI_1, 2},
bogdanm 13:0645d8841f51 30 {PTE2, SPI_1, 2},
bogdanm 13:0645d8841f51 31 {NC , NC , 0}
bogdanm 13:0645d8841f51 32 };
bogdanm 13:0645d8841f51 33
bogdanm 13:0645d8841f51 34 static const PinMap PinMap_SPI_MOSI[] = {
bogdanm 13:0645d8841f51 35 {PTA16, SPI_0, 2},
bogdanm 13:0645d8841f51 36 {PTA17, SPI_0, 5},
bogdanm 13:0645d8841f51 37 {PTB16, SPI_1, 2},
bogdanm 13:0645d8841f51 38 {PTB17, SPI_1, 5},
bogdanm 13:0645d8841f51 39 {PTC6, SPI_0, 2},
bogdanm 13:0645d8841f51 40 {PTC7, SPI_0, 5},
bogdanm 13:0645d8841f51 41 {PTD2, SPI_0, 2},
bogdanm 13:0645d8841f51 42 {PTD3, SPI_0, 5},
bogdanm 13:0645d8841f51 43 {PTD6, SPI_1, 2},
bogdanm 13:0645d8841f51 44 {PTD7, SPI_1, 5},
bogdanm 13:0645d8841f51 45 {PTE1, SPI_1, 2},
bogdanm 13:0645d8841f51 46 {PTE3, SPI_1, 5},
bogdanm 13:0645d8841f51 47 {NC , NC , 0}
bogdanm 13:0645d8841f51 48 };
bogdanm 13:0645d8841f51 49
bogdanm 13:0645d8841f51 50 static const PinMap PinMap_SPI_MISO[] = {
bogdanm 13:0645d8841f51 51 {PTA16, SPI_0, 5},
bogdanm 13:0645d8841f51 52 {PTA17, SPI_0, 2},
bogdanm 13:0645d8841f51 53 {PTB16, SPI_1, 5},
bogdanm 13:0645d8841f51 54 {PTB17, SPI_1, 2},
bogdanm 13:0645d8841f51 55 {PTC6, SPI_0, 5},
bogdanm 13:0645d8841f51 56 {PTC7, SPI_0, 2},
bogdanm 13:0645d8841f51 57 {PTD2, SPI_0, 5},
bogdanm 13:0645d8841f51 58 {PTD3, SPI_0, 2},
bogdanm 13:0645d8841f51 59 {PTD6, SPI_1, 5},
bogdanm 13:0645d8841f51 60 {PTD7, SPI_1, 2},
bogdanm 13:0645d8841f51 61 {PTE1, SPI_1, 5},
bogdanm 13:0645d8841f51 62 {PTE3, SPI_1, 2},
bogdanm 13:0645d8841f51 63 {NC , NC , 0}
bogdanm 13:0645d8841f51 64 };
bogdanm 13:0645d8841f51 65
bogdanm 13:0645d8841f51 66 static const PinMap PinMap_SPI_SSEL[] = {
bogdanm 13:0645d8841f51 67 {PTA14, SPI_0, 2},
bogdanm 13:0645d8841f51 68 {PTB10, SPI_1, 2},
bogdanm 13:0645d8841f51 69 {PTC4, SPI_0, 2},
bogdanm 13:0645d8841f51 70 {PTD0, SPI_0, 2},
bogdanm 13:0645d8841f51 71 {PTD4, SPI_1, 2},
bogdanm 13:0645d8841f51 72 {PTE4, SPI_1, 2},
bogdanm 13:0645d8841f51 73 {NC , NC , 0}
bogdanm 13:0645d8841f51 74 };
bogdanm 13:0645d8841f51 75
bogdanm 13:0645d8841f51 76 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) {
bogdanm 13:0645d8841f51 77 // determine the SPI to use
bogdanm 13:0645d8841f51 78 SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
bogdanm 13:0645d8841f51 79 SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
bogdanm 13:0645d8841f51 80 SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
bogdanm 13:0645d8841f51 81 SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
bogdanm 13:0645d8841f51 82 SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
bogdanm 13:0645d8841f51 83 SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
bogdanm 13:0645d8841f51 84
bogdanm 13:0645d8841f51 85 obj->spi = (SPI_Type*)pinmap_merge(spi_data, spi_cntl);
bogdanm 13:0645d8841f51 86 if ((int)obj->spi == NC) {
bogdanm 13:0645d8841f51 87 error("SPI pinout mapping failed");
bogdanm 13:0645d8841f51 88 }
bogdanm 13:0645d8841f51 89
bogdanm 13:0645d8841f51 90 // enable power and clocking
bogdanm 13:0645d8841f51 91 switch ((int)obj->spi) {
bogdanm 13:0645d8841f51 92 case SPI_0: SIM->SCGC5 |= 1 << 11; SIM->SCGC4 |= 1 << 22; break;
bogdanm 13:0645d8841f51 93 case SPI_1: SIM->SCGC5 |= 1 << 13; SIM->SCGC4 |= 1 << 23; break;
bogdanm 13:0645d8841f51 94 }
bogdanm 13:0645d8841f51 95
bogdanm 13:0645d8841f51 96 // set default format and frequency
bogdanm 13:0645d8841f51 97 if (ssel == NC) {
bogdanm 13:0645d8841f51 98 spi_format(obj, 8, 0, 0); // 8 bits, mode 0, master
bogdanm 13:0645d8841f51 99 } else {
bogdanm 13:0645d8841f51 100 spi_format(obj, 8, 0, 1); // 8 bits, mode 0, slave
bogdanm 13:0645d8841f51 101 }
bogdanm 13:0645d8841f51 102 spi_frequency(obj, 1000000);
bogdanm 13:0645d8841f51 103
bogdanm 13:0645d8841f51 104 // enable SPI
bogdanm 13:0645d8841f51 105 obj->spi->C1 |= SPI_C1_SPE_MASK;
bogdanm 13:0645d8841f51 106
bogdanm 13:0645d8841f51 107 // pin out the spi pins
bogdanm 13:0645d8841f51 108 pinmap_pinout(mosi, PinMap_SPI_MOSI);
bogdanm 13:0645d8841f51 109 pinmap_pinout(miso, PinMap_SPI_MISO);
bogdanm 13:0645d8841f51 110 pinmap_pinout(sclk, PinMap_SPI_SCLK);
bogdanm 13:0645d8841f51 111 if (ssel != NC) {
bogdanm 13:0645d8841f51 112 pinmap_pinout(ssel, PinMap_SPI_SSEL);
bogdanm 13:0645d8841f51 113 }
bogdanm 13:0645d8841f51 114 }
bogdanm 13:0645d8841f51 115
bogdanm 13:0645d8841f51 116 void spi_free(spi_t *obj) {
bogdanm 13:0645d8841f51 117 // [TODO]
bogdanm 13:0645d8841f51 118 }
bogdanm 13:0645d8841f51 119 void spi_format(spi_t *obj, int bits, int mode, int slave) {
bogdanm 13:0645d8841f51 120 if (bits != 8) {
bogdanm 13:0645d8841f51 121 error("Only 8bits SPI supported");
bogdanm 13:0645d8841f51 122 }
bogdanm 13:0645d8841f51 123
bogdanm 13:0645d8841f51 124 if ((mode < 0) || (mode > 3)) {
bogdanm 13:0645d8841f51 125 error("SPI mode unsupported");
bogdanm 13:0645d8841f51 126 }
bogdanm 13:0645d8841f51 127
bogdanm 13:0645d8841f51 128 uint8_t polarity = (mode & 0x2) ? 1 : 0;
bogdanm 13:0645d8841f51 129 uint8_t phase = (mode & 0x1) ? 1 : 0;
bogdanm 13:0645d8841f51 130 uint8_t c1_data = ((!slave) << 4) | (polarity << 3) | (phase << 2);
bogdanm 13:0645d8841f51 131
bogdanm 13:0645d8841f51 132 // clear MSTR, CPOL and CPHA bits
bogdanm 13:0645d8841f51 133 obj->spi->C1 &= ~(0x7 << 2);
bogdanm 13:0645d8841f51 134
bogdanm 13:0645d8841f51 135 // write new value
bogdanm 13:0645d8841f51 136 obj->spi->C1 |= c1_data;
bogdanm 13:0645d8841f51 137 }
bogdanm 13:0645d8841f51 138
bogdanm 13:0645d8841f51 139 void spi_frequency(spi_t *obj, int hz) {
bogdanm 13:0645d8841f51 140 uint32_t error = 0;
bogdanm 13:0645d8841f51 141 uint32_t p_error = 0xffffffff;
bogdanm 13:0645d8841f51 142 uint32_t ref = 0;
bogdanm 13:0645d8841f51 143 uint8_t spr = 0;
bogdanm 13:0645d8841f51 144 uint8_t ref_spr = 0;
bogdanm 13:0645d8841f51 145 uint8_t ref_prescaler = 0;
bogdanm 13:0645d8841f51 146
bogdanm 13:0645d8841f51 147 // bus clk
bogdanm 13:0645d8841f51 148 uint32_t PCLK = 48000000u;
bogdanm 13:0645d8841f51 149 uint8_t prescaler = 1;
bogdanm 13:0645d8841f51 150 uint8_t divisor = 2;
bogdanm 13:0645d8841f51 151
bogdanm 13:0645d8841f51 152 for (prescaler = 1; prescaler <= 8; prescaler++) {
bogdanm 13:0645d8841f51 153 divisor = 2;
bogdanm 13:0645d8841f51 154 for (spr = 0; spr <= 8; spr++, divisor *= 2) {
bogdanm 13:0645d8841f51 155 ref = PCLK / (prescaler*divisor);
bogdanm 13:0645d8841f51 156 if (ref > hz)
bogdanm 13:0645d8841f51 157 continue;
bogdanm 13:0645d8841f51 158 error = hz - ref;
bogdanm 13:0645d8841f51 159 if (error < p_error) {
bogdanm 13:0645d8841f51 160 ref_spr = spr;
bogdanm 13:0645d8841f51 161 ref_prescaler = prescaler - 1;
bogdanm 13:0645d8841f51 162 p_error = error;
bogdanm 13:0645d8841f51 163 }
bogdanm 13:0645d8841f51 164 }
bogdanm 13:0645d8841f51 165 }
bogdanm 13:0645d8841f51 166
bogdanm 13:0645d8841f51 167 // set SPPR and SPR
bogdanm 13:0645d8841f51 168 obj->spi->BR = ((ref_prescaler & 0x7) << 4) | (ref_spr & 0xf);
bogdanm 13:0645d8841f51 169 }
bogdanm 13:0645d8841f51 170
bogdanm 13:0645d8841f51 171 static inline int spi_writeable(spi_t * obj) {
bogdanm 13:0645d8841f51 172 return (obj->spi->S & SPI_S_SPTEF_MASK) ? 1 : 0;
bogdanm 13:0645d8841f51 173 }
bogdanm 13:0645d8841f51 174
bogdanm 13:0645d8841f51 175 static inline int spi_readable(spi_t * obj) {
bogdanm 13:0645d8841f51 176 return (obj->spi->S & SPI_S_SPRF_MASK) ? 1 : 0;
bogdanm 13:0645d8841f51 177 }
bogdanm 13:0645d8841f51 178
bogdanm 13:0645d8841f51 179 int spi_master_write(spi_t *obj, int value) {
bogdanm 13:0645d8841f51 180 // wait tx buffer empty
bogdanm 13:0645d8841f51 181 while(!spi_writeable(obj));
bogdanm 13:0645d8841f51 182 obj->spi->D = (value & 0xff);
bogdanm 13:0645d8841f51 183
bogdanm 13:0645d8841f51 184 // wait rx buffer full
bogdanm 13:0645d8841f51 185 while (!spi_readable(obj));
bogdanm 13:0645d8841f51 186 return obj->spi->D & 0xff;
bogdanm 13:0645d8841f51 187 }
bogdanm 13:0645d8841f51 188
bogdanm 13:0645d8841f51 189 int spi_slave_receive(spi_t *obj) {
bogdanm 13:0645d8841f51 190 return spi_readable(obj);
bogdanm 13:0645d8841f51 191 }
bogdanm 13:0645d8841f51 192
bogdanm 13:0645d8841f51 193 int spi_slave_read(spi_t *obj) {
bogdanm 13:0645d8841f51 194 return obj->spi->D;
bogdanm 13:0645d8841f51 195 }
bogdanm 13:0645d8841f51 196
bogdanm 13:0645d8841f51 197 void spi_slave_write(spi_t *obj, int value) {
bogdanm 13:0645d8841f51 198 while (!spi_writeable(obj));
bogdanm 13:0645d8841f51 199 obj->spi->D = value;
bogdanm 13:0645d8841f51 200 }