Home | History | Annotate | Download | only in mraa
      1 /*
      2  * Author: Brendan Le Foll <brendan.le.foll (at) 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 /**
     28  * @file
     29  * @brief Inter-Integrated Circuit
     30  *
     31  * An i2c context represents a master on an i2c bus and that context can
     32  * communicate to multiple i2c slaves by configuring the address.
     33  * @htmlinclude i2c.txt
     34  *
     35  * @snippet i2c_HMC5883L.c Interesting
     36  */
     37 
     38 #ifdef __cplusplus
     39 extern "C" {
     40 #endif
     41 
     42 #include <stdlib.h>
     43 #include <stdio.h>
     44 #include <fcntl.h>
     45 #include <stdint.h>
     46 
     47 #include "common.h"
     48 #include "gpio.h"
     49 
     50 /**
     51  * Opaque pointer definition to the internal struct _i2c
     52  */
     53 typedef struct _i2c* mraa_i2c_context;
     54 
     55 /**
     56  * Initialise i2c context, using board defintions
     57  *
     58  * @param bus i2c bus to use
     59  * @return i2c context or NULL
     60  */
     61 mraa_i2c_context mraa_i2c_init(int bus);
     62 
     63 /**
     64  * Initialise i2c context, passing in the i2c bus to use.
     65  *
     66  * @param bus The i2c bus to use i.e. /dev/i2c-2 would be "2"
     67  * @return i2c context or NULL
     68  */
     69 mraa_i2c_context mraa_i2c_init_raw(unsigned int bus);
     70 
     71 /**
     72  * Sets the frequency of the i2c context. Most platforms do not support this.
     73  *
     74  * @param dev The i2c context
     75  * @param mode The bus mode
     76  * @return Result of operation
     77  */
     78 mraa_result_t mraa_i2c_frequency(mraa_i2c_context dev, mraa_i2c_mode_t mode);
     79 
     80 /**
     81  * Simple bulk read from an i2c context, this will always begin with the i2c
     82  * offset 0x0
     83  *
     84  * @param dev The i2c context
     85  * @param data pointer to the byte array to read data in to
     86  * @param length max number of bytes to read
     87  * @return length of the read in bytes or 0
     88  */
     89 int mraa_i2c_read(mraa_i2c_context dev, uint8_t* data, int length);
     90 
     91 /**
     92  * Simple read for a single byte from the i2c context, this will always begin
     93  * with the i2c offset 0x0
     94  *
     95  * @param dev The i2c context
     96  * @return The result of the read and 0 if failed
     97  */
     98 uint8_t mraa_i2c_read_byte(mraa_i2c_context dev);
     99 
    100 /**
    101  * Read a single byte from i2c context, from designated register
    102  *
    103  * @param dev The i2c context
    104  * @param command The register
    105  * @return The result of the read and 0 if failed
    106  */
    107 uint8_t mraa_i2c_read_byte_data(mraa_i2c_context dev, const uint8_t command);
    108 
    109 /**
    110  * Read a single word from i2c context, from designated register
    111  *
    112  * @param dev The i2c context
    113  * @param command The register
    114  * @return The result of the read and 0 if failed
    115  */
    116 uint16_t mraa_i2c_read_word_data(mraa_i2c_context dev, const uint8_t command);
    117 
    118 /**
    119  * Bulk read from i2c context, starting from designated register
    120  *
    121  * @param dev The i2c context
    122  * @param command The register
    123  * @param data pointer to the byte array to read data in to
    124  * @param length max number of bytes to read
    125  * @return The length in bytes passed to the function or -1
    126  */
    127 int mraa_i2c_read_bytes_data(mraa_i2c_context dev, uint8_t command, uint8_t* data, int length);
    128 
    129 /**
    130  * Write length bytes to the bus, the first byte in the array is the
    131  * command/register to write
    132  *
    133  * @param dev The i2c context
    134  * @param data pointer to the byte array to be written
    135  * @param length the number of bytes to transmit
    136  * @return Result of operation
    137  */
    138 mraa_result_t mraa_i2c_write(mraa_i2c_context dev, const uint8_t* data, int length);
    139 
    140 /**
    141  * Write a single byte to an i2c context, always at offset 0x0
    142  *
    143  * @param dev The i2c context
    144  * @param data The byte to write
    145  * @return Result of operation
    146  */
    147 mraa_result_t mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data);
    148 
    149 /**
    150  * Write a single byte to an i2c context
    151  *
    152  * @param dev The i2c context
    153  * @param data The byte to write
    154  * @param command The register
    155  * @return Result of operation
    156  */
    157 mraa_result_t mraa_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data, const uint8_t command);
    158 
    159 /**
    160  * Write a single word to an i2c context
    161  *
    162  * @param dev The i2c context
    163  * @param data The word to write
    164  * @param command The register
    165  * @return Result of operation
    166  */
    167 mraa_result_t mraa_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data, const uint8_t command);
    168 
    169 /**
    170  * Sets the i2c context address.
    171  *
    172  * @param dev The i2c context
    173  * @param address The address to set for the slave (ignoring the least
    174  *   signifcant bit). If set to 0, the slave will only respond to the
    175  *   general call address.
    176  * @return Result of operation
    177  */
    178 mraa_result_t mraa_i2c_address(mraa_i2c_context dev, uint8_t address);
    179 
    180 /**
    181  * De-inits an mraa_i2c_context device
    182  *
    183  * @param dev The i2c context
    184  * @return Result of operation
    185  */
    186 mraa_result_t mraa_i2c_stop(mraa_i2c_context dev);
    187 
    188 #ifdef __cplusplus
    189 }
    190 #endif
    191