PIR grove sensor that sends the sensed data to Thingspeak.com through the wifi module ESP 8266

Dependencies:   mbed

Committer:
skrawool
Date:
Mon Dec 05 16:39:27 2016 +0000
Revision:
0:3954a906acc2
PIR grove sensor that sends the sensed data to thingspeak through the wifi module ESP 8266

Who changed what in which revision?

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