mbed libraries for KL25Z

Dependents:   FRDM_RGBLED

Committer:
emilmont
Date:
Fri Nov 09 11:33:53 2012 +0000
Revision:
8:c14af7958ef5
Parent:
0:8024c367e29f
Child:
9:663789d7729f
SPI driver; ADC driver; DAC driver; microlib support; general bugfixing

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