test

Committer:
elijahsj
Date:
Mon Nov 09 00:33:19 2020 -0500
Revision:
2:4364577b5ad8
Parent:
1:8a094db1347f
copied mbed library

Who changed what in which revision?

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