Direct copy of the library, but apparently I made a change?
Fork of SHTx by
i2c.cpp@0:d55659b0c4a0, 2010-11-18 (annotated)
- Committer:
- NegativeBlack
- Date:
- Thu Nov 18 10:23:43 2010 +0000
- Revision:
- 0:d55659b0c4a0
- Child:
- 1:8465801be23f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NegativeBlack | 0:d55659b0c4a0 | 1 | /** |
NegativeBlack | 0:d55659b0c4a0 | 2 | * Copyright (c) 2010 Roy van Dam <roy@negative-black.org> |
NegativeBlack | 0:d55659b0c4a0 | 3 | * All rights reserved. |
NegativeBlack | 0:d55659b0c4a0 | 4 | * |
NegativeBlack | 0:d55659b0c4a0 | 5 | * Redistribution and use in source and binary forms, with or without |
NegativeBlack | 0:d55659b0c4a0 | 6 | * modification, are permitted provided that the following conditions |
NegativeBlack | 0:d55659b0c4a0 | 7 | * are met: |
NegativeBlack | 0:d55659b0c4a0 | 8 | * 1. Redistributions of source code must retain the above copyright |
NegativeBlack | 0:d55659b0c4a0 | 9 | * notice, this list of conditions and the following disclaimer. |
NegativeBlack | 0:d55659b0c4a0 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
NegativeBlack | 0:d55659b0c4a0 | 11 | * notice, this list of conditions and the following disclaimer in the |
NegativeBlack | 0:d55659b0c4a0 | 12 | * documentation and/or other materials provided with the distribution. |
NegativeBlack | 0:d55659b0c4a0 | 13 | * |
NegativeBlack | 0:d55659b0c4a0 | 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
NegativeBlack | 0:d55659b0c4a0 | 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
NegativeBlack | 0:d55659b0c4a0 | 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
NegativeBlack | 0:d55659b0c4a0 | 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
NegativeBlack | 0:d55659b0c4a0 | 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
NegativeBlack | 0:d55659b0c4a0 | 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
NegativeBlack | 0:d55659b0c4a0 | 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
NegativeBlack | 0:d55659b0c4a0 | 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
NegativeBlack | 0:d55659b0c4a0 | 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
NegativeBlack | 0:d55659b0c4a0 | 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
NegativeBlack | 0:d55659b0c4a0 | 24 | * SUCH DAMAGE. |
NegativeBlack | 0:d55659b0c4a0 | 25 | */ |
NegativeBlack | 0:d55659b0c4a0 | 26 | |
NegativeBlack | 0:d55659b0c4a0 | 27 | #include "i2c.hpp" |
NegativeBlack | 0:d55659b0c4a0 | 28 | |
NegativeBlack | 0:d55659b0c4a0 | 29 | namespace SHTx { |
NegativeBlack | 0:d55659b0c4a0 | 30 | I2C::I2C(PinName sda, PinName scl) : |
NegativeBlack | 0:d55659b0c4a0 | 31 | scl_pin(scl), sda_pin(sda), frequency(10) { |
NegativeBlack | 0:d55659b0c4a0 | 32 | this->sda_pin.output(); |
NegativeBlack | 0:d55659b0c4a0 | 33 | this->scl_pin.output(); |
NegativeBlack | 0:d55659b0c4a0 | 34 | } |
NegativeBlack | 0:d55659b0c4a0 | 35 | |
NegativeBlack | 0:d55659b0c4a0 | 36 | void |
NegativeBlack | 0:d55659b0c4a0 | 37 | I2C::setFrequency(uint32_t hz) { |
NegativeBlack | 0:d55659b0c4a0 | 38 | this->frequency = (1000000 / hz); |
NegativeBlack | 0:d55659b0c4a0 | 39 | } |
NegativeBlack | 0:d55659b0c4a0 | 40 | |
NegativeBlack | 0:d55659b0c4a0 | 41 | void |
NegativeBlack | 0:d55659b0c4a0 | 42 | I2C::start(void) { |
NegativeBlack | 0:d55659b0c4a0 | 43 | this->output(); |
NegativeBlack | 0:d55659b0c4a0 | 44 | this->sda(1); |
NegativeBlack | 0:d55659b0c4a0 | 45 | this->scl(1); |
NegativeBlack | 0:d55659b0c4a0 | 46 | this->sda(0); |
NegativeBlack | 0:d55659b0c4a0 | 47 | this->scl(0); |
NegativeBlack | 0:d55659b0c4a0 | 48 | this->scl(1); |
NegativeBlack | 0:d55659b0c4a0 | 49 | this->sda(1); |
NegativeBlack | 0:d55659b0c4a0 | 50 | this->scl(0); |
NegativeBlack | 0:d55659b0c4a0 | 51 | } |
NegativeBlack | 0:d55659b0c4a0 | 52 | |
NegativeBlack | 0:d55659b0c4a0 | 53 | void |
NegativeBlack | 0:d55659b0c4a0 | 54 | I2C::stop(void) { |
NegativeBlack | 0:d55659b0c4a0 | 55 | this->output(); |
NegativeBlack | 0:d55659b0c4a0 | 56 | this->sda(0); |
NegativeBlack | 0:d55659b0c4a0 | 57 | this->scl(1); |
NegativeBlack | 0:d55659b0c4a0 | 58 | this->sda(1); |
NegativeBlack | 0:d55659b0c4a0 | 59 | } |
NegativeBlack | 0:d55659b0c4a0 | 60 | |
NegativeBlack | 0:d55659b0c4a0 | 61 | bool |
NegativeBlack | 0:d55659b0c4a0 | 62 | I2C::wait(void) { |
NegativeBlack | 0:d55659b0c4a0 | 63 | bool ack = false; |
NegativeBlack | 0:d55659b0c4a0 | 64 | |
NegativeBlack | 0:d55659b0c4a0 | 65 | this->input(); |
NegativeBlack | 0:d55659b0c4a0 | 66 | for (uint8_t i = 0; i < 500 && !ack; i++) { |
NegativeBlack | 0:d55659b0c4a0 | 67 | ack = !this->sda_pin; |
NegativeBlack | 0:d55659b0c4a0 | 68 | wait_ms(1); |
NegativeBlack | 0:d55659b0c4a0 | 69 | } |
NegativeBlack | 0:d55659b0c4a0 | 70 | |
NegativeBlack | 0:d55659b0c4a0 | 71 | return ack; |
NegativeBlack | 0:d55659b0c4a0 | 72 | } |
NegativeBlack | 0:d55659b0c4a0 | 73 | |
NegativeBlack | 0:d55659b0c4a0 | 74 | bool |
NegativeBlack | 0:d55659b0c4a0 | 75 | I2C::write(uint8_t data) { |
NegativeBlack | 0:d55659b0c4a0 | 76 | bool ack; |
NegativeBlack | 0:d55659b0c4a0 | 77 | |
NegativeBlack | 0:d55659b0c4a0 | 78 | this->output(); |
NegativeBlack | 0:d55659b0c4a0 | 79 | for (uint8_t i = 8; i; i--) { |
NegativeBlack | 0:d55659b0c4a0 | 80 | this->shift_out(data & 0x80); |
NegativeBlack | 0:d55659b0c4a0 | 81 | data <<= 1; |
NegativeBlack | 0:d55659b0c4a0 | 82 | } |
NegativeBlack | 0:d55659b0c4a0 | 83 | |
NegativeBlack | 0:d55659b0c4a0 | 84 | this->input(); |
NegativeBlack | 0:d55659b0c4a0 | 85 | ack = !this->shift_in(); |
NegativeBlack | 0:d55659b0c4a0 | 86 | |
NegativeBlack | 0:d55659b0c4a0 | 87 | return ack; |
NegativeBlack | 0:d55659b0c4a0 | 88 | } |
NegativeBlack | 0:d55659b0c4a0 | 89 | |
NegativeBlack | 0:d55659b0c4a0 | 90 | uint8_t |
NegativeBlack | 0:d55659b0c4a0 | 91 | I2C::read(bool ack) { |
NegativeBlack | 0:d55659b0c4a0 | 92 | uint8_t data = 0; |
NegativeBlack | 0:d55659b0c4a0 | 93 | |
NegativeBlack | 0:d55659b0c4a0 | 94 | this->input(); |
NegativeBlack | 0:d55659b0c4a0 | 95 | for (uint8_t i = 8; i; i--) { |
NegativeBlack | 0:d55659b0c4a0 | 96 | data <<= 1; |
NegativeBlack | 0:d55659b0c4a0 | 97 | data |= this->shift_in(); |
NegativeBlack | 0:d55659b0c4a0 | 98 | } |
NegativeBlack | 0:d55659b0c4a0 | 99 | |
NegativeBlack | 0:d55659b0c4a0 | 100 | this->output(); |
NegativeBlack | 0:d55659b0c4a0 | 101 | this->shift_out(!ack); |
NegativeBlack | 0:d55659b0c4a0 | 102 | |
NegativeBlack | 0:d55659b0c4a0 | 103 | return data; |
NegativeBlack | 0:d55659b0c4a0 | 104 | } |
NegativeBlack | 0:d55659b0c4a0 | 105 | |
NegativeBlack | 0:d55659b0c4a0 | 106 | void |
NegativeBlack | 0:d55659b0c4a0 | 107 | I2C::output(void) { |
NegativeBlack | 0:d55659b0c4a0 | 108 | this->sda_pin.output(); |
NegativeBlack | 0:d55659b0c4a0 | 109 | } |
NegativeBlack | 0:d55659b0c4a0 | 110 | |
NegativeBlack | 0:d55659b0c4a0 | 111 | void |
NegativeBlack | 0:d55659b0c4a0 | 112 | I2C::input(void) { |
NegativeBlack | 0:d55659b0c4a0 | 113 | this->sda_pin.input(); |
NegativeBlack | 0:d55659b0c4a0 | 114 | } |
NegativeBlack | 0:d55659b0c4a0 | 115 | |
NegativeBlack | 0:d55659b0c4a0 | 116 | void |
NegativeBlack | 0:d55659b0c4a0 | 117 | I2C::sda(bool value) { |
NegativeBlack | 0:d55659b0c4a0 | 118 | this->sda_pin = value; |
NegativeBlack | 0:d55659b0c4a0 | 119 | wait_us(this->frequency); |
NegativeBlack | 0:d55659b0c4a0 | 120 | } |
NegativeBlack | 0:d55659b0c4a0 | 121 | |
NegativeBlack | 0:d55659b0c4a0 | 122 | void |
NegativeBlack | 0:d55659b0c4a0 | 123 | I2C::scl(bool value) { |
NegativeBlack | 0:d55659b0c4a0 | 124 | this->scl_pin = value; |
NegativeBlack | 0:d55659b0c4a0 | 125 | wait_us(this->frequency); |
NegativeBlack | 0:d55659b0c4a0 | 126 | } |
NegativeBlack | 0:d55659b0c4a0 | 127 | |
NegativeBlack | 0:d55659b0c4a0 | 128 | void |
NegativeBlack | 0:d55659b0c4a0 | 129 | I2C::shift_out(bool bit) { |
NegativeBlack | 0:d55659b0c4a0 | 130 | this->sda(bit); |
NegativeBlack | 0:d55659b0c4a0 | 131 | this->scl(1); |
NegativeBlack | 0:d55659b0c4a0 | 132 | this->scl(0); |
NegativeBlack | 0:d55659b0c4a0 | 133 | } |
NegativeBlack | 0:d55659b0c4a0 | 134 | |
NegativeBlack | 0:d55659b0c4a0 | 135 | bool |
NegativeBlack | 0:d55659b0c4a0 | 136 | I2C::shift_in(void) { |
NegativeBlack | 0:d55659b0c4a0 | 137 | wait_us(this->frequency); |
NegativeBlack | 0:d55659b0c4a0 | 138 | this->scl(1); |
NegativeBlack | 0:d55659b0c4a0 | 139 | bool bit = this->sda_pin; |
NegativeBlack | 0:d55659b0c4a0 | 140 | this->scl(0); |
NegativeBlack | 0:d55659b0c4a0 | 141 | return bit; |
NegativeBlack | 0:d55659b0c4a0 | 142 | } |
NegativeBlack | 0:d55659b0c4a0 | 143 | } |