2 * Author: Brendan Le Foll <brendan.le.foll@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.
36 SPI_MODE0 = 0, /**< CPOL = 0, CPHA = 0, Clock idle low, data is clocked in on rising edge, output data (change) on falling edge */
37 SPI_MODE1 = 1, /**< CPOL = 0, CPHA = 1, Clock idle low, data is clocked in on falling edge, output data (change) on rising edge */
38 SPI_MODE2 = 2, /**< CPOL = 1, CPHA = 0, Clock idle low, data is clocked in on falling edge, output data (change) on rising edge */
39 SPI_MODE3 = 3, /**< CPOL = 1, CPHA = 1, Clock idle low, data is clocked in on rising, edge output data (change) on falling edge */
44 * @brief API to Serial Peripheral Interface
46 * This file defines the SPI interface for libmraa
48 * @snippet Spi-pot.cpp Interesting
53 * Initialise SPI object using the board mapping to set muxes
55 * @param bus to use, as listed in the platform definition, normally 0
58 m_spi = mraa_spi_init(bus);
61 throw std::invalid_argument("Error initialising SPI bus");
73 * Set the SPI device mode. see spidev0-3
75 * @param mode the mode. See Linux spidev doc
76 * @return Result of operation
78 mraa_result_t mode(Spi_Mode mode) {
79 return mraa_spi_mode(m_spi, (mraa_spi_mode_t) mode);
83 * Set the SPI device operating clock frequency
85 * @param hz the frequency to set in hz
86 * @return Result of operation
88 mraa_result_t frequency(int hz) {
89 return mraa_spi_frequency(m_spi, hz);
93 * Write single byte to the SPI device
95 * @param data the byte to send
96 * @return data received on the miso line
98 uint8_t writeByte(uint8_t data) {
99 return mraa_spi_write(m_spi, (uint8_t) data);
103 * Write buffer of bytes to SPI device The pointer return has to be
104 * free'd by the caller. It will return a NULL pointer in cases of
107 * @param txBuf buffer to send
108 * @param length size of buffer to send
109 * @return uint8_t* data received on the miso line. Same length as passed in
111 uint8_t* write(uint8_t* txBuf, int length) {
112 return mraa_spi_write_buf(m_spi, txBuf, length);
117 * Transfer data to and from SPI device Receive pointer may be null if
118 * return data is not needed.
120 * @param data buffer to send
121 * @param rxBuf buffer to optionally receive data from spi device
122 * @param length size of buffer to send
123 * @return Result of operation
125 mraa_result_t transfer(uint8_t* txBuf, uint8_t* rxBuf, int length) {
126 return mraa_spi_transfer_buf(m_spi, txBuf, rxBuf, length);
131 * Change the SPI lsb mode
133 * @param lsb Use least significant bit transmission - 0 for msbi
134 * @return Result of operation
136 mraa_result_t lsbmode(bool lsb) {
137 return mraa_spi_lsbmode(m_spi, (mraa_boolean_t) lsb);
141 * Set bits per mode on transaction, default is 8
143 * @param bits bits per word
144 * @return Result of operation
146 mraa_result_t bitPerWord(unsigned int bits) {
147 return mraa_spi_bit_per_word(m_spi, bits);
151 mraa_spi_context m_spi;