JeVois  1.22
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
IMUi2c.C
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2020 by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
18#include <jevois/Core/IMUi2c.H>
20#include <jevois/Core/Camera.H>
21#include <jevois/Util/Utils.H>
22#include <jevois/Debug/Log.H>
23
24namespace
25{
26 struct jevois_data
27 {
28 unsigned char addr;
29 unsigned char size;
30 unsigned char data[32];
31 };
32}
33
34// ####################################################################################################
35jevois::IMUi2c::IMUi2c(std::shared_ptr<Camera> cam) :
36 jevois::IMU(), itsCam(cam), itsIMUbank(0xff)
37{ }
38
39// ####################################################################################################
42
43// ####################################################################################################
45{ return false; }
46
47// ####################################################################################################
48void jevois::IMUi2c::selectBank(int fd, unsigned short reg)
49{
50 uint8_t const bank = (reg >> 7) & 0x03;
51 if (itsIMUbank == bank) return;
52
53 unsigned short data[2] = { ICM20948_REG_BANK_SEL, static_cast<unsigned short>(bank << 4) };
54
55 LDEBUG("Writing 0x" << std::hex << data[1] << " to 0x" << data[0]);
56 XIOCTL(fd, _IOW('V', 194, int), data);
57
58 itsIMUbank = bank;
59}
60
61// ##############################################################################################################
62void jevois::IMUi2c::writeRegister(unsigned short reg, unsigned char val)
63{
64 int fd = itsCam->lock();
65 try
66 {
67 selectBank(fd, reg);
68
69 unsigned short data[2] = { static_cast<unsigned short>(reg & 0x7f), val };
70
71 LDEBUG("Writing 0x" << std::hex << val << " to 0x" << reg);
72 XIOCTL(fd, _IOW('V', 194, int), data);
73 } catch (...) { }
74
75 itsCam->unlock();
76}
77
78// ##############################################################################################################
79unsigned char jevois::IMUi2c::readRegister(unsigned short reg)
80{
81 int fd = itsCam->lock();
82 unsigned short data[2] = { static_cast<unsigned short>(reg & 0x7f), 0 };
83 try
84 {
85 selectBank(fd, reg);
86
87 XIOCTL(fd, _IOWR('V', 195, int), data);
88 LDEBUG("Register 0x" << std::hex << reg << " has value 0x" << data[1]);
89 } catch (...) { }
90
91 itsCam->unlock();
92 return data[1];
93}
94
95// ##############################################################################################################
96void jevois::IMUi2c::writeRegisterArray(unsigned short reg, unsigned char const * vals, size_t num)
97{
98 if (num > 32) LFATAL("Maximum allowed size is 32 bytes. You must break down larger transfers into 32 byte chunks.");
99
100 int fd = itsCam->lock();
101 try
102 {
103 selectBank(fd, reg);
104
105 static jevois_data d;
106 d.addr = reg & 0x7f;
107 d.size = num;
108 memcpy(d.data, vals, num);
109
110 LDEBUG("Writing " << num << " values to 0x"<< std::hex << reg);
111 XIOCTL(fd, _IOW('V', 196, struct jevois_data), &d);
112 } catch (...) { }
113
114 itsCam->unlock();
115}
116
117// ##############################################################################################################
118void jevois::IMUi2c::readRegisterArray(unsigned short reg, unsigned char * vals, size_t num)
119{
120 if (num > 32) LFATAL("Maximum allowed size is 32 bytes. You must break down larger transfers into 32 byte chunks.");
121
122 int fd = itsCam->lock();
123 try
124 {
125 selectBank(fd, reg);
126
127 static jevois_data d;
128 d.addr = reg & 0x7f;
129 d.size = num;
130
131 XIOCTL(fd, _IOWR('V', 197, struct jevois_data), &d);
132 LDEBUG("Received " << num <<" values from register 0x" << std::hex << reg);
133 memcpy(vals, d.data, num);
134 } catch (...) { }
135
136 itsCam->unlock();
137}
138
Abstract interface to an ICM20948 inertial measurement unit (IMU)
Definition IMU.H:28
virtual ~IMUi2c()
Destructor.
Definition IMUi2c.C:40
virtual bool isSPI() const override
Returns true if we use SPI for transfers. Used when ICM20948_REG_USER_CTRL is written to.
Definition IMUi2c.C:44
void readRegisterArray(unsigned short reg, unsigned char *vals, size_t num) override
Read an array of values from the camera's IMU registers.
Definition IMUi2c.C:118
unsigned char readRegister(unsigned short reg) override
Read a value from one of the camera's IMU registers.
Definition IMUi2c.C:79
void writeRegisterArray(unsigned short reg, unsigned char const *vals, size_t num) override
Write an array of values to the camera's IMU registers.
Definition IMUi2c.C:96
void writeRegister(unsigned short reg, unsigned char val) override
Write a value to one of the IMU registers.
Definition IMUi2c.C:62
IMUi2c(std::shared_ptr< Camera > cam)
Constructor.
Definition IMUi2c.C:35
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
Definition Log.H:230
#define LDEBUG(msg)
Convenience macro for users to print out console or syslog messages, DEBUG level.
Definition Log.H:173
#define XIOCTL(dev, req, mem)
Helper macro to execute an ioctl, ignore interruptions, and, if error, issue a fatal message and thro...
Definition Utils.H:211
Main namespace for all JeVois classes and functions.
Definition Concepts.dox:2