]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/api/I2CSlave.h
remove experimental return, cleanup slash_question key
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / api / I2CSlave.h
1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #ifndef MBED_I2C_SLAVE_H
17 #define MBED_I2C_SLAVE_H
18
19 #include "platform.h"
20
21 #if DEVICE_I2CSLAVE
22
23 #include "i2c_api.h"
24
25 namespace mbed {
26
27 /** An I2C Slave, used for communicating with an I2C Master device
28 *
29 * Example:
30 * @code
31 * // Simple I2C responder
32 * #include <mbed.h>
33 *
34 * I2CSlave slave(p9, p10);
35 *
36 * int main() {
37 * char buf[10];
38 * char msg[] = "Slave!";
39 *
40 * slave.address(0xA0);
41 * while (1) {
42 * int i = slave.receive();
43 * switch (i) {
44 * case I2CSlave::ReadAddressed:
45 * slave.write(msg, strlen(msg) + 1); // Includes null char
46 * break;
47 * case I2CSlave::WriteGeneral:
48 * slave.read(buf, 10);
49 * printf("Read G: %s\n", buf);
50 * break;
51 * case I2CSlave::WriteAddressed:
52 * slave.read(buf, 10);
53 * printf("Read A: %s\n", buf);
54 * break;
55 * }
56 * for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer
57 * }
58 * }
59 * @endcode
60 */
61 class I2CSlave {
62
63 public:
64 enum RxStatus {
65 NoData = 0,
66 ReadAddressed = 1,
67 WriteGeneral = 2,
68 WriteAddressed = 3
69 };
70
71 /** Create an I2C Slave interface, connected to the specified pins.
72 *
73 * @param sda I2C data line pin
74 * @param scl I2C clock line pin
75 */
76 I2CSlave(PinName sda, PinName scl);
77
78 /** Set the frequency of the I2C interface
79 *
80 * @param hz The bus frequency in hertz
81 */
82 void frequency(int hz);
83
84 /** Checks to see if this I2C Slave has been addressed.
85 *
86 * @returns
87 * A status indicating if the device has been addressed, and how
88 * - NoData - the slave has not been addressed
89 * - ReadAddressed - the master has requested a read from this slave
90 * - WriteAddressed - the master is writing to this slave
91 * - WriteGeneral - the master is writing to all slave
92 */
93 int receive(void);
94
95 /** Read from an I2C master.
96 *
97 * @param data pointer to the byte array to read data in to
98 * @param length maximum number of bytes to read
99 *
100 * @returns
101 * 0 on success,
102 * non-0 otherwise
103 */
104 int read(char *data, int length);
105
106 /** Read a single byte from an I2C master.
107 *
108 * @returns
109 * the byte read
110 */
111 int read(void);
112
113 /** Write to an I2C master.
114 *
115 * @param data pointer to the byte array to be transmitted
116 * @param length the number of bytes to transmite
117 *
118 * @returns
119 * 0 on success,
120 * non-0 otherwise
121 */
122 int write(const char *data, int length);
123
124 /** Write a single byte to an I2C master.
125 *
126 * @data the byte to write
127 *
128 * @returns
129 * '1' if an ACK was received,
130 * '0' otherwise
131 */
132 int write(int data);
133
134 /** Sets the I2C slave address.
135 *
136 * @param address The address to set for the slave (ignoring the least
137 * signifcant bit). If set to 0, the slave will only respond to the
138 * general call address.
139 */
140 void address(int address);
141
142 /** Reset the I2C slave back into the known ready receiving state.
143 */
144 void stop(void);
145
146 protected:
147 i2c_t _i2c;
148 };
149
150 } // namespace mbed
151
152 #endif
153
154 #endif
Imprint / Impressum