docs: Add docs/ folder with in depth topics as well as DoxygenLayout.xml file
[contrib/mraa.git] / api / i2c.h
1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #pragma once
26
27 /** @file
28  *
29  * @brief Inter-Integrated Circuit
30  *
31  * This file defines the i2c/Iic interface for libmaa. A context represents a
32  * bus and that bus may contain multiple addresses or i2c slaves. It is
33  * considered best practice to make sure the address is correct before doing
34  * any calls on i2c, in case another application or even thread changed the
35  * addres on that bus. Multiple instances of the same bus can exist.
36  *
37  * @snippet i2c_HMC5883L.c Interesting
38  */
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <fcntl.h>
47 #include <stdint.h>
48
49 #include "maa.h"
50 #include "gpio.h"
51
52 /**
53  * Opaque pointer definition to the internal struct _i2c
54  */
55 typedef struct _i2c* maa_i2c_context;
56
57 /** Initialise i2c context, using board defintions
58  *
59  * @param bus i2c bus to use
60  * @return maa_i2c_context i2c context ready for other calls.
61  */
62 maa_i2c_context maa_i2c_init(int bus);
63
64 /** Initialise i2c context, passing in spi bus to use.
65  *
66  * @param bus The i2c bus to use i.e. /dev/i2c-2 would be "2"
67  * @return maa_i2c_context i2c context ready for other calls.
68  */
69 maa_i2c_context maa_i2c_init_raw(unsigned int bus);
70
71 /** Sets the frequency of the i2c context
72  *
73  *  @param dev the i2c context
74  *  @param hz The bus frequency in hertz
75  *
76  *  @return maa_result_t the maa result.
77  */
78 maa_result_t maa_i2c_frequency(maa_i2c_context dev, int hz);
79
80 /** Read from an i2c context
81  *
82  *  @param dev the i2c context
83  *  @param data pointer to the byte array to read data in to
84  *  @param length max number of bytes to read
85  *
86  *  @return maa_result_t the maa result.
87  */
88 maa_result_t maa_i2c_read(maa_i2c_context dev, uint8_t *data, int length);
89
90 /** Read a single byte from the i2c context
91  *
92  *  @param dev the i2c context
93  *
94  *  @return byte the result of the read or -1 if failed.
95  */
96 uint8_t maa_i2c_read_byte(maa_i2c_context dev);
97
98 /** Write to an i2c context
99  *
100  *  @param dev the i2c context
101  *  @param data pointer to the byte array to be written
102  *  @param length the number of bytes to transmit
103  *
104  *  @return maa_result_t the maa result.
105  */
106 maa_result_t maa_i2c_write(maa_i2c_context dev, const uint8_t *data, int length);
107
108 /** Write a single byte to an i2c context
109  *
110  *  @param dev the i2c context
111  *  @data the byte to write
112  *
113  *  @return maa_result_t the maa result.
114  */
115 maa_result_t maa_i2c_write_byte(maa_i2c_context dev, const uint8_t data);
116
117 /** Sets the i2c context address.
118  *
119  *  @param dev the i2c context
120  *  @param address The address to set for the slave (ignoring the least
121  *  signifcant bit). If set to 0, the slave will only respond to the
122  *  general call address.
123  *
124  *  @return maa_result_t the maa result.
125  */
126 maa_result_t maa_i2c_address(maa_i2c_context dev, int address);
127
128 /** De-inits an maa_i2c_context device
129  *
130  *  @param dev the i2c context
131  *
132  *  @return maa_result_t the maa result.
133  */
134 maa_result_t maa_i2c_stop(maa_i2c_context dev);
135
136 #ifdef __cplusplus
137 }
138 #endif