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