mbed libraries for KL25Z

Dependents:   FRDM_RGBLED

Committer:
emilmont
Date:
Fri Oct 05 09:16:41 2012 +0000
Revision:
0:8024c367e29f
Child:
8:c14af7958ef5
First release of the mbed libraries for KL25Z

Who changed what in which revision?

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