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:
Wed Apr 08 07:45:08 2015 +0100
Revision:
507:d4fc7603a669
Child:
546:a843b1cd80b8
Synchronized with git revision 158cbeb2927b64c560005dbec6f60463a468c9da

Full URL: https://github.com/mbedmicro/mbed/commit/158cbeb2927b64c560005dbec6f60463a468c9da/

USB - Add macros to alias the endpoint callback functions to support configurability

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 507:d4fc7603a669 1 /*******************************************************************************
mbed_official 507:d4fc7603a669 2 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
mbed_official 507:d4fc7603a669 3 *
mbed_official 507:d4fc7603a669 4 * Permission is hereby granted, free of charge, to any person obtaining a
mbed_official 507:d4fc7603a669 5 * copy of this software and associated documentation files (the "Software"),
mbed_official 507:d4fc7603a669 6 * to deal in the Software without restriction, including without limitation
mbed_official 507:d4fc7603a669 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
mbed_official 507:d4fc7603a669 8 * and/or sell copies of the Software, and to permit persons to whom the
mbed_official 507:d4fc7603a669 9 * Software is furnished to do so, subject to the following conditions:
mbed_official 507:d4fc7603a669 10 *
mbed_official 507:d4fc7603a669 11 * The above copyright notice and this permission notice shall be included
mbed_official 507:d4fc7603a669 12 * in all copies or substantial portions of the Software.
mbed_official 507:d4fc7603a669 13 *
mbed_official 507:d4fc7603a669 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
mbed_official 507:d4fc7603a669 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
mbed_official 507:d4fc7603a669 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
mbed_official 507:d4fc7603a669 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
mbed_official 507:d4fc7603a669 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
mbed_official 507:d4fc7603a669 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
mbed_official 507:d4fc7603a669 20 * OTHER DEALINGS IN THE SOFTWARE.
mbed_official 507:d4fc7603a669 21 *
mbed_official 507:d4fc7603a669 22 * Except as contained in this notice, the name of Maxim Integrated
mbed_official 507:d4fc7603a669 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
mbed_official 507:d4fc7603a669 24 * Products, Inc. Branding Policy.
mbed_official 507:d4fc7603a669 25 *
mbed_official 507:d4fc7603a669 26 * The mere transfer of this software does not imply any licenses
mbed_official 507:d4fc7603a669 27 * of trade secrets, proprietary technology, copyrights, patents,
mbed_official 507:d4fc7603a669 28 * trademarks, maskwork rights, or any other form of intellectual
mbed_official 507:d4fc7603a669 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
mbed_official 507:d4fc7603a669 30 * ownership rights.
mbed_official 507:d4fc7603a669 31 *******************************************************************************
mbed_official 507:d4fc7603a669 32 */
mbed_official 507:d4fc7603a669 33
mbed_official 507:d4fc7603a669 34 #include <string.h>
mbed_official 507:d4fc7603a669 35 #include "mbed_assert.h"
mbed_official 507:d4fc7603a669 36 #include "cmsis.h"
mbed_official 507:d4fc7603a669 37 #include "spi_api.h"
mbed_official 507:d4fc7603a669 38 #include "pinmap.h"
mbed_official 507:d4fc7603a669 39 #include "ioman_regs.h"
mbed_official 507:d4fc7603a669 40 #include "clkman_regs.h"
mbed_official 507:d4fc7603a669 41 #include "PeripheralPins.h"
mbed_official 507:d4fc7603a669 42
mbed_official 507:d4fc7603a669 43 #define DEFAULT_CHAR 8
mbed_official 507:d4fc7603a669 44 #define DEFAULT_MODE 0
mbed_official 507:d4fc7603a669 45 #define DEFAULT_FREQ 1000000
mbed_official 507:d4fc7603a669 46
mbed_official 507:d4fc7603a669 47 // Formatting settings
mbed_official 507:d4fc7603a669 48 static int spi_bits;
mbed_official 507:d4fc7603a669 49
mbed_official 507:d4fc7603a669 50 //******************************************************************************
mbed_official 507:d4fc7603a669 51 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
mbed_official 507:d4fc7603a669 52 {
mbed_official 507:d4fc7603a669 53 // Make sure pins are pointing to the same SPI instance
mbed_official 507:d4fc7603a669 54 SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
mbed_official 507:d4fc7603a669 55 SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
mbed_official 507:d4fc7603a669 56 SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
mbed_official 507:d4fc7603a669 57 SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
mbed_official 507:d4fc7603a669 58
mbed_official 507:d4fc7603a669 59 SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
mbed_official 507:d4fc7603a669 60 SPIName spi_cntl;
mbed_official 507:d4fc7603a669 61
mbed_official 507:d4fc7603a669 62 // Give the application the option to manually control Slave Select
mbed_official 507:d4fc7603a669 63 if((SPIName)spi_ssel != (SPIName)NC) {
mbed_official 507:d4fc7603a669 64 spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
mbed_official 507:d4fc7603a669 65 } else {
mbed_official 507:d4fc7603a669 66 spi_cntl = spi_sclk;
mbed_official 507:d4fc7603a669 67 }
mbed_official 507:d4fc7603a669 68
mbed_official 507:d4fc7603a669 69 SPIName spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
mbed_official 507:d4fc7603a669 70
mbed_official 507:d4fc7603a669 71 MBED_ASSERT((SPIName)spi != (SPIName)NC);
mbed_official 507:d4fc7603a669 72
mbed_official 507:d4fc7603a669 73 // Set the obj pointer to the proper SPI Instance
mbed_official 507:d4fc7603a669 74 obj->spi = (mxc_spi_regs_t*)spi;
mbed_official 507:d4fc7603a669 75
mbed_official 507:d4fc7603a669 76 // Set the SPI index and FIFOs
mbed_official 507:d4fc7603a669 77 obj->index = MXC_SPI_BASE_TO_INSTANCE(obj->spi);
mbed_official 507:d4fc7603a669 78 obj->rxfifo = MXC_SPI_GET_RXFIFO(obj->index);
mbed_official 507:d4fc7603a669 79 obj->txfifo = MXC_SPI_GET_TXFIFO(obj->index);
mbed_official 507:d4fc7603a669 80
mbed_official 507:d4fc7603a669 81 // Configure the pins
mbed_official 507:d4fc7603a669 82 pinmap_pinout(mosi, PinMap_SPI_MOSI);
mbed_official 507:d4fc7603a669 83 pinmap_pinout(miso, PinMap_SPI_MISO);
mbed_official 507:d4fc7603a669 84 pinmap_pinout(sclk, PinMap_SPI_SCLK);
mbed_official 507:d4fc7603a669 85 pinmap_pinout(ssel, PinMap_SPI_SSEL);
mbed_official 507:d4fc7603a669 86
mbed_official 507:d4fc7603a669 87 // Enable SPI and FIFOs
mbed_official 507:d4fc7603a669 88 obj->spi->gen_ctrl = (MXC_F_SPI_GEN_CTRL_SPI_MSTR_EN |
mbed_official 507:d4fc7603a669 89 MXC_F_SPI_GEN_CTRL_TX_FIFO_EN |
mbed_official 507:d4fc7603a669 90 MXC_F_SPI_GEN_CTRL_RX_FIFO_EN );
mbed_official 507:d4fc7603a669 91
mbed_official 507:d4fc7603a669 92 // Give instance the default settings
mbed_official 507:d4fc7603a669 93 spi_format(obj, DEFAULT_CHAR, DEFAULT_MODE, 0);
mbed_official 507:d4fc7603a669 94 spi_frequency(obj, DEFAULT_FREQ);
mbed_official 507:d4fc7603a669 95 }
mbed_official 507:d4fc7603a669 96
mbed_official 507:d4fc7603a669 97 //******************************************************************************
mbed_official 507:d4fc7603a669 98 void spi_format(spi_t *obj, int bits, int mode, int slave)
mbed_official 507:d4fc7603a669 99 {
mbed_official 507:d4fc7603a669 100 // Check the validity of the inputs
mbed_official 507:d4fc7603a669 101 MBED_ASSERT(((bits >= 1) && (bits <= 32)) && ((mode >= 0) && (mode <= 3)));
mbed_official 507:d4fc7603a669 102
mbed_official 507:d4fc7603a669 103 // Only supports master mode
mbed_official 507:d4fc7603a669 104 MBED_ASSERT(!slave);
mbed_official 507:d4fc7603a669 105
mbed_official 507:d4fc7603a669 106 // Save formatting data
mbed_official 507:d4fc7603a669 107 spi_bits = bits;
mbed_official 507:d4fc7603a669 108
mbed_official 507:d4fc7603a669 109 // Set the mode
mbed_official 507:d4fc7603a669 110 obj->spi->mstr_cfg &= ~(MXC_F_SPI_MSTR_CFG_SPI_MODE);
mbed_official 507:d4fc7603a669 111 obj->spi->mstr_cfg |= (mode << MXC_F_SPI_MSTR_CFG_SPI_MODE_POS);
mbed_official 507:d4fc7603a669 112 }
mbed_official 507:d4fc7603a669 113
mbed_official 507:d4fc7603a669 114 //******************************************************************************
mbed_official 507:d4fc7603a669 115 void spi_frequency(spi_t *obj, int hz)
mbed_official 507:d4fc7603a669 116 {
mbed_official 507:d4fc7603a669 117 // Maximum frequency is half the system frequency
mbed_official 507:d4fc7603a669 118 MBED_ASSERT((unsigned int)hz < (SystemCoreClock / 2));
mbed_official 507:d4fc7603a669 119 unsigned clocks = ((SystemCoreClock/2)/(hz));
mbed_official 507:d4fc7603a669 120
mbed_official 507:d4fc7603a669 121 // Figure out the divider ratio
mbed_official 507:d4fc7603a669 122 int clk_div = 1;
mbed_official 507:d4fc7603a669 123 while(clk_div < 10) {
mbed_official 507:d4fc7603a669 124 if(clocks < 0x10) {
mbed_official 507:d4fc7603a669 125 break;
mbed_official 507:d4fc7603a669 126 }
mbed_official 507:d4fc7603a669 127 clk_div++;
mbed_official 507:d4fc7603a669 128 clocks = clocks >> 1;
mbed_official 507:d4fc7603a669 129 }
mbed_official 507:d4fc7603a669 130
mbed_official 507:d4fc7603a669 131 // Turn on the SPI clock
mbed_official 507:d4fc7603a669 132 if(obj->index == 0) {
mbed_official 507:d4fc7603a669 133 MXC_CLKMAN->clk_ctrl_3_spi0 = clk_div;
mbed_official 507:d4fc7603a669 134 } else if(obj->index == 1) {
mbed_official 507:d4fc7603a669 135 MXC_CLKMAN->clk_ctrl_4_spi1 = clk_div;
mbed_official 507:d4fc7603a669 136 } else if(obj->index == 2) {
mbed_official 507:d4fc7603a669 137 MXC_CLKMAN->clk_ctrl_5_spi2 = clk_div;
mbed_official 507:d4fc7603a669 138 } else {
mbed_official 507:d4fc7603a669 139 MBED_ASSERT(0);
mbed_official 507:d4fc7603a669 140 }
mbed_official 507:d4fc7603a669 141
mbed_official 507:d4fc7603a669 142 // Set the number of clocks to hold sclk high and low
mbed_official 507:d4fc7603a669 143 MXC_SET_FIELD(&obj->spi->mstr_cfg, (MXC_F_SPI_MSTR_CFG_SCK_HI_CLK | MXC_F_SPI_MSTR_CFG_SCK_LO_CLK),
mbed_official 507:d4fc7603a669 144 ((clocks << MXC_F_SPI_MSTR_CFG_SCK_HI_CLK_POS) | (clocks << MXC_F_SPI_MSTR_CFG_SCK_LO_CLK_POS)));
mbed_official 507:d4fc7603a669 145 }
mbed_official 507:d4fc7603a669 146
mbed_official 507:d4fc7603a669 147 //******************************************************************************
mbed_official 507:d4fc7603a669 148 int spi_master_write(spi_t *obj, int value)
mbed_official 507:d4fc7603a669 149 {
mbed_official 507:d4fc7603a669 150 int bits = spi_bits;
mbed_official 507:d4fc7603a669 151 if(spi_bits == 32) {
mbed_official 507:d4fc7603a669 152 bits = 0;
mbed_official 507:d4fc7603a669 153 }
mbed_official 507:d4fc7603a669 154 // Create the header
mbed_official 507:d4fc7603a669 155 uint16_t header = ((0x3 << MXC_F_SPI_FIFO_DIR_POS ) | // TX and RX
mbed_official 507:d4fc7603a669 156 (0x0 << MXC_F_SPI_FIFO_UNIT_POS) | // Send bits
mbed_official 507:d4fc7603a669 157 (bits << MXC_F_SPI_FIFO_SIZE_POS) | // Number of units
mbed_official 507:d4fc7603a669 158 (0x1 << MXC_F_SPI_FIFO_DASS_POS)); // Deassert SS
mbed_official 507:d4fc7603a669 159
mbed_official 507:d4fc7603a669 160 // Send the message header
mbed_official 507:d4fc7603a669 161 obj->txfifo->txfifo_16 = header;
mbed_official 507:d4fc7603a669 162
mbed_official 507:d4fc7603a669 163 // Send the data
mbed_official 507:d4fc7603a669 164 if(spi_bits < 17) {
mbed_official 507:d4fc7603a669 165 obj->txfifo->txfifo_16 = (uint16_t)value;
mbed_official 507:d4fc7603a669 166 } else {
mbed_official 507:d4fc7603a669 167 obj->txfifo->txfifo_32 = (uint32_t)value;
mbed_official 507:d4fc7603a669 168 }
mbed_official 507:d4fc7603a669 169
mbed_official 507:d4fc7603a669 170 // Get the data
mbed_official 507:d4fc7603a669 171 bits = spi_bits;
mbed_official 507:d4fc7603a669 172 int result = 0;
mbed_official 507:d4fc7603a669 173 int i = 0;
mbed_official 507:d4fc7603a669 174 while(bits > 0) {
mbed_official 507:d4fc7603a669 175 // Wait for data
mbed_official 507:d4fc7603a669 176 while(((obj->spi->fifo_ctrl & MXC_F_SPI_FIFO_CTRL_RX_FIFO_USED)
mbed_official 507:d4fc7603a669 177 >> MXC_F_SPI_FIFO_CTRL_RX_FIFO_USED_POS) < 1) {}
mbed_official 507:d4fc7603a669 178
mbed_official 507:d4fc7603a669 179 result |= (obj->rxfifo->rxfifo_8 << (i++*8));
mbed_official 507:d4fc7603a669 180 bits-=8;
mbed_official 507:d4fc7603a669 181 }
mbed_official 507:d4fc7603a669 182
mbed_official 507:d4fc7603a669 183 return result;
mbed_official 507:d4fc7603a669 184 }
mbed_official 507:d4fc7603a669 185
mbed_official 507:d4fc7603a669 186 //******************************************************************************
mbed_official 507:d4fc7603a669 187 int spi_busy(spi_t *obj)
mbed_official 507:d4fc7603a669 188 {
mbed_official 507:d4fc7603a669 189 return !(obj->spi->intfl & MXC_F_SPI_INTFL_TX_READY);
mbed_official 507:d4fc7603a669 190 }