mbed

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Fri Feb 16 16:09:33 2018 +0000
Revision:
182:57724642e740
Parent:
171:19eb464bc2be
mbed-dev library. Release version 159.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 157:ff67d9f36b67 1 /*******************************************************************************
<> 157:ff67d9f36b67 2 * Copyright (c) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
<> 157:ff67d9f36b67 3 *
<> 157:ff67d9f36b67 4 * Permission is hereby granted, free of charge, to any person obtaining a
<> 157:ff67d9f36b67 5 * copy of this software and associated documentation files (the "Software"),
<> 157:ff67d9f36b67 6 * to deal in the Software without restriction, including without limitation
<> 157:ff67d9f36b67 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
<> 157:ff67d9f36b67 8 * and/or sell copies of the Software, and to permit persons to whom the
<> 157:ff67d9f36b67 9 * Software is furnished to do so, subject to the following conditions:
<> 157:ff67d9f36b67 10 *
<> 157:ff67d9f36b67 11 * The above copyright notice and this permission notice shall be included
<> 157:ff67d9f36b67 12 * in all copies or substantial portions of the Software.
<> 157:ff67d9f36b67 13 *
<> 157:ff67d9f36b67 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
<> 157:ff67d9f36b67 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
<> 157:ff67d9f36b67 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
<> 157:ff67d9f36b67 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
<> 157:ff67d9f36b67 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
<> 157:ff67d9f36b67 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
<> 157:ff67d9f36b67 20 * OTHER DEALINGS IN THE SOFTWARE.
<> 157:ff67d9f36b67 21 *
<> 157:ff67d9f36b67 22 * Except as contained in this notice, the name of Maxim Integrated
<> 157:ff67d9f36b67 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
<> 157:ff67d9f36b67 24 * Products, Inc. Branding Policy.
<> 157:ff67d9f36b67 25 *
<> 157:ff67d9f36b67 26 * The mere transfer of this software does not imply any licenses
<> 157:ff67d9f36b67 27 * of trade secrets, proprietary technology, copyrights, patents,
<> 157:ff67d9f36b67 28 * trademarks, maskwork rights, or any other form of intellectual
<> 157:ff67d9f36b67 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
<> 157:ff67d9f36b67 30 * ownership rights.
<> 157:ff67d9f36b67 31 *******************************************************************************
<> 157:ff67d9f36b67 32 */
<> 157:ff67d9f36b67 33
<> 157:ff67d9f36b67 34 #include "mbed_assert.h"
<> 157:ff67d9f36b67 35 #include "spi_api.h" // mbed HAL
<> 157:ff67d9f36b67 36 #include "spim_regs.h" // bare metal
<> 157:ff67d9f36b67 37 #include "spim.h" // Maxim CMSIS driver
<> 157:ff67d9f36b67 38 #include "pinmap.h"
<> 157:ff67d9f36b67 39 #include "PeripheralPins.h"
<> 157:ff67d9f36b67 40
<> 157:ff67d9f36b67 41 //******************************************************************************
<> 157:ff67d9f36b67 42 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
<> 157:ff67d9f36b67 43 {
<> 157:ff67d9f36b67 44 // Make sure pins are pointing to the same SPI instance
<> 157:ff67d9f36b67 45 SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
<> 157:ff67d9f36b67 46 SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
<> 157:ff67d9f36b67 47 SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
<> 157:ff67d9f36b67 48 SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
<> 157:ff67d9f36b67 49
<> 157:ff67d9f36b67 50 SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
<> 157:ff67d9f36b67 51 SPIName spi_cntl;
<> 157:ff67d9f36b67 52
<> 157:ff67d9f36b67 53 // Control is SCK and optionaly SS
<> 157:ff67d9f36b67 54 if ((SPIName)spi_ssel != (SPIName)NC) {
<> 157:ff67d9f36b67 55 spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
<> 157:ff67d9f36b67 56 } else {
<> 157:ff67d9f36b67 57 spi_cntl = spi_sclk;
<> 157:ff67d9f36b67 58 }
<> 157:ff67d9f36b67 59
<> 157:ff67d9f36b67 60 SPIName spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
<> 157:ff67d9f36b67 61
<> 157:ff67d9f36b67 62 MBED_ASSERT((SPIName)spi != (SPIName)NC);
<> 157:ff67d9f36b67 63
<> 157:ff67d9f36b67 64 obj->spi = (mxc_spim_regs_t *)spi;
<> 157:ff67d9f36b67 65
<> 157:ff67d9f36b67 66 // Merge pin function requests for use with CMSIS init func
<> 157:ff67d9f36b67 67 ioman_req_t io_req;
<> 157:ff67d9f36b67 68 pin_function_t *pin_func;
<> 157:ff67d9f36b67 69 pin_func = (pin_function_t *)pinmap_find_function(mosi, PinMap_SPI_MOSI);
<> 157:ff67d9f36b67 70 io_req.value = pin_func->req_val;
<> 157:ff67d9f36b67 71 pin_func = (pin_function_t *)pinmap_find_function(miso, PinMap_SPI_MISO);
<> 157:ff67d9f36b67 72 io_req.value |= pin_func->req_val;
<> 157:ff67d9f36b67 73 pin_func = (pin_function_t *)pinmap_find_function(sclk, PinMap_SPI_SCLK);
<> 157:ff67d9f36b67 74 io_req.value |= pin_func->req_val;
<> 157:ff67d9f36b67 75 if ((SPIName)spi_ssel != (SPIName)NC) {
<> 157:ff67d9f36b67 76 pin_func = (pin_function_t *)pinmap_find_function(ssel, PinMap_SPI_SSEL);
<> 157:ff67d9f36b67 77 io_req.value |= pin_func->req_val;
<> 157:ff67d9f36b67 78 }
<> 157:ff67d9f36b67 79
<> 157:ff67d9f36b67 80 // Using req and ack pointers of last pin function lookup
<> 157:ff67d9f36b67 81 sys_cfg_spim_t sys_cfg;
<> 157:ff67d9f36b67 82 sys_cfg.io_cfg.req_reg = pin_func->reg_req;
<> 157:ff67d9f36b67 83 sys_cfg.io_cfg.ack_reg = pin_func->reg_ack;
<> 157:ff67d9f36b67 84 sys_cfg.io_cfg.req_val = io_req;
<> 157:ff67d9f36b67 85 sys_cfg.clk_scale = CLKMAN_SCALE_AUTO;
<> 157:ff67d9f36b67 86
<> 157:ff67d9f36b67 87 // Defaults
<> 157:ff67d9f36b67 88 spim_cfg_t spim_cfg;
<> 157:ff67d9f36b67 89 spim_cfg.mode = 0;
<> 157:ff67d9f36b67 90 spim_cfg.ssel_pol = 0;
<> 157:ff67d9f36b67 91 spim_cfg.baud = 1000000;
<> 157:ff67d9f36b67 92
<> 157:ff67d9f36b67 93 SPIM_Init(obj->spi, &spim_cfg, &sys_cfg);
<> 157:ff67d9f36b67 94
<> 157:ff67d9f36b67 95 obj->index = MXC_SPIM_GET_IDX(obj->spi);
<> 157:ff67d9f36b67 96 }
<> 157:ff67d9f36b67 97
<> 157:ff67d9f36b67 98 //******************************************************************************
<> 157:ff67d9f36b67 99 void spi_format(spi_t *obj, int bits, int mode, int slave)
<> 157:ff67d9f36b67 100 {
<> 157:ff67d9f36b67 101 // Check the validity of the inputs
<> 157:ff67d9f36b67 102 MBED_ASSERT(bits == 8);
<> 157:ff67d9f36b67 103
<> 157:ff67d9f36b67 104 // Only supports master mode
<> 157:ff67d9f36b67 105 MBED_ASSERT(!slave);
<> 157:ff67d9f36b67 106
<> 157:ff67d9f36b67 107 // Set the mode
<> 157:ff67d9f36b67 108 obj->spi->mstr_cfg &= ~(MXC_F_SPIM_MSTR_CFG_SPI_MODE);
<> 157:ff67d9f36b67 109 obj->spi->mstr_cfg |= (mode << MXC_F_SPIM_MSTR_CFG_SPI_MODE_POS);
<> 157:ff67d9f36b67 110 }
<> 157:ff67d9f36b67 111
<> 157:ff67d9f36b67 112 //******************************************************************************
<> 157:ff67d9f36b67 113 void spi_frequency(spi_t *obj, int hz)
<> 157:ff67d9f36b67 114 {
<> 157:ff67d9f36b67 115 // Maximum frequency is half the system frequency
<> 157:ff67d9f36b67 116 MBED_ASSERT((unsigned int)hz <= (SystemCoreClock / 2));
<> 157:ff67d9f36b67 117 unsigned clocks = ((SystemCoreClock / 2) / hz);
<> 157:ff67d9f36b67 118
<> 157:ff67d9f36b67 119 // Figure out the divider ratio
<> 157:ff67d9f36b67 120 int clk_div = 1;
<> 157:ff67d9f36b67 121 while(clk_div < 10) {
<> 157:ff67d9f36b67 122 if(clocks < 0x10) {
<> 157:ff67d9f36b67 123 break;
<> 157:ff67d9f36b67 124 }
<> 157:ff67d9f36b67 125 clk_div++;
<> 157:ff67d9f36b67 126 clocks = clocks >> 1;
<> 157:ff67d9f36b67 127 }
<> 157:ff67d9f36b67 128
<> 157:ff67d9f36b67 129 // Turn on the SPI clock
<> 157:ff67d9f36b67 130 if(obj->index == 0) {
<> 157:ff67d9f36b67 131 MXC_CLKMAN->sys_clk_ctrl_11_spi0 = clk_div;
<> 157:ff67d9f36b67 132 } else if(obj->index == 1) {
<> 157:ff67d9f36b67 133 MXC_CLKMAN->sys_clk_ctrl_12_spi1 = clk_div;
<> 157:ff67d9f36b67 134 } else if(obj->index == 2) {
<> 157:ff67d9f36b67 135 MXC_CLKMAN->sys_clk_ctrl_13_spi2 = clk_div;
<> 157:ff67d9f36b67 136 } else {
<> 157:ff67d9f36b67 137 MBED_ASSERT(0);
<> 157:ff67d9f36b67 138 }
<> 157:ff67d9f36b67 139
<> 157:ff67d9f36b67 140 // Set the number of clocks to hold sclk high and low
<> 157:ff67d9f36b67 141 MXC_SET_FIELD(&obj->spi->mstr_cfg,
<> 157:ff67d9f36b67 142 (MXC_F_SPIM_MSTR_CFG_SCK_HI_CLK | MXC_F_SPIM_MSTR_CFG_SCK_LO_CLK),
<> 157:ff67d9f36b67 143 ((clocks << MXC_F_SPIM_MSTR_CFG_SCK_HI_CLK_POS) | (clocks << MXC_F_SPIM_MSTR_CFG_SCK_LO_CLK_POS)));
<> 157:ff67d9f36b67 144 }
<> 157:ff67d9f36b67 145
<> 157:ff67d9f36b67 146 //******************************************************************************
<> 157:ff67d9f36b67 147 int spi_master_write(spi_t *obj, int value)
<> 157:ff67d9f36b67 148 {
<> 157:ff67d9f36b67 149 spim_req_t req;
<> 157:ff67d9f36b67 150 uint8_t out;
<> 157:ff67d9f36b67 151 uint8_t in;
<> 157:ff67d9f36b67 152
<> 157:ff67d9f36b67 153 out = value;
<> 157:ff67d9f36b67 154
<> 157:ff67d9f36b67 155 req.ssel = 0;
<> 157:ff67d9f36b67 156 req.deass = 0;
<> 157:ff67d9f36b67 157 req.tx_data = &out;
<> 157:ff67d9f36b67 158 req.rx_data = &in;
<> 157:ff67d9f36b67 159 req.width = SPIM_WIDTH_1;
<> 157:ff67d9f36b67 160 req.len = 1;
<> 157:ff67d9f36b67 161 req.ssel = 0;
<> 157:ff67d9f36b67 162 req.deass = 1;
<> 157:ff67d9f36b67 163 req.callback = NULL;
<> 157:ff67d9f36b67 164
<> 157:ff67d9f36b67 165 SPIM_Trans(obj->spi, &req);
<> 157:ff67d9f36b67 166
<> 157:ff67d9f36b67 167 return *req.rx_data;
<> 157:ff67d9f36b67 168 }
<> 157:ff67d9f36b67 169
Kojto 171:19eb464bc2be 170 int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
Kojto 171:19eb464bc2be 171 char *rx_buffer, int rx_length, char write_fill) {
AnnaBridge 167:e84263d55307 172 int total = (tx_length > rx_length) ? tx_length : rx_length;
AnnaBridge 167:e84263d55307 173
AnnaBridge 167:e84263d55307 174 for (int i = 0; i < total; i++) {
Kojto 171:19eb464bc2be 175 char out = (i < tx_length) ? tx_buffer[i] : write_fill;
AnnaBridge 167:e84263d55307 176 char in = spi_master_write(obj, out);
AnnaBridge 167:e84263d55307 177 if (i < rx_length) {
AnnaBridge 167:e84263d55307 178 rx_buffer[i] = in;
AnnaBridge 167:e84263d55307 179 }
AnnaBridge 167:e84263d55307 180 }
AnnaBridge 167:e84263d55307 181
AnnaBridge 167:e84263d55307 182 return total;
AnnaBridge 167:e84263d55307 183 }
AnnaBridge 167:e84263d55307 184
<> 157:ff67d9f36b67 185 //******************************************************************************
<> 157:ff67d9f36b67 186 int spi_busy(spi_t *obj)
<> 157:ff67d9f36b67 187 {
<> 157:ff67d9f36b67 188 return SPIM_Busy(obj->spi);
<> 157:ff67d9f36b67 189 }
<> 157:ff67d9f36b67 190
<> 157:ff67d9f36b67 191 //******************************************************************************
<> 157:ff67d9f36b67 192 uint8_t spi_get_module(spi_t *obj)
<> 157:ff67d9f36b67 193 {
<> 157:ff67d9f36b67 194 return obj->index;
<> 157:ff67d9f36b67 195 }
<> 157:ff67d9f36b67 196