~aleteoryx/muditaos

f888c75f43d13d6d2887c6a02cd424907c0dd8fd — Dawid Wojtas 3 years ago 839d6f3
[MOS-589] Fix torch driver

Perform turn-off torch in only one place.
Before writing or reading i2c make sure the torch is power supplied.
Change old C-style cast to reinterpret_cast.
M module-bsp/board/rt1051/bsp/torch/AL3644TT.hpp => module-bsp/board/rt1051/bsp/torch/AL3644TT.hpp +6 -3
@@ 1,4 1,4 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once


@@ 30,7 30,7 @@ typedef struct
{
    al3644tt_brightness_code brightness_code : 7;
    uint8_t led2_brightness_override : 1;
} al3644tt_led1_torch_brightness;
} al3644tt_led1_torch_brightness_reg;

constexpr bool AL3644TT_LED1_TORCH_BRIGHTNESS_OVERRIDE      = 0b1;
constexpr auto AL3644TT_LED1_TORCH_BRIGHTNESS_DONT_OVERRIDE = 0b0;


@@ 79,8 79,11 @@ typedef struct
{
    uint8_t silicon_rev : 3;
    uint8_t device_id : 3;
} al3644tt_device_id;
} al3644tt_device_id_reg;

constexpr auto AL3644TT_ID    = 0b000;
constexpr auto AL3644TT_REV_1 = 0b100;
constexpr auto AL3644TT_REV_2 = 0b010;

constexpr auto AL3644TT_ENABLE  = 1;
constexpr auto AL3644TT_DISABLE = 0;

M module-bsp/board/rt1051/bsp/torch/torch.cpp => module-bsp/board/rt1051/bsp/torch/torch.cpp +63 -33
@@ 19,7 19,8 @@ namespace bsp

    namespace torch
    {
        static xQueueHandle qHandleIrq = NULL;
        static xQueueHandle qHandleIrq = nullptr;
        static constexpr auto delayMs  = 5;

        static I2CAddress addr = {.deviceAddress = 0x63, .subAddress = 0, .subAddressSize = 1};



@@ 27,11 28,44 @@ namespace bsp
        const unsigned short max_current_mA = 150;
        ColourTemperature currentColourTemp = ColourTemperature::warmest;

        void powerOnTorch()
        {
            gpio->WritePin(static_cast<std::uint32_t>(BoardDefinitions::TORCH_DRIVER_EN), AL3644TT_ENABLE);
        }

        void powerOffTorch()
        {
            gpio->WritePin(static_cast<std::uint32_t>(BoardDefinitions::TORCH_DRIVER_EN), AL3644TT_DISABLE);
        }

        void enablePowerSupply()
        {
            // Make sure torch power is supplied to write or read i2c message and wait a bit
            if (getState() == State::off) {
                powerOnTorch();
                vTaskDelay(pdMS_TO_TICKS(delayMs));
            }
        }

        bool write(const drivers::I2CAddress &addr, const uint8_t *txBuff, const size_t size)
        {
            enablePowerSupply();
            auto writeBytes = i2c->Write(addr, txBuff, size);
            return writeBytes == size;
        }

        bool read(const drivers::I2CAddress &addr, uint8_t *rxBuff, const size_t size)
        {
            enablePowerSupply();
            auto readBytes = i2c->Read(addr, rxBuff, size);
            return readBytes == size;
        }

        int32_t init(xQueueHandle qHandle)
        {
            i2c = DriverI2C::Create(
                static_cast<I2CInstances>(BoardDefinitions::TORCH_DRIVER_I2C),
                DriverI2CParams{.baudrate = static_cast<uint32_t>(BoardDefinitions::TORCH_DRIVER_I2C_BAUDRATE)});
                DriverI2CParams{.baudrate = static_cast<std::uint32_t>(BoardDefinitions::TORCH_DRIVER_I2C_BAUDRATE)});

            qHandleIrq = qHandle;



@@ 42,11 76,11 @@ namespace bsp
            gpio->ConfPin(DriverGPIOPinParams{.dir      = DriverGPIOPinParams::Direction::Output,
                                              .irqMode  = DriverGPIOPinParams::InterruptMode::NoIntmode,
                                              .defLogic = 0,
                                              .pin      = static_cast<uint32_t>(BoardDefinitions::TORCH_DRIVER_EN)});
            gpio->WritePin(static_cast<uint32_t>(BoardDefinitions::TORCH_DRIVER_EN), 0);
            vTaskDelay(pdMS_TO_TICKS(5));
            gpio->WritePin(static_cast<uint32_t>(BoardDefinitions::TORCH_DRIVER_EN), 1);
            vTaskDelay(pdMS_TO_TICKS(5));
                                              .pin = static_cast<std::uint32_t>(BoardDefinitions::TORCH_DRIVER_EN)});
            powerOffTorch();
            vTaskDelay(pdMS_TO_TICKS(delayMs));
            powerOnTorch();
            vTaskDelay(pdMS_TO_TICKS(delayMs));

            auto present = isPresent();
            turn(State::off);


