Mangue Baja Box

Dependencies:   mbed

Committer:
einsteingustavo
Date:
Mon Jul 29 20:38:00 2019 +0000
Revision:
0:0dee8840a1c0
Mangue Baja Box

Who changed what in which revision?

UserRevisionLine numberNew contents of line
einsteingustavo 0:0dee8840a1c0 1 /* mbed Microcontroller Library
einsteingustavo 0:0dee8840a1c0 2 * Copyright (c) 2006-2013 ARM Limited
einsteingustavo 0:0dee8840a1c0 3 *
einsteingustavo 0:0dee8840a1c0 4 * Licensed under the Apache License, Version 2.0 (the "License");
einsteingustavo 0:0dee8840a1c0 5 * you may not use this file except in compliance with the License.
einsteingustavo 0:0dee8840a1c0 6 * You may obtain a copy of the License at
einsteingustavo 0:0dee8840a1c0 7 *
einsteingustavo 0:0dee8840a1c0 8 * http://www.apache.org/licenses/LICENSE-2.0
einsteingustavo 0:0dee8840a1c0 9 *
einsteingustavo 0:0dee8840a1c0 10 * Unless required by applicable law or agreed to in writing, software
einsteingustavo 0:0dee8840a1c0 11 * distributed under the License is distributed on an "AS IS" BASIS,
einsteingustavo 0:0dee8840a1c0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
einsteingustavo 0:0dee8840a1c0 13 * See the License for the specific language governing permissions and
einsteingustavo 0:0dee8840a1c0 14 * limitations under the License.
einsteingustavo 0:0dee8840a1c0 15 */
einsteingustavo 0:0dee8840a1c0 16 #ifndef MBED_I2C_H
einsteingustavo 0:0dee8840a1c0 17 #define MBED_I2C_H
einsteingustavo 0:0dee8840a1c0 18
einsteingustavo 0:0dee8840a1c0 19 #include "platform.h"
einsteingustavo 0:0dee8840a1c0 20
einsteingustavo 0:0dee8840a1c0 21 #if DEVICE_I2C
einsteingustavo 0:0dee8840a1c0 22
einsteingustavo 0:0dee8840a1c0 23 #include "i2c_api.h"
einsteingustavo 0:0dee8840a1c0 24
einsteingustavo 0:0dee8840a1c0 25 namespace mbed {
einsteingustavo 0:0dee8840a1c0 26
einsteingustavo 0:0dee8840a1c0 27 /** An I2C Master, used for communicating with I2C slave devices
einsteingustavo 0:0dee8840a1c0 28 *
einsteingustavo 0:0dee8840a1c0 29 * Example:
einsteingustavo 0:0dee8840a1c0 30 * @code
einsteingustavo 0:0dee8840a1c0 31 * // Read from I2C slave at address 0x62
einsteingustavo 0:0dee8840a1c0 32 *
einsteingustavo 0:0dee8840a1c0 33 * #include "mbed.h"
einsteingustavo 0:0dee8840a1c0 34 *
einsteingustavo 0:0dee8840a1c0 35 * I2C i2c(p28, p27);
einsteingustavo 0:0dee8840a1c0 36 *
einsteingustavo 0:0dee8840a1c0 37 * int main() {
einsteingustavo 0:0dee8840a1c0 38 * int address = 0x62;
einsteingustavo 0:0dee8840a1c0 39 * char data[2];
einsteingustavo 0:0dee8840a1c0 40 * i2c.read(address, data, 2);
einsteingustavo 0:0dee8840a1c0 41 * }
einsteingustavo 0:0dee8840a1c0 42 * @endcode
einsteingustavo 0:0dee8840a1c0 43 */
einsteingustavo 0:0dee8840a1c0 44 class I2C {
einsteingustavo 0:0dee8840a1c0 45
einsteingustavo 0:0dee8840a1c0 46 public:
einsteingustavo 0:0dee8840a1c0 47 enum RxStatus {
einsteingustavo 0:0dee8840a1c0 48 NoData,
einsteingustavo 0:0dee8840a1c0 49 MasterGeneralCall,
einsteingustavo 0:0dee8840a1c0 50 MasterWrite,
einsteingustavo 0:0dee8840a1c0 51 MasterRead
einsteingustavo 0:0dee8840a1c0 52 };
einsteingustavo 0:0dee8840a1c0 53
einsteingustavo 0:0dee8840a1c0 54 enum Acknowledge {
einsteingustavo 0:0dee8840a1c0 55 NoACK = 0,
einsteingustavo 0:0dee8840a1c0 56 ACK = 1
einsteingustavo 0:0dee8840a1c0 57 };
einsteingustavo 0:0dee8840a1c0 58
einsteingustavo 0:0dee8840a1c0 59 /** Create an I2C Master interface, connected to the specified pins
einsteingustavo 0:0dee8840a1c0 60 *
einsteingustavo 0:0dee8840a1c0 61 * @param sda I2C data line pin
einsteingustavo 0:0dee8840a1c0 62 * @param scl I2C clock line pin
einsteingustavo 0:0dee8840a1c0 63 */
einsteingustavo 0:0dee8840a1c0 64 I2C(PinName sda, PinName scl);
einsteingustavo 0:0dee8840a1c0 65
einsteingustavo 0:0dee8840a1c0 66 /** Set the frequency of the I2C interface
einsteingustavo 0:0dee8840a1c0 67 *
einsteingustavo 0:0dee8840a1c0 68 * @param hz The bus frequency in hertz
einsteingustavo 0:0dee8840a1c0 69 */
einsteingustavo 0:0dee8840a1c0 70 void frequency(int hz);
einsteingustavo 0:0dee8840a1c0 71
einsteingustavo 0:0dee8840a1c0 72 /** Read from an I2C slave
einsteingustavo 0:0dee8840a1c0 73 *
einsteingustavo 0:0dee8840a1c0 74 * Performs a complete read transaction. The bottom bit of
einsteingustavo 0:0dee8840a1c0 75 * the address is forced to 1 to indicate a read.
einsteingustavo 0:0dee8840a1c0 76 *
einsteingustavo 0:0dee8840a1c0 77 * @param address 8-bit I2C slave address [ addr | 1 ]
einsteingustavo 0:0dee8840a1c0 78 * @param data Pointer to the byte-array to read data in to
einsteingustavo 0:0dee8840a1c0 79 * @param length Number of bytes to read
einsteingustavo 0:0dee8840a1c0 80 * @param repeated Repeated start, true - don't send stop at end
einsteingustavo 0:0dee8840a1c0 81 *
einsteingustavo 0:0dee8840a1c0 82 * @returns
einsteingustavo 0:0dee8840a1c0 83 * 0 on success (ack),
einsteingustavo 0:0dee8840a1c0 84 * non-0 on failure (nack)
einsteingustavo 0:0dee8840a1c0 85 */
einsteingustavo 0:0dee8840a1c0 86 int read(int address, char *data, int length, bool repeated = false);
einsteingustavo 0:0dee8840a1c0 87
einsteingustavo 0:0dee8840a1c0 88 /** Read a single byte from the I2C bus
einsteingustavo 0:0dee8840a1c0 89 *
einsteingustavo 0:0dee8840a1c0 90 * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
einsteingustavo 0:0dee8840a1c0 91 *
einsteingustavo 0:0dee8840a1c0 92 * @returns
einsteingustavo 0:0dee8840a1c0 93 * the byte read
einsteingustavo 0:0dee8840a1c0 94 */
einsteingustavo 0:0dee8840a1c0 95 int read(int ack);
einsteingustavo 0:0dee8840a1c0 96
einsteingustavo 0:0dee8840a1c0 97 /** Write to an I2C slave
einsteingustavo 0:0dee8840a1c0 98 *
einsteingustavo 0:0dee8840a1c0 99 * Performs a complete write transaction. The bottom bit of
einsteingustavo 0:0dee8840a1c0 100 * the address is forced to 0 to indicate a write.
einsteingustavo 0:0dee8840a1c0 101 *
einsteingustavo 0:0dee8840a1c0 102 * @param address 8-bit I2C slave address [ addr | 0 ]
einsteingustavo 0:0dee8840a1c0 103 * @param data Pointer to the byte-array data to send
einsteingustavo 0:0dee8840a1c0 104 * @param length Number of bytes to send
einsteingustavo 0:0dee8840a1c0 105 * @param repeated Repeated start, true - do not send stop at end
einsteingustavo 0:0dee8840a1c0 106 *
einsteingustavo 0:0dee8840a1c0 107 * @returns
einsteingustavo 0:0dee8840a1c0 108 * 0 on success (ack),
einsteingustavo 0:0dee8840a1c0 109 * non-0 on failure (nack)
einsteingustavo 0:0dee8840a1c0 110 */
einsteingustavo 0:0dee8840a1c0 111 int write(int address, const char *data, int length, bool repeated = false);
einsteingustavo 0:0dee8840a1c0 112
einsteingustavo 0:0dee8840a1c0 113 /** Write single byte out on the I2C bus
einsteingustavo 0:0dee8840a1c0 114 *
einsteingustavo 0:0dee8840a1c0 115 * @param data data to write out on bus
einsteingustavo 0:0dee8840a1c0 116 *
einsteingustavo 0:0dee8840a1c0 117 * @returns
einsteingustavo 0:0dee8840a1c0 118 * '1' if an ACK was received,
einsteingustavo 0:0dee8840a1c0 119 * '0' otherwise
einsteingustavo 0:0dee8840a1c0 120 */
einsteingustavo 0:0dee8840a1c0 121 int write(int data);
einsteingustavo 0:0dee8840a1c0 122
einsteingustavo 0:0dee8840a1c0 123 /** Creates a start condition on the I2C bus
einsteingustavo 0:0dee8840a1c0 124 */
einsteingustavo 0:0dee8840a1c0 125
einsteingustavo 0:0dee8840a1c0 126 void start(void);
einsteingustavo 0:0dee8840a1c0 127
einsteingustavo 0:0dee8840a1c0 128 /** Creates a stop condition on the I2C bus
einsteingustavo 0:0dee8840a1c0 129 */
einsteingustavo 0:0dee8840a1c0 130 void stop(void);
einsteingustavo 0:0dee8840a1c0 131
einsteingustavo 0:0dee8840a1c0 132 protected:
einsteingustavo 0:0dee8840a1c0 133 void aquire();
einsteingustavo 0:0dee8840a1c0 134
einsteingustavo 0:0dee8840a1c0 135 i2c_t _i2c;
einsteingustavo 0:0dee8840a1c0 136 static I2C *_owner;
einsteingustavo 0:0dee8840a1c0 137 int _hz;
einsteingustavo 0:0dee8840a1c0 138 };
einsteingustavo 0:0dee8840a1c0 139
einsteingustavo 0:0dee8840a1c0 140 } // namespace mbed
einsteingustavo 0:0dee8840a1c0 141
einsteingustavo 0:0dee8840a1c0 142 #endif
einsteingustavo 0:0dee8840a1c0 143
einsteingustavo 0:0dee8840a1c0 144 #endif
einsteingustavo 0:0dee8840a1c0 145