SPKT

Dependencies:   F746_GUI SD_PlayerSkeleton F746_SAI_IO

Committer:
phungductung
Date:
Tue Jun 04 09:08:29 2019 +0000
Revision:
0:aa3fc5ad02f7
SPKT

Who changed what in which revision?

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