I2C_EEPROM

Committer:
jhon309
Date:
Thu Aug 13 00:23:16 2015 +0000
Revision:
0:ac8863619623
I2C

Who changed what in which revision?

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