Line data Source code
1 : #pragma once 2 : 3 : #include "SIM_I2C.h" 4 : 5 : #include <SITL/SIM_Aircraft.h> 6 : #include <AP_HAL/utility/sparse-endian.h> 7 : #include <AP_Common/Bitmask.h> 8 : 9 : namespace SITL { 10 : 11 : class I2CRegEnum { 12 : // a class to hold register addresses as an enumeration 13 : }; 14 : 15 : class I2CRegisters { 16 : 17 : public: 18 : 19 : enum class RegMode { 20 : RDONLY = 11, 21 : WRONLY = 22, 22 : RDWR = 33, 23 : }; 24 : 25 : protected: 26 : 27 : virtual int rdwr(I2C::i2c_rdwr_ioctl_data *&data) = 0; 28 : 29 : void add_register(const char *name, uint8_t reg, RegMode mode); 30 : 31 : const char *regname[256]; 32 : Bitmask<256> writable_registers; 33 : Bitmask<256> readable_registers; 34 : 35 2094 : void set_debug(bool value) { debug = value; } 36 : bool get_debug() const { return debug; } 37 : 38 : private: 39 : bool debug; 40 : }; 41 : 42 : class I2CRegisters_ConfigurableLength : public I2CRegisters { 43 : public: 44 : void add_register(const char *name, uint8_t reg, uint8_t len, RegMode mode); 45 : int rdwr(I2C::i2c_rdwr_ioctl_data *&data) override; 46 : void set_register(uint8_t reg, uint8_t *data, uint8_t len); 47 : void set_register(uint8_t reg, uint16_t value); 48 : void set_register(uint8_t reg, int16_t value); 49 : void set_register(uint8_t reg, uint8_t value); 50 : void set_register(uint8_t reg, int8_t value); 51 : 52 : // void get_reg_value(uint8_t reg, int8_t &value) const; 53 : void get_reg_value(uint8_t reg, uint8_t &value) const; 54 : // void get_reg_value(uint8_t reg, int16_t &value) const; 55 : // void get_reg_value(uint8_t reg, uint16_t &value) const; 56 : // void get_reg_value(uint8_t reg, uint8_t *value, uint8_t len) const; 57 : 58 : protected: 59 : 60 : uint32_t reg_data[256]; // OK, so not *that* configurable ATM.... 61 : uint8_t reg_data_len[256]; 62 : }; 63 : 64 : class I2CRegisters_16Bit : public I2CRegisters { 65 : public: 66 : int rdwr(I2C::i2c_rdwr_ioctl_data *&data) override; 67 : void set_register(uint8_t reg, uint16_t value); 68 : void set_register(uint8_t reg, int16_t value); 69 : 70 147797 : uint16_t get_reg_value(uint8_t reg) { 71 147797 : return be16toh(word[reg]); 72 : } 73 : 74 : protected: 75 : 76 : uint16_t word[256]; 77 : }; 78 : 79 : class I2CRegisters_8Bit : public I2CRegisters { 80 : public: 81 : int rdwr(I2C::i2c_rdwr_ioctl_data *&data) override; 82 : void set_register(uint8_t reg, uint8_t value); 83 : void set_register(uint8_t reg, int8_t value); 84 : 85 1968843560 : uint8_t get_register(uint8_t num) { 86 1968843560 : return byte[(uint8_t)num]; 87 : } 88 : 89 : // dies if register does not have value value 90 : void assert_register_value(uint8_t reg, uint8_t value); 91 : 92 : protected: 93 : 94 : // this method is called by rdwr to store values into a register 95 3078242 : virtual void rdwr_store_register_value(uint8_t reg, uint8_t value) { 96 3078242 : byte[reg] = value; 97 3078242 : } 98 : 99 : uint8_t byte[256]; 100 : }; 101 : 102 : /* 103 : for devices that take a command then will provide data to a read(); 104 : for example, MCU writes 0x07 to device then expects to be able to 105 : read 2 bytes from it. 106 : */ 107 : class I2CCommandResponseDevice { 108 : public: 109 : int rdwr(I2C::i2c_rdwr_ioctl_data *&data); 110 : 111 : protected: 112 : 113 : // time taken for device to process command: 114 4502 : uint16_t command_processing_time_ms() const { return 20; } 115 : virtual uint8_t command_take_reading() const = 0; 116 : virtual uint16_t reading() const = 0; 117 : 118 : uint32_t cmd_take_reading_received_ms; 119 : }; 120 : 121 : class I2CDevice { 122 : public: 123 31410 : virtual void init() {} 124 : 125 536279826 : virtual void update(const class Aircraft &aircraft) { } 126 : 127 : virtual int rdwr(I2C::i2c_rdwr_ioctl_data *&data) = 0; 128 : }; 129 : 130 : } // namespace SITL