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.
37 SPI_MODE0 = 0, /**< CPOL = 0, CPHA = 0, Clock idle low, data is clocked in on rising edge,
38 output data (change) on falling edge */
39 SPI_MODE1 = 1, /**< CPOL = 0, CPHA = 1, Clock idle low, data is clocked in on falling edge,
40 output data (change) on rising edge */
41 SPI_MODE2 = 2, /**< CPOL = 1, CPHA = 0, Clock idle low, data is clocked in on falling edge,
42 output data (change) on rising edge */
43 SPI_MODE3 = 3, /**< CPOL = 1, CPHA = 1, Clock idle low, data is clocked in on rising, edge
44 output data (change) on falling edge */
49 * @brief API to Serial Peripheral Interface
51 * This file defines the SPI interface for libmraa
53 * @snippet Spi-pot.cpp Interesting
59 * Initialise SPI object using the board mapping to set muxes
61 * @param bus to use, as listed in the platform definition, normally 0
65 m_spi = mraa_spi_init(bus);
68 throw std::invalid_argument("Error initialising SPI bus");
81 * Set the SPI device mode. see spidev0-3
83 * @param mode the mode. See Linux spidev doc
84 * @return Result of operation
89 return mraa_spi_mode(m_spi, (mraa_spi_mode_t) mode);
93 * Set the SPI device operating clock frequency
95 * @param hz the frequency to set in hz
96 * @return Result of operation
101 return mraa_spi_frequency(m_spi, hz);
105 * Write single byte to the SPI device
107 * @param data the byte to send
108 * @return data received on the miso line or -1 in case of error
111 writeByte(uint8_t data)
113 return mraa_spi_write(m_spi, (uint8_t) data);
117 * Write single byte to the SPI device
119 * @param data the byte to send
120 * @return data received on the miso line
123 write_word(uint16_t data)
125 return mraa_spi_write_word(m_spi, (uint16_t) data);
129 * Write buffer of bytes to SPI device The pointer return has to be
130 * free'd by the caller. It will return a NULL pointer in cases of
133 * @param txBuf buffer to send
134 * @param length size of buffer to send
135 * @return uint8_t* data received on the miso line. Same length as passed in
138 write(uint8_t* txBuf, int length)
140 return mraa_spi_write_buf(m_spi, txBuf, length);
145 * Write buffer of bytes to SPI device The pointer return has to be
146 * free'd by the caller. It will return a NULL pointer in cases of
149 * @param txBuf buffer to send
150 * @param length size of buffer (in bytes) to send
151 * @return uint8_t* data received on the miso line. Same length as passed in
154 write_word(uint16_t* txBuf, int length)
156 return mraa_spi_write_buf_word(m_spi, txBuf, length);
162 * Transfer data to and from SPI device Receive pointer may be null if
163 * return data is not needed.
165 * @param txBuf buffer to send
166 * @param rxBuf buffer to optionally receive data from spi device
167 * @param length size of buffer to send
168 * @return Result of operation
171 transfer(uint8_t* txBuf, uint8_t* rxBuf, int length)
173 return mraa_spi_transfer_buf(m_spi, txBuf, rxBuf, length);
177 * Transfer data to and from SPI device Receive pointer may be null if
178 * return data is not needed.
180 * @param txBuf buffer to send
181 * @param rxBuf buffer to optionally receive data from spi device
182 * @param length size of buffer to send
183 * @return Result of operation
186 transfer_word(uint16_t* txBuf, uint16_t* rxBuf, int length)
188 return mraa_spi_transfer_buf_word(m_spi, txBuf, rxBuf, length);
193 * Change the SPI lsb mode
195 * @param lsb Use least significant bit transmission - 0 for msbi
196 * @return Result of operation
201 return mraa_spi_lsbmode(m_spi, (mraa_boolean_t) lsb);
205 * Set bits per mode on transaction, default is 8
207 * @param bits bits per word
208 * @return Result of operation
211 bitPerWord(unsigned int bits)
213 return mraa_spi_bit_per_word(m_spi, bits);
217 mraa_spi_context m_spi;