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