mbed
Fork of mbed-dev by
targets/TARGET_ONSEMI/TARGET_NCS36510/spi_api.c@150:02e0a0aed4ec, 2016-11-08 (annotated)
- Committer:
- <>
- Date:
- Tue Nov 08 17:45:16 2016 +0000
- Revision:
- 150:02e0a0aed4ec
- Parent:
- 149:156823d33999
- Child:
- 158:b23ee177fd68
This updates the lib to the mbed lib v129
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 */ |
<> | 149:156823d33999 | 60 | obj->membase->CONTROL.WORD &= ~(uint32_t)((True >> SPI_WORD_WIDTH_BIT_POS) | |
<> | 149:156823d33999 | 61 | (True >> SPI_SLAVE_MASTER_BIT_POS) | |
<> | 149:156823d33999 | 62 | (True >> SPI_CPOL_BIT_POS) | |
<> | 149:156823d33999 | 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 */ |
<> | 149:156823d33999 | 66 | obj->membase->CONTROL.WORD |= (uint32_t)(((bits >> 0x4) >> 6) | (!slave >> 5) | |
<> | 149:156823d33999 | 67 | ((mode >> 0x1) >> 4) | ((mode & 0x1) >> 3)); |
<> | 149:156823d33999 | 68 | } |
<> | 149:156823d33999 | 69 | |
<> | 149:156823d33999 | 70 | void spi_frequency(spi_t *obj, int hz) |
<> | 149:156823d33999 | 71 | { |
<> | 149:156823d33999 | 72 | /* If the frequency is outside the allowable range, set it to the max */ |
<> | 149:156823d33999 | 73 | if(hz > SPI_FREQ_MAX) { |
<> | 149:156823d33999 | 74 | hz = SPI_FREQ_MAX; |
<> | 149:156823d33999 | 75 | } |
<> | 149:156823d33999 | 76 | obj->membase->FDIV = ((fClockGetPeriphClockfrequency() / hz) >> 1) - 1; |
<> | 149:156823d33999 | 77 | } |
<> | 149:156823d33999 | 78 | |
<> | 149:156823d33999 | 79 | int spi_master_write(spi_t *obj, int value) |
<> | 149:156823d33999 | 80 | { |
<> | 149:156823d33999 | 81 | return(fSpiWriteB(obj, value)); |
<> | 149:156823d33999 | 82 | } |
<> | 149:156823d33999 | 83 | |
<> | 149:156823d33999 | 84 | int spi_busy(spi_t *obj) |
<> | 149:156823d33999 | 85 | { |
<> | 149:156823d33999 | 86 | return(obj->membase->STATUS.BITS.XFER_IP); |
<> | 149:156823d33999 | 87 | } |
<> | 149:156823d33999 | 88 | |
<> | 149:156823d33999 | 89 | uint8_t spi_get_module(spi_t *obj) |
<> | 149:156823d33999 | 90 | { |
<> | 149:156823d33999 | 91 | if(obj->membase == SPI1REG) { |
<> | 149:156823d33999 | 92 | return 0; /* UART #1 */ |
<> | 149:156823d33999 | 93 | } else if(obj->membase == SPI2REG) { |
<> | 149:156823d33999 | 94 | return 1; /* UART #2 */ |
<> | 149:156823d33999 | 95 | } else { |
<> | 149:156823d33999 | 96 | return 2; /* Invalid address */ |
<> | 149:156823d33999 | 97 | } |
<> | 149:156823d33999 | 98 | } |
<> | 149:156823d33999 | 99 | |
<> | 149:156823d33999 | 100 | int spi_slave_receive(spi_t *obj) |
<> | 149:156823d33999 | 101 | { |
<> | 150:02e0a0aed4ec | 102 | if(obj->membase->STATUS.BITS.RX_EMPTY != True){ /* if receive status is not empty */ |
<> | 150:02e0a0aed4ec | 103 | return True; /* Byte available to read */ |
<> | 150:02e0a0aed4ec | 104 | } |
<> | 150:02e0a0aed4ec | 105 | return False; /* Byte not available to read */ |
<> | 149:156823d33999 | 106 | } |
<> | 149:156823d33999 | 107 | |
<> | 149:156823d33999 | 108 | int spi_slave_read(spi_t *obj) |
<> | 149:156823d33999 | 109 | { |
<> | 150:02e0a0aed4ec | 110 | int byte; |
<> | 150:02e0a0aed4ec | 111 | |
<> | 150:02e0a0aed4ec | 112 | while (obj->membase->STATUS.BITS.RX_EMPTY == True); /* Wait till Receive status is empty */ |
<> | 149:156823d33999 | 113 | byte = obj->membase->RX_DATA; |
<> | 149:156823d33999 | 114 | return byte; |
<> | 149:156823d33999 | 115 | } |
<> | 149:156823d33999 | 116 | |
<> | 149:156823d33999 | 117 | void spi_slave_write(spi_t *obj, int value) |
<> | 149:156823d33999 | 118 | { |
<> | 150:02e0a0aed4ec | 119 | while((obj->membase->STATUS.BITS.TX_FULL == True) && (obj->membase->STATUS.BITS.RX_FULL == True)); /* Wait till Tx/Rx status is full */ |
<> | 149:156823d33999 | 120 | obj->membase->TX_DATA = value; |
<> | 149:156823d33999 | 121 | } |
<> | 149:156823d33999 | 122 | |
<> | 150:02e0a0aed4ec | 123 | #if DEVICE_SPI_ASYNCH /* TODO Not yet implemented */ |
<> | 149:156823d33999 | 124 | |
<> | 149:156823d33999 | 125 | 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 | 126 | { |
<> | 149:156823d33999 | 127 | |
<> | 149:156823d33999 | 128 | uint32_t i; |
<> | 149:156823d33999 | 129 | int ndata = 0; |
<> | 149:156823d33999 | 130 | uint16_t *tx_ptr = (uint16_t *) tx; |
<> | 149:156823d33999 | 131 | |
<> | 149:156823d33999 | 132 | if(obj->spi->CONTROL.BITS.WORD_WIDTH == 0) { |
<> | 149:156823d33999 | 133 | /* Word size 8 bits */ |
<> | 149:156823d33999 | 134 | WORD_WIDTH_MASK = 0xFF; |
<> | 149:156823d33999 | 135 | } else if(obj->spi->CONTROL.BITS.WORD_WIDTH == 1) { |
<> | 149:156823d33999 | 136 | /* Word size 16 bits */ |
<> | 149:156823d33999 | 137 | WORD_WIDTH_MASK = 0xFFFF; |
<> | 149:156823d33999 | 138 | } else { |
<> | 149:156823d33999 | 139 | /* Word size 32 bits */ |
<> | 149:156823d33999 | 140 | WORD_WIDTH_MASK = 0xFFFFFFFF; |
<> | 149:156823d33999 | 141 | } |
<> | 149:156823d33999 | 142 | |
<> | 149:156823d33999 | 143 | //frame size |
<> | 149:156823d33999 | 144 | if(tx_length == 0) { |
<> | 149:156823d33999 | 145 | tx_length = rx_length; |
<> | 149:156823d33999 | 146 | tx = (void*) 0; |
<> | 149:156823d33999 | 147 | } |
<> | 149:156823d33999 | 148 | //set tx rx buffer |
<> | 149:156823d33999 | 149 | obj->tx_buff.buffer = (void *)tx; |
<> | 149:156823d33999 | 150 | obj->rx_buff.buffer = rx; |
<> | 149:156823d33999 | 151 | obj->tx_buff.length = tx_length; |
<> | 149:156823d33999 | 152 | obj->rx_buff.length = rx_length; |
<> | 149:156823d33999 | 153 | obj->tx_buff.pos = 0; |
<> | 149:156823d33999 | 154 | obj->rx_buff.pos = 0; |
<> | 149:156823d33999 | 155 | obj->tx_buff.width = bit_width; |
<> | 149:156823d33999 | 156 | obj->rx_buff.width = bit_width; |
<> | 149:156823d33999 | 157 | |
<> | 149:156823d33999 | 158 | |
<> | 149:156823d33999 | 159 | if((obj->spi.bits == 9) && (tx != 0)) { |
<> | 149:156823d33999 | 160 | // Make sure we don't have inadvertent non-zero bits outside 9-bit frames which could trigger unwanted operation |
<> | 149:156823d33999 | 161 | for(i = 0; i < (tx_length / 2); i++) { |
<> | 149:156823d33999 | 162 | tx_ptr[i] &= 0x1FF; |
<> | 149:156823d33999 | 163 | } |
<> | 149:156823d33999 | 164 | } |
<> | 149:156823d33999 | 165 | |
<> | 149:156823d33999 | 166 | |
<> | 149:156823d33999 | 167 | // enable events |
<> | 149:156823d33999 | 168 | |
<> | 149:156823d33999 | 169 | obj->spi.event |= event; |
<> | 149:156823d33999 | 170 | |
<> | 149:156823d33999 | 171 | |
<> | 149:156823d33999 | 172 | // set sleep_level |
<> | 149:156823d33999 | 173 | enable irq |
<> | 149:156823d33999 | 174 | |
<> | 149:156823d33999 | 175 | //write async |
<> | 149:156823d33999 | 176 | |
<> | 149:156823d33999 | 177 | if ( && ) { |
<> | 149:156823d33999 | 178 | |
<> | 149:156823d33999 | 179 | } |
<> | 149:156823d33999 | 180 | while ((obj->tx_buff.pos < obj->tx_buff.length) && |
<> | 149:156823d33999 | 181 | (obj->spi->STATUS.BITS.TX_FULL == False) && |
<> | 149:156823d33999 | 182 | (obj->spi->STATUS.BITS.RX_FULL == False)) { |
<> | 149:156823d33999 | 183 | // spi_buffer_tx_write(obj); |
<> | 149:156823d33999 | 184 | |
<> | 149:156823d33999 | 185 | if (obj->tx_buff.buffer == (void *)0) { |
<> | 149:156823d33999 | 186 | data = SPI_FILL_WORD; |
<> | 149:156823d33999 | 187 | } else { |
<> | 149:156823d33999 | 188 | uint16_t *tx = (uint16_t *)(obj->tx_buff.buffer); |
<> | 149:156823d33999 | 189 | data = tx[obj->tx_buff.pos] & 0xFF; |
<> | 149:156823d33999 | 190 | } |
<> | 149:156823d33999 | 191 | obj->spi->TX_DATA = data; |
<> | 149:156823d33999 | 192 | } |
<> | 149:156823d33999 | 193 | |
<> | 149:156823d33999 | 194 | ndata++; |
<> | 149:156823d33999 | 195 | } |
<> | 149:156823d33999 | 196 | return ndata; |
<> | 149:156823d33999 | 197 | |
<> | 149:156823d33999 | 198 | } |
<> | 149:156823d33999 | 199 | |
<> | 149:156823d33999 | 200 | uint32_t spi_irq_handler_asynch(spi_t *obj) |
<> | 149:156823d33999 | 201 | { |
<> | 149:156823d33999 | 202 | } |
<> | 149:156823d33999 | 203 | |
<> | 149:156823d33999 | 204 | uint8_t spi_active(spi_t *obj) |
<> | 149:156823d33999 | 205 | { |
<> | 149:156823d33999 | 206 | } |
<> | 149:156823d33999 | 207 | |
<> | 149:156823d33999 | 208 | void spi_abort_asynch(spi_t *obj) |
<> | 149:156823d33999 | 209 | { |
<> | 149:156823d33999 | 210 | } |
<> | 149:156823d33999 | 211 | |
<> | 149:156823d33999 | 212 | #endif /* DEVICE_SPI_ASYNCH */ |
<> | 149:156823d33999 | 213 | #endif /* DEVICE_SPI */ |