mbed library sources, include can_api for nucleo-f091rc

Dependents:   CanNucleoF0_example

Fork of mbed-src by mbed official

Committer:
emilmont
Date:
Fri Jun 14 17:49:17 2013 +0100
Revision:
10:3bc89ef62ce7
Unify mbed library sources

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 10:3bc89ef62ce7 1 /* mbed Microcontroller Library
emilmont 10:3bc89ef62ce7 2 * Copyright (c) 2006-2013 ARM Limited
emilmont 10:3bc89ef62ce7 3 *
emilmont 10:3bc89ef62ce7 4 * Licensed under the Apache License, Version 2.0 (the "License");
emilmont 10:3bc89ef62ce7 5 * you may not use this file except in compliance with the License.
emilmont 10:3bc89ef62ce7 6 * You may obtain a copy of the License at
emilmont 10:3bc89ef62ce7 7 *
emilmont 10:3bc89ef62ce7 8 * http://www.apache.org/licenses/LICENSE-2.0
emilmont 10:3bc89ef62ce7 9 *
emilmont 10:3bc89ef62ce7 10 * Unless required by applicable law or agreed to in writing, software
emilmont 10:3bc89ef62ce7 11 * distributed under the License is distributed on an "AS IS" BASIS,
emilmont 10:3bc89ef62ce7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
emilmont 10:3bc89ef62ce7 13 * See the License for the specific language governing permissions and
emilmont 10:3bc89ef62ce7 14 * limitations under the License.
emilmont 10:3bc89ef62ce7 15 */
emilmont 10:3bc89ef62ce7 16 #include <math.h>
emilmont 10:3bc89ef62ce7 17
emilmont 10:3bc89ef62ce7 18 #include "spi_api.h"
emilmont 10:3bc89ef62ce7 19 #include "cmsis.h"
emilmont 10:3bc89ef62ce7 20 #include "pinmap.h"
emilmont 10:3bc89ef62ce7 21 #include "error.h"
emilmont 10:3bc89ef62ce7 22
emilmont 10:3bc89ef62ce7 23 static const PinMap PinMap_SPI_SCLK[] = {
emilmont 10:3bc89ef62ce7 24 {P0_7 , SPI_1, 2},
emilmont 10:3bc89ef62ce7 25 {P0_15, SPI_0, 2},
emilmont 10:3bc89ef62ce7 26 {P1_0, SPI_2, 4},
emilmont 10:3bc89ef62ce7 27 {P1_19, SPI_1, 5},
emilmont 10:3bc89ef62ce7 28 {P1_20, SPI_0, 5},
emilmont 10:3bc89ef62ce7 29 {P1_31, SPI_1, 2},
emilmont 10:3bc89ef62ce7 30 {P2_22, SPI_0, 2},
emilmont 10:3bc89ef62ce7 31 {P4_20, SPI_1, 3},
emilmont 10:3bc89ef62ce7 32 {P5_2, SPI_2, 2},
emilmont 10:3bc89ef62ce7 33 {NC , NC , 0}
emilmont 10:3bc89ef62ce7 34 };
emilmont 10:3bc89ef62ce7 35
emilmont 10:3bc89ef62ce7 36 static const PinMap PinMap_SPI_MOSI[] = {
emilmont 10:3bc89ef62ce7 37 {P0_9 , SPI_1, 2},
emilmont 10:3bc89ef62ce7 38 {P0_13, SPI_1, 2},
emilmont 10:3bc89ef62ce7 39 {P0_18, SPI_0, 2},
emilmont 10:3bc89ef62ce7 40 {P1_1, SPI_2, 4},
emilmont 10:3bc89ef62ce7 41 {P1_22, SPI_1, 5},
emilmont 10:3bc89ef62ce7 42 {P1_24, SPI_0, 5},
emilmont 10:3bc89ef62ce7 43 {P2_27, SPI_0, 2},
emilmont 10:3bc89ef62ce7 44 {P4_23, SPI_1, 3},
emilmont 10:3bc89ef62ce7 45 {P5_0, SPI_2, 2},
emilmont 10:3bc89ef62ce7 46 {NC , NC , 0}
emilmont 10:3bc89ef62ce7 47 };
emilmont 10:3bc89ef62ce7 48
emilmont 10:3bc89ef62ce7 49 static const PinMap PinMap_SPI_MISO[] = {
emilmont 10:3bc89ef62ce7 50 {P0_8 , SPI_1, 2},
emilmont 10:3bc89ef62ce7 51 {P0_12, SPI_1, 2},
emilmont 10:3bc89ef62ce7 52 {P0_17, SPI_0, 2},
emilmont 10:3bc89ef62ce7 53 {P1_4, SPI_2, 4},
emilmont 10:3bc89ef62ce7 54 {P1_18, SPI_1, 5},
emilmont 10:3bc89ef62ce7 55 {P1_23, SPI_0, 5},
emilmont 10:3bc89ef62ce7 56 {P2_26, SPI_0, 2},
emilmont 10:3bc89ef62ce7 57 {P4_22, SPI_1, 3},
emilmont 10:3bc89ef62ce7 58 {P5_1, SPI_2, 2},
emilmont 10:3bc89ef62ce7 59 {NC , NC , 0}
emilmont 10:3bc89ef62ce7 60 };
emilmont 10:3bc89ef62ce7 61
emilmont 10:3bc89ef62ce7 62 static const PinMap PinMap_SPI_SSEL[] = {
emilmont 10:3bc89ef62ce7 63 {P0_6 , SPI_1, 2},
emilmont 10:3bc89ef62ce7 64 {P0_14, SPI_1, 2},
emilmont 10:3bc89ef62ce7 65 {P0_16, SPI_0, 2},
emilmont 10:3bc89ef62ce7 66 {P1_8, SPI_2, 4},
emilmont 10:3bc89ef62ce7 67 {P1_21, SPI_0, 3},
emilmont 10:3bc89ef62ce7 68 {P1_26, SPI_1, 5},
emilmont 10:3bc89ef62ce7 69 {P1_28, SPI_0, 5},
emilmont 10:3bc89ef62ce7 70 {P2_23, SPI_0, 2},
emilmont 10:3bc89ef62ce7 71 {P4_21, SPI_1, 3},
emilmont 10:3bc89ef62ce7 72 {NC , NC , 0}
emilmont 10:3bc89ef62ce7 73 };
emilmont 10:3bc89ef62ce7 74
emilmont 10:3bc89ef62ce7 75 static inline int ssp_disable(spi_t *obj);
emilmont 10:3bc89ef62ce7 76 static inline int ssp_enable(spi_t *obj);
emilmont 10:3bc89ef62ce7 77
emilmont 10:3bc89ef62ce7 78 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) {
emilmont 10:3bc89ef62ce7 79 // determine the SPI to use
emilmont 10:3bc89ef62ce7 80 SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
emilmont 10:3bc89ef62ce7 81 SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
emilmont 10:3bc89ef62ce7 82 SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
emilmont 10:3bc89ef62ce7 83 SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
emilmont 10:3bc89ef62ce7 84 SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
emilmont 10:3bc89ef62ce7 85 SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
emilmont 10:3bc89ef62ce7 86 obj->spi = (LPC_SSP_TypeDef*)pinmap_merge(spi_data, spi_cntl);
emilmont 10:3bc89ef62ce7 87 if ((int)obj->spi == NC) {
emilmont 10:3bc89ef62ce7 88 error("SPI pinout mapping failed");
emilmont 10:3bc89ef62ce7 89 }
emilmont 10:3bc89ef62ce7 90
emilmont 10:3bc89ef62ce7 91 // enable power and clocking
emilmont 10:3bc89ef62ce7 92 switch ((int)obj->spi) {
emilmont 10:3bc89ef62ce7 93 case SPI_0: LPC_SC->PCONP |= 1 << 21; break;
emilmont 10:3bc89ef62ce7 94 case SPI_1: LPC_SC->PCONP |= 1 << 10; break;
emilmont 10:3bc89ef62ce7 95 case SPI_2: LPC_SC->PCONP |= 1 << 20; break;
emilmont 10:3bc89ef62ce7 96 }
emilmont 10:3bc89ef62ce7 97
emilmont 10:3bc89ef62ce7 98 // set default format and frequency
emilmont 10:3bc89ef62ce7 99 if (ssel == NC) {
emilmont 10:3bc89ef62ce7 100 spi_format(obj, 8, 0, 0); // 8 bits, mode 0, master
emilmont 10:3bc89ef62ce7 101 } else {
emilmont 10:3bc89ef62ce7 102 spi_format(obj, 8, 0, 1); // 8 bits, mode 0, slave
emilmont 10:3bc89ef62ce7 103 }
emilmont 10:3bc89ef62ce7 104 spi_frequency(obj, 1000000);
emilmont 10:3bc89ef62ce7 105
emilmont 10:3bc89ef62ce7 106 // enable the ssp channel
emilmont 10:3bc89ef62ce7 107 ssp_enable(obj);
emilmont 10:3bc89ef62ce7 108
emilmont 10:3bc89ef62ce7 109 // pin out the spi pins
emilmont 10:3bc89ef62ce7 110 pinmap_pinout(mosi, PinMap_SPI_MOSI);
emilmont 10:3bc89ef62ce7 111 pinmap_pinout(miso, PinMap_SPI_MISO);
emilmont 10:3bc89ef62ce7 112 pinmap_pinout(sclk, PinMap_SPI_SCLK);
emilmont 10:3bc89ef62ce7 113 if (ssel != NC) {
emilmont 10:3bc89ef62ce7 114 pinmap_pinout(ssel, PinMap_SPI_SSEL);
emilmont 10:3bc89ef62ce7 115 }
emilmont 10:3bc89ef62ce7 116 }
emilmont 10:3bc89ef62ce7 117
emilmont 10:3bc89ef62ce7 118 void spi_free(spi_t *obj) {}
emilmont 10:3bc89ef62ce7 119
emilmont 10:3bc89ef62ce7 120 void spi_format(spi_t *obj, int bits, int mode, int slave) {
emilmont 10:3bc89ef62ce7 121 ssp_disable(obj);
emilmont 10:3bc89ef62ce7 122
emilmont 10:3bc89ef62ce7 123 if (!(bits >= 4 && bits <= 16) || !(mode >= 0 && mode <= 3)) {
emilmont 10:3bc89ef62ce7 124 error("SPI format error");
emilmont 10:3bc89ef62ce7 125 }
emilmont 10:3bc89ef62ce7 126
emilmont 10:3bc89ef62ce7 127 int polarity = (mode & 0x2) ? 1 : 0;
emilmont 10:3bc89ef62ce7 128 int phase = (mode & 0x1) ? 1 : 0;
emilmont 10:3bc89ef62ce7 129
emilmont 10:3bc89ef62ce7 130 // set it up
emilmont 10:3bc89ef62ce7 131 int DSS = bits - 1; // DSS (data select size)
emilmont 10:3bc89ef62ce7 132 int SPO = (polarity) ? 1 : 0; // SPO - clock out polarity
emilmont 10:3bc89ef62ce7 133 int SPH = (phase) ? 1 : 0; // SPH - clock out phase
emilmont 10:3bc89ef62ce7 134
emilmont 10:3bc89ef62ce7 135 int FRF = 0; // FRF (frame format) = SPI
emilmont 10:3bc89ef62ce7 136 uint32_t tmp = obj->spi->CR0;
emilmont 10:3bc89ef62ce7 137 tmp &= ~(0xFFFF);
emilmont 10:3bc89ef62ce7 138 tmp |= DSS << 0
emilmont 10:3bc89ef62ce7 139 | FRF << 4
emilmont 10:3bc89ef62ce7 140 | SPO << 6
emilmont 10:3bc89ef62ce7 141 | SPH << 7;
emilmont 10:3bc89ef62ce7 142 obj->spi->CR0 = tmp;
emilmont 10:3bc89ef62ce7 143
emilmont 10:3bc89ef62ce7 144 tmp = obj->spi->CR1;
emilmont 10:3bc89ef62ce7 145 tmp &= ~(0xD);
emilmont 10:3bc89ef62ce7 146 tmp |= 0 << 0 // LBM - loop back mode - off
emilmont 10:3bc89ef62ce7 147 | ((slave) ? 1 : 0) << 2 // MS - master slave mode, 1 = slave
emilmont 10:3bc89ef62ce7 148 | 0 << 3; // SOD - slave output disable - na
emilmont 10:3bc89ef62ce7 149 obj->spi->CR1 = tmp;
emilmont 10:3bc89ef62ce7 150 ssp_enable(obj);
emilmont 10:3bc89ef62ce7 151 }
emilmont 10:3bc89ef62ce7 152
emilmont 10:3bc89ef62ce7 153 void spi_frequency(spi_t *obj, int hz) {
emilmont 10:3bc89ef62ce7 154 ssp_disable(obj);
emilmont 10:3bc89ef62ce7 155
emilmont 10:3bc89ef62ce7 156 uint32_t PCLK = PeripheralClock;
emilmont 10:3bc89ef62ce7 157
emilmont 10:3bc89ef62ce7 158 int prescaler;
emilmont 10:3bc89ef62ce7 159
emilmont 10:3bc89ef62ce7 160 for (prescaler = 2; prescaler <= 254; prescaler += 2) {
emilmont 10:3bc89ef62ce7 161 int prescale_hz = PCLK / prescaler;
emilmont 10:3bc89ef62ce7 162
emilmont 10:3bc89ef62ce7 163 // calculate the divider
emilmont 10:3bc89ef62ce7 164 int divider = floor(((float)prescale_hz / (float)hz) + 0.5f);
emilmont 10:3bc89ef62ce7 165
emilmont 10:3bc89ef62ce7 166 // check we can support the divider
emilmont 10:3bc89ef62ce7 167 if (divider < 256) {
emilmont 10:3bc89ef62ce7 168 // prescaler
emilmont 10:3bc89ef62ce7 169 obj->spi->CPSR = prescaler;
emilmont 10:3bc89ef62ce7 170
emilmont 10:3bc89ef62ce7 171 // divider
emilmont 10:3bc89ef62ce7 172 obj->spi->CR0 &= ~(0xFFFF << 8);
emilmont 10:3bc89ef62ce7 173 obj->spi->CR0 |= (divider - 1) << 8;
emilmont 10:3bc89ef62ce7 174 ssp_enable(obj);
emilmont 10:3bc89ef62ce7 175 return;
emilmont 10:3bc89ef62ce7 176 }
emilmont 10:3bc89ef62ce7 177 }
emilmont 10:3bc89ef62ce7 178 error("Couldn't setup requested SPI frequency");
emilmont 10:3bc89ef62ce7 179 }
emilmont 10:3bc89ef62ce7 180
emilmont 10:3bc89ef62ce7 181 static inline int ssp_disable(spi_t *obj) {
emilmont 10:3bc89ef62ce7 182 return obj->spi->CR1 &= ~(1 << 1);
emilmont 10:3bc89ef62ce7 183 }
emilmont 10:3bc89ef62ce7 184
emilmont 10:3bc89ef62ce7 185 static inline int ssp_enable(spi_t *obj) {
emilmont 10:3bc89ef62ce7 186 return obj->spi->CR1 |= (1 << 1);
emilmont 10:3bc89ef62ce7 187 }
emilmont 10:3bc89ef62ce7 188
emilmont 10:3bc89ef62ce7 189 static inline int ssp_readable(spi_t *obj) {
emilmont 10:3bc89ef62ce7 190 return obj->spi->SR & (1 << 2);
emilmont 10:3bc89ef62ce7 191 }
emilmont 10:3bc89ef62ce7 192
emilmont 10:3bc89ef62ce7 193 static inline int ssp_writeable(spi_t *obj) {
emilmont 10:3bc89ef62ce7 194 return obj->spi->SR & (1 << 1);
emilmont 10:3bc89ef62ce7 195 }
emilmont 10:3bc89ef62ce7 196
emilmont 10:3bc89ef62ce7 197 static inline void ssp_write(spi_t *obj, int value) {
emilmont 10:3bc89ef62ce7 198 while (!ssp_writeable(obj));
emilmont 10:3bc89ef62ce7 199 obj->spi->DR = value;
emilmont 10:3bc89ef62ce7 200 }
emilmont 10:3bc89ef62ce7 201
emilmont 10:3bc89ef62ce7 202 static inline int ssp_read(spi_t *obj) {
emilmont 10:3bc89ef62ce7 203 while (!ssp_readable(obj));
emilmont 10:3bc89ef62ce7 204 return obj->spi->DR;
emilmont 10:3bc89ef62ce7 205 }
emilmont 10:3bc89ef62ce7 206
emilmont 10:3bc89ef62ce7 207 static inline int ssp_busy(spi_t *obj) {
emilmont 10:3bc89ef62ce7 208 return (obj->spi->SR & (1 << 4)) ? (1) : (0);
emilmont 10:3bc89ef62ce7 209 }
emilmont 10:3bc89ef62ce7 210
emilmont 10:3bc89ef62ce7 211 int spi_master_write(spi_t *obj, int value) {
emilmont 10:3bc89ef62ce7 212 ssp_write(obj, value);
emilmont 10:3bc89ef62ce7 213 return ssp_read(obj);
emilmont 10:3bc89ef62ce7 214 }
emilmont 10:3bc89ef62ce7 215
emilmont 10:3bc89ef62ce7 216 int spi_slave_receive(spi_t *obj) {
emilmont 10:3bc89ef62ce7 217 return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0);
emilmont 10:3bc89ef62ce7 218 };
emilmont 10:3bc89ef62ce7 219
emilmont 10:3bc89ef62ce7 220 int spi_slave_read(spi_t *obj) {
emilmont 10:3bc89ef62ce7 221 return obj->spi->DR;
emilmont 10:3bc89ef62ce7 222 }
emilmont 10:3bc89ef62ce7 223
emilmont 10:3bc89ef62ce7 224 void spi_slave_write(spi_t *obj, int value) {
emilmont 10:3bc89ef62ce7 225 while (ssp_writeable(obj) == 0) ;
emilmont 10:3bc89ef62ce7 226 obj->spi->DR = value;
emilmont 10:3bc89ef62ce7 227 }
emilmont 10:3bc89ef62ce7 228
emilmont 10:3bc89ef62ce7 229 int spi_busy(spi_t *obj) {
emilmont 10:3bc89ef62ce7 230 return ssp_busy(obj);
emilmont 10:3bc89ef62ce7 231 }