~aleteoryx/muditaos

ref: 4b66a8974f369f4f3820a53cc348a4c3b78e01b0 muditaos/module-bsp/devices/power/MP2639B.hpp -rw-r--r-- 2.1 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
// 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 "FreeRTOS.h"
#include "timers.h"

#include <drivers/gpio/DriverGPIO.hpp>

#include <functional>

namespace bsp::devices::power
{
    /// MP2639B charger driver
    /// To enable interrupts support call \ref enable_irq and then invoke \ref handle_irq in the corresponding IRQ
    /// routine.
    class MP2639B
    {
      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 MP2639B(const Configuration &conf);
        ~MP2639B();

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

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

        void enable_irq();
        void handle_irq();

      private:
        bool is_charging() const;
        bool is_valid_voltage() const;
        bool is_charging_enabled() const;
        Configuration configuration;
        xTimerHandle irq_filter_timer;
    };

} // namespace bsp::devices::power