Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SHTx by
i2c.cpp@1:8465801be23f, 2010-11-19 (annotated)
- Committer:
- NegativeBlack
- Date:
- Fri Nov 19 16:24:46 2010 +0000
- Revision:
- 1:8465801be23f
- Parent:
- 0:d55659b0c4a0
Added: control over OTP reload
Added: connection reset.
Fixed: invalid humidity conversion when using the lower resolution
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 | #include "i2c.hpp" |
NegativeBlack | 1:8465801be23f | 28 | |
NegativeBlack | 1:8465801be23f | 29 | namespace SHTx { |
NegativeBlack | 1:8465801be23f | 30 | I2C::I2C(PinName sda, PinName scl) : |
NegativeBlack | 1:8465801be23f | 31 | scl_pin(scl), sda_pin(sda), frequency(10) { |
NegativeBlack | 1:8465801be23f | 32 | this->sda_pin.output(); |
NegativeBlack | 1:8465801be23f | 33 | this->scl_pin.output(); |
NegativeBlack | 1:8465801be23f | 34 | } |
NegativeBlack | 1:8465801be23f | 35 | |
NegativeBlack | 1:8465801be23f | 36 | void |
NegativeBlack | 1:8465801be23f | 37 | I2C::setFrequency(uint32_t hz) { |
NegativeBlack | 1:8465801be23f | 38 | this->frequency = (1000000 / hz); |
NegativeBlack | 1:8465801be23f | 39 | } |
NegativeBlack | 1:8465801be23f | 40 | |
NegativeBlack | 1:8465801be23f | 41 | void |
NegativeBlack | 1:8465801be23f | 42 | I2C::start(void) { |
NegativeBlack | 1:8465801be23f | 43 | this->output(); |
NegativeBlack | 1:8465801be23f | 44 | this->sda(1); |
NegativeBlack | 1:8465801be23f | 45 | this->scl(1); |
NegativeBlack | 1:8465801be23f | 46 | this->sda(0); |
NegativeBlack | 1:8465801be23f | 47 | this->scl(0); |
NegativeBlack | 1:8465801be23f | 48 | this->scl(1); |
NegativeBlack | 1:8465801be23f | 49 | this->sda(1); |
NegativeBlack | 1:8465801be23f | 50 | this->scl(0); |
NegativeBlack | 1:8465801be23f | 51 | } |
NegativeBlack | 1:8465801be23f | 52 | |
NegativeBlack | 1:8465801be23f | 53 | void |
NegativeBlack | 1:8465801be23f | 54 | I2C::stop(void) { |
NegativeBlack | 1:8465801be23f | 55 | this->output(); |
NegativeBlack | 1:8465801be23f | 56 | this->sda(0); |
NegativeBlack | 1:8465801be23f | 57 | this->scl(1); |
NegativeBlack | 1:8465801be23f | 58 | this->sda(1); |
NegativeBlack | 1:8465801be23f | 59 | } |
NegativeBlack | 1:8465801be23f | 60 | |
NegativeBlack | 1:8465801be23f | 61 | bool |
NegativeBlack | 1:8465801be23f | 62 | I2C::wait(void) { |
NegativeBlack | 1:8465801be23f | 63 | bool ack = false; |
NegativeBlack | 1:8465801be23f | 64 | |
NegativeBlack | 1:8465801be23f | 65 | this->input(); |
NegativeBlack | 1:8465801be23f | 66 | for (uint8_t i = 0; i < 500 && !ack; i++) { |
NegativeBlack | 1:8465801be23f | 67 | wait_ms(1); |
NegativeBlack | 1:8465801be23f | 68 | ack = !this->sda_pin; |
NegativeBlack | 1:8465801be23f | 69 | } |
NegativeBlack | 1:8465801be23f | 70 | |
NegativeBlack | 1:8465801be23f | 71 | return ack; |
NegativeBlack | 1:8465801be23f | 72 | } |
NegativeBlack | 1:8465801be23f | 73 | |
NegativeBlack | 1:8465801be23f | 74 | void |
NegativeBlack | 1:8465801be23f | 75 | I2C::reset(void) { |
NegativeBlack | 1:8465801be23f | 76 | this->output(); |
NegativeBlack | 1:8465801be23f | 77 | for (uint8_t i = 9; i; i--) { |
NegativeBlack | 1:8465801be23f | 78 | this->shift_out(1); |
NegativeBlack | 1:8465801be23f | 79 | } |
NegativeBlack | 1:8465801be23f | 80 | this->start(); |
NegativeBlack | 1:8465801be23f | 81 | this->scl(1); |
NegativeBlack | 1:8465801be23f | 82 | } |
NegativeBlack | 1:8465801be23f | 83 | |
NegativeBlack | 1:8465801be23f | 84 | bool |
NegativeBlack | 1:8465801be23f | 85 | I2C::write(uint8_t data) { |
NegativeBlack | 1:8465801be23f | 86 | bool ack; |
NegativeBlack | 1:8465801be23f | 87 | |
NegativeBlack | 1:8465801be23f | 88 | this->output(); |
NegativeBlack | 1:8465801be23f | 89 | for (uint8_t i = 8; i; i--) { |
NegativeBlack | 1:8465801be23f | 90 | this->shift_out(data & 0x80); |
NegativeBlack | 1:8465801be23f | 91 | data <<= 1; |
NegativeBlack | 1:8465801be23f | 92 | } |
NegativeBlack | 1:8465801be23f | 93 | |
NegativeBlack | 1:8465801be23f | 94 | this->input(); |
NegativeBlack | 1:8465801be23f | 95 | ack = !this->shift_in(); |
NegativeBlack | 1:8465801be23f | 96 | |
NegativeBlack | 1:8465801be23f | 97 | return ack; |
NegativeBlack | 1:8465801be23f | 98 | } |
NegativeBlack | 1:8465801be23f | 99 | |
NegativeBlack | 1:8465801be23f | 100 | uint8_t |
NegativeBlack | 1:8465801be23f | 101 | I2C::read(bool ack) { |
NegativeBlack | 1:8465801be23f | 102 | uint8_t data = 0; |
NegativeBlack | 1:8465801be23f | 103 | |
NegativeBlack | 1:8465801be23f | 104 | this->input(); |
NegativeBlack | 1:8465801be23f | 105 | for (uint8_t i = 8; i; i--) { |
NegativeBlack | 1:8465801be23f | 106 | data <<= 1; |
NegativeBlack | 1:8465801be23f | 107 | data |= this->shift_in(); |
NegativeBlack | 1:8465801be23f | 108 | } |
NegativeBlack | 1:8465801be23f | 109 | |
NegativeBlack | 1:8465801be23f | 110 | this->output(); |
NegativeBlack | 1:8465801be23f | 111 | this->shift_out(!ack); |
NegativeBlack | 1:8465801be23f | 112 | |
NegativeBlack | 1:8465801be23f | 113 | return data; |
NegativeBlack | 1:8465801be23f | 114 | } |
NegativeBlack | 1:8465801be23f | 115 | |
NegativeBlack | 1:8465801be23f | 116 | void |
NegativeBlack | 1:8465801be23f | 117 | I2C::output(void) { |
NegativeBlack | 1:8465801be23f | 118 | this->sda_pin.output(); |
NegativeBlack | 1:8465801be23f | 119 | } |
NegativeBlack | 1:8465801be23f | 120 | |
NegativeBlack | 1:8465801be23f | 121 | void |
NegativeBlack | 1:8465801be23f | 122 | I2C::input(void) { |
NegativeBlack | 1:8465801be23f | 123 | this->sda_pin.input(); |
NegativeBlack | 1:8465801be23f | 124 | } |
NegativeBlack | 1:8465801be23f | 125 | |
NegativeBlack | 1:8465801be23f | 126 | void |
NegativeBlack | 1:8465801be23f | 127 | I2C::sda(bool value) { |
NegativeBlack | 1:8465801be23f | 128 | this->sda_pin = value; |
NegativeBlack | 1:8465801be23f | 129 | wait_us(this->frequency); |
NegativeBlack | 1:8465801be23f | 130 | } |
NegativeBlack | 1:8465801be23f | 131 | |
NegativeBlack | 1:8465801be23f | 132 | void |
NegativeBlack | 1:8465801be23f | 133 | I2C::scl(bool value) { |
NegativeBlack | 1:8465801be23f | 134 | this->scl_pin = value; |
NegativeBlack | 1:8465801be23f | 135 | wait_us(this->frequency); |
NegativeBlack | 1:8465801be23f | 136 | } |
NegativeBlack | 1:8465801be23f | 137 | |
NegativeBlack | 1:8465801be23f | 138 | void |
NegativeBlack | 1:8465801be23f | 139 | I2C::shift_out(bool bit) { |
NegativeBlack | 1:8465801be23f | 140 | this->sda(bit); |
NegativeBlack | 1:8465801be23f | 141 | this->scl(1); |
NegativeBlack | 1:8465801be23f | 142 | this->scl(0); |
NegativeBlack | 1:8465801be23f | 143 | } |
NegativeBlack | 1:8465801be23f | 144 | |
NegativeBlack | 1:8465801be23f | 145 | bool |
NegativeBlack | 1:8465801be23f | 146 | I2C::shift_in(void) { |
NegativeBlack | 1:8465801be23f | 147 | wait_us(this->frequency); |
NegativeBlack | 1:8465801be23f | 148 | this->scl(1); |
NegativeBlack | 1:8465801be23f | 149 | bool bit = this->sda_pin; |
NegativeBlack | 1:8465801be23f | 150 | this->scl(0); |
NegativeBlack | 1:8465801be23f | 151 | return bit; |
NegativeBlack | 1:8465801be23f | 152 | } |
NegativeBlack | 0:d55659b0c4a0 | 153 | } |