@@ 56,17 90,17 @@ namespace bsp

        void deinit()
        {
            qHandleIrq = NULL;
            qHandleIrq = nullptr;
            turn(State::off);
        }

        bool isPresent(void)
        {
            al3644tt_device_id id;
            al3644tt_device_id_reg id;
            addr.subAddress = AL3644TT_DEVICE_ID_REG;
            auto read       = i2c->Read(addr, (uint8_t *)(&id), 1);
            auto status     = read(addr, reinterpret_cast<std::uint8_t *>(&id), sizeof(al3644tt_device_id_reg));

            if (read != 1) {
            if (!status) {
                return false;
            }



@@ 82,20 116,18 @@ namespace bsp
        {
            // set the same current for both channels
            addr.subAddress = AL3644TT_LED1_TORCH_BRIGHTNESS_REG;
            al3644tt_led1_torch_brightness led1_brightness{
            al3644tt_led1_torch_brightness_reg led1_brightness{
                .brightness_code          = al3644tt_current_convert(mA > max_current_mA ? max_current_mA : mA),
                .led2_brightness_override = AL3644TT_LED1_TORCH_BRIGHTNESS_OVERRIDE,
            };
            auto wrote = i2c->Write(addr, (uint8_t *)(&led1_brightness), 1);
            if (wrote != 1) {
                return false;
            }
            return true;
        };
            return write(
                addr, reinterpret_cast<std::uint8_t *>(&led1_brightness), sizeof(al3644tt_led1_torch_brightness_reg));
        }

        bool turn(State state, ColourTemperature colourTemp)
        {
            if (state == State::on) {
                gpio->WritePin(static_cast<uint32_t>(BoardDefinitions::TORCH_DRIVER_EN), 1);
                powerOnTorch();
                setCurrent(max_current_mA);
            }



@@ 105,33 137,31 @@ namespace bsp

            addr.subAddress = AL3644TT_ENABLE_REG;
            al3644tt_enable_reg en_reg{
                .led1_en = static_cast<uint8_t>(currentColourTemp == ColourTemperature::warmest && state == State::on
                                                    ? AL3644TT_LED_ENABLED
                                                    : AL3644TT_LED_DISABLED),
                .led2_en = static_cast<uint8_t>(currentColourTemp == ColourTemperature::coldest && state == State::on
                                                    ? AL3644TT_LED_ENABLED
                                                    : AL3644TT_LED_DISABLED),
                .mode    = 0b10,
                .led1_en = static_cast<std::uint8_t>(
                    currentColourTemp == ColourTemperature::warmest && state == State::on ? AL3644TT_LED_ENABLED
                                                                                          : AL3644TT_LED_DISABLED),
                .led2_en = static_cast<std::uint8_t>(
                    currentColourTemp == ColourTemperature::coldest && state == State::on ? AL3644TT_LED_ENABLED
                                                                                          : AL3644TT_LED_DISABLED),
                .mode          = 0b10,
                .torch_temp_en = 0,
                .strobe_en     = 0,
                .strobe_type   = 0,
                .tx_pin_en     = 0,
            };
            auto wrote = i2c->Write(addr, (uint8_t *)(&en_reg), 1);

            auto status = write(addr, reinterpret_cast<std::uint8_t *>(&en_reg), sizeof(al3644tt_enable_reg));

            if (state == State::off) {
                gpio->WritePin(static_cast<uint32_t>(BoardDefinitions::TORCH_DRIVER_EN), 0);
                powerOffTorch();
            }

            if (wrote != 1) {
                return false;
            }
            return true;
            return status;
        }

        State getState()
        {
            auto read = gpio->ReadPin(static_cast<uint32_t>(BoardDefinitions::TORCH_DRIVER_EN));
            auto read = gpio->ReadPin(static_cast<std::uint32_t>(BoardDefinitions::TORCH_DRIVER_EN));
            return read ? State::on : State::off;
        }


M products/PurePhone/services/evtmgr/EventManager.cpp => products/PurePhone/services/evtmgr/EventManager.cpp +1 -3
@@ 129,9 129,7 @@ void EventManager::toggleTorchColor()
}

void EventManager::ProcessCloseReason(sys::CloseReason closeReason)
{
    bsp::torch::turn(bsp::torch::State::off);
}
{}

sys::MessagePointer EventManager::DataReceivedHandler(sys::DataMessage *msgl, sys::ResponseMessage *resp)
{