mraa  0.9.6
Low Level Skeleton Library for Communication on GNU/Linux platforms
 All Data Structures Namespaces Files Functions Typedefs Enumerations Enumerator Macros Pages
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 
73  Spi(int bus, int cs)
74  {
75  m_spi = mraa_spi_init_raw(bus, cs);
76 
77  if (m_spi == NULL) {
78  throw std::invalid_argument("Error initialising SPI bus");
79  }
80  }
81 
85  ~Spi()
86  {
87  mraa_spi_stop(m_spi);
88  }
89 
96  Result
98  {
99  return (Result) mraa_spi_mode(m_spi, (mraa_spi_mode_t) mode);
100  }
101 
108  Result
109  frequency(int hz)
110  {
111  return (Result) mraa_spi_frequency(m_spi, hz);
112  }
113 
120  int
121  writeByte(uint8_t data)
122  {
123  return mraa_spi_write(m_spi, (uint8_t) data);
124  }
125 
132  uint16_t
133  write_word(uint16_t data)
134  {
135  return mraa_spi_write_word(m_spi, (uint16_t) data);
136  }
137 
147  uint8_t*
148  write(uint8_t* txBuf, int length)
149  {
150  return mraa_spi_write_buf(m_spi, txBuf, length);
151  }
152 
153 #ifndef SWIG
154 
163  uint16_t*
164  write_word(uint16_t* txBuf, int length)
165  {
166  return mraa_spi_write_buf_word(m_spi, txBuf, length);
167  }
168 #endif
169 
170 #ifndef SWIG
171 
180  Result
181  transfer(uint8_t* txBuf, uint8_t* rxBuf, int length)
182  {
183  return (Result) mraa_spi_transfer_buf(m_spi, txBuf, rxBuf, length);
184  }
185 
195  Result
196  transfer_word(uint16_t* txBuf, uint16_t* rxBuf, int length)
197  {
198  return (Result) mraa_spi_transfer_buf_word(m_spi, txBuf, rxBuf, length);
199  }
200 #endif
201 
208  Result
209  lsbmode(bool lsb)
210  {
211  return (Result) mraa_spi_lsbmode(m_spi, (mraa_boolean_t) lsb);
212  }
213 
220  Result
221  bitPerWord(unsigned int bits)
222  {
223  return (Result) mraa_spi_bit_per_word(m_spi, bits);
224  }
225 
226  private:
227  mraa_spi_context m_spi;
228 };
229 }
Result
Definition: types.hpp:185
Result frequency(int hz)
Definition: spi.hpp:109
unsigned int mraa_boolean_t
Definition: common.h:60
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)
mraa_result_t mraa_spi_bit_per_word(mraa_spi_context dev, unsigned int bits)
Result bitPerWord(unsigned int bits)
Definition: spi.hpp:221
Definition: spi.hpp:38
struct _spi * mraa_spi_context
Definition: spi.h:67
Serial Peripheral Interface.
Result mode(Spi_Mode mode)
Definition: spi.hpp:97
uint16_t write_word(uint16_t data)
Definition: spi.hpp:133
Result transfer(uint8_t *txBuf, uint8_t *rxBuf, int length)
Definition: spi.hpp:181
Result lsbmode(bool lsb)
Definition: spi.hpp:209
uint8_t * write(uint8_t *txBuf, int length)
Definition: spi.hpp:148
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:121
uint16_t * write_word(uint16_t *txBuf, int length)
Definition: spi.hpp:164
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)
uint16_t mraa_spi_write_word(mraa_spi_context dev, uint16_t data)
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:196
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:85