~aleteoryx/muditaos

ref: de5b923e81ed56bc845b75b2833cb040fafc3409 muditaos/module-bsp/board/linux/battery-charger/battery_charger.cpp -rw-r--r-- 6.9 KiB
de5b923e — DariuszSabala [BH-369] Turned UTF8 into separate library 4 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#include <sys/stat.h>
#include <fcntl.h>
#include <module-utils/common_data/EventStore.hpp>

#include "board.h"
#include "bsp/battery-charger/battery_charger.hpp"

namespace bsp::battery_charger
{
    namespace
    {
        xQueueHandle IRQQueueHandle         = nullptr;
        xQueueHandle DCDQueueHandle         = nullptr;
        TaskHandle_t batteryWorkerHandle    = nullptr;
        constexpr auto batteryFIFO          = "/tmp/fifoBattKeys";
        constexpr auto fifoFileAccessRights = 0666;
        constexpr auto fifoBuffSize         = 10;
        constexpr auto queueTimeoutTicks    = 100;
        constexpr auto taskDelay            = 50;

        StateOfCharge battLevel             = 100;
        constexpr StateOfCharge fullBattery = 100;
        bool plugged                        = false;
        topControllerIRQsource IRQSrc       = topControllerIRQsource::CHGR_INT;

        constexpr auto chargerPlugStateChange = 'p';
        constexpr auto batteryLevelUp         = ']';
        constexpr auto batteryLevelDown       = '[';

        constexpr auto chargerTypeDcdSDP = 'l';
        constexpr auto chargerTypeDcdCDP = ';';
        constexpr auto chargerTypeDcdDCP = '\'';

        void battery_worker(void *parameters)
        {
            mkfifo(batteryFIFO, fifoFileAccessRights);

            // Open FIFO for write only
            int fd = open(batteryFIFO, O_RDONLY | O_NONBLOCK);

            xQueueHandle targetQueueHandle = nullptr;

            while (true) {
                std::uint8_t buff[fifoBuffSize];
                std::int32_t readBytes = read(fd, buff, fifoBuffSize);

                if (readBytes > 0) {
                    std::uint8_t notification = 0;
                    switch (static_cast<char>(buff[0])) {
                    case chargerPlugStateChange:
                        notification      = static_cast<std::uint8_t>(batteryIRQSource::INTB);
                        IRQSrc            = topControllerIRQsource::CHGR_INT;
                        plugged           = !plugged;
                        targetQueueHandle = IRQQueueHandle;
                        break;
                    case batteryLevelUp:
                        notification = static_cast<std::uint8_t>(batteryIRQSource::INTB);
                        IRQSrc       = topControllerIRQsource::FG_INT;
                        if (battLevel < fullBattery)
                            battLevel++;
                        else {
                            // second 100% in a row
                            if (plugged && Store::Battery::get().level == fullBattery) {
                                Store::Battery::modify().state = Store::Battery::State::ChargingDone;
                            }
                        }
                        targetQueueHandle = IRQQueueHandle;
                        break;
                    case batteryLevelDown:
                        notification = static_cast<std::uint8_t>(batteryIRQSource::INTB);
                        IRQSrc       = topControllerIRQsource::FG_INT;
                        if (battLevel >= 1)
                            battLevel--;
                        if (plugged && Store::Battery::get().level == fullBattery) {
                            // charging but not 100% anymore
                            Store::Battery::modify().state = Store::Battery::State::Charging;
                        }
                        targetQueueHandle = IRQQueueHandle;
                        break;
                    case chargerTypeDcdSDP:
                        notification      = static_cast<std::uint8_t>(batteryChargerType::DcdSDP);
                        targetQueueHandle = DCDQueueHandle;
                        break;
                    case chargerTypeDcdCDP:
                        notification      = static_cast<std::uint8_t>(batteryChargerType::DcdCDP);
                        targetQueueHandle = DCDQueueHandle;
                        break;
                    case chargerTypeDcdDCP:
                        notification      = static_cast<std::uint8_t>(batteryChargerType::DcdDCP);
                        targetQueueHandle = DCDQueueHandle;
                        break;
                    default:
                        continue;
                    }
                    xQueueSend(targetQueueHandle, &notification, queueTimeoutTicks);
                }
                vTaskDelay(taskDelay);
            }
        }
    } // namespace

    int init(xQueueHandle irqQueueHandle, xQueueHandle dcdQueueHandle)
    {
        IRQQueueHandle = irqQueueHandle;
        DCDQueueHandle = dcdQueueHandle;

        if (xTaskCreate(battery_worker, "battery", 512, nullptr, 0, &batteryWorkerHandle) != pdPASS) {
            return 1;
        }
        Store::Battery::modify().level = battLevel;

        return 0;
    }

    void deinit()
    {
        IRQQueueHandle = nullptr;
        vTaskDelete(batteryWorkerHandle);
    }

    StateOfCharge getBatteryLevel()
    {
        Store::Battery::modify().level = battLevel;
        return battLevel;
    }

    bool getChargeStatus()
    {
        bool status = plugged;
        if (status) {
            Store::Battery::modify().state = Store::Battery::State::Charging;
        }
        else {
            Store::Battery::modify().state = Store::Battery::State::Discharging;
        }
        return status;
    }

    // TODO function unused in linux driver, left for compatibility with target driver
    void clearAllChargerIRQs()
    {}
    // TODO function unused in linux driver, left for compatibility with target driver
    void clearFuelGuageIRQ(std::uint16_t)
    {}

    std::uint16_t getStatusRegister()
    {
        return static_cast<std::uint16_t>(batteryINTBSource::SOCOnePercentChange);
    }

    std::uint8_t getTopControllerINTSource()
    {
        return static_cast<std::uint8_t>(IRQSrc);
    }

    void checkTemperatureRange()
    {}

    void setUSBCurrentLimit(batteryChargerType chargerType)
    {
        switch (chargerType) {
        case batteryChargerType::DcdTimeOut:
            [[fallthrough]];
        case batteryChargerType::DcdUnknownType:
            [[fallthrough]];
        case batteryChargerType::DcdError:
            [[fallthrough]];
        case batteryChargerType::DcdSDP:
            LOG_INFO("USB current limit set to 500mA");
            break;
        case batteryChargerType::DcdCDP:
            [[fallthrough]];
        case batteryChargerType::DcdDCP:
            LOG_INFO("USB current limit set to 1000mA");
            break;
        }
    }

    void actionIfChargerUnplugged()
    {
        if (Store::Battery::get().state == Store::Battery::State::Discharging) {
            LOG_INFO("USB unplugged");
        }
    }

    int getVoltageFilteredMeasurement()
    {
        constexpr auto nominalVoltage = 4350;
        return nominalVoltage;
    }

    BaseType_t INTB_IRQHandler()
    {
        return 1;
    }

} // namespace bsp::battery_charger