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:
mbed_official
Date:
Fri Apr 11 17:30:07 2014 +0100
Revision:
158:3121b9889f7b
Parent:
149:1fb5f62b92bd
Child:
227:7bd0639b8911
Synchronized with git revision efbbb2f3070ba66a206581185b033ef0343242d7

Full URL: https://github.com/mbedmicro/mbed/commit/efbbb2f3070ba66a206581185b033ef0343242d7/

Conflicts:
libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/gpio_irq_api.c
libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/gpio_irq_api.c
libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/gpio_irq_api.c

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 146:f64d43ff0c18 1 /* mbed Microcontroller Library
mbed_official 146:f64d43ff0c18 2 * Copyright (c) 2013 ARM Limited
mbed_official 146:f64d43ff0c18 3 *
mbed_official 146:f64d43ff0c18 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 146:f64d43ff0c18 5 * you may not use this file except in compliance with the License.
mbed_official 146:f64d43ff0c18 6 * You may obtain a copy of the License at
mbed_official 146:f64d43ff0c18 7 *
mbed_official 146:f64d43ff0c18 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 146:f64d43ff0c18 9 *
mbed_official 146:f64d43ff0c18 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 146:f64d43ff0c18 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 146:f64d43ff0c18 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 146:f64d43ff0c18 13 * See the License for the specific language governing permissions and
mbed_official 146:f64d43ff0c18 14 * limitations under the License.
mbed_official 146:f64d43ff0c18 15 */
mbed_official 146:f64d43ff0c18 16 #include "spi_api.h"
mbed_official 146:f64d43ff0c18 17
mbed_official 146:f64d43ff0c18 18 #include <math.h>
mbed_official 146:f64d43ff0c18 19
mbed_official 146:f64d43ff0c18 20 #include "cmsis.h"
mbed_official 146:f64d43ff0c18 21 #include "pinmap.h"
mbed_official 146:f64d43ff0c18 22 #include "error.h"
mbed_official 146:f64d43ff0c18 23 #include "fsl_clock_manager.h"
mbed_official 146:f64d43ff0c18 24 #include "fsl_dspi_hal.h"
mbed_official 146:f64d43ff0c18 25
mbed_official 146:f64d43ff0c18 26 static const PinMap PinMap_SPI_SCLK[] = {
mbed_official 146:f64d43ff0c18 27 {PTD1 , SPI_0, 2},
mbed_official 146:f64d43ff0c18 28 {PTE2 , SPI_1, 2},
mbed_official 146:f64d43ff0c18 29 {PTA15, SPI_0, 2},
mbed_official 146:f64d43ff0c18 30 {PTB11, SPI_1, 2},
mbed_official 146:f64d43ff0c18 31 {PTB21, SPI_2, 2},
mbed_official 146:f64d43ff0c18 32 {PTC5 , SPI_0, 2},
mbed_official 146:f64d43ff0c18 33 {PTD5 , SPI_1, 7},
mbed_official 146:f64d43ff0c18 34 {NC , NC , 0}
mbed_official 146:f64d43ff0c18 35 };
mbed_official 146:f64d43ff0c18 36
mbed_official 146:f64d43ff0c18 37 static const PinMap PinMap_SPI_MOSI[] = {
mbed_official 146:f64d43ff0c18 38 {PTD2 , SPI_0, 2},
mbed_official 146:f64d43ff0c18 39 {PTE1 , SPI_1, 2},
mbed_official 146:f64d43ff0c18 40 {PTE3 , SPI_1, 7},
mbed_official 146:f64d43ff0c18 41 {PTA16, SPI_0, 2},
mbed_official 146:f64d43ff0c18 42 {PTB16, SPI_1, 2},
mbed_official 146:f64d43ff0c18 43 {PTB22, SPI_2, 2},
mbed_official 146:f64d43ff0c18 44 {PTC6 , SPI_0, 2},
mbed_official 158:3121b9889f7b 45 {PTD6 , SPI_1, 7},
mbed_official 146:f64d43ff0c18 46 {NC , NC , 0}
mbed_official 146:f64d43ff0c18 47 };
mbed_official 146:f64d43ff0c18 48
mbed_official 146:f64d43ff0c18 49 static const PinMap PinMap_SPI_MISO[] = {
mbed_official 146:f64d43ff0c18 50 {PTD3 , SPI_0, 2},
mbed_official 146:f64d43ff0c18 51 {PTE1 , SPI_1, 7},
mbed_official 146:f64d43ff0c18 52 {PTE3 , SPI_1, 2},
mbed_official 146:f64d43ff0c18 53 {PTA17, SPI_0, 2},
mbed_official 146:f64d43ff0c18 54 {PTB17, SPI_1, 2},
mbed_official 146:f64d43ff0c18 55 {PTB23, SPI_2, 2},
mbed_official 146:f64d43ff0c18 56 {PTC7 , SPI_0, 2},
mbed_official 146:f64d43ff0c18 57 {PTD7 , SPI_1, 7},
mbed_official 146:f64d43ff0c18 58 {NC , NC , 0}
mbed_official 146:f64d43ff0c18 59 };
mbed_official 146:f64d43ff0c18 60
mbed_official 146:f64d43ff0c18 61 static const PinMap PinMap_SPI_SSEL[] = {
mbed_official 146:f64d43ff0c18 62 {PTD0 , SPI_0, 2},
mbed_official 146:f64d43ff0c18 63 {PTE4 , SPI_1, 2},
mbed_official 146:f64d43ff0c18 64 {PTA14, SPI_0, 2},
mbed_official 146:f64d43ff0c18 65 {PTB10, SPI_1, 2},
mbed_official 146:f64d43ff0c18 66 {PTB20, SPI_2, 2},
mbed_official 146:f64d43ff0c18 67 {PTC4 , SPI_0, 2},
mbed_official 146:f64d43ff0c18 68 {PTD4 , SPI_1, 7},
mbed_official 146:f64d43ff0c18 69 {NC , NC , 0}
mbed_official 146:f64d43ff0c18 70 };
mbed_official 146:f64d43ff0c18 71
mbed_official 146:f64d43ff0c18 72 static void spi_set_delays(uint32_t instance) {
mbed_official 146:f64d43ff0c18 73 dspi_delay_settings_config_t delay_config;
mbed_official 146:f64d43ff0c18 74 delay_config.pcsToSck = 1; /*!< PCS to SCK delay (CSSCK): initialize the scalar
mbed_official 146:f64d43ff0c18 75 * value to '1' to provide the master with a little
mbed_official 146:f64d43ff0c18 76 * more data-in read setup time.
mbed_official 146:f64d43ff0c18 77 */
mbed_official 146:f64d43ff0c18 78 delay_config.pcsToSckPre = 0; /*!< PCS to SCK delay prescalar (PCSSCK) */
mbed_official 146:f64d43ff0c18 79 delay_config.afterSckPre = 0; /*!< After SCK delay prescalar (PASC)*/
mbed_official 146:f64d43ff0c18 80 delay_config.afterSck = 0; /*!< After SCK delay scalar (ASC)*/
mbed_official 146:f64d43ff0c18 81 delay_config.afterTransferPre = 0; /*!< Delay after transfer prescalar (PDT)*/
mbed_official 146:f64d43ff0c18 82 delay_config.afterTransfer = 0;
mbed_official 146:f64d43ff0c18 83 dspi_hal_configure_delays(instance, kDspiCtar0, &delay_config);
mbed_official 146:f64d43ff0c18 84 }
mbed_official 146:f64d43ff0c18 85
mbed_official 146:f64d43ff0c18 86 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) {
mbed_official 146:f64d43ff0c18 87 // determine the SPI to use
mbed_official 146:f64d43ff0c18 88 uint32_t spi_mosi = pinmap_peripheral(mosi, PinMap_SPI_MOSI);
mbed_official 146:f64d43ff0c18 89 uint32_t spi_miso = pinmap_peripheral(miso, PinMap_SPI_MISO);
mbed_official 146:f64d43ff0c18 90 uint32_t spi_sclk = pinmap_peripheral(sclk, PinMap_SPI_SCLK);
mbed_official 146:f64d43ff0c18 91 uint32_t spi_ssel = pinmap_peripheral(ssel, PinMap_SPI_SSEL);
mbed_official 146:f64d43ff0c18 92 uint32_t spi_data = pinmap_merge(spi_mosi, spi_miso);
mbed_official 146:f64d43ff0c18 93 uint32_t spi_cntl = pinmap_merge(spi_sclk, spi_ssel);
mbed_official 146:f64d43ff0c18 94
mbed_official 146:f64d43ff0c18 95 obj->instance = pinmap_merge(spi_data, spi_cntl);
mbed_official 146:f64d43ff0c18 96 if ((int)obj->instance == NC) {
mbed_official 146:f64d43ff0c18 97 error("SPI pinout mapping failed");
mbed_official 146:f64d43ff0c18 98 }
mbed_official 146:f64d43ff0c18 99
mbed_official 146:f64d43ff0c18 100 // enable power and clocking
mbed_official 146:f64d43ff0c18 101 clock_manager_set_gate(kClockModuleSPI, obj->instance, true);
mbed_official 146:f64d43ff0c18 102
mbed_official 146:f64d43ff0c18 103 dspi_hal_disable(obj->instance);
mbed_official 146:f64d43ff0c18 104 // set default format and frequency
mbed_official 146:f64d43ff0c18 105 if (ssel == NC) {
mbed_official 146:f64d43ff0c18 106 spi_format(obj, 8, 0, 0); // 8 bits, mode 0, master
mbed_official 146:f64d43ff0c18 107 } else {
mbed_official 146:f64d43ff0c18 108 spi_format(obj, 8, 0, 1); // 8 bits, mode 0, slave
mbed_official 146:f64d43ff0c18 109 }
mbed_official 146:f64d43ff0c18 110 spi_set_delays(obj->instance);
mbed_official 146:f64d43ff0c18 111 spi_frequency(obj, 1000000);
mbed_official 146:f64d43ff0c18 112
mbed_official 146:f64d43ff0c18 113 dspi_hal_enable(obj->instance);
mbed_official 146:f64d43ff0c18 114 dspi_hal_start_transfer(obj->instance);
mbed_official 146:f64d43ff0c18 115
mbed_official 146:f64d43ff0c18 116 // pin out the spi pins
mbed_official 146:f64d43ff0c18 117 pinmap_pinout(mosi, PinMap_SPI_MOSI);
mbed_official 146:f64d43ff0c18 118 pinmap_pinout(miso, PinMap_SPI_MISO);
mbed_official 146:f64d43ff0c18 119 pinmap_pinout(sclk, PinMap_SPI_SCLK);
mbed_official 146:f64d43ff0c18 120 if (ssel != NC) {
mbed_official 146:f64d43ff0c18 121 pinmap_pinout(ssel, PinMap_SPI_SSEL);
mbed_official 146:f64d43ff0c18 122 }
mbed_official 146:f64d43ff0c18 123 }
mbed_official 146:f64d43ff0c18 124
mbed_official 146:f64d43ff0c18 125 void spi_free(spi_t *obj) {
mbed_official 146:f64d43ff0c18 126 // [TODO]
mbed_official 146:f64d43ff0c18 127 }
mbed_official 146:f64d43ff0c18 128 void spi_format(spi_t *obj, int bits, int mode, int slave) {
mbed_official 146:f64d43ff0c18 129 dspi_data_format_config_t config = {0};
mbed_official 146:f64d43ff0c18 130 config.bitsPerFrame = (uint32_t)bits;
mbed_official 146:f64d43ff0c18 131 config.clkPolarity = (mode & 0x2) ? kDspiClockPolarity_ActiveLow : kDspiClockPolarity_ActiveHigh;
mbed_official 146:f64d43ff0c18 132 config.clkPhase = (mode & 0x1) ? kDspiClockPhase_SecondEdge : kDspiClockPhase_FirstEdge;
mbed_official 146:f64d43ff0c18 133 config.direction = kDspiMsbFirst;
mbed_official 146:f64d43ff0c18 134 dspi_status_t result = dspi_hal_configure_data_format(obj->instance, kDspiCtar0, &config);
mbed_official 146:f64d43ff0c18 135 if (result != kStatus_DSPI_Success) {
mbed_official 146:f64d43ff0c18 136 error("Failed to configure SPI data format");
mbed_official 146:f64d43ff0c18 137 }
mbed_official 146:f64d43ff0c18 138
mbed_official 146:f64d43ff0c18 139 if (slave) {
mbed_official 146:f64d43ff0c18 140 dspi_hal_set_master_slave(obj->instance, kDspiSlave);
mbed_official 146:f64d43ff0c18 141 } else {
mbed_official 146:f64d43ff0c18 142 dspi_hal_set_master_slave(obj->instance, kDspiMaster);
mbed_official 146:f64d43ff0c18 143 }
mbed_official 146:f64d43ff0c18 144 }
mbed_official 146:f64d43ff0c18 145
mbed_official 146:f64d43ff0c18 146 void spi_frequency(spi_t *obj, int hz) {
mbed_official 146:f64d43ff0c18 147 uint32_t busClock;
mbed_official 146:f64d43ff0c18 148 clock_manager_get_frequency(kBusClock, &busClock);
mbed_official 146:f64d43ff0c18 149 dspi_hal_set_baud(obj->instance, kDspiCtar0, (uint32_t)hz, busClock);
mbed_official 146:f64d43ff0c18 150 }
mbed_official 146:f64d43ff0c18 151
mbed_official 146:f64d43ff0c18 152 static inline int spi_writeable(spi_t * obj) {
mbed_official 146:f64d43ff0c18 153 return dspi_hal_get_status_flag(obj->instance, kDspiTxFifoFillRequest);
mbed_official 146:f64d43ff0c18 154 }
mbed_official 146:f64d43ff0c18 155
mbed_official 146:f64d43ff0c18 156 static inline int spi_readable(spi_t * obj) {
mbed_official 146:f64d43ff0c18 157 return dspi_hal_get_status_flag(obj->instance, kDspiRxFifoDrainRequest);
mbed_official 146:f64d43ff0c18 158 }
mbed_official 146:f64d43ff0c18 159
mbed_official 146:f64d43ff0c18 160 int spi_master_write(spi_t *obj, int value) {
mbed_official 146:f64d43ff0c18 161 // wait tx buffer empty
mbed_official 146:f64d43ff0c18 162 while(!spi_writeable(obj));
mbed_official 146:f64d43ff0c18 163 dspi_command_config_t command = {0};
mbed_official 146:f64d43ff0c18 164 command.isEndOfQueue = true;
mbed_official 146:f64d43ff0c18 165 command.isChipSelectContinuous = 0;
mbed_official 146:f64d43ff0c18 166 dspi_hal_write_data_master_mode(obj->instance, &command, (uint16_t)value);
mbed_official 146:f64d43ff0c18 167 dspi_hal_clear_status_flag(obj->instance, kDspiTxFifoFillRequest);
mbed_official 146:f64d43ff0c18 168
mbed_official 146:f64d43ff0c18 169 // wait rx buffer full
mbed_official 146:f64d43ff0c18 170 while (!spi_readable(obj));
mbed_official 146:f64d43ff0c18 171 dspi_hal_clear_status_flag(obj->instance, kDspiRxFifoDrainRequest);
mbed_official 146:f64d43ff0c18 172 return dspi_hal_read_data(obj->instance) & 0xff;
mbed_official 146:f64d43ff0c18 173 }
mbed_official 146:f64d43ff0c18 174
mbed_official 146:f64d43ff0c18 175 int spi_slave_receive(spi_t *obj) {
mbed_official 146:f64d43ff0c18 176 return spi_readable(obj);
mbed_official 146:f64d43ff0c18 177 }
mbed_official 146:f64d43ff0c18 178
mbed_official 146:f64d43ff0c18 179 int spi_slave_read(spi_t *obj) {
mbed_official 146:f64d43ff0c18 180 return dspi_hal_read_data(obj->instance);
mbed_official 146:f64d43ff0c18 181 }
mbed_official 146:f64d43ff0c18 182
mbed_official 146:f64d43ff0c18 183 void spi_slave_write(spi_t *obj, int value) {
mbed_official 146:f64d43ff0c18 184 while (!spi_writeable(obj));
mbed_official 146:f64d43ff0c18 185 dspi_hal_write_data_slave_mode(obj->instance, (uint32_t)value);
mbed_official 146:f64d43ff0c18 186 }