peary/interfaces/I2C/i2c.hpp

Peary I2C Interface. More…

Namespaces

Name
peary
peary::interface

Classes

Name
class peary::interface::iface_i2c_config Configuration class for I2C interface.
class peary::interface::iface_i2c I2C interface via Kernel I2C module.

Detailed Description

Peary I2C Interface.

Copyright: Copyright (c) 2016-2025 CERN and the Peary Caribou authors. This software is distributed under the terms of the LGPL-3.0-only License, copied verbatim in the file “LICENSE.md”. SPDX-License-Identifier: LGPL-3.0-only

Source code

  
#pragma once

#include <vector>

#include "peary/interfaces/Interface.hpp"
#include "peary/interfaces/InterfaceManager.hpp"
#include "peary/utils/exceptions.hpp"

namespace peary::interface {

    using i2c_address_t = uint8_t;
    using i2c_t = uint8_t;
    using i2c_reg_t = uint8_t;

    class iface_i2c_config : public InterfaceConfiguration {
    public:
        iface_i2c_config(std::string const& devpath, const i2c_address_t devaddress);

        // I2C device address
        i2c_address_t _devaddress;

        using InterfaceConfiguration::operator<;
        virtual bool operator<(const iface_i2c_config& rhs) const;
    };

    class iface_i2c : public Interface<i2c_reg_t, i2c_t, iface_i2c_config> {
    public:
        ~iface_i2c() override;

    private:
        explicit iface_i2c(const configuration_type& config);

        inline void setAddress(i2c_address_t const address);

        // File descriptor for the I2C module
        int i2cDesc;

        // I2C device address
        i2c_address_t const _devAddress;

        GENERATE_FRIENDS()

    public:
        iface_i2c() = delete;

        i2c_address_t devAddress() const { return _devAddress; }

        i2c_t write(const i2c_t& data) override;

        std::pair<i2c_reg_t, i2c_t> write(const std::pair<i2c_reg_t, i2c_t>& data) override;

        dataVector_type write(const i2c_reg_t& reg, const dataVector_type& data) override;

        dataVector_type read(const unsigned int length) override;

        dataVector_type read(const i2c_reg_t& reg, const unsigned int length) override;

        // Special functions to read/write to devices with up to 16bit register
        dataVector_type wordwrite(const uint16_t& reg, const dataVector_type& data);

        dataVector_type wordread(const uint16_t reg, const unsigned int length);

        bool probe(const i2c_reg_t& reg) const;

    private:
        friend iface_i2c& InterfaceManager::getInterface<iface_i2c>(const configuration_type&);

        friend void InterfaceManager::deleteInterface<iface_i2c>(iface_i2c*);
    };

} // namespace peary::interface
  

Updated on 2025-11-14 at 11:31:23 +0100