test

Dependents:   robotic_fish_6

Committer:
juansal12
Date:
Fri Dec 03 23:00:34 2021 +0000
Revision:
0:c792b17d9f78
uploaded sofi code ;

Who changed what in which revision?

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