Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed-rtos EthernetInterface FatFileSystemCpp MCP23S17 SDFileSystem mbed
Fork of HTTPServerHelloWorld by
mbed/I2CSlave.h@3:5dc0023e6284, 2014-01-31 (annotated)
- Committer:
- wyunreal
- Date:
- Fri Jan 31 23:19:28 2014 +0000
- Revision:
- 3:5dc0023e6284
First approach of EthernetService class
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| wyunreal | 3:5dc0023e6284 | 1 | /* mbed Microcontroller Library - I2CSlave |
| wyunreal | 3:5dc0023e6284 | 2 | * Copyright (c) 2007-2010 ARM Limited. All rights reserved. |
| wyunreal | 3:5dc0023e6284 | 3 | * jward |
| wyunreal | 3:5dc0023e6284 | 4 | */ |
| wyunreal | 3:5dc0023e6284 | 5 | |
| wyunreal | 3:5dc0023e6284 | 6 | #ifndef MBED_I2C_SLAVE_H |
| wyunreal | 3:5dc0023e6284 | 7 | #define MBED_I2C_SLAVE_H |
| wyunreal | 3:5dc0023e6284 | 8 | |
| wyunreal | 3:5dc0023e6284 | 9 | #include "platform.h" |
| wyunreal | 3:5dc0023e6284 | 10 | #include "PinNames.h" |
| wyunreal | 3:5dc0023e6284 | 11 | #include "PeripheralNames.h" |
| wyunreal | 3:5dc0023e6284 | 12 | #include "Base.h" |
| wyunreal | 3:5dc0023e6284 | 13 | |
| wyunreal | 3:5dc0023e6284 | 14 | namespace mbed { |
| wyunreal | 3:5dc0023e6284 | 15 | |
| wyunreal | 3:5dc0023e6284 | 16 | /* Class: I2CSlave |
| wyunreal | 3:5dc0023e6284 | 17 | * An I2C Slave, used for communicating with an I2C Master device |
| wyunreal | 3:5dc0023e6284 | 18 | * |
| wyunreal | 3:5dc0023e6284 | 19 | * Example: |
| wyunreal | 3:5dc0023e6284 | 20 | * > // Simple I2C responder |
| wyunreal | 3:5dc0023e6284 | 21 | * > #include <mbed.h> |
| wyunreal | 3:5dc0023e6284 | 22 | * > |
| wyunreal | 3:5dc0023e6284 | 23 | * > I2CSlave slave(p9, p10); |
| wyunreal | 3:5dc0023e6284 | 24 | * > |
| wyunreal | 3:5dc0023e6284 | 25 | * > int main() { |
| wyunreal | 3:5dc0023e6284 | 26 | * > char buf[10]; |
| wyunreal | 3:5dc0023e6284 | 27 | * > char msg[] = "Slave!"; |
| wyunreal | 3:5dc0023e6284 | 28 | * > |
| wyunreal | 3:5dc0023e6284 | 29 | * > slave.address(0xA0); |
| wyunreal | 3:5dc0023e6284 | 30 | * > while (1) { |
| wyunreal | 3:5dc0023e6284 | 31 | * > int i = slave.receive(); |
| wyunreal | 3:5dc0023e6284 | 32 | * > switch (i) { |
| wyunreal | 3:5dc0023e6284 | 33 | * > case I2CSlave::ReadAddressed: |
| wyunreal | 3:5dc0023e6284 | 34 | * > slave.write(msg, strlen(msg) + 1); // Includes null char |
| wyunreal | 3:5dc0023e6284 | 35 | * > break; |
| wyunreal | 3:5dc0023e6284 | 36 | * > case I2CSlave::WriteGeneral: |
| wyunreal | 3:5dc0023e6284 | 37 | * > slave.read(buf, 10); |
| wyunreal | 3:5dc0023e6284 | 38 | * > printf("Read G: %s\n", buf); |
| wyunreal | 3:5dc0023e6284 | 39 | * > break; |
| wyunreal | 3:5dc0023e6284 | 40 | * > case I2CSlave::WriteAddressed: |
| wyunreal | 3:5dc0023e6284 | 41 | * > slave.read(buf, 10); |
| wyunreal | 3:5dc0023e6284 | 42 | * > printf("Read A: %s\n", buf); |
| wyunreal | 3:5dc0023e6284 | 43 | * > break; |
| wyunreal | 3:5dc0023e6284 | 44 | * > } |
| wyunreal | 3:5dc0023e6284 | 45 | * > for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer |
| wyunreal | 3:5dc0023e6284 | 46 | * > } |
| wyunreal | 3:5dc0023e6284 | 47 | * > } |
| wyunreal | 3:5dc0023e6284 | 48 | * > |
| wyunreal | 3:5dc0023e6284 | 49 | */ |
| wyunreal | 3:5dc0023e6284 | 50 | class I2CSlave : public Base { |
| wyunreal | 3:5dc0023e6284 | 51 | |
| wyunreal | 3:5dc0023e6284 | 52 | public: |
| wyunreal | 3:5dc0023e6284 | 53 | |
| wyunreal | 3:5dc0023e6284 | 54 | enum RxStatus { |
| wyunreal | 3:5dc0023e6284 | 55 | NoData = 0 |
| wyunreal | 3:5dc0023e6284 | 56 | , ReadAddressed = 1 |
| wyunreal | 3:5dc0023e6284 | 57 | , WriteGeneral = 2 |
| wyunreal | 3:5dc0023e6284 | 58 | , WriteAddressed = 3 |
| wyunreal | 3:5dc0023e6284 | 59 | }; |
| wyunreal | 3:5dc0023e6284 | 60 | |
| wyunreal | 3:5dc0023e6284 | 61 | /* Constructor: I2CSlave |
| wyunreal | 3:5dc0023e6284 | 62 | * Create an I2C Slave interface, connected to the specified pins. |
| wyunreal | 3:5dc0023e6284 | 63 | * |
| wyunreal | 3:5dc0023e6284 | 64 | * Variables: |
| wyunreal | 3:5dc0023e6284 | 65 | * sda - I2C data line pin |
| wyunreal | 3:5dc0023e6284 | 66 | * scl - I2C clock line pin |
| wyunreal | 3:5dc0023e6284 | 67 | */ |
| wyunreal | 3:5dc0023e6284 | 68 | I2CSlave(PinName sda, PinName scl, const char *name = NULL); |
| wyunreal | 3:5dc0023e6284 | 69 | |
| wyunreal | 3:5dc0023e6284 | 70 | /* Function: frequency |
| wyunreal | 3:5dc0023e6284 | 71 | * Set the frequency of the I2C interface |
| wyunreal | 3:5dc0023e6284 | 72 | * |
| wyunreal | 3:5dc0023e6284 | 73 | * Variables: |
| wyunreal | 3:5dc0023e6284 | 74 | * hz - The bus frequency in hertz |
| wyunreal | 3:5dc0023e6284 | 75 | */ |
| wyunreal | 3:5dc0023e6284 | 76 | void frequency(int hz); |
| wyunreal | 3:5dc0023e6284 | 77 | |
| wyunreal | 3:5dc0023e6284 | 78 | /* Function: receive |
| wyunreal | 3:5dc0023e6284 | 79 | * Checks to see if this I2C Slave has been addressed. |
| wyunreal | 3:5dc0023e6284 | 80 | * |
| wyunreal | 3:5dc0023e6284 | 81 | * Variables: |
| wyunreal | 3:5dc0023e6284 | 82 | * returns - a status indicating if the device has been addressed, and how |
| wyunreal | 3:5dc0023e6284 | 83 | * > NoData - the slave has not been addressed |
| wyunreal | 3:5dc0023e6284 | 84 | * > ReadAddressed - the master has requested a read from this slave |
| wyunreal | 3:5dc0023e6284 | 85 | * > WriteAddressed - the master is writing to this slave |
| wyunreal | 3:5dc0023e6284 | 86 | * > WriteGeneral - the master is writing to all slave |
| wyunreal | 3:5dc0023e6284 | 87 | */ |
| wyunreal | 3:5dc0023e6284 | 88 | int receive(void); |
| wyunreal | 3:5dc0023e6284 | 89 | |
| wyunreal | 3:5dc0023e6284 | 90 | /* Function: read |
| wyunreal | 3:5dc0023e6284 | 91 | * Read from an I2C master. |
| wyunreal | 3:5dc0023e6284 | 92 | * |
| wyunreal | 3:5dc0023e6284 | 93 | * Variables: |
| wyunreal | 3:5dc0023e6284 | 94 | * data - pointer to the byte array to read data in to |
| wyunreal | 3:5dc0023e6284 | 95 | * length - maximum number of bytes to read |
| wyunreal | 3:5dc0023e6284 | 96 | * returns - 0 on success, non-0 otherwise |
| wyunreal | 3:5dc0023e6284 | 97 | */ |
| wyunreal | 3:5dc0023e6284 | 98 | int read(char *data, int length); |
| wyunreal | 3:5dc0023e6284 | 99 | |
| wyunreal | 3:5dc0023e6284 | 100 | /* Function: read |
| wyunreal | 3:5dc0023e6284 | 101 | * Read a single byte from an I2C master. |
| wyunreal | 3:5dc0023e6284 | 102 | * |
| wyunreal | 3:5dc0023e6284 | 103 | * Variables: |
| wyunreal | 3:5dc0023e6284 | 104 | * returns - the byte read |
| wyunreal | 3:5dc0023e6284 | 105 | */ |
| wyunreal | 3:5dc0023e6284 | 106 | int read(void); |
| wyunreal | 3:5dc0023e6284 | 107 | |
| wyunreal | 3:5dc0023e6284 | 108 | /* Function: write |
| wyunreal | 3:5dc0023e6284 | 109 | * Write to an I2C master. |
| wyunreal | 3:5dc0023e6284 | 110 | * |
| wyunreal | 3:5dc0023e6284 | 111 | * Variables: |
| wyunreal | 3:5dc0023e6284 | 112 | * data - pointer to the byte array to be transmitted |
| wyunreal | 3:5dc0023e6284 | 113 | * length - the number of bytes to transmite |
| wyunreal | 3:5dc0023e6284 | 114 | * returns - a 0 on success, non-0 otherwise |
| wyunreal | 3:5dc0023e6284 | 115 | */ |
| wyunreal | 3:5dc0023e6284 | 116 | int write(const char *data, int length); |
| wyunreal | 3:5dc0023e6284 | 117 | |
| wyunreal | 3:5dc0023e6284 | 118 | /* Function: write |
| wyunreal | 3:5dc0023e6284 | 119 | * Write a single byte to an I2C master. |
| wyunreal | 3:5dc0023e6284 | 120 | * |
| wyunreal | 3:5dc0023e6284 | 121 | * Variables |
| wyunreal | 3:5dc0023e6284 | 122 | * data - the byte to write |
| wyunreal | 3:5dc0023e6284 | 123 | * returns - a '1' if an ACK was received, a '0' otherwise |
| wyunreal | 3:5dc0023e6284 | 124 | */ |
| wyunreal | 3:5dc0023e6284 | 125 | int write(int data); |
| wyunreal | 3:5dc0023e6284 | 126 | |
| wyunreal | 3:5dc0023e6284 | 127 | /* Function: address |
| wyunreal | 3:5dc0023e6284 | 128 | * Sets the I2C slave address. |
| wyunreal | 3:5dc0023e6284 | 129 | * |
| wyunreal | 3:5dc0023e6284 | 130 | * Variables |
| wyunreal | 3:5dc0023e6284 | 131 | * address - the address to set for the slave (ignoring the least |
| wyunreal | 3:5dc0023e6284 | 132 | * signifcant bit). If set to 0, the slave will only respond to the |
| wyunreal | 3:5dc0023e6284 | 133 | * general call address. |
| wyunreal | 3:5dc0023e6284 | 134 | */ |
| wyunreal | 3:5dc0023e6284 | 135 | void address(int address); |
| wyunreal | 3:5dc0023e6284 | 136 | |
| wyunreal | 3:5dc0023e6284 | 137 | /* Function: stop |
| wyunreal | 3:5dc0023e6284 | 138 | * Reset the I2C slave back into the known ready receiving state. |
| wyunreal | 3:5dc0023e6284 | 139 | */ |
| wyunreal | 3:5dc0023e6284 | 140 | void stop(void); |
| wyunreal | 3:5dc0023e6284 | 141 | |
| wyunreal | 3:5dc0023e6284 | 142 | protected: |
| wyunreal | 3:5dc0023e6284 | 143 | |
| wyunreal | 3:5dc0023e6284 | 144 | I2CName _i2c; |
| wyunreal | 3:5dc0023e6284 | 145 | }; |
| wyunreal | 3:5dc0023e6284 | 146 | |
| wyunreal | 3:5dc0023e6284 | 147 | } // namespace mbed |
| wyunreal | 3:5dc0023e6284 | 148 | |
| wyunreal | 3:5dc0023e6284 | 149 | #endif |
