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.
33 * @brief API to System Packet Interface
35 * This file defines the SPI interface for libmraa
37 * @snippet Spi-pot.cpp Interesting
42 * Initialise SPI object using the board mapping to set muxes
44 * @param bus to use, as listed in the platform definition, normally 0
47 m_spi = mraa_spi_init(bus);
50 throw std::invalid_argument("Error initialising SPI bus");
60 * Set the SPI device mode. see spidev0-3
62 * @param mode the mode. See Linux spidev doc
63 * @return Result of operation
65 mraa_result_t mode(mraa_spi_mode_t mode) {
66 return mraa_spi_mode(m_spi, mode);
69 * Set the SPI device operating clock frequency
71 * @param hz the frequency to set in hz
72 * @return Result of operation
74 mraa_result_t frequency(int hz) {
75 return mraa_spi_frequency(m_spi, hz);
78 * Write single byte to the SPI device
80 * @param data the byte to send
81 * @return data received on the miso line
83 char write(char data) {
84 return (char) mraa_spi_write(m_spi, (uint8_t) data);
87 * Write buffer of bytes to SPI device The pointer return has to be
88 * free'd by the caller. It will return a NULL pointer in cases of
91 * @param data buffer to send
92 * @param length size of buffer to send
93 * @return char* data received on the miso line. Same length as passed in
95 char* write(char* data, size_t length) {
96 return (char*) mraa_spi_write_buf(m_spi, (uint8_t *) data, (int) length);
100 * Transfer data to and from SPI device Receive pointer may be null if return
101 * data is not needed.
103 * @param data buffer to send
104 * @param rxBuf buffer to optionally receive data from spi device
105 * @param length size of buffer to send
106 * @return Result of operation
108 mraa_result_t transfer(char* data, char* rxBuf, size_t length) {
109 return mraa_spi_transfer_buf(m_spi, (uint8_t *) data, (uint8_t *)rxBuf, (int) length);
113 * Change the SPI lsb mode
115 * @param lsb Use least significant bit transmission - 0 for msbi
116 * @return Result of operation
118 mraa_result_t lsbmode(bool lsb) {
119 return mraa_spi_lsbmode(m_spi, (mraa_boolean_t) lsb);
122 * Set bits per mode on transaction, default is 8
124 * @param bits bits per word
125 * @return Result of operation
127 mraa_result_t bitPerWord(unsigned int bits) {
128 return mraa_spi_bit_per_word(m_spi, bits);
131 mraa_spi_context m_spi;