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