mbed

Fork of mbed-dev by mbed official

Committer:
Kojto
Date:
Thu Aug 03 13:13:39 2017 +0100
Revision:
171:19eb464bc2be
Parent:
167:e84263d55307
This updates the lib to the mbed lib v 148

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 149:156823d33999 1 /**
<> 149:156823d33999 2 *******************************************************************************
<> 149:156823d33999 3 * @file spi_api.c
<> 149:156823d33999 4 * @brief Implementation of a sleep functionality
<> 149:156823d33999 5 * @internal
<> 149:156823d33999 6 * @author ON Semiconductor
<> 149:156823d33999 7 * $Rev: 0.1 $
<> 149:156823d33999 8 * $Date: 02-05-2016 $
<> 149:156823d33999 9 ******************************************************************************
<> 149:156823d33999 10 * Copyright 2016 Semiconductor Components Industries LLC (d/b/a “ON Semiconductor”).
<> 149:156823d33999 11 * All rights reserved. This software and/or documentation is licensed by ON Semiconductor
<> 149:156823d33999 12 * under limited terms and conditions. The terms and conditions pertaining to the software
<> 149:156823d33999 13 * and/or documentation are available at http://www.onsemi.com/site/pdf/ONSEMI_T&C.pdf
<> 149:156823d33999 14 * (“ON Semiconductor Standard Terms and Conditions of Sale, Section 8 Software”) and
<> 149:156823d33999 15 * if applicable the software license agreement. Do not use this software and/or
<> 149:156823d33999 16 * documentation unless you have carefully read and you agree to the limited terms and
<> 149:156823d33999 17 * conditions. By using this software and/or documentation, you agree to the limited
<> 149:156823d33999 18 * terms and conditions.
<> 149:156823d33999 19 *
<> 149:156823d33999 20 * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
<> 149:156823d33999 21 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
<> 149:156823d33999 22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
<> 149:156823d33999 23 * ON SEMICONDUCTOR SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL,
<> 149:156823d33999 24 * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
<> 149:156823d33999 25 * @endinternal
<> 149:156823d33999 26 *
<> 149:156823d33999 27 * @ingroup spi_api
<> 149:156823d33999 28 *
<> 149:156823d33999 29 * @details
<> 149:156823d33999 30 * SPI implementation
<> 149:156823d33999 31 *
<> 149:156823d33999 32 */
<> 149:156823d33999 33 #if DEVICE_SPI
<> 149:156823d33999 34 #include "spi.h"
<> 149:156823d33999 35 #include "PeripheralPins.h"
<> 149:156823d33999 36 #include "objects.h"
<> 149:156823d33999 37 #include "spi_api.h"
<> 149:156823d33999 38 #include "mbed_assert.h"
<> 149:156823d33999 39 #include "memory_map.h"
<> 149:156823d33999 40 #include "spi_ipc7207_map.h"
<> 149:156823d33999 41 #include "crossbar.h"
<> 149:156823d33999 42 #include "clock.h"
<> 149:156823d33999 43 #include "cmsis_nvic.h"
<> 149:156823d33999 44
<> 149:156823d33999 45
<> 149:156823d33999 46 #define SPI_FREQ_MAX 4000000
<> 149:156823d33999 47
<> 149:156823d33999 48 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
<> 149:156823d33999 49 {
<> 149:156823d33999 50 fSpiInit(obj, mosi, miso, sclk, ssel);
<> 149:156823d33999 51 }
<> 149:156823d33999 52 void spi_free(spi_t *obj)
<> 149:156823d33999 53 {
<> 149:156823d33999 54 fSpiClose(obj);
<> 149:156823d33999 55 }
<> 149:156823d33999 56
<> 149:156823d33999 57 void spi_format(spi_t *obj, int bits, int mode, int slave)
<> 149:156823d33999 58 {
<> 149:156823d33999 59 /* Clear word width | Slave/Master | CPOL | CPHA | MSB first bits in control register */
Kojto 158:b23ee177fd68 60 obj->membase->CONTROL.WORD &= ~(uint32_t)((True << SPI_WORD_WIDTH_BIT_POS) |
Kojto 158:b23ee177fd68 61 (True << SPI_SLAVE_MASTER_BIT_POS) |
Kojto 158:b23ee177fd68 62 (True << SPI_CPOL_BIT_POS) |
Kojto 158:b23ee177fd68 63 (True << SPI_CPHA_BIT_POS));
<> 149:156823d33999 64
<> 149:156823d33999 65 /* Configure word width | Slave/Master | CPOL | CPHA | MSB first bits in control register */
Kojto 158:b23ee177fd68 66 obj->membase->CONTROL.WORD |= (uint32_t)(((bits >> 0x4) << SPI_WORD_WIDTH_BIT_POS) |
Kojto 158:b23ee177fd68 67 (!slave << SPI_SLAVE_MASTER_BIT_POS) |
Kojto 158:b23ee177fd68 68 ((mode >> 0x1) << SPI_CPOL_BIT_POS) |
Kojto 158:b23ee177fd68 69 ((mode & 0x1) << SPI_CPHA_BIT_POS));
<> 149:156823d33999 70 }
<> 149:156823d33999 71
<> 149:156823d33999 72 void spi_frequency(spi_t *obj, int hz)
<> 149:156823d33999 73 {
<> 149:156823d33999 74 /* If the frequency is outside the allowable range, set it to the max */
<> 149:156823d33999 75 if(hz > SPI_FREQ_MAX) {
<> 149:156823d33999 76 hz = SPI_FREQ_MAX;
<> 149:156823d33999 77 }
<> 149:156823d33999 78 obj->membase->FDIV = ((fClockGetPeriphClockfrequency() / hz) >> 1) - 1;
<> 149:156823d33999 79 }
<> 149:156823d33999 80
<> 149:156823d33999 81 int spi_master_write(spi_t *obj, int value)
<> 149:156823d33999 82 {
<> 149:156823d33999 83 return(fSpiWriteB(obj, value));
<> 149:156823d33999 84 }
<> 149:156823d33999 85
Kojto 171:19eb464bc2be 86 int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
Kojto 171:19eb464bc2be 87 char *rx_buffer, int rx_length, char write_fill) {
AnnaBridge 167:e84263d55307 88 int total = (tx_length > rx_length) ? tx_length : rx_length;
AnnaBridge 167:e84263d55307 89
AnnaBridge 167:e84263d55307 90 for (int i = 0; i < total; i++) {
Kojto 171:19eb464bc2be 91 char out = (i < tx_length) ? tx_buffer[i] : write_fill;
AnnaBridge 167:e84263d55307 92 char in = spi_master_write(obj, out);
AnnaBridge 167:e84263d55307 93 if (i < rx_length) {
AnnaBridge 167:e84263d55307 94 rx_buffer[i] = in;
AnnaBridge 167:e84263d55307 95 }
AnnaBridge 167:e84263d55307 96 }
AnnaBridge 167:e84263d55307 97
AnnaBridge 167:e84263d55307 98 return total;
AnnaBridge 167:e84263d55307 99 }
AnnaBridge 167:e84263d55307 100
<> 149:156823d33999 101 int spi_busy(spi_t *obj)
<> 149:156823d33999 102 {
<> 149:156823d33999 103 return(obj->membase->STATUS.BITS.XFER_IP);
<> 149:156823d33999 104 }
<> 149:156823d33999 105
<> 149:156823d33999 106 uint8_t spi_get_module(spi_t *obj)
<> 149:156823d33999 107 {
<> 149:156823d33999 108 if(obj->membase == SPI1REG) {
<> 149:156823d33999 109 return 0; /* UART #1 */
<> 149:156823d33999 110 } else if(obj->membase == SPI2REG) {
<> 149:156823d33999 111 return 1; /* UART #2 */
<> 149:156823d33999 112 } else {
<> 149:156823d33999 113 return 2; /* Invalid address */
<> 149:156823d33999 114 }
<> 149:156823d33999 115 }
<> 149:156823d33999 116
<> 149:156823d33999 117 int spi_slave_receive(spi_t *obj)
<> 149:156823d33999 118 {
<> 150:02e0a0aed4ec 119 if(obj->membase->STATUS.BITS.RX_EMPTY != True){ /* if receive status is not empty */
<> 150:02e0a0aed4ec 120 return True; /* Byte available to read */
<> 150:02e0a0aed4ec 121 }
<> 150:02e0a0aed4ec 122 return False; /* Byte not available to read */
<> 149:156823d33999 123 }
<> 149:156823d33999 124
<> 149:156823d33999 125 int spi_slave_read(spi_t *obj)
<> 149:156823d33999 126 {
<> 150:02e0a0aed4ec 127 int byte;
<> 150:02e0a0aed4ec 128
<> 150:02e0a0aed4ec 129 while (obj->membase->STATUS.BITS.RX_EMPTY == True); /* Wait till Receive status is empty */
<> 149:156823d33999 130 byte = obj->membase->RX_DATA;
<> 149:156823d33999 131 return byte;
<> 149:156823d33999 132 }
<> 149:156823d33999 133
<> 149:156823d33999 134 void spi_slave_write(spi_t *obj, int value)
<> 149:156823d33999 135 {
<> 150:02e0a0aed4ec 136 while((obj->membase->STATUS.BITS.TX_FULL == True) && (obj->membase->STATUS.BITS.RX_FULL == True)); /* Wait till Tx/Rx status is full */
<> 149:156823d33999 137 obj->membase->TX_DATA = value;
<> 149:156823d33999 138 }
<> 149:156823d33999 139
<> 150:02e0a0aed4ec 140 #if DEVICE_SPI_ASYNCH /* TODO Not yet implemented */
<> 149:156823d33999 141
<> 149:156823d33999 142 void spi_master_transfer(spi_t *obj, void *tx, size_t tx_length, void *rx, size_t rx_length, uint32_t handler, uint32_t event, DMAUsage hint)
<> 149:156823d33999 143 {
<> 149:156823d33999 144
<> 149:156823d33999 145 uint32_t i;
<> 149:156823d33999 146 int ndata = 0;
<> 149:156823d33999 147 uint16_t *tx_ptr = (uint16_t *) tx;
<> 149:156823d33999 148
<> 149:156823d33999 149 if(obj->spi->CONTROL.BITS.WORD_WIDTH == 0) {
<> 149:156823d33999 150 /* Word size 8 bits */
<> 149:156823d33999 151 WORD_WIDTH_MASK = 0xFF;
<> 149:156823d33999 152 } else if(obj->spi->CONTROL.BITS.WORD_WIDTH == 1) {
<> 149:156823d33999 153 /* Word size 16 bits */
<> 149:156823d33999 154 WORD_WIDTH_MASK = 0xFFFF;
<> 149:156823d33999 155 } else {
<> 149:156823d33999 156 /* Word size 32 bits */
<> 149:156823d33999 157 WORD_WIDTH_MASK = 0xFFFFFFFF;
<> 149:156823d33999 158 }
<> 149:156823d33999 159
<> 149:156823d33999 160 //frame size
<> 149:156823d33999 161 if(tx_length == 0) {
<> 149:156823d33999 162 tx_length = rx_length;
<> 149:156823d33999 163 tx = (void*) 0;
<> 149:156823d33999 164 }
<> 149:156823d33999 165 //set tx rx buffer
<> 149:156823d33999 166 obj->tx_buff.buffer = (void *)tx;
<> 149:156823d33999 167 obj->rx_buff.buffer = rx;
<> 149:156823d33999 168 obj->tx_buff.length = tx_length;
<> 149:156823d33999 169 obj->rx_buff.length = rx_length;
<> 149:156823d33999 170 obj->tx_buff.pos = 0;
<> 149:156823d33999 171 obj->rx_buff.pos = 0;
<> 149:156823d33999 172 obj->tx_buff.width = bit_width;
<> 149:156823d33999 173 obj->rx_buff.width = bit_width;
<> 149:156823d33999 174
<> 149:156823d33999 175
<> 149:156823d33999 176 if((obj->spi.bits == 9) && (tx != 0)) {
<> 149:156823d33999 177 // Make sure we don't have inadvertent non-zero bits outside 9-bit frames which could trigger unwanted operation
<> 149:156823d33999 178 for(i = 0; i < (tx_length / 2); i++) {
<> 149:156823d33999 179 tx_ptr[i] &= 0x1FF;
<> 149:156823d33999 180 }
<> 149:156823d33999 181 }
<> 149:156823d33999 182
<> 149:156823d33999 183
<> 149:156823d33999 184 // enable events
<> 149:156823d33999 185
<> 149:156823d33999 186 obj->spi.event |= event;
<> 149:156823d33999 187
<> 149:156823d33999 188
<> 149:156823d33999 189 // set sleep_level
<> 149:156823d33999 190 enable irq
<> 149:156823d33999 191
<> 149:156823d33999 192 //write async
<> 149:156823d33999 193
<> 149:156823d33999 194 if ( && ) {
<> 149:156823d33999 195
<> 149:156823d33999 196 }
<> 149:156823d33999 197 while ((obj->tx_buff.pos < obj->tx_buff.length) &&
<> 149:156823d33999 198 (obj->spi->STATUS.BITS.TX_FULL == False) &&
<> 149:156823d33999 199 (obj->spi->STATUS.BITS.RX_FULL == False)) {
<> 149:156823d33999 200 // spi_buffer_tx_write(obj);
<> 149:156823d33999 201
<> 149:156823d33999 202 if (obj->tx_buff.buffer == (void *)0) {
<> 149:156823d33999 203 data = SPI_FILL_WORD;
<> 149:156823d33999 204 } else {
<> 149:156823d33999 205 uint16_t *tx = (uint16_t *)(obj->tx_buff.buffer);
<> 149:156823d33999 206 data = tx[obj->tx_buff.pos] & 0xFF;
<> 149:156823d33999 207 }
<> 149:156823d33999 208 obj->spi->TX_DATA = data;
<> 149:156823d33999 209 }
<> 149:156823d33999 210
<> 149:156823d33999 211 ndata++;
<> 149:156823d33999 212 }
<> 149:156823d33999 213 return ndata;
<> 149:156823d33999 214
<> 149:156823d33999 215 }
<> 149:156823d33999 216
<> 149:156823d33999 217 uint32_t spi_irq_handler_asynch(spi_t *obj)
<> 149:156823d33999 218 {
<> 149:156823d33999 219 }
<> 149:156823d33999 220
<> 149:156823d33999 221 uint8_t spi_active(spi_t *obj)
<> 149:156823d33999 222 {
<> 149:156823d33999 223 }
<> 149:156823d33999 224
<> 149:156823d33999 225 void spi_abort_asynch(spi_t *obj)
<> 149:156823d33999 226 {
<> 149:156823d33999 227 }
<> 149:156823d33999 228
<> 149:156823d33999 229 #endif /* DEVICE_SPI_ASYNCH */
<> 149:156823d33999 230 #endif /* DEVICE_SPI */