~aleteoryx/muditaos

ref: 4b66a8974f369f4f3820a53cc348a4c3b78e01b0 muditaos/module-bsp/devices/power/CW2015.hpp -rw-r--r-- 2.4 KiB
4b66a897 — Lefucjusz [BH-1782] Add brightness fade in 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "drivers/gpio/DriverGPIO.hpp"
#include "drivers/i2c/DriverI2C.hpp"

#include <Units.hpp>

namespace bsp::devices::power
{
    /// MP2639B fuel gauge driver
    /// To enable interrupts support call \ref init_irq_pin and then invoke \ref handle_irq in the corresponding IRQ
    /// routine.
    class CW2015
    {
      public:
        enum class RetCodes : std::uint8_t
        {
            Ok,
            CommunicationError,
            ProfileInvalid,
            NotReady
        };

        CW2015(drivers::DriverI2C &i2c, units::SOC soc);
        ~CW2015();

        CW2015(const CW2015 &)     = delete;
        CW2015(CW2015 &&) noexcept = default;
        CW2015 &operator=(const CW2015 &) = delete;
        CW2015 &operator=(CW2015 &&) noexcept = delete;

        /// Returns the result of the chip initialization
        RetCodes poll() const;
        /// Returns true if the chip initialization success, otherwise false
        explicit operator bool() const;
        /// Explicitly triggers chip initialization procedure. The result can be polled by \ref poll or using bool
        /// operator
        void reinit();

        RetCodes calibrate();

        std::optional<units::SOC> get_battery_soc();

        std::optional<units::Voltage> get_battery_voltage();

        void init_irq_pin(std::shared_ptr<drivers::DriverGPIO> gpio, uint32_t pin);
        void irq_handler();

      private:
        /// Might return valid value which then can be tested for true of false
        using TriState = std::optional<bool>;

        RetCodes clear_irq();

        RetCodes init_chip();
        RetCodes reset_chip();
        RetCodes load_profile();
        RetCodes verify_profile();
        RetCodes write_profile();

        RetCodes quick_start();
        RetCodes sleep();
        RetCodes wake_up();
        RetCodes set_soc_threshold(std::uint8_t threshold);
        RetCodes set_update_flag();
        RetCodes wait_for_ready();
        TriState is_update_needed();
        TriState is_sleep_mode();

        void dumpRegisters();

        std::optional<std::uint8_t> read(std::uint8_t reg);
        bool write(std::uint8_t reg, std::uint8_t value);

        drivers::DriverI2C &i2c;
        RetCodes status{};
        units::SOC alert_threshold;
    };

} // namespace bsp::devices::power