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