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