mbed library for slider v2

Dependents:   kl46z_slider_v2

Committer:
mturner5
Date:
Wed Sep 14 07:04:27 2016 +0000
Revision:
0:b7116bd48af6
Tried to use the timer.

Who changed what in which revision?

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