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