mraa  0.6.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
 All Data Structures Namespaces Files Functions Typedefs Enumerations Enumerator 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 <stdexcept>
29 
30 namespace mraa {
31 
35 typedef enum {
36  SPI_MODE0 = 0,
37  SPI_MODE1 = 1,
38  SPI_MODE2 = 2,
39  SPI_MODE3 = 3,
40 } Spi_Mode;
41 
42 
50 class Spi {
51  public:
57  Spi(int bus) {
58  m_spi = mraa_spi_init(bus);
59 
60  if (m_spi == NULL) {
61  throw std::invalid_argument("Error initialising SPI bus");
62  }
63  }
64 
68  ~Spi() {
69  mraa_spi_stop(m_spi);
70  }
71 
79  return mraa_spi_mode(m_spi, (mraa_spi_mode_t) mode);
80  }
81 
89  return mraa_spi_frequency(m_spi, hz);
90  }
91 
98  uint8_t writeByte(uint8_t data) {
99  return mraa_spi_write(m_spi, (uint8_t) data);
100  }
101 
111  uint8_t* write(uint8_t* txBuf, int length) {
112  return mraa_spi_write_buf(m_spi, txBuf, length);
113  }
114 
115 #ifndef SWIG
116 
125  mraa_result_t transfer(uint8_t* txBuf, uint8_t* rxBuf, int length) {
126  return mraa_spi_transfer_buf(m_spi, txBuf, rxBuf, length);
127  }
128 #endif
129 
136  mraa_result_t lsbmode(bool lsb) {
137  return mraa_spi_lsbmode(m_spi, (mraa_boolean_t) lsb);
138  }
139 
146  mraa_result_t bitPerWord(unsigned int bits) {
147  return mraa_spi_bit_per_word(m_spi, bits);
148  }
149 
150  private:
151  mraa_spi_context m_spi;
152 };
153 }
unsigned int mraa_boolean_t
Definition: common.h:44
Definition: spi.hpp:37
mraa_result_t frequency(int hz)
Definition: spi.hpp:88
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)
Definition: spi.hpp:36
struct _spi * mraa_spi_context
Definition: spi.h:63
Serial Peripheral Interface.
uint8_t * write(uint8_t *txBuf, int length)
Definition: spi.hpp:111
uint8_t writeByte(uint8_t data)
Definition: spi.hpp:98
mraa_result_t mraa_spi_stop(mraa_spi_context dev)
mraa_result_t transfer(uint8_t *txBuf, uint8_t *rxBuf, int length)
Definition: spi.hpp:125
mraa_result_t mode(Spi_Mode mode)
Definition: spi.hpp:78
mraa_spi_mode_t
Definition: spi.h:53
mraa_result_t bitPerWord(unsigned int bits)
Definition: spi.hpp:146
Spi(int bus)
Definition: spi.hpp:57
mraa_result_t lsbmode(bool lsb)
Definition: spi.hpp:136
mraa_result_t mraa_spi_transfer_buf(mraa_spi_context dev, uint8_t *data, uint8_t *rxbuf, int length)
Definition: spi.hpp:38
mraa_spi_context mraa_spi_init(int bus)
uint8_t mraa_spi_write(mraa_spi_context dev, uint8_t data)
mraa_result_t
Definition: types.h:145
Definition: spi.hpp:39
Spi_Mode
Definition: spi.hpp:35
uint8_t * mraa_spi_write_buf(mraa_spi_context dev, uint8_t *data, int length)
mraa_result_t mraa_spi_mode(mraa_spi_context dev, mraa_spi_mode_t mode)
API to Serial Peripheral Interface.
Definition: spi.hpp:50
~Spi()
Definition: spi.hpp:68