.

Dependents:   RTC

Committer:
jhon309
Date:
Thu Aug 13 00:20:09 2015 +0000
Revision:
0:88e313c910d0
RTC Example

Who changed what in which revision?

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