svn / mbed / trunk / I2CSlave.h

Revision 29, 3.8 kB (checked in by emilmont, 6 months ago)

New Libraries 11.11

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