MAA version 0.2.0 moves to a standard C API
[contrib/mraa.git] / api / i2c.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 <stdlib.h>
22 #include <stdio.h>
23 #include <fcntl.h>
24 #include "gpio.h"
25
26 typedef struct {
27     int hz;
28     int fh;
29     int addr;
30     gpio_t gpio;
31 } i2c_t;
32
33 int maa_i2c_init(i2c_t* dev);
34
35 /** Set the frequency of the I2C interface
36  *
37  *  @param hz The bus frequency in hertz
38  */
39 void maa_i2c_frequency(i2c_t* dev, int hz);
40
41 /** Checks to see if this I2C Slave has been addressed.
42  *
43  *  @returns
44  *  A status indicating if the device has been addressed, and how
45  *  - NoData            - the slave has not been addressed
46  *  - ReadAddressed     - the master has requested a read from this slave
47  *  - WriteAddressed    - the master is writing to this slave
48  *  - WriteGeneral      - the master is writing to all slave
49  */
50 int maa_i2c_receive(i2c_t* dev);
51
52 /** Read from an I2C master.
53  *
54  *  @param data pointer to the byte array to read data in to
55  *  @param length maximum number of bytes to read
56  *
57  *  @returns
58  *       0 on success,
59  *   non-0 otherwise
60  */
61 int maa_i2c_read(i2c_t* dev, char *data, int length);
62
63 /** Read a single byte from an I2C master.
64  *
65  *  @returns
66  *    the byte read
67  */
68 int maa_i2c_read_byte(i2c_t* dev);
69
70 /** Write to an I2C master.
71  *
72  *  @param data pointer to the byte array to be transmitted
73  *  @param length the number of bytes to transmite
74  *
75  *  @returns
76  *       0 on success,
77  *   non-0 otherwise
78  */
79 int maa_i2c_write(i2c_t* dev, const char *data, int length);
80
81 /** Write a single byte to an I2C master.
82  *
83  *  @data the byte to write
84  *
85  *  @returns
86  *    '1' if an ACK was received,
87  *    '0' otherwise
88  */
89 int maa_i2c_write_byte(i2c_t* dev, int data);
90
91 /** Sets the I2C slave address.
92  *
93  *  @param address The address to set for the slave (ignoring the least
94  *  signifcant bit). If set to 0, the slave will only respond to the
95  *  general call address.
96  */
97 void maa_i2c_address(i2c_t* dev, int address);
98
99 /** De-inits an i2c_t device
100  */
101 void maa_i2c_stop(i2c_t* dev);