This is my quadcopter prototype software, still in development!

Committer:
Anaesthetix
Date:
Tue Jul 23 14:01:42 2013 +0000
Revision:
1:ac68f0368a77
Parent:
0:978110f7f027
Other accelerometer added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Anaesthetix 0:978110f7f027 1 /* mbed Microcontroller Library - I2C
Anaesthetix 0:978110f7f027 2 * Copyright (c) 2007-2011 ARM Limited. All rights reserved.
Anaesthetix 0:978110f7f027 3 */
Anaesthetix 0:978110f7f027 4
Anaesthetix 0:978110f7f027 5 #ifndef MBED_I2C_H
Anaesthetix 0:978110f7f027 6 #define MBED_I2C_H
Anaesthetix 0:978110f7f027 7
Anaesthetix 0:978110f7f027 8 #include "device.h"
Anaesthetix 0:978110f7f027 9
Anaesthetix 0:978110f7f027 10 #if DEVICE_I2C
Anaesthetix 0:978110f7f027 11
Anaesthetix 0:978110f7f027 12 #include "platform.h"
Anaesthetix 0:978110f7f027 13 #include "PinNames.h"
Anaesthetix 0:978110f7f027 14 #include "PeripheralNames.h"
Anaesthetix 0:978110f7f027 15 #include "Base.h"
Anaesthetix 0:978110f7f027 16
Anaesthetix 0:978110f7f027 17 namespace mbed {
Anaesthetix 0:978110f7f027 18
Anaesthetix 0:978110f7f027 19 /* Class: I2C
Anaesthetix 0:978110f7f027 20 * An I2C Master, used for communicating with I2C slave devices
Anaesthetix 0:978110f7f027 21 *
Anaesthetix 0:978110f7f027 22 * Example:
Anaesthetix 0:978110f7f027 23 * > // Read from I2C slave at address 0x62
Anaesthetix 0:978110f7f027 24 * >
Anaesthetix 0:978110f7f027 25 * > #include "mbed.h"
Anaesthetix 0:978110f7f027 26 * >
Anaesthetix 0:978110f7f027 27 * > I2C i2c(p28, p27);
Anaesthetix 0:978110f7f027 28 * >
Anaesthetix 0:978110f7f027 29 * > int main() {
Anaesthetix 0:978110f7f027 30 * > int address = 0x62;
Anaesthetix 0:978110f7f027 31 * > char data[2];
Anaesthetix 0:978110f7f027 32 * > i2c.read(address, data, 2);
Anaesthetix 0:978110f7f027 33 * > }
Anaesthetix 0:978110f7f027 34 */
Anaesthetix 0:978110f7f027 35 class I2C : public Base {
Anaesthetix 0:978110f7f027 36
Anaesthetix 0:978110f7f027 37 public:
Anaesthetix 0:978110f7f027 38
Anaesthetix 0:978110f7f027 39 enum RxStatus {
Anaesthetix 0:978110f7f027 40 NoData
Anaesthetix 0:978110f7f027 41 , MasterGeneralCall
Anaesthetix 0:978110f7f027 42 , MasterWrite
Anaesthetix 0:978110f7f027 43 , MasterRead
Anaesthetix 0:978110f7f027 44 };
Anaesthetix 0:978110f7f027 45
Anaesthetix 0:978110f7f027 46 enum Acknowledge {
Anaesthetix 0:978110f7f027 47 NoACK = 0
Anaesthetix 0:978110f7f027 48 , ACK = 1
Anaesthetix 0:978110f7f027 49 };
Anaesthetix 0:978110f7f027 50
Anaesthetix 0:978110f7f027 51 /* Constructor: I2C
Anaesthetix 0:978110f7f027 52 * Create an I2C Master interface, connected to the specified pins
Anaesthetix 0:978110f7f027 53 *
Anaesthetix 0:978110f7f027 54 * Variables:
Anaesthetix 0:978110f7f027 55 * sda - I2C data line pin
Anaesthetix 0:978110f7f027 56 * scl - I2C clock line pin
Anaesthetix 0:978110f7f027 57 */
Anaesthetix 0:978110f7f027 58 I2C(PinName sda, PinName scl, const char *name = NULL);
Anaesthetix 0:978110f7f027 59
Anaesthetix 0:978110f7f027 60 /* Function: frequency
Anaesthetix 0:978110f7f027 61 * Set the frequency of the I2C interface
Anaesthetix 0:978110f7f027 62 *
Anaesthetix 0:978110f7f027 63 * Variables:
Anaesthetix 0:978110f7f027 64 * hz - The bus frequency in hertz
Anaesthetix 0:978110f7f027 65 */
Anaesthetix 0:978110f7f027 66 void frequency(int hz);
Anaesthetix 0:978110f7f027 67
Anaesthetix 0:978110f7f027 68 /* Function: read
Anaesthetix 0:978110f7f027 69 * Read from an I2C slave
Anaesthetix 0:978110f7f027 70 *
Anaesthetix 0:978110f7f027 71 * Performs a complete read transaction. The bottom bit of
Anaesthetix 0:978110f7f027 72 * the address is forced to 1 to indicate a read.
Anaesthetix 0:978110f7f027 73 *
Anaesthetix 0:978110f7f027 74 * Variables:
Anaesthetix 0:978110f7f027 75 * address - 8-bit I2C slave address [ addr | 1 ]
Anaesthetix 0:978110f7f027 76 * data - Pointer to the byte-array to read data in to
Anaesthetix 0:978110f7f027 77 * length - Number of bytes to read
Anaesthetix 0:978110f7f027 78 * repeated - Repeated start, true - don't send stop at end
Anaesthetix 0:978110f7f027 79 * returns - 0 on success (ack), or non-0 on failure (nack)
Anaesthetix 0:978110f7f027 80 */
Anaesthetix 0:978110f7f027 81 int read(int address, char *data, int length, bool repeated = false);
Anaesthetix 0:978110f7f027 82
Anaesthetix 0:978110f7f027 83 /* Function: read
Anaesthetix 0:978110f7f027 84 * Read a single byte from the I2C bus
Anaesthetix 0:978110f7f027 85 *
Anaesthetix 0:978110f7f027 86 * Variables:
Anaesthetix 0:978110f7f027 87 * ack - indicates if the byte is to be acknowledged (1 = acknowledge)
Anaesthetix 0:978110f7f027 88 * returns - the byte read
Anaesthetix 0:978110f7f027 89 */
Anaesthetix 0:978110f7f027 90 int read(int ack);
Anaesthetix 0:978110f7f027 91
Anaesthetix 0:978110f7f027 92 /* Function: write
Anaesthetix 0:978110f7f027 93 * Write to an I2C slave
Anaesthetix 0:978110f7f027 94 *
Anaesthetix 0:978110f7f027 95 * Performs a complete write transaction. The bottom bit of
Anaesthetix 0:978110f7f027 96 * the address is forced to 0 to indicate a write.
Anaesthetix 0:978110f7f027 97 *
Anaesthetix 0:978110f7f027 98 * Variables:
Anaesthetix 0:978110f7f027 99 * address - 8-bit I2C slave address [ addr | 0 ]
Anaesthetix 0:978110f7f027 100 * data - Pointer to the byte-array data to send
Anaesthetix 0:978110f7f027 101 * length - Number of bytes to send
Anaesthetix 0:978110f7f027 102 * repeated - Repeated start, true - do not send stop at end
Anaesthetix 0:978110f7f027 103 * returns - 0 on success (ack), or non-0 on failure (nack)
Anaesthetix 0:978110f7f027 104 */
Anaesthetix 0:978110f7f027 105 int write(int address, const char *data, int length, bool repeated = false);
Anaesthetix 0:978110f7f027 106
Anaesthetix 0:978110f7f027 107 /* Function: write
Anaesthetix 0:978110f7f027 108 * Write single byte out on the I2C bus
Anaesthetix 0:978110f7f027 109 *
Anaesthetix 0:978110f7f027 110 * Variables:
Anaesthetix 0:978110f7f027 111 * data - data to write out on bus
Anaesthetix 0:978110f7f027 112 * returns - a '1' if an ACK was received, a '0' otherwise
Anaesthetix 0:978110f7f027 113 */
Anaesthetix 0:978110f7f027 114 int write(int data);
Anaesthetix 0:978110f7f027 115
Anaesthetix 0:978110f7f027 116 /* Function: start
Anaesthetix 0:978110f7f027 117 * Creates a start condition on the I2C bus
Anaesthetix 0:978110f7f027 118 */
Anaesthetix 0:978110f7f027 119
Anaesthetix 0:978110f7f027 120 void start(void);
Anaesthetix 0:978110f7f027 121
Anaesthetix 0:978110f7f027 122 /* Function: stop
Anaesthetix 0:978110f7f027 123 * Creates a stop condition on the I2C bus
Anaesthetix 0:978110f7f027 124 */
Anaesthetix 0:978110f7f027 125 void stop(void);
Anaesthetix 0:978110f7f027 126
Anaesthetix 0:978110f7f027 127 protected:
Anaesthetix 0:978110f7f027 128
Anaesthetix 0:978110f7f027 129 void aquire();
Anaesthetix 0:978110f7f027 130
Anaesthetix 0:978110f7f027 131 I2CName _i2c;
Anaesthetix 0:978110f7f027 132 static I2C *_owner;
Anaesthetix 0:978110f7f027 133 int _hz;
Anaesthetix 0:978110f7f027 134
Anaesthetix 0:978110f7f027 135 };
Anaesthetix 0:978110f7f027 136
Anaesthetix 0:978110f7f027 137 } // namespace mbed
Anaesthetix 0:978110f7f027 138
Anaesthetix 0:978110f7f027 139 #endif
Anaesthetix 0:978110f7f027 140
Anaesthetix 0:978110f7f027 141 #endif
Anaesthetix 0:978110f7f027 142