test
Fork of mbed-dev by
targets/TARGET_ONSEMI/TARGET_NCS36510/spi_api.c@168:e84263d55307, 2017-06-21 (annotated)
- Committer:
- AnnaBridge
- Date:
- Wed Jun 21 17:46:44 2017 +0100
- Revision:
- 168:e84263d55307
- Parent:
- 158:b23ee177fd68
- Child:
- 171:19eb464bc2be
This updates the lib to the mbed lib v 145
Who changed what in which revision?
User | Revision | Line number | New 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 | |
AnnaBridge | 168:e84263d55307 | 86 | int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) { |
AnnaBridge | 168:e84263d55307 | 87 | int total = (tx_length > rx_length) ? tx_length : rx_length; |
AnnaBridge | 168:e84263d55307 | 88 | |
AnnaBridge | 168:e84263d55307 | 89 | for (int i = 0; i < total; i++) { |
AnnaBridge | 168:e84263d55307 | 90 | char out = (i < tx_length) ? tx_buffer[i] : 0xff; |
AnnaBridge | 168:e84263d55307 | 91 | char in = spi_master_write(obj, out); |
AnnaBridge | 168:e84263d55307 | 92 | if (i < rx_length) { |
AnnaBridge | 168:e84263d55307 | 93 | rx_buffer[i] = in; |
AnnaBridge | 168:e84263d55307 | 94 | } |
AnnaBridge | 168:e84263d55307 | 95 | } |
AnnaBridge | 168:e84263d55307 | 96 | |
AnnaBridge | 168:e84263d55307 | 97 | return total; |
AnnaBridge | 168:e84263d55307 | 98 | } |
AnnaBridge | 168:e84263d55307 | 99 | |
<> | 149:156823d33999 | 100 | int spi_busy(spi_t *obj) |
<> | 149:156823d33999 | 101 | { |
<> | 149:156823d33999 | 102 | return(obj->membase->STATUS.BITS.XFER_IP); |
<> | 149:156823d33999 | 103 | } |
<> | 149:156823d33999 | 104 | |
<> | 149:156823d33999 | 105 | uint8_t spi_get_module(spi_t *obj) |
<> | 149:156823d33999 | 106 | { |
<> | 149:156823d33999 | 107 | if(obj->membase == SPI1REG) { |
<> | 149:156823d33999 | 108 | return 0; /* UART #1 */ |
<> | 149:156823d33999 | 109 | } else if(obj->membase == SPI2REG) { |
<> | 149:156823d33999 | 110 | return 1; /* UART #2 */ |
<> | 149:156823d33999 | 111 | } else { |
<> | 149:156823d33999 | 112 | return 2; /* Invalid address */ |
<> | 149:156823d33999 | 113 | } |
<> | 149:156823d33999 | 114 | } |
<> | 149:156823d33999 | 115 | |
<> | 149:156823d33999 | 116 | int spi_slave_receive(spi_t *obj) |
<> | 149:156823d33999 | 117 | { |
<> | 150:02e0a0aed4ec | 118 | if(obj->membase->STATUS.BITS.RX_EMPTY != True){ /* if receive status is not empty */ |
<> | 150:02e0a0aed4ec | 119 | return True; /* Byte available to read */ |
<> | 150:02e0a0aed4ec | 120 | } |
<> | 150:02e0a0aed4ec | 121 | return False; /* Byte not available to read */ |
<> | 149:156823d33999 | 122 | } |
<> | 149:156823d33999 | 123 | |
<> | 149:156823d33999 | 124 | int spi_slave_read(spi_t *obj) |
<> | 149:156823d33999 | 125 | { |
<> | 150:02e0a0aed4ec | 126 | int byte; |
<> | 150:02e0a0aed4ec | 127 | |
<> | 150:02e0a0aed4ec | 128 | while (obj->membase->STATUS.BITS.RX_EMPTY == True); /* Wait till Receive status is empty */ |
<> | 149:156823d33999 | 129 | byte = obj->membase->RX_DATA; |
<> | 149:156823d33999 | 130 | return byte; |
<> | 149:156823d33999 | 131 | } |
<> | 149:156823d33999 | 132 | |
<> | 149:156823d33999 | 133 | void spi_slave_write(spi_t *obj, int value) |
<> | 149:156823d33999 | 134 | { |
<> | 150:02e0a0aed4ec | 135 | while((obj->membase->STATUS.BITS.TX_FULL == True) && (obj->membase->STATUS.BITS.RX_FULL == True)); /* Wait till Tx/Rx status is full */ |
<> | 149:156823d33999 | 136 | obj->membase->TX_DATA = value; |
<> | 149:156823d33999 | 137 | } |
<> | 149:156823d33999 | 138 | |
<> | 150:02e0a0aed4ec | 139 | #if DEVICE_SPI_ASYNCH /* TODO Not yet implemented */ |
<> | 149:156823d33999 | 140 | |
<> | 149:156823d33999 | 141 | 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 | 142 | { |
<> | 149:156823d33999 | 143 | |
<> | 149:156823d33999 | 144 | uint32_t i; |
<> | 149:156823d33999 | 145 | int ndata = 0; |
<> | 149:156823d33999 | 146 | uint16_t *tx_ptr = (uint16_t *) tx; |
<> | 149:156823d33999 | 147 | |
<> | 149:156823d33999 | 148 | if(obj->spi->CONTROL.BITS.WORD_WIDTH == 0) { |
<> | 149:156823d33999 | 149 | /* Word size 8 bits */ |
<> | 149:156823d33999 | 150 | WORD_WIDTH_MASK = 0xFF; |
<> | 149:156823d33999 | 151 | } else if(obj->spi->CONTROL.BITS.WORD_WIDTH == 1) { |
<> | 149:156823d33999 | 152 | /* Word size 16 bits */ |
<> | 149:156823d33999 | 153 | WORD_WIDTH_MASK = 0xFFFF; |
<> | 149:156823d33999 | 154 | } else { |
<> | 149:156823d33999 | 155 | /* Word size 32 bits */ |
<> | 149:156823d33999 | 156 | WORD_WIDTH_MASK = 0xFFFFFFFF; |
<> | 149:156823d33999 | 157 | } |
<> | 149:156823d33999 | 158 | |
<> | 149:156823d33999 | 159 | //frame size |
<> | 149:156823d33999 | 160 | if(tx_length == 0) { |
<> | 149:156823d33999 | 161 | tx_length = rx_length; |
<> | 149:156823d33999 | 162 | tx = (void*) 0; |
<> | 149:156823d33999 | 163 | } |
<> | 149:156823d33999 | 164 | //set tx rx buffer |
<> | 149:156823d33999 | 165 | obj->tx_buff.buffer = (void *)tx; |
<> | 149:156823d33999 | 166 | obj->rx_buff.buffer = rx; |
<> | 149:156823d33999 | 167 | obj->tx_buff.length = tx_length; |
<> | 149:156823d33999 | 168 | obj->rx_buff.length = rx_length; |
<> | 149:156823d33999 | 169 | obj->tx_buff.pos = 0; |
<> | 149:156823d33999 | 170 | obj->rx_buff.pos = 0; |
<> | 149:156823d33999 | 171 | obj->tx_buff.width = bit_width; |
<> | 149:156823d33999 | 172 | obj->rx_buff.width = bit_width; |
<> | 149:156823d33999 | 173 | |
<> | 149:156823d33999 | 174 | |
<> | 149:156823d33999 | 175 | if((obj->spi.bits == 9) && (tx != 0)) { |
<> | 149:156823d33999 | 176 | // Make sure we don't have inadvertent non-zero bits outside 9-bit frames which could trigger unwanted operation |
<> | 149:156823d33999 | 177 | for(i = 0; i < (tx_length / 2); i++) { |
<> | 149:156823d33999 | 178 | tx_ptr[i] &= 0x1FF; |
<> | 149:156823d33999 | 179 | } |
<> | 149:156823d33999 | 180 | } |
<> | 149:156823d33999 | 181 | |
<> | 149:156823d33999 | 182 | |
<> | 149:156823d33999 | 183 | // enable events |
<> | 149:156823d33999 | 184 | |
<> | 149:156823d33999 | 185 | obj->spi.event |= event; |
<> | 149:156823d33999 | 186 | |
<> | 149:156823d33999 | 187 | |
<> | 149:156823d33999 | 188 | // set sleep_level |
<> | 149:156823d33999 | 189 | enable irq |
<> | 149:156823d33999 | 190 | |
<> | 149:156823d33999 | 191 | //write async |
<> | 149:156823d33999 | 192 | |
<> | 149:156823d33999 | 193 | if ( && ) { |
<> | 149:156823d33999 | 194 | |
<> | 149:156823d33999 | 195 | } |
<> | 149:156823d33999 | 196 | while ((obj->tx_buff.pos < obj->tx_buff.length) && |
<> | 149:156823d33999 | 197 | (obj->spi->STATUS.BITS.TX_FULL == False) && |
<> | 149:156823d33999 | 198 | (obj->spi->STATUS.BITS.RX_FULL == False)) { |
<> | 149:156823d33999 | 199 | // spi_buffer_tx_write(obj); |
<> | 149:156823d33999 | 200 | |
<> | 149:156823d33999 | 201 | if (obj->tx_buff.buffer == (void *)0) { |
<> | 149:156823d33999 | 202 | data = SPI_FILL_WORD; |
<> | 149:156823d33999 | 203 | } else { |
<> | 149:156823d33999 | 204 | uint16_t *tx = (uint16_t *)(obj->tx_buff.buffer); |
<> | 149:156823d33999 | 205 | data = tx[obj->tx_buff.pos] & 0xFF; |
<> | 149:156823d33999 | 206 | } |
<> | 149:156823d33999 | 207 | obj->spi->TX_DATA = data; |
<> | 149:156823d33999 | 208 | } |
<> | 149:156823d33999 | 209 | |
<> | 149:156823d33999 | 210 | ndata++; |
<> | 149:156823d33999 | 211 | } |
<> | 149:156823d33999 | 212 | return ndata; |
<> | 149:156823d33999 | 213 | |
<> | 149:156823d33999 | 214 | } |
<> | 149:156823d33999 | 215 | |
<> | 149:156823d33999 | 216 | uint32_t spi_irq_handler_asynch(spi_t *obj) |
<> | 149:156823d33999 | 217 | { |
<> | 149:156823d33999 | 218 | } |
<> | 149:156823d33999 | 219 | |
<> | 149:156823d33999 | 220 | uint8_t spi_active(spi_t *obj) |
<> | 149:156823d33999 | 221 | { |
<> | 149:156823d33999 | 222 | } |
<> | 149:156823d33999 | 223 | |
<> | 149:156823d33999 | 224 | void spi_abort_asynch(spi_t *obj) |
<> | 149:156823d33999 | 225 | { |
<> | 149:156823d33999 | 226 | } |
<> | 149:156823d33999 | 227 | |
<> | 149:156823d33999 | 228 | #endif /* DEVICE_SPI_ASYNCH */ |
<> | 149:156823d33999 | 229 | #endif /* DEVICE_SPI */ |