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