The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Wed Feb 20 20:53:29 2019 +0000
Revision:
172:65be27845400
Parent:
165:d1b4690b3f8b
mbed library release version 165

Who changed what in which revision?

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