inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NYX 0:85b3fd62ea1a 1
NYX 0:85b3fd62ea1a 2 /** \addtogroup hal */
NYX 0:85b3fd62ea1a 3 /** @{*/
NYX 0:85b3fd62ea1a 4 /* mbed Microcontroller Library
NYX 0:85b3fd62ea1a 5 * Copyright (c) 2006-2013 ARM Limited
NYX 0:85b3fd62ea1a 6 *
NYX 0:85b3fd62ea1a 7 * Licensed under the Apache License, Version 2.0 (the "License");
NYX 0:85b3fd62ea1a 8 * you may not use this file except in compliance with the License.
NYX 0:85b3fd62ea1a 9 * You may obtain a copy of the License at
NYX 0:85b3fd62ea1a 10 *
NYX 0:85b3fd62ea1a 11 * http://www.apache.org/licenses/LICENSE-2.0
NYX 0:85b3fd62ea1a 12 *
NYX 0:85b3fd62ea1a 13 * Unless required by applicable law or agreed to in writing, software
NYX 0:85b3fd62ea1a 14 * distributed under the License is distributed on an "AS IS" BASIS,
NYX 0:85b3fd62ea1a 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
NYX 0:85b3fd62ea1a 16 * See the License for the specific language governing permissions and
NYX 0:85b3fd62ea1a 17 * limitations under the License.
NYX 0:85b3fd62ea1a 18 */
NYX 0:85b3fd62ea1a 19 #ifndef MBED_SPI_API_H
NYX 0:85b3fd62ea1a 20 #define MBED_SPI_API_H
NYX 0:85b3fd62ea1a 21
NYX 0:85b3fd62ea1a 22 #include "device.h"
NYX 0:85b3fd62ea1a 23 #include "hal/dma_api.h"
NYX 0:85b3fd62ea1a 24 #include "hal/buffer.h"
NYX 0:85b3fd62ea1a 25
NYX 0:85b3fd62ea1a 26 #if DEVICE_SPI
NYX 0:85b3fd62ea1a 27
NYX 0:85b3fd62ea1a 28 #define SPI_EVENT_ERROR (1 << 1)
NYX 0:85b3fd62ea1a 29 #define SPI_EVENT_COMPLETE (1 << 2)
NYX 0:85b3fd62ea1a 30 #define SPI_EVENT_RX_OVERFLOW (1 << 3)
NYX 0:85b3fd62ea1a 31 #define SPI_EVENT_ALL (SPI_EVENT_ERROR | SPI_EVENT_COMPLETE | SPI_EVENT_RX_OVERFLOW)
NYX 0:85b3fd62ea1a 32
NYX 0:85b3fd62ea1a 33 #define SPI_EVENT_INTERNAL_TRANSFER_COMPLETE (1 << 30) // Internal flag to report that an event occurred
NYX 0:85b3fd62ea1a 34
NYX 0:85b3fd62ea1a 35 #define SPI_FILL_WORD (0xFFFF)
NYX 0:85b3fd62ea1a 36 #define SPI_FILL_CHAR (0xFF)
NYX 0:85b3fd62ea1a 37
NYX 0:85b3fd62ea1a 38 #if DEVICE_SPI_ASYNCH
NYX 0:85b3fd62ea1a 39 /** Asynch SPI HAL structure
NYX 0:85b3fd62ea1a 40 */
NYX 0:85b3fd62ea1a 41 typedef struct {
NYX 0:85b3fd62ea1a 42 struct spi_s spi; /**< Target specific SPI structure */
NYX 0:85b3fd62ea1a 43 struct buffer_s tx_buff; /**< Tx buffer */
NYX 0:85b3fd62ea1a 44 struct buffer_s rx_buff; /**< Rx buffer */
NYX 0:85b3fd62ea1a 45 } spi_t;
NYX 0:85b3fd62ea1a 46
NYX 0:85b3fd62ea1a 47 #else
NYX 0:85b3fd62ea1a 48 /** Non-asynch SPI HAL structure
NYX 0:85b3fd62ea1a 49 */
NYX 0:85b3fd62ea1a 50 typedef struct spi_s spi_t;
NYX 0:85b3fd62ea1a 51
NYX 0:85b3fd62ea1a 52 #endif
NYX 0:85b3fd62ea1a 53
NYX 0:85b3fd62ea1a 54 #ifdef __cplusplus
NYX 0:85b3fd62ea1a 55 extern "C" {
NYX 0:85b3fd62ea1a 56 #endif
NYX 0:85b3fd62ea1a 57
NYX 0:85b3fd62ea1a 58 /**
NYX 0:85b3fd62ea1a 59 * \defgroup hal_GeneralSPI SPI Configuration Functions
NYX 0:85b3fd62ea1a 60 * @{
NYX 0:85b3fd62ea1a 61 */
NYX 0:85b3fd62ea1a 62
NYX 0:85b3fd62ea1a 63 /** Initialize the SPI peripheral
NYX 0:85b3fd62ea1a 64 *
NYX 0:85b3fd62ea1a 65 * Configures the pins used by SPI, sets a default format and frequency, and enables the peripheral
NYX 0:85b3fd62ea1a 66 * @param[out] obj The SPI object to initialize
NYX 0:85b3fd62ea1a 67 * @param[in] mosi The pin to use for MOSI
NYX 0:85b3fd62ea1a 68 * @param[in] miso The pin to use for MISO
NYX 0:85b3fd62ea1a 69 * @param[in] sclk The pin to use for SCLK
NYX 0:85b3fd62ea1a 70 * @param[in] ssel The pin to use for SSEL
NYX 0:85b3fd62ea1a 71 */
NYX 0:85b3fd62ea1a 72 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel);
NYX 0:85b3fd62ea1a 73
NYX 0:85b3fd62ea1a 74 /** Release a SPI object
NYX 0:85b3fd62ea1a 75 *
NYX 0:85b3fd62ea1a 76 * TODO: spi_free is currently unimplemented
NYX 0:85b3fd62ea1a 77 * This will require reference counting at the C++ level to be safe
NYX 0:85b3fd62ea1a 78 *
NYX 0:85b3fd62ea1a 79 * Return the pins owned by the SPI object to their reset state
NYX 0:85b3fd62ea1a 80 * Disable the SPI peripheral
NYX 0:85b3fd62ea1a 81 * Disable the SPI clock
NYX 0:85b3fd62ea1a 82 * @param[in] obj The SPI object to deinitialize
NYX 0:85b3fd62ea1a 83 */
NYX 0:85b3fd62ea1a 84 void spi_free(spi_t *obj);
NYX 0:85b3fd62ea1a 85
NYX 0:85b3fd62ea1a 86 /** Configure the SPI format
NYX 0:85b3fd62ea1a 87 *
NYX 0:85b3fd62ea1a 88 * Set the number of bits per frame, configure clock polarity and phase, shift order and master/slave mode.
NYX 0:85b3fd62ea1a 89 * The default bit order is MSB.
NYX 0:85b3fd62ea1a 90 * @param[in,out] obj The SPI object to configure
NYX 0:85b3fd62ea1a 91 * @param[in] bits The number of bits per frame
NYX 0:85b3fd62ea1a 92 * @param[in] mode The SPI mode (clock polarity, phase, and shift direction)
NYX 0:85b3fd62ea1a 93 * @param[in] slave Zero for master mode or non-zero for slave mode
NYX 0:85b3fd62ea1a 94 */
NYX 0:85b3fd62ea1a 95 void spi_format(spi_t *obj, int bits, int mode, int slave);
NYX 0:85b3fd62ea1a 96
NYX 0:85b3fd62ea1a 97 /** Set the SPI baud rate
NYX 0:85b3fd62ea1a 98 *
NYX 0:85b3fd62ea1a 99 * Actual frequency may differ from the desired frequency due to available dividers and bus clock
NYX 0:85b3fd62ea1a 100 * Configures the SPI peripheral's baud rate
NYX 0:85b3fd62ea1a 101 * @param[in,out] obj The SPI object to configure
NYX 0:85b3fd62ea1a 102 * @param[in] hz The baud rate in Hz
NYX 0:85b3fd62ea1a 103 */
NYX 0:85b3fd62ea1a 104 void spi_frequency(spi_t *obj, int hz);
NYX 0:85b3fd62ea1a 105
NYX 0:85b3fd62ea1a 106 /**@}*/
NYX 0:85b3fd62ea1a 107 /**
NYX 0:85b3fd62ea1a 108 * \defgroup SynchSPI Synchronous SPI Hardware Abstraction Layer
NYX 0:85b3fd62ea1a 109 * @{
NYX 0:85b3fd62ea1a 110 */
NYX 0:85b3fd62ea1a 111
NYX 0:85b3fd62ea1a 112 /** Write a byte out in master mode and receive a value
NYX 0:85b3fd62ea1a 113 *
NYX 0:85b3fd62ea1a 114 * @param[in] obj The SPI peripheral to use for sending
NYX 0:85b3fd62ea1a 115 * @param[in] value The value to send
NYX 0:85b3fd62ea1a 116 * @return Returns the value received during send
NYX 0:85b3fd62ea1a 117 */
NYX 0:85b3fd62ea1a 118 int spi_master_write(spi_t *obj, int value);
NYX 0:85b3fd62ea1a 119
NYX 0:85b3fd62ea1a 120 /** Write a block out in master mode and receive a value
NYX 0:85b3fd62ea1a 121 *
NYX 0:85b3fd62ea1a 122 * The total number of bytes sent and recieved will be the maximum of
NYX 0:85b3fd62ea1a 123 * tx_length and rx_length. The bytes written will be padded with the
NYX 0:85b3fd62ea1a 124 * value 0xff.
NYX 0:85b3fd62ea1a 125 *
NYX 0:85b3fd62ea1a 126 * @param[in] obj The SPI peripheral to use for sending
NYX 0:85b3fd62ea1a 127 * @param[in] tx_buffer Pointer to the byte-array of data to write to the device
NYX 0:85b3fd62ea1a 128 * @param[in] tx_length Number of bytes to write, may be zero
NYX 0:85b3fd62ea1a 129 * @param[in] rx_buffer Pointer to the byte-array of data to read from the device
NYX 0:85b3fd62ea1a 130 * @param[in] rx_length Number of bytes to read, may be zero
NYX 0:85b3fd62ea1a 131 * @param[in] write_fill Default data transmitted while performing a read
NYX 0:85b3fd62ea1a 132 * @returns
NYX 0:85b3fd62ea1a 133 * The number of bytes written and read from the device. This is
NYX 0:85b3fd62ea1a 134 * maximum of tx_length and rx_length.
NYX 0:85b3fd62ea1a 135 */
NYX 0:85b3fd62ea1a 136 int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, char write_fill);
NYX 0:85b3fd62ea1a 137
NYX 0:85b3fd62ea1a 138 /** Check if a value is available to read
NYX 0:85b3fd62ea1a 139 *
NYX 0:85b3fd62ea1a 140 * @param[in] obj The SPI peripheral to check
NYX 0:85b3fd62ea1a 141 * @return non-zero if a value is available
NYX 0:85b3fd62ea1a 142 */
NYX 0:85b3fd62ea1a 143 int spi_slave_receive(spi_t *obj);
NYX 0:85b3fd62ea1a 144
NYX 0:85b3fd62ea1a 145 /** Get a received value out of the SPI receive buffer in slave mode
NYX 0:85b3fd62ea1a 146 *
NYX 0:85b3fd62ea1a 147 * Blocks until a value is available
NYX 0:85b3fd62ea1a 148 * @param[in] obj The SPI peripheral to read
NYX 0:85b3fd62ea1a 149 * @return The value received
NYX 0:85b3fd62ea1a 150 */
NYX 0:85b3fd62ea1a 151 int spi_slave_read(spi_t *obj);
NYX 0:85b3fd62ea1a 152
NYX 0:85b3fd62ea1a 153 /** Write a value to the SPI peripheral in slave mode
NYX 0:85b3fd62ea1a 154 *
NYX 0:85b3fd62ea1a 155 * Blocks until the SPI peripheral can be written to
NYX 0:85b3fd62ea1a 156 * @param[in] obj The SPI peripheral to write
NYX 0:85b3fd62ea1a 157 * @param[in] value The value to write
NYX 0:85b3fd62ea1a 158 */
NYX 0:85b3fd62ea1a 159 void spi_slave_write(spi_t *obj, int value);
NYX 0:85b3fd62ea1a 160
NYX 0:85b3fd62ea1a 161 /** Checks if the specified SPI peripheral is in use
NYX 0:85b3fd62ea1a 162 *
NYX 0:85b3fd62ea1a 163 * @param[in] obj The SPI peripheral to check
NYX 0:85b3fd62ea1a 164 * @return non-zero if the peripheral is currently transmitting
NYX 0:85b3fd62ea1a 165 */
NYX 0:85b3fd62ea1a 166 int spi_busy(spi_t *obj);
NYX 0:85b3fd62ea1a 167
NYX 0:85b3fd62ea1a 168 /** Get the module number
NYX 0:85b3fd62ea1a 169 *
NYX 0:85b3fd62ea1a 170 * @param[in] obj The SPI peripheral to check
NYX 0:85b3fd62ea1a 171 * @return The module number
NYX 0:85b3fd62ea1a 172 */
NYX 0:85b3fd62ea1a 173 uint8_t spi_get_module(spi_t *obj);
NYX 0:85b3fd62ea1a 174
NYX 0:85b3fd62ea1a 175 /**@}*/
NYX 0:85b3fd62ea1a 176
NYX 0:85b3fd62ea1a 177 #if DEVICE_SPI_ASYNCH
NYX 0:85b3fd62ea1a 178 /**
NYX 0:85b3fd62ea1a 179 * \defgroup AsynchSPI Asynchronous SPI Hardware Abstraction Layer
NYX 0:85b3fd62ea1a 180 * @{
NYX 0:85b3fd62ea1a 181 */
NYX 0:85b3fd62ea1a 182
NYX 0:85b3fd62ea1a 183 /** Begin the SPI transfer. Buffer pointers and lengths are specified in tx_buff and rx_buff
NYX 0:85b3fd62ea1a 184 *
NYX 0:85b3fd62ea1a 185 * @param[in] obj The SPI object that holds the transfer information
NYX 0:85b3fd62ea1a 186 * @param[in] tx The transmit buffer
NYX 0:85b3fd62ea1a 187 * @param[in] tx_length The number of bytes to transmit
NYX 0:85b3fd62ea1a 188 * @param[in] rx The receive buffer
NYX 0:85b3fd62ea1a 189 * @param[in] rx_length The number of bytes to receive
NYX 0:85b3fd62ea1a 190 * @param[in] bit_width The bit width of buffer words
NYX 0:85b3fd62ea1a 191 * @param[in] event The logical OR of events to be registered
NYX 0:85b3fd62ea1a 192 * @param[in] handler SPI interrupt handler
NYX 0:85b3fd62ea1a 193 * @param[in] hint A suggestion for how to use DMA with this transfer
NYX 0:85b3fd62ea1a 194 */
NYX 0:85b3fd62ea1a 195 void spi_master_transfer(spi_t *obj, const void *tx, size_t tx_length, void *rx, size_t rx_length, uint8_t bit_width, uint32_t handler, uint32_t event, DMAUsage hint);
NYX 0:85b3fd62ea1a 196
NYX 0:85b3fd62ea1a 197 /** The asynchronous IRQ handler
NYX 0:85b3fd62ea1a 198 *
NYX 0:85b3fd62ea1a 199 * Reads the received values out of the RX FIFO, writes values into the TX FIFO and checks for transfer termination
NYX 0:85b3fd62ea1a 200 * conditions, such as buffer overflows or transfer complete.
NYX 0:85b3fd62ea1a 201 * @param[in] obj The SPI object that holds the transfer information
NYX 0:85b3fd62ea1a 202 * @return Event flags if a transfer termination condition was met; otherwise 0.
NYX 0:85b3fd62ea1a 203 */
NYX 0:85b3fd62ea1a 204 uint32_t spi_irq_handler_asynch(spi_t *obj);
NYX 0:85b3fd62ea1a 205
NYX 0:85b3fd62ea1a 206 /** Attempts to determine if the SPI peripheral is already in use
NYX 0:85b3fd62ea1a 207 *
NYX 0:85b3fd62ea1a 208 * If a temporary DMA channel has been allocated, peripheral is in use.
NYX 0:85b3fd62ea1a 209 * If a permanent DMA channel has been allocated, check if the DMA channel is in use. If not, proceed as though no DMA
NYX 0:85b3fd62ea1a 210 * channel were allocated.
NYX 0:85b3fd62ea1a 211 * If no DMA channel is allocated, check whether tx and rx buffers have been assigned. For each assigned buffer, check
NYX 0:85b3fd62ea1a 212 * if the corresponding buffer position is less than the buffer length. If buffers do not indicate activity, check if
NYX 0:85b3fd62ea1a 213 * there are any bytes in the FIFOs.
NYX 0:85b3fd62ea1a 214 * @param[in] obj The SPI object to check for activity
NYX 0:85b3fd62ea1a 215 * @return Non-zero if the SPI port is active or zero if it is not.
NYX 0:85b3fd62ea1a 216 */
NYX 0:85b3fd62ea1a 217 uint8_t spi_active(spi_t *obj);
NYX 0:85b3fd62ea1a 218
NYX 0:85b3fd62ea1a 219 /** Abort an SPI transfer
NYX 0:85b3fd62ea1a 220 *
NYX 0:85b3fd62ea1a 221 * @param obj The SPI peripheral to stop
NYX 0:85b3fd62ea1a 222 */
NYX 0:85b3fd62ea1a 223 void spi_abort_asynch(spi_t *obj);
NYX 0:85b3fd62ea1a 224
NYX 0:85b3fd62ea1a 225
NYX 0:85b3fd62ea1a 226 #endif
NYX 0:85b3fd62ea1a 227
NYX 0:85b3fd62ea1a 228 /**@}*/
NYX 0:85b3fd62ea1a 229
NYX 0:85b3fd62ea1a 230 #ifdef __cplusplus
NYX 0:85b3fd62ea1a 231 }
NYX 0:85b3fd62ea1a 232 #endif // __cplusplus
NYX 0:85b3fd62ea1a 233
NYX 0:85b3fd62ea1a 234 #endif // SPI_DEVICE
NYX 0:85b3fd62ea1a 235
NYX 0:85b3fd62ea1a 236 #endif // MBED_SPI_API_H
NYX 0:85b3fd62ea1a 237
NYX 0:85b3fd62ea1a 238 /** @}*/