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