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:
Thu Apr 03 11:45:06 2014 +0100
Revision:
149:1fb5f62b92bd
Parent:
targets/hal/TARGET_Freescale/TARGET_KSDK_MCUS/TARGET_K64F/spi_api.c@146:f64d43ff0c18
Child:
158:3121b9889f7b
Synchronized with git revision 220c0bb39ceee40016e1e86350c058963d01ed42

Full URL: https://github.com/mbedmicro/mbed/commit/220c0bb39ceee40016e1e86350c058963d01ed42/

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 146:f64d43ff0c18 45 {PTD2 , SPI_0, 2},
mbed_official 146:f64d43ff0c18 46 {PTD6 , SPI_0, 7},
mbed_official 146:f64d43ff0c18 47 {NC , NC , 0}
mbed_official 146:f64d43ff0c18 48 };
mbed_official 146:f64d43ff0c18 49
mbed_official 146:f64d43ff0c18 50 static const PinMap PinMap_SPI_MISO[] = {
mbed_official 146:f64d43ff0c18 51 {PTD3 , SPI_0, 2},
mbed_official 146:f64d43ff0c18 52 {PTE1 , SPI_1, 7},
mbed_official 146:f64d43ff0c18 53 {PTE3 , SPI_1, 2},
mbed_official 146:f64d43ff0c18 54 {PTA17, SPI_0, 2},
mbed_official 146:f64d43ff0c18 55 {PTB17, SPI_1, 2},
mbed_official 146:f64d43ff0c18 56 {PTB23, SPI_2, 2},
mbed_official 146:f64d43ff0c18 57 {PTC7 , SPI_0, 2},
mbed_official 146:f64d43ff0c18 58 {PTD3 , SPI_0, 2},
mbed_official 146:f64d43ff0c18 59 {PTD7 , SPI_1, 7},
mbed_official 146:f64d43ff0c18 60 {NC , NC , 0}
mbed_official 146:f64d43ff0c18 61 };
mbed_official 146:f64d43ff0c18 62
mbed_official 146:f64d43ff0c18 63 static const PinMap PinMap_SPI_SSEL[] = {
mbed_official 146:f64d43ff0c18 64 {PTD0 , SPI_0, 2},
mbed_official 146:f64d43ff0c18 65 {PTE4 , SPI_1, 2},
mbed_official 146:f64d43ff0c18 66 {PTA14, SPI_0, 2},
mbed_official 146:f64d43ff0c18 67 {PTB10, SPI_1, 2},
mbed_official 146:f64d43ff0c18 68 {PTB20, SPI_2, 2},
mbed_official 146:f64d43ff0c18 69 {PTC4 , SPI_0, 2},
mbed_official 146:f64d43ff0c18 70 {PTD4 , SPI_1, 7},
mbed_official 146:f64d43ff0c18 71 {NC , NC , 0}
mbed_official 146:f64d43ff0c18 72 };
mbed_official 146:f64d43ff0c18 73
mbed_official 146:f64d43ff0c18 74 static void spi_set_delays(uint32_t instance) {
mbed_official 146:f64d43ff0c18 75 dspi_delay_settings_config_t delay_config;
mbed_official 146:f64d43ff0c18 76 delay_config.pcsToSck = 1; /*!< PCS to SCK delay (CSSCK): initialize the scalar
mbed_official 146:f64d43ff0c18 77 * value to '1' to provide the master with a little
mbed_official 146:f64d43ff0c18 78 * more data-in read setup time.
mbed_official 146:f64d43ff0c18 79 */
mbed_official 146:f64d43ff0c18 80 delay_config.pcsToSckPre = 0; /*!< PCS to SCK delay prescalar (PCSSCK) */
mbed_official 146:f64d43ff0c18 81 delay_config.afterSckPre = 0; /*!< After SCK delay prescalar (PASC)*/
mbed_official 146:f64d43ff0c18 82 delay_config.afterSck = 0; /*!< After SCK delay scalar (ASC)*/
mbed_official 146:f64d43ff0c18 83 delay_config.afterTransferPre = 0; /*!< Delay after transfer prescalar (PDT)*/
mbed_official 146:f64d43ff0c18 84 delay_config.afterTransfer = 0;
mbed_official 146:f64d43ff0c18 85 dspi_hal_configure_delays(instance, kDspiCtar0, &delay_config);
mbed_official 146:f64d43ff0c18 86 }
mbed_official 146:f64d43ff0c18 87
mbed_official 146:f64d43ff0c18 88 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) {
mbed_official 146:f64d43ff0c18 89 // determine the SPI to use
mbed_official 146:f64d43ff0c18 90 uint32_t spi_mosi = pinmap_peripheral(mosi, PinMap_SPI_MOSI);
mbed_official 146:f64d43ff0c18 91 uint32_t spi_miso = pinmap_peripheral(miso, PinMap_SPI_MISO);
mbed_official 146:f64d43ff0c18 92 uint32_t spi_sclk = pinmap_peripheral(sclk, PinMap_SPI_SCLK);
mbed_official 146:f64d43ff0c18 93 uint32_t spi_ssel = pinmap_peripheral(ssel, PinMap_SPI_SSEL);
mbed_official 146:f64d43ff0c18 94 uint32_t spi_data = pinmap_merge(spi_mosi, spi_miso);
mbed_official 146:f64d43ff0c18 95 uint32_t spi_cntl = pinmap_merge(spi_sclk, spi_ssel);
mbed_official 146:f64d43ff0c18 96
mbed_official 146:f64d43ff0c18 97 obj->instance = pinmap_merge(spi_data, spi_cntl);
mbed_official 146:f64d43ff0c18 98 if ((int)obj->instance == NC) {
mbed_official 146:f64d43ff0c18 99 error("SPI pinout mapping failed");
mbed_official 146:f64d43ff0c18 100 }
mbed_official 146:f64d43ff0c18 101
mbed_official 146:f64d43ff0c18 102 // enable power and clocking
mbed_official 146:f64d43ff0c18 103 clock_manager_set_gate(kClockModuleSPI, obj->instance, true);
mbed_official 146:f64d43ff0c18 104
mbed_official 146:f64d43ff0c18 105 dspi_hal_disable(obj->instance);
mbed_official 146:f64d43ff0c18 106 // set default format and frequency
mbed_official 146:f64d43ff0c18 107 if (ssel == NC) {
mbed_official 146:f64d43ff0c18 108 spi_format(obj, 8, 0, 0); // 8 bits, mode 0, master
mbed_official 146:f64d43ff0c18 109 } else {
mbed_official 146:f64d43ff0c18 110 spi_format(obj, 8, 0, 1); // 8 bits, mode 0, slave
mbed_official 146:f64d43ff0c18 111 }
mbed_official 146:f64d43ff0c18 112 spi_set_delays(obj->instance);
mbed_official 146:f64d43ff0c18 113 spi_frequency(obj, 1000000);
mbed_official 146:f64d43ff0c18 114
mbed_official 146:f64d43ff0c18 115 dspi_hal_enable(obj->instance);
mbed_official 146:f64d43ff0c18 116 dspi_hal_start_transfer(obj->instance);
mbed_official 146:f64d43ff0c18 117
mbed_official 146:f64d43ff0c18 118 // pin out the spi pins
mbed_official 146:f64d43ff0c18 119 pinmap_pinout(mosi, PinMap_SPI_MOSI);
mbed_official 146:f64d43ff0c18 120 pinmap_pinout(miso, PinMap_SPI_MISO);
mbed_official 146:f64d43ff0c18 121 pinmap_pinout(sclk, PinMap_SPI_SCLK);
mbed_official 146:f64d43ff0c18 122 if (ssel != NC) {
mbed_official 146:f64d43ff0c18 123 pinmap_pinout(ssel, PinMap_SPI_SSEL);
mbed_official 146:f64d43ff0c18 124 }
mbed_official 146:f64d43ff0c18 125 }
mbed_official 146:f64d43ff0c18 126
mbed_official 146:f64d43ff0c18 127 void spi_free(spi_t *obj) {
mbed_official 146:f64d43ff0c18 128 // [TODO]
mbed_official 146:f64d43ff0c18 129 }
mbed_official 146:f64d43ff0c18 130 void spi_format(spi_t *obj, int bits, int mode, int slave) {
mbed_official 146:f64d43ff0c18 131 dspi_data_format_config_t config = {0};
mbed_official 146:f64d43ff0c18 132 config.bitsPerFrame = (uint32_t)bits;
mbed_official 146:f64d43ff0c18 133 config.clkPolarity = (mode & 0x2) ? kDspiClockPolarity_ActiveLow : kDspiClockPolarity_ActiveHigh;
mbed_official 146:f64d43ff0c18 134 config.clkPhase = (mode & 0x1) ? kDspiClockPhase_SecondEdge : kDspiClockPhase_FirstEdge;
mbed_official 146:f64d43ff0c18 135 config.direction = kDspiMsbFirst;
mbed_official 146:f64d43ff0c18 136 dspi_status_t result = dspi_hal_configure_data_format(obj->instance, kDspiCtar0, &config);
mbed_official 146:f64d43ff0c18 137 if (result != kStatus_DSPI_Success) {
mbed_official 146:f64d43ff0c18 138 error("Failed to configure SPI data format");
mbed_official 146:f64d43ff0c18 139 }
mbed_official 146:f64d43ff0c18 140
mbed_official 146:f64d43ff0c18 141 if (slave) {
mbed_official 146:f64d43ff0c18 142 dspi_hal_set_master_slave(obj->instance, kDspiSlave);
mbed_official 146:f64d43ff0c18 143 } else {
mbed_official 146:f64d43ff0c18 144 dspi_hal_set_master_slave(obj->instance, kDspiMaster);
mbed_official 146:f64d43ff0c18 145 }
mbed_official 146:f64d43ff0c18 146 }
mbed_official 146:f64d43ff0c18 147
mbed_official 146:f64d43ff0c18 148 void spi_frequency(spi_t *obj, int hz) {
mbed_official 146:f64d43ff0c18 149 uint32_t busClock;
mbed_official 146:f64d43ff0c18 150 clock_manager_get_frequency(kBusClock, &busClock);
mbed_official 146:f64d43ff0c18 151 dspi_hal_set_baud(obj->instance, kDspiCtar0, (uint32_t)hz, busClock);
mbed_official 146:f64d43ff0c18 152 }
mbed_official 146:f64d43ff0c18 153
mbed_official 146:f64d43ff0c18 154 static inline int spi_writeable(spi_t * obj) {
mbed_official 146:f64d43ff0c18 155 return dspi_hal_get_status_flag(obj->instance, kDspiTxFifoFillRequest);
mbed_official 146:f64d43ff0c18 156 }
mbed_official 146:f64d43ff0c18 157
mbed_official 146:f64d43ff0c18 158 static inline int spi_readable(spi_t * obj) {
mbed_official 146:f64d43ff0c18 159 return dspi_hal_get_status_flag(obj->instance, kDspiRxFifoDrainRequest);
mbed_official 146:f64d43ff0c18 160 }
mbed_official 146:f64d43ff0c18 161
mbed_official 146:f64d43ff0c18 162 int spi_master_write(spi_t *obj, int value) {
mbed_official 146:f64d43ff0c18 163 // wait tx buffer empty
mbed_official 146:f64d43ff0c18 164 while(!spi_writeable(obj));
mbed_official 146:f64d43ff0c18 165 dspi_command_config_t command = {0};
mbed_official 146:f64d43ff0c18 166 command.isEndOfQueue = true;
mbed_official 146:f64d43ff0c18 167 command.isChipSelectContinuous = 0;
mbed_official 146:f64d43ff0c18 168 dspi_hal_write_data_master_mode(obj->instance, &command, (uint16_t)value);
mbed_official 146:f64d43ff0c18 169 dspi_hal_clear_status_flag(obj->instance, kDspiTxFifoFillRequest);
mbed_official 146:f64d43ff0c18 170
mbed_official 146:f64d43ff0c18 171 // wait rx buffer full
mbed_official 146:f64d43ff0c18 172 while (!spi_readable(obj));
mbed_official 146:f64d43ff0c18 173 dspi_hal_clear_status_flag(obj->instance, kDspiRxFifoDrainRequest);
mbed_official 146:f64d43ff0c18 174 return dspi_hal_read_data(obj->instance) & 0xff;
mbed_official 146:f64d43ff0c18 175 }
mbed_official 146:f64d43ff0c18 176
mbed_official 146:f64d43ff0c18 177 int spi_slave_receive(spi_t *obj) {
mbed_official 146:f64d43ff0c18 178 return spi_readable(obj);
mbed_official 146:f64d43ff0c18 179 }
mbed_official 146:f64d43ff0c18 180
mbed_official 146:f64d43ff0c18 181 int spi_slave_read(spi_t *obj) {
mbed_official 146:f64d43ff0c18 182 return dspi_hal_read_data(obj->instance);
mbed_official 146:f64d43ff0c18 183 }
mbed_official 146:f64d43ff0c18 184
mbed_official 146:f64d43ff0c18 185 void spi_slave_write(spi_t *obj, int value) {
mbed_official 146:f64d43ff0c18 186 while (!spi_writeable(obj));
mbed_official 146:f64d43ff0c18 187 dspi_hal_write_data_slave_mode(obj->instance, (uint32_t)value);
mbed_official 146:f64d43ff0c18 188 }