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