DHT11

Committer:
jhon309
Date:
Thu Aug 13 00:21:57 2015 +0000
Revision:
0:c52df770855b
DHT11

Who changed what in which revision?

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