Dataloger

Committer:
jhon309
Date:
Thu Aug 20 00:37:14 2015 +0000
Revision:
0:666850d06c9f
ok

Who changed what in which revision?

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