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