mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Thu Sep 10 20:14:29 2015 +1000
Revision:
11:8123a070ade6
Parent:
1:71204b8406f2
Added .hgignore

Who changed what in which revision?

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