docs: add common i2c doc page i2c.txt
[contrib/mraa.git] / api / mraa / spi.h
1 /*
2  * Author: Thomas Ingleby <thomas.c.ingleby@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 System Packet Interface
30  *
31  * This file defines the spi interface for libmraa. A Spi object in libmraa
32  * represents a spidev device. Linux spidev devices are created per spi bus and
33  * every chip select available on that bus has another spidev 'file'. A lot
34  * more information on spidev devices is available
35  * [here](https://www.kernel.org/doc/Documentation/spi/spidev).
36  *
37  * @snippet spi_mcp4261.c Interesting
38  */
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 #include <stdio.h>
45 #include <fcntl.h>
46 #include <stdint.h>
47
48 #include "common.h"
49
50 /**
51  * MRAA supported platform types
52  */
53 typedef enum {
54     MRAA_SPI_MODE0 = 0, /**< CPOL = 0, CPHA = 0, Clock idle low, data is clocked in on rising edge, output data (change) on falling edge */
55     MRAA_SPI_MODE1 = 1, /**< CPOL = 0, CPHA = 1, Clock idle low, data is clocked in on falling edge, output data (change) on rising edge */
56     MRAA_SPI_MODE2 = 2, /**< CPOL = 1, CPHA = 0, Clock idle low, data is clocked in on falling edge, output data (change) on rising edge */
57     MRAA_SPI_MODE3 = 3, /**< CPOL = 1, CPHA = 1, Clock idle low, data is clocked in on rising, edge output data (change) on falling edge */
58 } mraa_spi_mode_t;
59
60 /**
61  * Opaque pointer definition to the internal struct _spi
62  */
63 typedef struct _spi* mraa_spi_context;
64
65 /**
66  * Initialise SPI_context, uses board mapping. Sets the muxes
67  *
68  * @param bus Bus to use, as listed in platform definition, normally 0
69  * @return Spi context or NULL
70  */
71 mraa_spi_context mraa_spi_init(int bus);
72
73 /**
74  * Set the SPI device mode. see spidev 0-3.
75  *
76  * @param dev The Spi context
77  * @param mode The SPI mode, See Linux spidev
78  * @return Spi context or NULL
79  */
80 mraa_result_t mraa_spi_mode(mraa_spi_context dev, mraa_spi_mode_t mode);
81
82 /** Set the SPI device operating clock frequency.
83  *
84  * @param dev the Spi context
85  * @param hz the frequency in hz
86  * @return mraa_spi_context The returned initialised SPI context
87  */
88 mraa_result_t mraa_spi_frequency(mraa_spi_context dev, int hz);
89
90 /** Write Single Byte to the SPI device.
91  *
92  * @param dev The Spi context
93  * @param data Data to send
94  * @return Data received on the miso line
95  */
96 uint8_t mraa_spi_write(mraa_spi_context dev, uint8_t data);
97
98 /** Write Buffer of bytes to the SPI device. The pointer return has to be
99  * free'd by the caller.
100  *
101  * @param dev The Spi context
102  * @param data to send
103  * @param length elements within buffer, Max 4096
104  * @return Data received on the miso line, same length as passed in
105  */
106 uint8_t* mraa_spi_write_buf(mraa_spi_context dev, uint8_t* data, int length);
107
108 /**
109  * Change the SPI lsb mode
110  *
111  * @param dev The Spi context
112  * @param lsb Use least significant bit transmission. 0 for msbi
113  * @return Result of operation
114  */
115 mraa_result_t mraa_spi_lsbmode(mraa_spi_context dev, mraa_boolean_t lsb);
116
117 /**
118  * Set bits per mode on transaction, defaults at 8
119  *
120  * @param dev The Spi context
121  * @param bits bits per word
122  * @return Result of operation
123  */
124 mraa_result_t mraa_spi_bit_per_word(mraa_spi_context dev, unsigned int bits);
125
126 /**
127  * De-inits an mraa_spi_context device
128  *
129  * @param dev The Spi context
130  * @return Result of operation
131  */
132 mraa_result_t mraa_spi_stop(mraa_spi_context dev);
133
134 #ifdef __cplusplus
135 }
136 #endif