SPKT

Dependencies:   F746_GUI SD_PlayerSkeleton F746_SAI_IO

Committer:
phungductung
Date:
Tue Jun 04 21:37:21 2019 +0000
Revision:
0:8ede47d38d10
SPKT

Who changed what in which revision?

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