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