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:
Kojto
Date:
Wed Jul 19 16:46:19 2017 +0100
Revision:
147:a97add6d7e64
Parent:
145:64910690c574
Child:
148:fd96258d940d
Release 147 of the mbed library.

Who changed what in which revision?

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