The prosthetic control(MIT)

Committer:
ganlikun
Date:
Thu Jun 23 05:23:34 2022 +0000
Revision:
0:20e0c61e0684
01

Who changed what in which revision?

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