STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
sfe_bus.h
Go to the documentation of this file.
1// sfe_bus.cpp
2//
3// This is a library written for SparkFun Qwiic ISM330DHCX boards
4//
5// SparkFun sells these bpards at its website: www.sparkfun.com
6//
7// Do you like this library? Help support SparkFun. Buy a board!
8//
9//SparkFun Qwiic 6DoF - ISM330DHCX https://www.sparkfun.com/products/19764
10//
11// Written by Kirk Benell @ SparkFun Electronics
12// Modified by Elias Santistevan @ SparkFun Electronics, April 2022
13//
14// Repository:
15// https://github.com/sparkfun/SparkFun_6DoF_ISM330DHCX_Arduino_Library
16//
17//
18// SparkFun code, firmware, and software is released under the MIT
19// License(http://opensource.org/licenses/MIT).
20//
21// SPDX-License-Identifier: MIT
22//
23// The MIT License (MIT)
24//
25// Copyright (c) 2022 SparkFun Electronics
26// Permission is hereby granted, free of charge, to any person obtaining a
27// copy of this software and associated documentation files (the "Software"),
28// to deal in the Software without restriction, including without limitation
29// the rights to use, copy, modify, merge, publish, distribute, sublicense,
30// and/or sell copies of the Software, and to permit persons to whom the
31// Software is furnished to do so, subject to the following conditions: The
32// above copyright notice and this permission notice shall be included in all
33// copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
34// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
35// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
36// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
37// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
38// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
39// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40
41// The following classes specify the behavior for communicating
42// over the respective data buses: Inter-Integrated Circuit (I2C)
43// and Serial Peripheral Interface (SPI). For ease of implementation
44// an abstract interface (QwIDeviceBus) is used.
45
46#pragma once
47
48
49
50#include <Wire.h>
51#include <SPI.h>
52
53namespace sfe_ISM330DHCX {
54
55// The following abstract class is used an interface for upstream implementation.
57{
58 public:
59
60 virtual bool ping(uint8_t address) = 0;
61
62 virtual bool writeRegisterByte(uint8_t address, uint8_t offset, uint8_t data) = 0;
63
64 virtual int writeRegisterRegion(uint8_t address, uint8_t offset, const uint8_t* data, uint16_t length) = 0;
65
66 virtual int readRegisterRegion(uint8_t addr, uint8_t reg, uint8_t* data, uint16_t numBytes) = 0;
67
68};
69
70// The QwI2C device defines behavior for I2C implementation based around the TwoWire class (Wire).
71// This is Arduino specific.
72class QwI2C : public QwIDeviceBus
73{
74 public:
75
76 QwI2C(void);
77
78 bool init();
79
80 bool init(TwoWire& wirePort, bool bInit=false);
81
82 bool ping(uint8_t address);
83
84 bool writeRegisterByte(uint8_t address, uint8_t offset, uint8_t data);
85
86 int writeRegisterRegion(uint8_t address, uint8_t offset, const uint8_t* data, uint16_t length);
87
88 int readRegisterRegion(uint8_t addr, uint8_t reg, uint8_t* data, uint16_t numBytes);
89
90 private:
91
92 TwoWire* _i2cPort;
93};
94
95// The SfeSPI class defines behavior for SPI implementation based around the SPIClass class (SPI).
96// This is Arduino specific.
97// Paramaters like "address" are kept although irrelevant to SPI due to the use of the abstract class
98// as interface, QwIDeviceBus.
99class SfeSPI : public QwIDeviceBus
100{
101 public:
102
103 SfeSPI(void);
104
105 bool init(uint8_t cs, bool bInit=false);
106
107 bool init(SPIClass& spiPort, SPISettings& ismSPISettings, uint8_t cs, bool bInit=false);
108
109 bool ping(uint8_t address);
110
111 bool writeRegisterByte(uint8_t address, uint8_t offset, uint8_t data);
112
113 int writeRegisterRegion(uint8_t address, uint8_t offset, const uint8_t* data, uint16_t length);
114
115 int readRegisterRegion(uint8_t addr, uint8_t reg, uint8_t* data, uint16_t numBytes);
116
117 private:
118
119 SPIClass* _spiPort;
120 // Settings are used for every transaction.
121 SPISettings _sfeSPISettings;
122 uint8_t _cs;
123};
124
125};
void ping()
TwoWire * _i2cPort
Definition sfe_bus.h:92
int writeRegisterRegion(uint8_t address, uint8_t offset, const uint8_t *data, uint16_t length)
Definition sfe_bus.cpp:139
bool writeRegisterByte(uint8_t address, uint8_t offset, uint8_t data)
Definition sfe_bus.cpp:120
int readRegisterRegion(uint8_t addr, uint8_t reg, uint8_t *data, uint16_t numBytes)
Definition sfe_bus.cpp:157
virtual bool ping(uint8_t address)=0
virtual int readRegisterRegion(uint8_t addr, uint8_t reg, uint8_t *data, uint16_t numBytes)=0
virtual bool writeRegisterByte(uint8_t address, uint8_t offset, uint8_t data)=0
virtual int writeRegisterRegion(uint8_t address, uint8_t offset, const uint8_t *data, uint16_t length)=0
int readRegisterRegion(uint8_t addr, uint8_t reg, uint8_t *data, uint16_t numBytes)
Definition sfe_bus.cpp:339
bool writeRegisterByte(uint8_t address, uint8_t offset, uint8_t data)
Definition sfe_bus.cpp:281
SPISettings _sfeSPISettings
Definition sfe_bus.h:121
SPIClass * _spiPort
Definition sfe_bus.h:119
int writeRegisterRegion(uint8_t address, uint8_t offset, const uint8_t *data, uint16_t length)
Definition sfe_bus.cpp:308
bool init(uint8_t cs, bool bInit=false)
Definition sfe_bus.cpp:251
uint8_t reg