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