// 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 extern "C" { #include "FreeRTOS.h" #include "task.h" #include "queue.h" } #include #include #include #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(, ) 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(bsp::batteryIRQSource::INOKB); plugged = 1 - plugged; break; case ']': notification = static_cast(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(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, ¬ification, 100); } vTaskDelay(50); } } std::uint16_t battery_getStatusRegister() { return static_cast(batteryINTBSource::SOCOnePercentChange); } } // namespace bsp