001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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