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
33 * @snippet spi_mcp4261.c Interesting
47 * MRAA supported platform types
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 */
57 * Opaque pointer definition to the internal struct _spi
59 typedef struct _spi* mraa_spi_context;
62 * Initialise SPI_context, uses board mapping. Sets the muxes
64 * @param bus Bus to use, as listed in platform definition, normally 0
65 * @return Spi context or NULL
67 mraa_spi_context mraa_spi_init(int bus);
70 * Set the SPI device mode. see spidev 0-3.
72 * @param dev The Spi context
73 * @param mode The SPI mode, See Linux spidev
74 * @return Spi context or NULL
76 mraa_result_t mraa_spi_mode(mraa_spi_context dev, mraa_spi_mode_t mode);
78 /** Set the SPI device operating clock frequency.
80 * @param dev the Spi context
81 * @param hz the frequency in hz
82 * @return mraa_spi_context The returned initialised SPI context
84 mraa_result_t mraa_spi_frequency(mraa_spi_context dev, int hz);
86 /** Write Single Byte to the SPI device.
88 * @param dev The Spi context
89 * @param data Data to send
90 * @return Data received on the miso line
92 uint8_t mraa_spi_write(mraa_spi_context dev, uint8_t data);
94 /** Write Buffer of bytes to the SPI device. The pointer return has to be
95 * free'd by the caller.
97 * @param dev The Spi context
99 * @param length elements within buffer, Max 4096
100 * @return Data received on the miso line, same length as passed in
102 uint8_t* mraa_spi_write_buf(mraa_spi_context dev, uint8_t* data, int length);
105 * Change the SPI lsb mode
107 * @param dev The Spi context
108 * @param lsb Use least significant bit transmission. 0 for msbi
109 * @return Result of operation
111 mraa_result_t mraa_spi_lsbmode(mraa_spi_context dev, mraa_boolean_t lsb);
114 * Set bits per mode on transaction, defaults at 8
116 * @param dev The Spi context
117 * @param bits bits per word
118 * @return Result of operation
120 mraa_result_t mraa_spi_bit_per_word(mraa_spi_context dev, unsigned int bits);
123 * De-inits an mraa_spi_context device
125 * @param dev The Spi context
126 * @return Result of operation
128 mraa_result_t mraa_spi_stop(mraa_spi_context dev);