NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
i2c_new.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef I2C_I2C_NEW_H_
20 #define I2C_I2C_NEW_H_
21 
22 #include <cstdint>
23 #include <cstddef>
24 #include <memory>
25 #include <mutex>
26 #include <string>
27 
28 namespace i2c {
29 
30 using Error = int;
31 using Address = std::uint16_t;
32 
33 class Device;
34 
35 // Represents an i2c bus.
36 // Holds an open file descriptor that is automatically closed on
37 // destruction (plus holds a mutex), thus non-copyable and non-movable.
38 // This class *should* be thread-safe.
39 class Bus {
40  public:
41  using Ptr = std::shared_ptr<Bus>;
42  using WeakPtr = std::weak_ptr<Bus>;
43 
44  Bus(Bus const& other) = delete;
45  Bus(Bus &&other) = delete;
46  Bus& operator=(Bus const& other) = delete;
47  Bus& operator=(Bus &&other) = delete;
48  ~Bus();
49 
50  Device DeviceAt(const Address addr) const noexcept;
51 
52  int Write(const Address addr,
53  const std::uint8_t* data, const std::size_t size,
54  Error* ec);
55 
56  int Read(const Address addr,
57  std::uint8_t *data, const std::size_t size,
58  Error* ec);
59  int ReadRegister(const Address addr, const std::uint8_t reg,
60  std::uint8_t *data, const std::size_t size,
61  Error *ec);
62 
63  void SetRetries(const int retries) noexcept;
64 
65  private:
66  explicit Bus(int fd);
67 
68  bool SetAddress(const Address addr) noexcept;
69 
70  std::mutex mtx_;
71  const int fd_;
72  Address current_addr_ = -1;
73 
74  WeakPtr wptr_;
75 
76  friend Bus::Ptr Open(std::string const&, Error*);
77 };
78 
79 Bus::Ptr Open(std::string const &dev, Error* err);
80 
81 // Reperesents a device at a certain address on an i2c bus.
82 class Device {
83  public:
84  Device(Bus::Ptr bus, const Address addr);
85  Device() = default;
86 
87  explicit operator bool() const noexcept;
88 
89  int Write(const std::uint8_t* data, const std::size_t size,
90  Error* ec);
91  int Write(const std::uint8_t data, Error* ec);
92 
93  int WriteRegister(const std::uint8_t reg, const std::uint8_t data,
94  Error* ec);
95  int WriteRegister(const std::uint8_t reg,
96  const std::uint8_t *data, const std::size_t size,
97  Error* ec);
98 
99  int Read(std::uint8_t *data, const std::size_t size,
100  Error* ec);
101  int ReadRegister(const std::uint8_t reg,
102  std::uint8_t *data, const std::size_t size,
103  Error *ec);
104 
105  i2c::Address addr() const noexcept;
106 
107  const Bus::Ptr& bus() const noexcept;
108 
109  private:
110  const Address addr_ = -1;
111  const Bus::Ptr bus_;
112 };
113 
114 } // end namespace i2c
115 
116 #endif // I2C_I2C_NEW_H_
i2c::Device::Write
int Write(const std::uint8_t *data, const std::size_t size, Error *ec)
Definition: i2c_new.cc:195
i2c::Bus::SetRetries
void SetRetries(const int retries) noexcept
Definition: i2c_new.cc:171
i2c::Bus::Bus
Bus(Bus const &other)=delete
i2c::Address
std::uint16_t Address
Definition: i2c_new.h:31
i2c::Device::Device
Device()=default
i2c::Bus::Read
int Read(const Address addr, std::uint8_t *data, const std::size_t size, Error *ec)
Definition: i2c_new.cc:115
i2c::Bus::Ptr
std::shared_ptr< Bus > Ptr
Definition: i2c_new.h:41
i2c::Bus::DeviceAt
Device DeviceAt(const Address addr) const noexcept
Definition: i2c_new.cc:78
i2c::Device::Read
int Read(std::uint8_t *data, const std::size_t size, Error *ec)
Definition: i2c_new.cc:241
i2c::Bus
Definition: i2c_new.h:39
i2c::Bus::Open
friend Bus::Ptr Open(std::string const &, Error *)
i2c::Open
Bus::Ptr Open(std::string const &dev, Error *err)
Definition: i2c_new.cc:46
i2c::Device::WriteRegister
int WriteRegister(const std::uint8_t reg, const std::uint8_t data, Error *ec)
Definition: i2c_new.cc:204
i2c::Device::ReadRegister
int ReadRegister(const std::uint8_t reg, std::uint8_t *data, const std::size_t size, Error *ec)
Definition: i2c_new.cc:245
i2c::Bus::Write
int Write(const Address addr, const std::uint8_t *data, const std::size_t size, Error *ec)
Definition: i2c_new.cc:92
i2c::Device::addr
i2c::Address addr() const noexcept
Definition: i2c_new.cc:187
i2c
Definition: i2c_new.h:28
i2c::Bus::operator=
Bus & operator=(Bus const &other)=delete
i2c::Error
int Error
Definition: i2c_new.h:30
i2c::Device
Definition: i2c_new.h:82
i2c::Bus::ReadRegister
int ReadRegister(const Address addr, const std::uint8_t reg, std::uint8_t *data, const std::size_t size, Error *ec)
Definition: i2c_new.cc:138
i2c::Device::bus
const Bus::Ptr & bus() const noexcept
Definition: i2c_new.cc:191
i2c::Bus::~Bus
~Bus()
Definition: i2c_new.cc:87
i2c::Bus::WeakPtr
std::weak_ptr< Bus > WeakPtr
Definition: i2c_new.h:42