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-2015 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_H
jhon309 0:ac8863619623 17 #define MBED_SPI_H
jhon309 0:ac8863619623 18
jhon309 0:ac8863619623 19 #include "platform.h"
jhon309 0:ac8863619623 20
jhon309 0:ac8863619623 21 #if DEVICE_SPI
jhon309 0:ac8863619623 22
jhon309 0:ac8863619623 23 #include "spi_api.h"
jhon309 0:ac8863619623 24
jhon309 0:ac8863619623 25 #if DEVICE_SPI_ASYNCH
jhon309 0:ac8863619623 26 #include "CThunk.h"
jhon309 0:ac8863619623 27 #include "dma_api.h"
jhon309 0:ac8863619623 28 #include "CircularBuffer.h"
jhon309 0:ac8863619623 29 #include "FunctionPointer.h"
jhon309 0:ac8863619623 30 #include "Transaction.h"
jhon309 0:ac8863619623 31 #endif
jhon309 0:ac8863619623 32
jhon309 0:ac8863619623 33 namespace mbed {
jhon309 0:ac8863619623 34
jhon309 0:ac8863619623 35 /** A SPI Master, used for communicating with SPI slave devices
jhon309 0:ac8863619623 36 *
jhon309 0:ac8863619623 37 * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz
jhon309 0:ac8863619623 38 *
jhon309 0:ac8863619623 39 * Most SPI devices will also require Chip Select and Reset signals. These
jhon309 0:ac8863619623 40 * can be controlled using <DigitalOut> pins
jhon309 0:ac8863619623 41 *
jhon309 0:ac8863619623 42 * Example:
jhon309 0:ac8863619623 43 * @code
jhon309 0:ac8863619623 44 * // Send a byte to a SPI slave, and record the response
jhon309 0:ac8863619623 45 *
jhon309 0:ac8863619623 46 * #include "mbed.h"
jhon309 0:ac8863619623 47 *
jhon309 0:ac8863619623 48 * // hardware ssel (where applicable)
jhon309 0:ac8863619623 49 * //SPI device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
jhon309 0:ac8863619623 50 *
jhon309 0:ac8863619623 51 * // software ssel
jhon309 0:ac8863619623 52 * SPI device(p5, p6, p7); // mosi, miso, sclk
jhon309 0:ac8863619623 53 * DigitalOut cs(p8); // ssel
jhon309 0:ac8863619623 54 *
jhon309 0:ac8863619623 55 * int main() {
jhon309 0:ac8863619623 56 * // hardware ssel (where applicable)
jhon309 0:ac8863619623 57 * //int response = device.write(0xFF);
jhon309 0:ac8863619623 58 *
jhon309 0:ac8863619623 59 * // software ssel
jhon309 0:ac8863619623 60 * cs = 0;
jhon309 0:ac8863619623 61 * int response = device.write(0xFF);
jhon309 0:ac8863619623 62 * cs = 1;
jhon309 0:ac8863619623 63 * }
jhon309 0:ac8863619623 64 * @endcode
jhon309 0:ac8863619623 65 */
jhon309 0:ac8863619623 66 class SPI {
jhon309 0:ac8863619623 67
jhon309 0:ac8863619623 68 public:
jhon309 0:ac8863619623 69
jhon309 0:ac8863619623 70 /** Create a SPI master connected to the specified pins
jhon309 0:ac8863619623 71 *
jhon309 0:ac8863619623 72 * mosi or miso can be specfied as NC if not used
jhon309 0:ac8863619623 73 *
jhon309 0:ac8863619623 74 * @param mosi SPI Master Out, Slave In pin
jhon309 0:ac8863619623 75 * @param miso SPI Master In, Slave Out pin
jhon309 0:ac8863619623 76 * @param sclk SPI Clock pin
jhon309 0:ac8863619623 77 * @param ssel SPI chip select pin
jhon309 0:ac8863619623 78 */
jhon309 0:ac8863619623 79 SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel=NC);
jhon309 0:ac8863619623 80
jhon309 0:ac8863619623 81 /** Configure the data transmission format
jhon309 0:ac8863619623 82 *
jhon309 0:ac8863619623 83 * @param bits Number of bits per SPI frame (4 - 16)
jhon309 0:ac8863619623 84 * @param mode Clock polarity and phase mode (0 - 3)
jhon309 0:ac8863619623 85 *
jhon309 0:ac8863619623 86 * @code
jhon309 0:ac8863619623 87 * mode | POL PHA
jhon309 0:ac8863619623 88 * -----+--------
jhon309 0:ac8863619623 89 * 0 | 0 0
jhon309 0:ac8863619623 90 * 1 | 0 1
jhon309 0:ac8863619623 91 * 2 | 1 0
jhon309 0:ac8863619623 92 * 3 | 1 1
jhon309 0:ac8863619623 93 * @endcode
jhon309 0:ac8863619623 94 */
jhon309 0:ac8863619623 95 void format(int bits, int mode = 0);
jhon309 0:ac8863619623 96
jhon309 0:ac8863619623 97 /** Set the spi bus clock frequency
jhon309 0:ac8863619623 98 *
jhon309 0:ac8863619623 99 * @param hz SCLK frequency in hz (default = 1MHz)
jhon309 0:ac8863619623 100 */
jhon309 0:ac8863619623 101 void frequency(int hz = 1000000);
jhon309 0:ac8863619623 102
jhon309 0:ac8863619623 103 /** Write to the SPI Slave and return the response
jhon309 0:ac8863619623 104 *
jhon309 0:ac8863619623 105 * @param value Data to be sent to the SPI slave
jhon309 0:ac8863619623 106 *
jhon309 0:ac8863619623 107 * @returns
jhon309 0:ac8863619623 108 * Response from the SPI slave
jhon309 0:ac8863619623 109 */
jhon309 0:ac8863619623 110 virtual int write(int value);
jhon309 0:ac8863619623 111
jhon309 0:ac8863619623 112 #if DEVICE_SPI_ASYNCH
jhon309 0:ac8863619623 113
jhon309 0:ac8863619623 114 /** Start non-blocking SPI transfer using 8bit buffers.
jhon309 0:ac8863619623 115 *
jhon309 0:ac8863619623 116 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
jhon309 0:ac8863619623 117 * the default SPI value is sent
jhon309 0:ac8863619623 118 * @param tx_length The length of TX buffer in bytes
jhon309 0:ac8863619623 119 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
jhon309 0:ac8863619623 120 * received data are ignored
jhon309 0:ac8863619623 121 * @param rx_length The length of RX buffer in bytes
jhon309 0:ac8863619623 122 * @param callback The event callback function
jhon309 0:ac8863619623 123 * @param event The logical OR of events to modify. Look at spi hal header file for SPI events.
jhon309 0:ac8863619623 124 * @return Zero if the transfer has started, or -1 if SPI peripheral is busy
jhon309 0:ac8863619623 125 */
jhon309 0:ac8863619623 126 template<typename Type>
jhon309 0:ac8863619623 127 int transfer(const Type *tx_buffer, int tx_length, Type *rx_buffer, int rx_length, const event_callback_t& callback, int event = SPI_EVENT_COMPLETE) {
jhon309 0:ac8863619623 128 if (spi_active(&_spi)) {
jhon309 0:ac8863619623 129 return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event);
jhon309 0:ac8863619623 130 }
jhon309 0:ac8863619623 131 start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event);
jhon309 0:ac8863619623 132 return 0;
jhon309 0:ac8863619623 133 }
jhon309 0:ac8863619623 134
jhon309 0:ac8863619623 135 /** Abort the on-going SPI transfer, and continue with transfer's in the queue if any.
jhon309 0:ac8863619623 136 */
jhon309 0:ac8863619623 137 void abort_transfer();
jhon309 0:ac8863619623 138
jhon309 0:ac8863619623 139 /** Clear the transaction buffer
jhon309 0:ac8863619623 140 */
jhon309 0:ac8863619623 141 void clear_transfer_buffer();
jhon309 0:ac8863619623 142
jhon309 0:ac8863619623 143 /** Clear the transaction buffer and abort on-going transfer.
jhon309 0:ac8863619623 144 */
jhon309 0:ac8863619623 145 void abort_all_transfers();
jhon309 0:ac8863619623 146
jhon309 0:ac8863619623 147 /** Configure DMA usage suggestion for non-blocking transfers
jhon309 0:ac8863619623 148 *
jhon309 0:ac8863619623 149 * @param usage The usage DMA hint for peripheral
jhon309 0:ac8863619623 150 * @return Zero if the usage was set, -1 if a transaction is on-going
jhon309 0:ac8863619623 151 */
jhon309 0:ac8863619623 152 int set_dma_usage(DMAUsage usage);
jhon309 0:ac8863619623 153
jhon309 0:ac8863619623 154 protected:
jhon309 0:ac8863619623 155 /** SPI IRQ handler
jhon309 0:ac8863619623 156 *
jhon309 0:ac8863619623 157 */
jhon309 0:ac8863619623 158 void irq_handler_asynch(void);
jhon309 0:ac8863619623 159
jhon309 0:ac8863619623 160 /** Common transfer method
jhon309 0:ac8863619623 161 *
jhon309 0:ac8863619623 162 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
jhon309 0:ac8863619623 163 * the default SPI value is sent
jhon309 0:ac8863619623 164 * @param tx_length The length of TX buffer in bytes
jhon309 0:ac8863619623 165 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
jhon309 0:ac8863619623 166 * received data are ignored
jhon309 0:ac8863619623 167 * @param rx_length The length of RX buffer in bytes
jhon309 0:ac8863619623 168 * @param bit_width The buffers element width
jhon309 0:ac8863619623 169 * @param callback The event callback function
jhon309 0:ac8863619623 170 * @param event The logical OR of events to modify
jhon309 0:ac8863619623 171 * @return Zero if the transfer has started or was added to the queue, or -1 if SPI peripheral is busy/buffer is full
jhon309 0:ac8863619623 172 */
jhon309 0:ac8863619623 173 int transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event);
jhon309 0:ac8863619623 174
jhon309 0:ac8863619623 175 /**
jhon309 0:ac8863619623 176 *
jhon309 0:ac8863619623 177 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
jhon309 0:ac8863619623 178 * the default SPI value is sent
jhon309 0:ac8863619623 179 * @param tx_length The length of TX buffer in bytes
jhon309 0:ac8863619623 180 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
jhon309 0:ac8863619623 181 * received data are ignored
jhon309 0:ac8863619623 182 * @param rx_length The length of RX buffer in bytes
jhon309 0:ac8863619623 183 * @param bit_width The buffers element width
jhon309 0:ac8863619623 184 * @param callback The event callback function
jhon309 0:ac8863619623 185 * @param event The logical OR of events to modify
jhon309 0:ac8863619623 186 * @return Zero if a transfer was added to the queue, or -1 if the queue is full
jhon309 0:ac8863619623 187 */
jhon309 0:ac8863619623 188 int queue_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event);
jhon309 0:ac8863619623 189
jhon309 0:ac8863619623 190 /** Configures a callback, spi peripheral and initiate a new transfer
jhon309 0:ac8863619623 191 *
jhon309 0:ac8863619623 192 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
jhon309 0:ac8863619623 193 * the default SPI value is sent
jhon309 0:ac8863619623 194 * @param tx_length The length of TX buffer in bytes
jhon309 0:ac8863619623 195 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
jhon309 0:ac8863619623 196 * received data are ignored
jhon309 0:ac8863619623 197 * @param rx_length The length of RX buffer in bytes
jhon309 0:ac8863619623 198 * @param bit_width The buffers element width
jhon309 0:ac8863619623 199 * @param callback The event callback function
jhon309 0:ac8863619623 200 * @param event The logical OR of events to modify
jhon309 0:ac8863619623 201 */
jhon309 0:ac8863619623 202 void start_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event);
jhon309 0:ac8863619623 203
jhon309 0:ac8863619623 204 #if TRANSACTION_QUEUE_SIZE_SPI
jhon309 0:ac8863619623 205
jhon309 0:ac8863619623 206 /** Start a new transaction
jhon309 0:ac8863619623 207 *
jhon309 0:ac8863619623 208 * @param data Transaction data
jhon309 0:ac8863619623 209 */
jhon309 0:ac8863619623 210 void start_transaction(transaction_t *data);
jhon309 0:ac8863619623 211
jhon309 0:ac8863619623 212 /** Dequeue a transaction
jhon309 0:ac8863619623 213 *
jhon309 0:ac8863619623 214 */
jhon309 0:ac8863619623 215 void dequeue_transaction();
jhon309 0:ac8863619623 216 static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer;
jhon309 0:ac8863619623 217 #endif
jhon309 0:ac8863619623 218
jhon309 0:ac8863619623 219 #endif
jhon309 0:ac8863619623 220
jhon309 0:ac8863619623 221 public:
jhon309 0:ac8863619623 222 virtual ~SPI() {
jhon309 0:ac8863619623 223 }
jhon309 0:ac8863619623 224
jhon309 0:ac8863619623 225 protected:
jhon309 0:ac8863619623 226 spi_t _spi;
jhon309 0:ac8863619623 227
jhon309 0:ac8863619623 228 #if DEVICE_SPI_ASYNCH
jhon309 0:ac8863619623 229 CThunk<SPI> _irq;
jhon309 0:ac8863619623 230 event_callback_t _callback;
jhon309 0:ac8863619623 231 DMAUsage _usage;
jhon309 0:ac8863619623 232 #endif
jhon309 0:ac8863619623 233
jhon309 0:ac8863619623 234 void aquire(void);
jhon309 0:ac8863619623 235 static SPI *_owner;
jhon309 0:ac8863619623 236 int _bits;
jhon309 0:ac8863619623 237 int _mode;
jhon309 0:ac8863619623 238 int _hz;
jhon309 0:ac8863619623 239 };
jhon309 0:ac8863619623 240
jhon309 0:ac8863619623 241 } // namespace mbed
jhon309 0:ac8863619623 242
jhon309 0:ac8863619623 243 #endif
jhon309 0:ac8863619623 244
jhon309 0:ac8863619623 245 #endif