mraa  1.2.1
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 
88  Spi(void* spi_context)
89  {
90  m_spi = (mraa_spi_context) spi_context;
91  if (m_spi == NULL) {
92  throw std::invalid_argument("Invalid SPI context");
93  }
94  }
95 
99  ~Spi()
100  {
101  mraa_spi_stop(m_spi);
102  }
103 
110  Result
112  {
113  return (Result) mraa_spi_mode(m_spi, (mraa_spi_mode_t) mode);
114  }
115 
122  Result
123  frequency(int hz)
124  {
125  return (Result) mraa_spi_frequency(m_spi, hz);
126  }
127 
134  int
135  writeByte(uint8_t data)
136  {
137  return mraa_spi_write(m_spi, (uint8_t) data);
138  }
139 
146  int
147  writeWord(uint16_t data)
148  {
149  return mraa_spi_write_word(m_spi, (uint16_t) data);
150  }
151 
161  uint8_t*
162  write(uint8_t* txBuf, int length)
163  {
164  return mraa_spi_write_buf(m_spi, txBuf, length);
165  }
166 
167 #ifndef SWIG
168 
177  uint16_t*
178  writeWord(uint16_t* txBuf, int length)
179  {
180  return mraa_spi_write_buf_word(m_spi, txBuf, length);
181  }
182 #endif
183 
184 #ifndef SWIG
185 
194  Result
195  transfer(uint8_t* txBuf, uint8_t* rxBuf, int length)
196  {
197  return (Result) mraa_spi_transfer_buf(m_spi, txBuf, rxBuf, length);
198  }
199 
209  Result
210  transfer_word(uint16_t* txBuf, uint16_t* rxBuf, int length)
211  {
212  return (Result) mraa_spi_transfer_buf_word(m_spi, txBuf, rxBuf, length);
213  }
214 #endif
215 
222  Result
223  lsbmode(bool lsb)
224  {
225  return (Result) mraa_spi_lsbmode(m_spi, (mraa_boolean_t) lsb);
226  }
227 
234  Result
235  bitPerWord(unsigned int bits)
236  {
237  return (Result) mraa_spi_bit_per_word(m_spi, bits);
238  }
239 
240  private:
241  mraa_spi_context m_spi;
242 };
243 }
Result
Definition: types.hpp:196
Result frequency(int hz)
Definition: spi.hpp:123
unsigned int mraa_boolean_t
Definition: common.h:61
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:147
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:178
Result bitPerWord(unsigned int bits)
Definition: spi.hpp:235
Definition: spi.hpp:38
struct _spi * mraa_spi_context
Definition: spi.h:67
Spi(void *spi_context)
Definition: spi.hpp:88
Serial Peripheral Interface.
Result mode(Spi_Mode mode)
Definition: spi.hpp:111
Result transfer(uint8_t *txBuf, uint8_t *rxBuf, int length)
Definition: spi.hpp:195
Result lsbmode(bool lsb)
Definition: spi.hpp:223
uint8_t * write(uint8_t *txBuf, int length)
Definition: spi.hpp:162
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:135
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: 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:210
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:99