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_I2C_API_H
jhon309 0:c52df770855b 17 #define MBED_I2C_API_H
jhon309 0:c52df770855b 18
jhon309 0:c52df770855b 19 #include "device.h"
jhon309 0:c52df770855b 20 #include "buffer.h"
jhon309 0:c52df770855b 21
jhon309 0:c52df770855b 22 #if DEVICE_I2C
jhon309 0:c52df770855b 23
jhon309 0:c52df770855b 24 /**
jhon309 0:c52df770855b 25 * @defgroup I2CEvents I2C Events Macros
jhon309 0:c52df770855b 26 *
jhon309 0:c52df770855b 27 * @{
jhon309 0:c52df770855b 28 */
jhon309 0:c52df770855b 29 #define I2C_EVENT_ERROR (1 << 1)
jhon309 0:c52df770855b 30 #define I2C_EVENT_ERROR_NO_SLAVE (1 << 2)
jhon309 0:c52df770855b 31 #define I2C_EVENT_TRANSFER_COMPLETE (1 << 3)
jhon309 0:c52df770855b 32 #define I2C_EVENT_TRANSFER_EARLY_NACK (1 << 4)
jhon309 0:c52df770855b 33 #define I2C_EVENT_ALL (I2C_EVENT_ERROR | I2C_EVENT_TRANSFER_COMPLETE | I2C_EVENT_ERROR_NO_SLAVE | I2C_EVENT_TRANSFER_EARLY_NACK)
jhon309 0:c52df770855b 34
jhon309 0:c52df770855b 35 /**@}*/
jhon309 0:c52df770855b 36
jhon309 0:c52df770855b 37 #if DEVICE_I2C_ASYNCH
jhon309 0:c52df770855b 38 /** Asynch i2c hal structure
jhon309 0:c52df770855b 39 */
jhon309 0:c52df770855b 40 typedef struct {
jhon309 0:c52df770855b 41 struct i2c_s i2c; /**< Target specific i2c structure */
jhon309 0:c52df770855b 42 struct buffer_s tx_buff; /**< Tx buffer */
jhon309 0:c52df770855b 43 struct buffer_s rx_buff; /**< Rx buffer */
jhon309 0:c52df770855b 44 } i2c_t;
jhon309 0:c52df770855b 45
jhon309 0:c52df770855b 46 #else
jhon309 0:c52df770855b 47 /** Non-asynch i2c hal structure
jhon309 0:c52df770855b 48 */
jhon309 0:c52df770855b 49 typedef struct i2c_s i2c_t;
jhon309 0:c52df770855b 50
jhon309 0:c52df770855b 51 #endif
jhon309 0:c52df770855b 52
jhon309 0:c52df770855b 53 enum {
jhon309 0:c52df770855b 54 I2C_ERROR_NO_SLAVE = -1,
jhon309 0:c52df770855b 55 I2C_ERROR_BUS_BUSY = -2
jhon309 0:c52df770855b 56 };
jhon309 0:c52df770855b 57
jhon309 0:c52df770855b 58 #ifdef __cplusplus
jhon309 0:c52df770855b 59 extern "C" {
jhon309 0:c52df770855b 60 #endif
jhon309 0:c52df770855b 61
jhon309 0:c52df770855b 62 /**
jhon309 0:c52df770855b 63 * \defgroup GeneralI2C I2C Configuration Functions
jhon309 0:c52df770855b 64 * @{
jhon309 0:c52df770855b 65 */
jhon309 0:c52df770855b 66
jhon309 0:c52df770855b 67 /** Initialize the I2C peripheral. It sets the default parameters for I2C
jhon309 0:c52df770855b 68 * peripheral, and configure its specifieds pins.
jhon309 0:c52df770855b 69 * @param obj The i2c object
jhon309 0:c52df770855b 70 * @param sda The sda pin
jhon309 0:c52df770855b 71 * @param scl The scl pin
jhon309 0:c52df770855b 72 */
jhon309 0:c52df770855b 73 void i2c_init(i2c_t *obj, PinName sda, PinName scl);
jhon309 0:c52df770855b 74
jhon309 0:c52df770855b 75 /** Configure the I2C frequency.
jhon309 0:c52df770855b 76 * @param obj The i2c object
jhon309 0:c52df770855b 77 * @param hz Frequency in Hz
jhon309 0:c52df770855b 78 */
jhon309 0:c52df770855b 79 void i2c_frequency(i2c_t *obj, int hz);
jhon309 0:c52df770855b 80
jhon309 0:c52df770855b 81 /** Send START command.
jhon309 0:c52df770855b 82 * @param obj The i2c object
jhon309 0:c52df770855b 83 */
jhon309 0:c52df770855b 84 int i2c_start(i2c_t *obj);
jhon309 0:c52df770855b 85
jhon309 0:c52df770855b 86 /** Send STOP command.
jhon309 0:c52df770855b 87 * @param obj The i2c object
jhon309 0:c52df770855b 88 */
jhon309 0:c52df770855b 89 int i2c_stop(i2c_t *obj);
jhon309 0:c52df770855b 90
jhon309 0:c52df770855b 91 /** Blocking reading data.
jhon309 0:c52df770855b 92 * @param obj The i2c object
jhon309 0:c52df770855b 93 * @param address 7-bit address (last bit is 1)
jhon309 0:c52df770855b 94 * @param data The buffer for receiving
jhon309 0:c52df770855b 95 * @param length Number of bytes to read
jhon309 0:c52df770855b 96 * @param stop Stop to be generated after the transfer is done
jhon309 0:c52df770855b 97 * @return Number of read bytes
jhon309 0:c52df770855b 98 */
jhon309 0:c52df770855b 99 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop);
jhon309 0:c52df770855b 100
jhon309 0:c52df770855b 101 /** Blocking sending data.
jhon309 0:c52df770855b 102 * @param obj The i2c object
jhon309 0:c52df770855b 103 * @param address 7-bit address (last bit is 0)
jhon309 0:c52df770855b 104 * @param data The buffer for sending
jhon309 0:c52df770855b 105 * @param length Number of bytes to wrte
jhon309 0:c52df770855b 106 * @param stop Stop to be generated after the transfer is done
jhon309 0:c52df770855b 107 * @return Number of written bytes
jhon309 0:c52df770855b 108 */
jhon309 0:c52df770855b 109 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop);
jhon309 0:c52df770855b 110
jhon309 0:c52df770855b 111 /** Reset I2C peripheral. TODO: The action here. Most of the implementation sends stop().
jhon309 0:c52df770855b 112 * @param obj The i2c object
jhon309 0:c52df770855b 113 */
jhon309 0:c52df770855b 114 void i2c_reset(i2c_t *obj);
jhon309 0:c52df770855b 115
jhon309 0:c52df770855b 116 /** Read one byte.
jhon309 0:c52df770855b 117 * @param obj The i2c object
jhon309 0:c52df770855b 118 * @param last Acknoledge
jhon309 0:c52df770855b 119 * @return The read byte
jhon309 0:c52df770855b 120 */
jhon309 0:c52df770855b 121 int i2c_byte_read(i2c_t *obj, int last);
jhon309 0:c52df770855b 122
jhon309 0:c52df770855b 123 /** Write one byte.
jhon309 0:c52df770855b 124 * @param obj The i2c object
jhon309 0:c52df770855b 125 * @param data Byte to be written
jhon309 0:c52df770855b 126 * @return 1 if NAK was received, 0 if ACK was received, 2 for timeout.
jhon309 0:c52df770855b 127 */
jhon309 0:c52df770855b 128 int i2c_byte_write(i2c_t *obj, int data);
jhon309 0:c52df770855b 129
jhon309 0:c52df770855b 130 /**@}*/
jhon309 0:c52df770855b 131
jhon309 0:c52df770855b 132 #if DEVICE_I2CSLAVE
jhon309 0:c52df770855b 133
jhon309 0:c52df770855b 134 /**
jhon309 0:c52df770855b 135 * \defgroup SynchI2C Synchronous I2C Hardware Abstraction Layer for slave
jhon309 0:c52df770855b 136 * @{
jhon309 0:c52df770855b 137 */
jhon309 0:c52df770855b 138
jhon309 0:c52df770855b 139 /** Configure I2C as slave or master.
jhon309 0:c52df770855b 140 * @param obj The I2C object
jhon309 0:c52df770855b 141 * @return non-zero if a value is available
jhon309 0:c52df770855b 142 */
jhon309 0:c52df770855b 143 void i2c_slave_mode(i2c_t *obj, int enable_slave);
jhon309 0:c52df770855b 144
jhon309 0:c52df770855b 145 /** Check to see if the I2C slave has been addressed.
jhon309 0:c52df770855b 146 * @param obj The I2C object
jhon309 0:c52df770855b 147 * @return The status - 1 - read addresses, 2 - write to all slaves,
jhon309 0:c52df770855b 148 * 3 write addressed, 0 - the slave has not been addressed
jhon309 0:c52df770855b 149 */
jhon309 0:c52df770855b 150 int i2c_slave_receive(i2c_t *obj);
jhon309 0:c52df770855b 151
jhon309 0:c52df770855b 152 /** Configure I2C as slave or master.
jhon309 0:c52df770855b 153 * @param obj The I2C object
jhon309 0:c52df770855b 154 * @return non-zero if a value is available
jhon309 0:c52df770855b 155 */
jhon309 0:c52df770855b 156 int i2c_slave_read(i2c_t *obj, char *data, int length);
jhon309 0:c52df770855b 157
jhon309 0:c52df770855b 158 /** Configure I2C as slave or master.
jhon309 0:c52df770855b 159 * @param obj The I2C object
jhon309 0:c52df770855b 160 * @return non-zero if a value is available
jhon309 0:c52df770855b 161 */
jhon309 0:c52df770855b 162 int i2c_slave_write(i2c_t *obj, const char *data, int length);
jhon309 0:c52df770855b 163
jhon309 0:c52df770855b 164 /** Configure I2C address.
jhon309 0:c52df770855b 165 * @param obj The I2C object
jhon309 0:c52df770855b 166 * @param idx Currently not used
jhon309 0:c52df770855b 167 * @param address The address to be set
jhon309 0:c52df770855b 168 * @param mask Currently not used
jhon309 0:c52df770855b 169 */
jhon309 0:c52df770855b 170 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask);
jhon309 0:c52df770855b 171
jhon309 0:c52df770855b 172 #endif
jhon309 0:c52df770855b 173
jhon309 0:c52df770855b 174 /**@}*/
jhon309 0:c52df770855b 175
jhon309 0:c52df770855b 176 #if DEVICE_I2C_ASYNCH
jhon309 0:c52df770855b 177
jhon309 0:c52df770855b 178 /**
jhon309 0:c52df770855b 179 * \defgroup AsynchI2C Asynchronous I2C Hardware Abstraction Layer
jhon309 0:c52df770855b 180 * @{
jhon309 0:c52df770855b 181 */
jhon309 0:c52df770855b 182
jhon309 0:c52df770855b 183 /** Start i2c asynchronous transfer.
jhon309 0:c52df770855b 184 * @param obj The I2C object
jhon309 0:c52df770855b 185 * @param tx The buffer to send
jhon309 0:c52df770855b 186 * @param tx_length The number of words to transmit
jhon309 0:c52df770855b 187 * @param rx The buffer to receive
jhon309 0:c52df770855b 188 * @param rx_length The number of words to receive
jhon309 0:c52df770855b 189 * @param address The address to be set - 7bit or 9 bit
jhon309 0:c52df770855b 190 * @param stop If true, stop will be generated after the transfer is done
jhon309 0:c52df770855b 191 * @param handler The I2C IRQ handler to be set
jhon309 0:c52df770855b 192 * @param hint DMA hint usage
jhon309 0:c52df770855b 193 */
jhon309 0:c52df770855b 194 void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx, size_t rx_length, uint32_t address, uint32_t stop, uint32_t handler, uint32_t event, DMAUsage hint);
jhon309 0:c52df770855b 195
jhon309 0:c52df770855b 196 /** The asynchronous IRQ handler
jhon309 0:c52df770855b 197 * @param obj The I2C object which holds the transfer information
jhon309 0:c52df770855b 198 * @return event flags if a transfer termination condition was met or 0 otherwise.
jhon309 0:c52df770855b 199 */
jhon309 0:c52df770855b 200 uint32_t i2c_irq_handler_asynch(i2c_t *obj);
jhon309 0:c52df770855b 201
jhon309 0:c52df770855b 202 /** Attempts to determine if I2C peripheral is already in use.
jhon309 0:c52df770855b 203 * @param obj The I2C object
jhon309 0:c52df770855b 204 * @return non-zero if the I2C module is active or zero if it is not
jhon309 0:c52df770855b 205 */
jhon309 0:c52df770855b 206 uint8_t i2c_active(i2c_t *obj);
jhon309 0:c52df770855b 207
jhon309 0:c52df770855b 208 /** Abort ongoing asynchronous transaction.
jhon309 0:c52df770855b 209 * @param obj The I2C object
jhon309 0:c52df770855b 210 */
jhon309 0:c52df770855b 211 void i2c_abort_asynch(i2c_t *obj);
jhon309 0:c52df770855b 212
jhon309 0:c52df770855b 213 #endif
jhon309 0:c52df770855b 214
jhon309 0:c52df770855b 215 /**@}*/
jhon309 0:c52df770855b 216
jhon309 0:c52df770855b 217 #ifdef __cplusplus
jhon309 0:c52df770855b 218 }
jhon309 0:c52df770855b 219 #endif
jhon309 0:c52df770855b 220
jhon309 0:c52df770855b 221 #endif
jhon309 0:c52df770855b 222
jhon309 0:c52df770855b 223 #endif