Direct copy of the library, but apparently I made a change?
Fork of SHTx by
i2c.hpp@2:6e39013b40dd, 2016-09-10 (annotated)
- Committer:
- bobgiesberts
- Date:
- Sat Sep 10 12:45:09 2016 +0000
- Revision:
- 2:6e39013b40dd
- Parent:
- 1:8465801be23f
Copied the bit bang-bit from this library and adapted to the I2C protocol of the LDC1614
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NegativeBlack | 1:8465801be23f | 1 | /** |
NegativeBlack | 1:8465801be23f | 2 | * Copyright (c) 2010 Roy van Dam <roy@negative-black.org> |
NegativeBlack | 1:8465801be23f | 3 | * All rights reserved. |
NegativeBlack | 1:8465801be23f | 4 | * |
NegativeBlack | 1:8465801be23f | 5 | * Redistribution and use in source and binary forms, with or without |
NegativeBlack | 1:8465801be23f | 6 | * modification, are permitted provided that the following conditions |
NegativeBlack | 1:8465801be23f | 7 | * are met: |
NegativeBlack | 1:8465801be23f | 8 | * 1. Redistributions of source code must retain the above copyright |
NegativeBlack | 1:8465801be23f | 9 | * notice, this list of conditions and the following disclaimer. |
NegativeBlack | 1:8465801be23f | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
NegativeBlack | 1:8465801be23f | 11 | * notice, this list of conditions and the following disclaimer in the |
NegativeBlack | 1:8465801be23f | 12 | * documentation and/or other materials provided with the distribution. |
NegativeBlack | 1:8465801be23f | 13 | * |
NegativeBlack | 1:8465801be23f | 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
NegativeBlack | 1:8465801be23f | 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
NegativeBlack | 1:8465801be23f | 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
NegativeBlack | 1:8465801be23f | 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
NegativeBlack | 1:8465801be23f | 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
NegativeBlack | 1:8465801be23f | 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
NegativeBlack | 1:8465801be23f | 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
NegativeBlack | 1:8465801be23f | 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
NegativeBlack | 1:8465801be23f | 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
NegativeBlack | 1:8465801be23f | 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
NegativeBlack | 1:8465801be23f | 24 | * SUCH DAMAGE. |
NegativeBlack | 1:8465801be23f | 25 | */ |
NegativeBlack | 1:8465801be23f | 26 | |
NegativeBlack | 1:8465801be23f | 27 | #ifndef _I2C_HPP_ |
NegativeBlack | 1:8465801be23f | 28 | #define _I2C_HPP_ |
NegativeBlack | 1:8465801be23f | 29 | |
NegativeBlack | 1:8465801be23f | 30 | #include "mbed.h" |
NegativeBlack | 1:8465801be23f | 31 | |
NegativeBlack | 1:8465801be23f | 32 | namespace SHTx { |
NegativeBlack | 1:8465801be23f | 33 | /** |
NegativeBlack | 1:8465801be23f | 34 | * Class: I2C |
NegativeBlack | 1:8465801be23f | 35 | * Humidity and Temperature Sensor - SHT15 |
NegativeBlack | 1:8465801be23f | 36 | * I2C Bit-bang master driver. |
NegativeBlack | 1:8465801be23f | 37 | */ |
NegativeBlack | 1:8465801be23f | 38 | class I2C { |
NegativeBlack | 1:8465801be23f | 39 | private: |
NegativeBlack | 1:8465801be23f | 40 | DigitalInOut scl_pin; |
NegativeBlack | 1:8465801be23f | 41 | DigitalInOut sda_pin; |
NegativeBlack | 1:8465801be23f | 42 | |
NegativeBlack | 1:8465801be23f | 43 | uint32_t frequency; |
NegativeBlack | 1:8465801be23f | 44 | |
NegativeBlack | 1:8465801be23f | 45 | public: |
NegativeBlack | 1:8465801be23f | 46 | /** |
NegativeBlack | 1:8465801be23f | 47 | * Constructor: SHTx::I2C |
NegativeBlack | 1:8465801be23f | 48 | * Create an I2C Master interface, connected to the specified pins. |
NegativeBlack | 1:8465801be23f | 49 | * Bit-bang I2C driver to get around the lousy I2C implementation in the |
NegativeBlack | 1:8465801be23f | 50 | * SHTx interface... |
NegativeBlack | 1:8465801be23f | 51 | * |
NegativeBlack | 1:8465801be23f | 52 | * Variables: |
NegativeBlack | 1:8465801be23f | 53 | * sda - I2C data line pin |
NegativeBlack | 1:8465801be23f | 54 | * scl - I2C clock line pin |
NegativeBlack | 1:8465801be23f | 55 | */ |
NegativeBlack | 1:8465801be23f | 56 | I2C(PinName sda, PinName scl); |
NegativeBlack | 1:8465801be23f | 57 | |
NegativeBlack | 1:8465801be23f | 58 | /** |
NegativeBlack | 1:8465801be23f | 59 | * Function: setFrequency |
NegativeBlack | 1:8465801be23f | 60 | * Set the frequency of the SHTx::I2C interface |
NegativeBlack | 1:8465801be23f | 61 | * |
NegativeBlack | 1:8465801be23f | 62 | * Variables: |
NegativeBlack | 1:8465801be23f | 63 | * hz - The bus frequency in hertz |
NegativeBlack | 1:8465801be23f | 64 | */ |
NegativeBlack | 1:8465801be23f | 65 | void setFrequency(uint32_t hz); |
NegativeBlack | 1:8465801be23f | 66 | |
NegativeBlack | 1:8465801be23f | 67 | /** |
NegativeBlack | 1:8465801be23f | 68 | * Function: start |
NegativeBlack | 1:8465801be23f | 69 | * Issue start condition on the SHTx::I2C bus |
NegativeBlack | 1:8465801be23f | 70 | */ |
NegativeBlack | 1:8465801be23f | 71 | void start(void); |
NegativeBlack | 1:8465801be23f | 72 | |
NegativeBlack | 1:8465801be23f | 73 | /** |
NegativeBlack | 1:8465801be23f | 74 | * Function: stop |
NegativeBlack | 1:8465801be23f | 75 | * Issue stop condition on the SHTx::I2C bus |
NegativeBlack | 1:8465801be23f | 76 | */ |
NegativeBlack | 1:8465801be23f | 77 | void stop(void); |
NegativeBlack | 1:8465801be23f | 78 | |
NegativeBlack | 1:8465801be23f | 79 | /** |
NegativeBlack | 1:8465801be23f | 80 | * Function: wait |
NegativeBlack | 1:8465801be23f | 81 | * Wait for SHT15 to complete measurement. |
NegativeBlack | 1:8465801be23f | 82 | * Max timeout 500ms. |
NegativeBlack | 1:8465801be23f | 83 | * |
NegativeBlack | 1:8465801be23f | 84 | * Variables: |
NegativeBlack | 1:8465801be23f | 85 | * returns - true if an ACK was received, false otherwise |
NegativeBlack | 1:8465801be23f | 86 | */ |
NegativeBlack | 1:8465801be23f | 87 | bool wait(void); |
NegativeBlack | 1:8465801be23f | 88 | |
NegativeBlack | 1:8465801be23f | 89 | /** |
NegativeBlack | 1:8465801be23f | 90 | * Function: reset |
NegativeBlack | 1:8465801be23f | 91 | * If communication with the device is lost |
NegativeBlack | 1:8465801be23f | 92 | * the command will reset the serial interface |
NegativeBlack | 1:8465801be23f | 93 | */ |
NegativeBlack | 1:8465801be23f | 94 | void reset(void); |
NegativeBlack | 1:8465801be23f | 95 | |
NegativeBlack | 1:8465801be23f | 96 | /** |
NegativeBlack | 1:8465801be23f | 97 | * Function: write |
NegativeBlack | 1:8465801be23f | 98 | * Write single byte out on the SHTx::I2C bus |
NegativeBlack | 1:8465801be23f | 99 | * |
NegativeBlack | 1:8465801be23f | 100 | * Variables: |
NegativeBlack | 1:8465801be23f | 101 | * data - data to write out on bus |
NegativeBlack | 1:8465801be23f | 102 | * returns - true if an ACK was received, false otherwise |
NegativeBlack | 1:8465801be23f | 103 | */ |
NegativeBlack | 1:8465801be23f | 104 | bool write(uint8_t data); |
NegativeBlack | 1:8465801be23f | 105 | |
NegativeBlack | 1:8465801be23f | 106 | /** |
NegativeBlack | 1:8465801be23f | 107 | * Function: write |
NegativeBlack | 1:8465801be23f | 108 | * Read single byte form the I2C bus |
NegativeBlack | 1:8465801be23f | 109 | * |
NegativeBlack | 1:8465801be23f | 110 | * Variables: |
NegativeBlack | 1:8465801be23f | 111 | * ack - indicates if the byte is to be acknowledged |
NegativeBlack | 1:8465801be23f | 112 | * returns - the read byte |
NegativeBlack | 1:8465801be23f | 113 | */ |
NegativeBlack | 1:8465801be23f | 114 | uint8_t read(bool ack); |
NegativeBlack | 1:8465801be23f | 115 | |
NegativeBlack | 1:8465801be23f | 116 | private: |
NegativeBlack | 1:8465801be23f | 117 | /** |
NegativeBlack | 1:8465801be23f | 118 | * Function: output |
NegativeBlack | 1:8465801be23f | 119 | * Configures sda pin as output |
NegativeBlack | 1:8465801be23f | 120 | */ |
NegativeBlack | 1:8465801be23f | 121 | void output(void); |
NegativeBlack | 1:8465801be23f | 122 | |
NegativeBlack | 1:8465801be23f | 123 | /** |
NegativeBlack | 1:8465801be23f | 124 | * Function: input |
NegativeBlack | 1:8465801be23f | 125 | * Configures sda pin as input |
NegativeBlack | 1:8465801be23f | 126 | */ |
NegativeBlack | 1:8465801be23f | 127 | void input(void); |
NegativeBlack | 1:8465801be23f | 128 | |
NegativeBlack | 1:8465801be23f | 129 | /** |
NegativeBlack | 1:8465801be23f | 130 | * Function: sda |
NegativeBlack | 1:8465801be23f | 131 | * Drive sda pin. |
NegativeBlack | 1:8465801be23f | 132 | * |
NegativeBlack | 1:8465801be23f | 133 | * Variables: |
NegativeBlack | 1:8465801be23f | 134 | * value - drive pin high or low |
NegativeBlack | 1:8465801be23f | 135 | */ |
NegativeBlack | 1:8465801be23f | 136 | void sda(bool value); |
NegativeBlack | 1:8465801be23f | 137 | |
NegativeBlack | 1:8465801be23f | 138 | /** |
NegativeBlack | 1:8465801be23f | 139 | * Function: scl |
NegativeBlack | 1:8465801be23f | 140 | * Drive scl pin. |
NegativeBlack | 1:8465801be23f | 141 | * |
NegativeBlack | 1:8465801be23f | 142 | * Variables: |
NegativeBlack | 1:8465801be23f | 143 | * value - drive pin high or low |
NegativeBlack | 1:8465801be23f | 144 | */ |
NegativeBlack | 1:8465801be23f | 145 | void scl(bool value); |
NegativeBlack | 1:8465801be23f | 146 | |
NegativeBlack | 1:8465801be23f | 147 | /** |
NegativeBlack | 1:8465801be23f | 148 | * Function: shift_out |
NegativeBlack | 1:8465801be23f | 149 | * Write single bit out on the SHTx::I2C bus |
NegativeBlack | 1:8465801be23f | 150 | * |
NegativeBlack | 1:8465801be23f | 151 | * Variables: |
NegativeBlack | 1:8465801be23f | 152 | * bit - value of the bit to be written. |
NegativeBlack | 1:8465801be23f | 153 | */ |
NegativeBlack | 1:8465801be23f | 154 | void shift_out(bool bit); |
NegativeBlack | 1:8465801be23f | 155 | |
NegativeBlack | 1:8465801be23f | 156 | /** |
NegativeBlack | 1:8465801be23f | 157 | * Function: shift_in |
NegativeBlack | 1:8465801be23f | 158 | * Read single bit from the SHTx::I2C bus |
NegativeBlack | 1:8465801be23f | 159 | * |
NegativeBlack | 1:8465801be23f | 160 | * Variables: |
NegativeBlack | 1:8465801be23f | 161 | * return - value of the bit read. |
NegativeBlack | 1:8465801be23f | 162 | */ |
NegativeBlack | 1:8465801be23f | 163 | bool shift_in(void); |
NegativeBlack | 1:8465801be23f | 164 | }; |
NegativeBlack | 1:8465801be23f | 165 | } |
NegativeBlack | 1:8465801be23f | 166 | |
NegativeBlack | 1:8465801be23f | 167 | /* !_I2C_HPP_ */ |
NegativeBlack | 0:d55659b0c4a0 | 168 | #endif |