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