Line data Source code
1 : /* File: i2c_slave.h 2 : * This file is part of the program hardiocore 3 : * Program description : Abstract low level I/O removing direct hardware 4 : * implementation Copyright (C) 2018-2020 - Robin Passama (CNRS / LIRMM) 5 : * Clement Rebut (EPITA / LIRMM) Charles Villard (EPITA / LIRMM). All Right 6 : * reserved. 7 : * 8 : * This software is free software: you can redistribute it and/or modify 9 : * it under the terms of the CeCILL-C license as published by 10 : * the CEA CNRS INRIA, either version 1 11 : * of the License, or (at your option) any later version. 12 : * This software is distributed in the hope that it will be useful, 13 : * but WITHOUT ANY WARRANTY without even the implied warranty of 14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 : * CeCILL-C License for more details. 16 : * 17 : * You should have received a copy of the CeCILL-C License 18 : * along with this software. If not, it can be found on the official 19 : * website of the CeCILL licenses family (http://www.cecill.info/index.en.html). 20 : */ 21 : /** 22 : * @file interfaces/i2c_slave.h 23 : * @author Robin Passama 24 : * @brief header file for I2CSlaveInterface class. 25 : * @date May 2025 26 : * @ingroup core 27 : */ 28 : #pragma once 29 : 30 : #include <hardio/common/interface.h> 31 : #include <hardio/protocols/i2c.h> 32 : 33 : namespace hardio { 34 : 35 : /** 36 : * @brief Interface for accessing a I2C slave device 37 : * 38 : */ 39 : class I2CSlaveInterface : public ProtocolInterface<I2CProtocol> { 40 : public: 41 : using base_type = I2CSlaveInterface; 42 : I2CSlaveInterface(size_t slave_addr); 43 : 44 : /** 45 : * @brief Get slave id 46 : * 47 : * @return strng representing the id of the slave 48 : */ 49 : std::string id() const override; 50 : 51 0 : virtual ~I2CSlaveInterface() = default; 52 : 53 : /** 54 : * @brief Write a single byte to the I2C slave device. 55 : * @details Used is for simple devices that have a unique implicit register 56 : * @param[in] data The byte value 57 : * @return true on success, false otherwise. 58 : */ 59 : bool write(uint8_t data); 60 : 61 : /** 62 : * @brief Read a single byte from a I2C slave device. 63 : * @details Used is for simple devices that have a unique implicit register 64 : * @param[out] data The byte value read 65 : * @return true on success, false otherwise. 66 : */ 67 : bool read(uint8_t* data); 68 : 69 : /** 70 : * @brief Write a single byte to a register of the I2C slave device. 71 : * 72 : * @param[in] reg The register of the device to write to 73 : * @param[in] data The byte value to write 74 : * @return true on success, false otherwise. 75 : */ 76 : bool write(uint8_t reg, uint8_t data); 77 : 78 : /** 79 : * @brief Read a single byte from a register of the I2C slave device. 80 : * 81 : * @param[in] reg The register of the device to read from 82 : * @param[out] data The byte value read 83 : * @return true on success, false otherwise. 84 : */ 85 : bool read(uint8_t reg, uint8_t* data); 86 : 87 : /** 88 : * @brief Write a set of bytes to the I2C slave device. 89 : * @param[in] reg The register of the device to write to 90 : * @param[in] length number of bytes to write 91 : * @param[in] data The set of bytes to write 92 : * @return true on success, false otherwise. 93 : */ 94 : bool write(uint8_t reg, uint8_t length, const uint8_t* data); 95 : 96 : /** 97 : * @brief Read a set of bytes from the I2C slave device. 98 : * @param[in] reg The register of the device to read in 99 : * @param[in] length number of bytes to read 100 : * @param[out] data The buffer containing the read bytes 101 : * @param[out] read_data The number of bytes effectively read 102 : * @return true on success, false otherwise. 103 : */ 104 : bool read(uint8_t reg, uint8_t length, uint8_t* data, size_t& read_data); 105 : 106 : /** 107 : * @brief Get slave I2C address 108 : * 109 : * @return the address 110 : */ 111 : size_t address() const; 112 : 113 : private: 114 : size_t i2c_addr_; 115 : }; 116 : 117 : } // namespace hardio