~aleteoryx/muditaos

muditaos/module-bsp/devices/power/MP2615GQ.hpp -rw-r--r-- 2.3 KiB
a405cad6Aleteoryx trim readme 7 days 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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#pragma once

#include "FreeRTOS.h"
#include "timers.h"

#include <drivers/gpio/DriverGPIO.hpp>

#include <functional>

namespace bsp::devices::power
{
    /// MP2615GQ charger driver
    /// To enable interrupts support call \ref enable_irq and then invoke \ref handle_irq in the corresponding IRQ
    /// routine.
    class MP2615GQ
    {
      public:
        enum class ChargingStatus : std::uint8_t
        {
            Discharging,
            Charging,
            Complete,
            PluggedNotCharging, /// Charger plugged-in, input voltage valid but charging explicitly disabled
            Error,
        };

        struct Configuration
        {
            std::shared_ptr<drivers::DriverGPIO> mode_gpio;
            uint32_t mode_pin;

            std::shared_ptr<drivers::DriverGPIO> acok_gpio;
            uint32_t acok_pin;

            std::shared_ptr<drivers::DriverGPIO> chgok_gpio;
            uint32_t chgok_pin;

            /// User defined notification handler called when the driver wants to inform that charging state has
            /// changed. Notifications are called from the software timer context hence it is not allowed to perform
            /// blocking or memory demanding operations from it.
            std::function<void(const ChargingStatus)> notification;
        };

        explicit MP2615GQ(const Configuration &conf);
        ~MP2615GQ();

        MP2615GQ(const MP2615GQ &) = delete;
        MP2615GQ &operator=(const MP2615GQ &) = delete;
        MP2615GQ(MP2615GQ &&)                 = default;
        MP2615GQ &operator=(MP2615GQ &&) = delete;

        ChargingStatus get_charge_status();
        void enable_charging(bool ctrl);

        void enable_irq();
        void handle_irq();

      private:
        [[nodiscard]] bool is_charging() const;
        [[nodiscard]] bool is_valid_voltage() const;
        [[nodiscard]] bool is_charging_enabled() const;

        static constexpr auto irq_filter_timer_interval_ms    = 50;
        static constexpr auto irq_filter_timer_name           = "irq_charger_filter";
        static constexpr auto irq_filter_timer_cmd_timeout_ms = 100;

        Configuration configuration;
        xTimerHandle irq_filter_timer;
    };
} // namespace bsp::devices::power