test

Dependents:   robotic_fish_7

Committer:
juansal12
Date:
Tue Jan 14 19:14:29 2020 +0000
Revision:
0:44931ff4a3ed
Sofi 7 code;

Who changed what in which revision?

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