3641e9dd0d379d794f7c335e1e897a5d268490db
[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 /**
34  * MRAA SPI Modes
35  */
36 typedef enum {
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 */
45 } Spi_Mode;
46
47
48 /**
49 * @brief API to Serial Peripheral Interface
50 *
51 * This file defines the SPI interface for libmraa
52 *
53 * @snippet Spi-pot.cpp Interesting
54 */
55 class Spi
56 {
57   public:
58     /**
59      * Initialise SPI object using the board mapping to set muxes
60      *
61      * @param bus to use, as listed in the platform definition, normally 0
62      */
63     Spi(int bus)
64     {
65         m_spi = mraa_spi_init(bus);
66
67         if (m_spi == NULL) {
68             throw std::invalid_argument("Error initialising SPI bus");
69         }
70     }
71
72     /**
73      * Closes spi bus
74      */
75     ~Spi()
76     {
77         mraa_spi_stop(m_spi);
78     }
79
80     /**
81      * Set the SPI device mode. see spidev0-3
82      *
83      * @param mode the mode. See Linux spidev doc
84      * @return Result of operation
85      */
86     mraa_result_t
87     mode(Spi_Mode mode)
88     {
89         return mraa_spi_mode(m_spi, (mraa_spi_mode_t) mode);
90     }
91
92     /**
93      * Set the SPI device operating clock frequency
94      *
95      * @param hz the frequency to set in hz
96      * @return Result of operation
97      */
98     mraa_result_t
99     frequency(int hz)
100     {
101         return mraa_spi_frequency(m_spi, hz);
102     }
103
104     /**
105      * Write single byte to the SPI device
106      *
107      * @param data the byte to send
108      * @return data received on the miso line or -1 in case of error
109      */
110     int
111     writeByte(uint8_t data)
112     {
113         return mraa_spi_write(m_spi, (uint8_t) data);
114     }
115
116     /**
117      * Write single byte to the SPI device
118      *
119      * @param data the byte to send
120      * @return data received on the miso line
121      */
122     uint16_t
123     write_word(uint16_t data)
124     {
125         return mraa_spi_write_word(m_spi, (uint16_t) data);
126     }
127
128     /**
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
131      * error
132      *
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
136      */
137     uint8_t*
138     write(uint8_t* txBuf, int length)
139     {
140         return mraa_spi_write_buf(m_spi, txBuf, length);
141     }
142
143 #ifndef SWIG
144     /**
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
147      * error
148      *
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
152      */
153     uint16_t*
154     write_word(uint16_t* txBuf, int length)
155     {
156         return mraa_spi_write_buf_word(m_spi, txBuf, length);
157     }
158 #endif
159
160 #ifndef SWIG
161     /**
162      * Transfer data to and from SPI device Receive pointer may be null if
163      * return data is not needed.
164      *
165      * @param data 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
169      */
170     mraa_result_t
171     transfer(uint8_t* txBuf, uint8_t* rxBuf, int length)
172     {
173         return mraa_spi_transfer_buf(m_spi, txBuf, rxBuf, length);
174     }
175
176     /**
177      * Transfer data to and from SPI device Receive pointer may be null if
178      * return data is not needed.
179      *
180      * @param data 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
184      */
185     mraa_result_t
186     transfer_word(uint16_t* txBuf, uint16_t* rxBuf, int length)
187     {
188         return mraa_spi_transfer_buf_word(m_spi, txBuf, rxBuf, length);
189     }
190 #endif
191
192     /**
193      * Change the SPI lsb mode
194      *
195      * @param lsb Use least significant bit transmission - 0 for msbi
196      * @return Result of operation
197      */
198     mraa_result_t
199     lsbmode(bool lsb)
200     {
201         return mraa_spi_lsbmode(m_spi, (mraa_boolean_t) lsb);
202     }
203
204     /**
205      * Set bits per mode on transaction, default is 8
206      *
207      * @param bits bits per word
208      * @return Result of operation
209      */
210     mraa_result_t
211     bitPerWord(unsigned int bits)
212     {
213         return mraa_spi_bit_per_word(m_spi, bits);
214     }
215
216   private:
217     mraa_spi_context m_spi;
218 };
219 }