Programme d'utilisation des AX12 avec rajout de l'MX12
Fork of test_carteAToutFaire_PR by
mbed/I2CSlave.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 - I2CSlave |
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_SLAVE_H |
ClementBreteau | 1:f3f702086a30 | 6 | #define MBED_I2C_SLAVE_H |
ClementBreteau | 1:f3f702086a30 | 7 | |
ClementBreteau | 1:f3f702086a30 | 8 | #include "device.h" |
ClementBreteau | 1:f3f702086a30 | 9 | |
ClementBreteau | 1:f3f702086a30 | 10 | #if DEVICE_I2CSLAVE |
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: I2CSlave |
ClementBreteau | 1:f3f702086a30 | 20 | * An I2C Slave, used for communicating with an I2C Master device |
ClementBreteau | 1:f3f702086a30 | 21 | * |
ClementBreteau | 1:f3f702086a30 | 22 | * Example: |
ClementBreteau | 1:f3f702086a30 | 23 | * > // Simple I2C responder |
ClementBreteau | 1:f3f702086a30 | 24 | * > #include <mbed.h> |
ClementBreteau | 1:f3f702086a30 | 25 | * > |
ClementBreteau | 1:f3f702086a30 | 26 | * > I2CSlave slave(p9, p10); |
ClementBreteau | 1:f3f702086a30 | 27 | * > |
ClementBreteau | 1:f3f702086a30 | 28 | * > int main() { |
ClementBreteau | 1:f3f702086a30 | 29 | * > char buf[10]; |
ClementBreteau | 1:f3f702086a30 | 30 | * > char msg[] = "Slave!"; |
ClementBreteau | 1:f3f702086a30 | 31 | * > |
ClementBreteau | 1:f3f702086a30 | 32 | * > slave.address(0xA0); |
ClementBreteau | 1:f3f702086a30 | 33 | * > while (1) { |
ClementBreteau | 1:f3f702086a30 | 34 | * > int i = slave.receive(); |
ClementBreteau | 1:f3f702086a30 | 35 | * > switch (i) { |
ClementBreteau | 1:f3f702086a30 | 36 | * > case I2CSlave::ReadAddressed: |
ClementBreteau | 1:f3f702086a30 | 37 | * > slave.write(msg, strlen(msg) + 1); // Includes null char |
ClementBreteau | 1:f3f702086a30 | 38 | * > break; |
ClementBreteau | 1:f3f702086a30 | 39 | * > case I2CSlave::WriteGeneral: |
ClementBreteau | 1:f3f702086a30 | 40 | * > slave.read(buf, 10); |
ClementBreteau | 1:f3f702086a30 | 41 | * > printf("Read G: %s\n", buf); |
ClementBreteau | 1:f3f702086a30 | 42 | * > break; |
ClementBreteau | 1:f3f702086a30 | 43 | * > case I2CSlave::WriteAddressed: |
ClementBreteau | 1:f3f702086a30 | 44 | * > slave.read(buf, 10); |
ClementBreteau | 1:f3f702086a30 | 45 | * > printf("Read A: %s\n", buf); |
ClementBreteau | 1:f3f702086a30 | 46 | * > break; |
ClementBreteau | 1:f3f702086a30 | 47 | * > } |
ClementBreteau | 1:f3f702086a30 | 48 | * > for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer |
ClementBreteau | 1:f3f702086a30 | 49 | * > } |
ClementBreteau | 1:f3f702086a30 | 50 | * > } |
ClementBreteau | 1:f3f702086a30 | 51 | * > |
ClementBreteau | 1:f3f702086a30 | 52 | */ |
ClementBreteau | 1:f3f702086a30 | 53 | class I2CSlave : public Base { |
ClementBreteau | 1:f3f702086a30 | 54 | |
ClementBreteau | 1:f3f702086a30 | 55 | public: |
ClementBreteau | 1:f3f702086a30 | 56 | |
ClementBreteau | 1:f3f702086a30 | 57 | enum RxStatus { |
ClementBreteau | 1:f3f702086a30 | 58 | NoData = 0 |
ClementBreteau | 1:f3f702086a30 | 59 | , ReadAddressed = 1 |
ClementBreteau | 1:f3f702086a30 | 60 | , WriteGeneral = 2 |
ClementBreteau | 1:f3f702086a30 | 61 | , WriteAddressed = 3 |
ClementBreteau | 1:f3f702086a30 | 62 | }; |
ClementBreteau | 1:f3f702086a30 | 63 | |
ClementBreteau | 1:f3f702086a30 | 64 | /* Constructor: I2CSlave |
ClementBreteau | 1:f3f702086a30 | 65 | * Create an I2C Slave interface, connected to the specified pins. |
ClementBreteau | 1:f3f702086a30 | 66 | * |
ClementBreteau | 1:f3f702086a30 | 67 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 68 | * sda - I2C data line pin |
ClementBreteau | 1:f3f702086a30 | 69 | * scl - I2C clock line pin |
ClementBreteau | 1:f3f702086a30 | 70 | */ |
ClementBreteau | 1:f3f702086a30 | 71 | I2CSlave(PinName sda, PinName scl, const char *name = NULL); |
ClementBreteau | 1:f3f702086a30 | 72 | |
ClementBreteau | 1:f3f702086a30 | 73 | /* Function: frequency |
ClementBreteau | 1:f3f702086a30 | 74 | * Set the frequency of the I2C interface |
ClementBreteau | 1:f3f702086a30 | 75 | * |
ClementBreteau | 1:f3f702086a30 | 76 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 77 | * hz - The bus frequency in hertz |
ClementBreteau | 1:f3f702086a30 | 78 | */ |
ClementBreteau | 1:f3f702086a30 | 79 | void frequency(int hz); |
ClementBreteau | 1:f3f702086a30 | 80 | |
ClementBreteau | 1:f3f702086a30 | 81 | /* Function: receive |
ClementBreteau | 1:f3f702086a30 | 82 | * Checks to see if this I2C Slave has been addressed. |
ClementBreteau | 1:f3f702086a30 | 83 | * |
ClementBreteau | 1:f3f702086a30 | 84 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 85 | * returns - a status indicating if the device has been addressed, and how |
ClementBreteau | 1:f3f702086a30 | 86 | * > NoData - the slave has not been addressed |
ClementBreteau | 1:f3f702086a30 | 87 | * > ReadAddressed - the master has requested a read from this slave |
ClementBreteau | 1:f3f702086a30 | 88 | * > WriteAddressed - the master is writing to this slave |
ClementBreteau | 1:f3f702086a30 | 89 | * > WriteGeneral - the master is writing to all slave |
ClementBreteau | 1:f3f702086a30 | 90 | */ |
ClementBreteau | 1:f3f702086a30 | 91 | int receive(void); |
ClementBreteau | 1:f3f702086a30 | 92 | |
ClementBreteau | 1:f3f702086a30 | 93 | /* Function: read |
ClementBreteau | 1:f3f702086a30 | 94 | * Read from an I2C master. |
ClementBreteau | 1:f3f702086a30 | 95 | * |
ClementBreteau | 1:f3f702086a30 | 96 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 97 | * data - pointer to the byte array to read data in to |
ClementBreteau | 1:f3f702086a30 | 98 | * length - maximum number of bytes to read |
ClementBreteau | 1:f3f702086a30 | 99 | * returns - 0 on success, non-0 otherwise |
ClementBreteau | 1:f3f702086a30 | 100 | */ |
ClementBreteau | 1:f3f702086a30 | 101 | int read(char *data, int length); |
ClementBreteau | 1:f3f702086a30 | 102 | |
ClementBreteau | 1:f3f702086a30 | 103 | /* Function: read |
ClementBreteau | 1:f3f702086a30 | 104 | * Read a single byte from an I2C master. |
ClementBreteau | 1:f3f702086a30 | 105 | * |
ClementBreteau | 1:f3f702086a30 | 106 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 107 | * returns - the byte read |
ClementBreteau | 1:f3f702086a30 | 108 | */ |
ClementBreteau | 1:f3f702086a30 | 109 | int read(void); |
ClementBreteau | 1:f3f702086a30 | 110 | |
ClementBreteau | 1:f3f702086a30 | 111 | /* Function: write |
ClementBreteau | 1:f3f702086a30 | 112 | * Write to an I2C master. |
ClementBreteau | 1:f3f702086a30 | 113 | * |
ClementBreteau | 1:f3f702086a30 | 114 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 115 | * data - pointer to the byte array to be transmitted |
ClementBreteau | 1:f3f702086a30 | 116 | * length - the number of bytes to transmite |
ClementBreteau | 1:f3f702086a30 | 117 | * returns - a 0 on success, non-0 otherwise |
ClementBreteau | 1:f3f702086a30 | 118 | */ |
ClementBreteau | 1:f3f702086a30 | 119 | int write(const char *data, int length); |
ClementBreteau | 1:f3f702086a30 | 120 | |
ClementBreteau | 1:f3f702086a30 | 121 | /* Function: write |
ClementBreteau | 1:f3f702086a30 | 122 | * Write a single byte to an I2C master. |
ClementBreteau | 1:f3f702086a30 | 123 | * |
ClementBreteau | 1:f3f702086a30 | 124 | * Variables |
ClementBreteau | 1:f3f702086a30 | 125 | * data - the byte to write |
ClementBreteau | 1:f3f702086a30 | 126 | * returns - a '1' if an ACK was received, a '0' otherwise |
ClementBreteau | 1:f3f702086a30 | 127 | */ |
ClementBreteau | 1:f3f702086a30 | 128 | int write(int data); |
ClementBreteau | 1:f3f702086a30 | 129 | |
ClementBreteau | 1:f3f702086a30 | 130 | /* Function: address |
ClementBreteau | 1:f3f702086a30 | 131 | * Sets the I2C slave address. |
ClementBreteau | 1:f3f702086a30 | 132 | * |
ClementBreteau | 1:f3f702086a30 | 133 | * Variables |
ClementBreteau | 1:f3f702086a30 | 134 | * address - the address to set for the slave (ignoring the least |
ClementBreteau | 1:f3f702086a30 | 135 | * signifcant bit). If set to 0, the slave will only respond to the |
ClementBreteau | 1:f3f702086a30 | 136 | * general call address. |
ClementBreteau | 1:f3f702086a30 | 137 | */ |
ClementBreteau | 1:f3f702086a30 | 138 | void address(int address); |
ClementBreteau | 1:f3f702086a30 | 139 | |
ClementBreteau | 1:f3f702086a30 | 140 | /* Function: stop |
ClementBreteau | 1:f3f702086a30 | 141 | * Reset the I2C slave back into the known ready receiving state. |
ClementBreteau | 1:f3f702086a30 | 142 | */ |
ClementBreteau | 1:f3f702086a30 | 143 | void stop(void); |
ClementBreteau | 1:f3f702086a30 | 144 | |
ClementBreteau | 1:f3f702086a30 | 145 | protected: |
ClementBreteau | 1:f3f702086a30 | 146 | |
ClementBreteau | 1:f3f702086a30 | 147 | I2CName _i2c; |
ClementBreteau | 1:f3f702086a30 | 148 | }; |
ClementBreteau | 1:f3f702086a30 | 149 | |
ClementBreteau | 1:f3f702086a30 | 150 | } // namespace mbed |
ClementBreteau | 1:f3f702086a30 | 151 | |
ClementBreteau | 1:f3f702086a30 | 152 | #endif |
ClementBreteau | 1:f3f702086a30 | 153 | |
ClementBreteau | 1:f3f702086a30 | 154 | #endif |
ClementBreteau | 1:f3f702086a30 | 155 |