CDY version that shares functionality with Counter

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Committer:
Charles David Young
Date:
Mon Nov 05 09:52:17 2018 -0700
Revision:
3:c547dba5d39b
Parent:
0:aa13e1c335cd
debug

Who changed what in which revision?

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