8bea35f08c82972198aa660a8f5f4165f3300708
[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  * @brief API to System Packet Interface
34  *
35  * This file defines the SPI interface for libmraa
36  *
37  * @snippet Spi-pot.cpp Interesting
38  */
39 class Spi {
40     public:
41         /**
42          * Initialise SPI object using the board mapping to set muxes
43          *
44          * @param bus to use, as listed in the platform definition, normally 0
45          */
46         Spi(int bus) {
47             m_spi = mraa_spi_init(bus);
48
49             if (m_spi == NULL) {
50                 throw std::invalid_argument("Error initialising SPI bus");
51             }
52         }
53         /**
54          * Closes spi bus
55          */
56         ~Spi() {
57             mraa_spi_stop(m_spi);
58         }
59         /**
60          * Set the SPI device mode. see spidev0-3
61          *
62          * @param mode the mode. See Linux spidev doc
63          * @return Result of operation
64          */
65         mraa_result_t mode(mraa_spi_mode_t mode) {
66             return mraa_spi_mode(m_spi, mode);
67         }
68         /**
69          * Set the SPI device operating clock frequency
70          *
71          * @param hz the frequency to set in hz
72          * @return Result of operation
73          */
74         mraa_result_t frequency(int hz) {
75             return mraa_spi_frequency(m_spi, hz);
76         }
77         /**
78          * Write single byte to the SPI device
79          *
80          * @param data the byte to send
81          * @return data received on the miso line
82          */
83         char write(char data) {
84             return (char) mraa_spi_write(m_spi, (uint8_t) data);
85         }
86         /**
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
89          * error
90          *
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
94          */
95         char* write(char* data, size_t length) {
96             return (char*) mraa_spi_write_buf(m_spi, (uint8_t *) data, (int) length);
97         }
98 #ifndef SWIG
99         /**
100          * Transfer data to and from SPI device Receive pointer may be null if return
101          * data is not needed.
102          *
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
107          */
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);
110         }
111 #endif
112         /**
113          * Change the SPI lsb mode
114          *
115          * @param lsb Use least significant bit transmission - 0 for msbi
116          * @return Result of operation
117          */
118         mraa_result_t lsbmode(bool lsb) {
119             return mraa_spi_lsbmode(m_spi, (mraa_boolean_t) lsb);
120         }
121         /**
122          * Set bits per mode on transaction, default is 8
123          *
124          * @param bits bits per word
125          * @return Result of operation
126          */
127         mraa_result_t bitPerWord(unsigned int bits) {
128             return mraa_spi_bit_per_word(m_spi, bits);
129         }
130             private:
131                 mraa_spi_context m_spi;
132         };
133 }