2 * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
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:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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.
29 * @brief System Packet Interface
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).
37 * @snippet spi_mcp4261.c Interesting
51 * MRAA supported platform types
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 */
61 * Opaque pointer definition to the internal struct _spi
63 typedef struct _spi* mraa_spi_context;
66 * Initialise SPI_context, uses board mapping. Sets the muxes
68 * @param bus Bus to use, as listed in platform definition, normally 0
69 * @return Spi context or NULL
71 mraa_spi_context mraa_spi_init(int bus);
74 * Set the SPI device mode. see spidev 0-3.
76 * @param dev The Spi context
77 * @param mode The SPI mode, See Linux spidev
78 * @return Spi context or NULL
80 mraa_result_t mraa_spi_mode(mraa_spi_context dev, mraa_spi_mode_t mode);
82 /** Set the SPI device operating clock frequency.
84 * @param dev the Spi context
85 * @param hz the frequency in hz
86 * @return mraa_spi_context The returned initialised SPI context
88 mraa_result_t mraa_spi_frequency(mraa_spi_context dev, int hz);
90 /** Write Single Byte to the SPI device.
92 * @param dev The Spi context
93 * @param data Data to send
94 * @return Data received on the miso line
96 uint8_t mraa_spi_write(mraa_spi_context dev, uint8_t data);
98 /** Write Buffer of bytes to the SPI device. The pointer return has to be
99 * free'd by the caller.
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
106 uint8_t* mraa_spi_write_buf(mraa_spi_context dev, uint8_t* data, int length);
109 * Change the SPI lsb mode
111 * @param dev The Spi context
112 * @param lsb Use least significant bit transmission. 0 for msbi
113 * @return Result of operation
115 mraa_result_t mraa_spi_lsbmode(mraa_spi_context dev, mraa_boolean_t lsb);
118 * Set bits per mode on transaction, defaults at 8
120 * @param dev The Spi context
121 * @param bits bits per word
122 * @return Result of operation
124 mraa_result_t mraa_spi_bit_per_word(mraa_spi_context dev, unsigned int bits);
127 * De-inits an mraa_spi_context device
129 * @param dev The Spi context
130 * @return Result of operation
132 mraa_result_t mraa_spi_stop(mraa_spi_context dev);