Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 00:01:50 2018 +0000
Revision:
0:6ad07c9019fd
Codigo de tales para todos los pasculaes

Who changed what in which revision?

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