~aleteoryx/muditaos

ref: f554354452aeb966ccd0e96013b9b5339cc9e584 muditaos/module-bsp/board/linux/battery-charger/battery_charger.cpp -rw-r--r-- 3.9 KiB
f5543544 — Marcin Smoczyński [EGD-5344] Fix commit subject check 5 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

/*
 * battery_charger.cpp
 *
 *  Created on: Jul 1, 2019
 *      Author: kuba
 */
#include <stdint.h>

extern "C"
{
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
}

#include <sys/stat.h>
#include <fcntl.h>
#include <module-utils/common_data/EventStore.hpp>

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

#define BSP_BATTERY_CHARGER_I2C_ADDR (0xD2 >> 1)
#define BSP_FUEL_GAUGE_I2C_ADDR      (0x6C >> 1)
#define BSP_TOP_CONTROLLER_I2C_ADDR  (0xCC >> 1)

static xQueueHandle qHandleIrq            = NULL;
static TaskHandle_t battery_worker_handle = NULL;

static uint8_t battLevel = 100;
static bool plugged      = false;
namespace bsp
{

    static void battery_worker(void *pvp);

    int battery_Init(xQueueHandle qHandle)
    {
        qHandleIrq = qHandle;
        if (xTaskCreate(battery_worker, "battery", 512, qHandle, 0, &battery_worker_handle) != pdPASS) {
            return 1;
        }
        Store::Battery::modify().level = battLevel;
        return 0;
    }

    void battery_Deinit(void)
    {
        qHandleIrq = NULL;
        vTaskDelete(battery_worker_handle);
    }
    void battery_getBatteryLevel(uint8_t &levelPercent)
    {
        levelPercent                   = battLevel;
        Store::Battery::modify().level = battLevel;
    }

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

    // TODO function unused in linux driver, left for compatibility with target driver
    void battery_ClearAllIRQs(void)
    {}
    // TODO function unused in linux driver, left for compatibility with target driver
    void battery_clearFuelGuageIRQ(void)
    {}

    static void battery_worker(void *pvp)
    {

        const char *myfifo = "/tmp/fifoBattKeys";

        // Creating the named file(FIFO)
        // mkfifo(<pathname>, <permission>)
        mkfifo(myfifo, 0666);

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

        while (1) {
            uint8_t buff[10];
            int32_t readedBytes = read(fd, buff, 10);

            if (readedBytes > 0) {

                uint8_t notification = 0;
                switch (buff[0]) {
                case 'p':
                    notification = static_cast<uint8_t>(bsp::batteryIRQSource::INOKB);
                    plugged      = 1 - plugged;
                    break;
                case ']':
                    notification = static_cast<uint8_t>(bsp::batteryIRQSource::INTB);
                    if (battLevel < 100)
                        battLevel++;
                    else {
                        // second 100% in a row
                        if (plugged && Store::Battery::get().level == 100) {
                            Store::Battery::modify().state = Store::Battery::State::PluggedNotCharging;
                        }
                    }
                    break;
                case '[':
                    notification = static_cast<uint8_t>(bsp::batteryIRQSource::INTB);
                    if (battLevel >= 1)
                        battLevel--;
                    if (plugged && Store::Battery::get().level == 100) {
                        // charging but not 100% anymore
                        Store::Battery::modify().state = Store::Battery::State::Charging;
                    }
                    break;
                }
                xQueueSend(qHandleIrq, &notification, 100);
            }
            vTaskDelay(50);
        }
    }

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

} // namespace bsp