mraa  2.0.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
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 "types.hpp"
29 #include <stdexcept>
30 
31 namespace mraa
32 {
33 
37 typedef enum {
38  SPI_MODE0 = 0,
40  SPI_MODE1 = 1,
42  SPI_MODE2 = 2,
44  SPI_MODE3 = 3,
46 } Spi_Mode;
47 
48 
56 class Spi
57 {
58  public:
64  Spi(int bus)
65  {
66  m_spi = mraa_spi_init(bus);
67 
68  if (m_spi == NULL) {
69  throw std::invalid_argument("Error initialising SPI bus");
70  }
71  }
72 
79  Spi(int bus, int cs)
80  {
81  m_spi = mraa_spi_init_raw(bus, cs);
82 
83  if (m_spi == NULL) {
84  throw std::invalid_argument("Error initialising SPI bus");
85  }
86  }
87 
94  Spi(void* spi_context)
95  {
96  m_spi = (mraa_spi_context) spi_context;
97  if (m_spi == NULL) {
98  throw std::invalid_argument("Invalid SPI context");
99  }
100  }
101 
106  {
107  mraa_spi_stop(m_spi);
108  }
109 
116  Result
118  {
119  return (Result) mraa_spi_mode(m_spi, (mraa_spi_mode_t) mode);
120  }
121 
128  Result
129  frequency(int hz)
130  {
131  return (Result) mraa_spi_frequency(m_spi, hz);
132  }
133 
140  int
141  writeByte(uint8_t data)
142  {
143  return mraa_spi_write(m_spi, (uint8_t) data);
144  }
145 
152  int
153  writeWord(uint16_t data)
154  {
155  return mraa_spi_write_word(m_spi, (uint16_t) data);
156  }
157 
167  uint8_t*
168  write(uint8_t* txBuf, int length)
169  {
170  return mraa_spi_write_buf(m_spi, txBuf, length);
171  }
172 
173 #ifndef SWIG
174 
183  uint16_t*
184  writeWord(uint16_t* txBuf, int length)
185  {
186  return mraa_spi_write_buf_word(m_spi, txBuf, length);
187  }
188 #endif
189 
190 #ifndef SWIG
191 
200  Result
201  transfer(uint8_t* txBuf, uint8_t* rxBuf, int length)
202  {
203  return (Result) mraa_spi_transfer_buf(m_spi, txBuf, rxBuf, length);
204  }
205 
215  Result
216  transfer_word(uint16_t* txBuf, uint16_t* rxBuf, int length)
217  {
218  return (Result) mraa_spi_transfer_buf_word(m_spi, txBuf, rxBuf, length);
219  }
220 #endif
221 
228  Result
229  lsbmode(bool lsb)
230  {
231  return (Result) mraa_spi_lsbmode(m_spi, (mraa_boolean_t) lsb);
232  }
233 
240  Result
241  bitPerWord(unsigned int bits)
242  {
243  return (Result) mraa_spi_bit_per_word(m_spi, bits);
244  }
245 
246  private:
247  mraa_spi_context m_spi;
248 };
249 }
Result
Definition: types.hpp:204
Result frequency(int hz)
Definition: spi.hpp:129
unsigned int mraa_boolean_t
Definition: common.h:78
Definition: spi.hpp:40
mraa_spi_context mraa_spi_init_raw(unsigned int bus, unsigned int cs)
mraa_result_t mraa_spi_lsbmode(mraa_spi_context dev, mraa_boolean_t lsb)
mraa_result_t mraa_spi_frequency(mraa_spi_context dev, int hz)
int writeWord(uint16_t data)
Definition: spi.hpp:153
mraa_result_t mraa_spi_bit_per_word(mraa_spi_context dev, unsigned int bits)
uint16_t * writeWord(uint16_t *txBuf, int length)
Definition: spi.hpp:184
Result bitPerWord(unsigned int bits)
Definition: spi.hpp:241
Definition: spi.hpp:38
struct _spi * mraa_spi_context
Definition: spi.h:67
Spi(int bus, int cs)
Definition: spi.hpp:79
Spi(void *spi_context)
Definition: spi.hpp:94
Serial Peripheral Interface.
Result mode(Spi_Mode mode)
Definition: spi.hpp:117
Result transfer(uint8_t *txBuf, uint8_t *rxBuf, int length)
Definition: spi.hpp:201
Result lsbmode(bool lsb)
Definition: spi.hpp:229
uint8_t * write(uint8_t *txBuf, int length)
Definition: spi.hpp:168
int mraa_spi_write_word(mraa_spi_context dev, uint16_t data)
mraa_result_t mraa_spi_stop(mraa_spi_context dev)
int mraa_spi_write(mraa_spi_context dev, uint8_t data)
mraa_spi_mode_t
Definition: spi.h:53
int writeByte(uint8_t data)
Definition: spi.hpp:141
Spi(int bus)
Definition: spi.hpp:64
mraa_result_t mraa_spi_transfer_buf(mraa_spi_context dev, uint8_t *data, uint8_t *rxbuf, int length)
Definition: spi.hpp:42
mraa_spi_context mraa_spi_init(int bus)
Definition: aio.hpp:31
Definition: spi.hpp:44
Spi_Mode
Definition: spi.hpp:37
uint8_t * mraa_spi_write_buf(mraa_spi_context dev, uint8_t *data, int length)
Result transfer_word(uint16_t *txBuf, uint16_t *rxBuf, int length)
Definition: spi.hpp:216
mraa_result_t mraa_spi_mode(mraa_spi_context dev, mraa_spi_mode_t mode)
API to Serial Peripheral Interface.
Definition: spi.hpp:56
uint16_t * mraa_spi_write_buf_word(mraa_spi_context dev, uint16_t *data, int length)
mraa_result_t mraa_spi_transfer_buf_word(mraa_spi_context dev, uint16_t *data, uint16_t *rxbuf, int length)
~Spi()
Definition: spi.hpp:105