spi.hpp: styling/identation fix
[contrib/mraa.git] / api / mraa / spi.hpp
1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
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:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
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.
23  */
24
25 #pragma once
26
27 #include "spi.h"
28 #include <stdexcept>
29
30 namespace mraa {
31
32 /**
33  * MRAA SPI Modes
34  */
35 typedef enum {
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 */
40 } Spi_Mode;
41
42
43   /**
44  * @brief API to Serial Peripheral Interface
45  *
46  * This file defines the SPI interface for libmraa
47  *
48  * @snippet Spi-pot.cpp Interesting
49  */
50 class Spi {
51     public:
52         /**
53          * Initialise SPI object using the board mapping to set muxes
54          *
55          * @param bus to use, as listed in the platform definition, normally 0
56          */
57         Spi(int bus) {
58             m_spi = mraa_spi_init(bus);
59
60             if (m_spi == NULL) {
61                 throw std::invalid_argument("Error initialising SPI bus");
62             }
63         }
64
65         /**
66          * Closes spi bus
67          */
68         ~Spi() {
69             mraa_spi_stop(m_spi);
70         }
71
72         /**
73          * Set the SPI device mode. see spidev0-3
74          *
75          * @param mode the mode. See Linux spidev doc
76          * @return Result of operation
77          */
78         mraa_result_t mode(Spi_Mode mode) {
79             return mraa_spi_mode(m_spi, (mraa_spi_mode_t) mode);
80         }
81
82         /**
83          * Set the SPI device operating clock frequency
84          *
85          * @param hz the frequency to set in hz
86          * @return Result of operation
87          */
88         mraa_result_t frequency(int hz) {
89             return mraa_spi_frequency(m_spi, hz);
90         }
91
92         /**
93          * Write single byte to the SPI device
94          *
95          * @param data the byte to send
96          * @return data received on the miso line
97          */
98         uint8_t writeByte(uint8_t data) {
99             return mraa_spi_write(m_spi, (uint8_t) data);
100         }
101
102         /**
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
105          * error
106          *
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
110          */
111         uint8_t* write(uint8_t* txBuf, int length) {
112             return mraa_spi_write_buf(m_spi, txBuf, length);
113         }
114
115 #ifndef SWIG
116         /**
117          * Transfer data to and from SPI device Receive pointer may be null if
118          * return data is not needed.
119          *
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
124          */
125         mraa_result_t transfer(uint8_t* txBuf, uint8_t* rxBuf, int length) {
126             return mraa_spi_transfer_buf(m_spi, txBuf, rxBuf, length);
127         }
128 #endif
129
130         /**
131          * Change the SPI lsb mode
132          *
133          * @param lsb Use least significant bit transmission - 0 for msbi
134          * @return Result of operation
135          */
136         mraa_result_t lsbmode(bool lsb) {
137             return mraa_spi_lsbmode(m_spi, (mraa_boolean_t) lsb);
138         }
139
140         /**
141          * Set bits per mode on transaction, default is 8
142          *
143          * @param bits bits per word
144          * @return Result of operation
145          */
146         mraa_result_t bitPerWord(unsigned int bits) {
147             return mraa_spi_bit_per_word(m_spi, bits);
148         }
149
150     private:
151         mraa_spi_context m_spi;
152 };
153 }