~aleteoryx/muditaos

ref: 14f78ffc0757589530c7cf95132e0b40246ed2f1 muditaos/module-bsp/board/rt1051/bellpx/hal/temperature_source/TemperatureSource.cpp -rw-r--r-- 2.5 KiB
14f78ffc — Mateusz Piesta [BH-1552] Harmony crashes during startup 3 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
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "hal/temperature_source/TemperatureSource.hpp"
#include "hal/GenericFactory.hpp"
#include "devices/temperature/CT7117.hpp"
#include "drivers/gpio/DriverGPIO.hpp"
#include "board/BoardDefinitions.hpp"

#include <stdexcept>

namespace hal::temperature
{
    using namespace drivers;

    class PowerEnable
    {
      public:
        PowerEnable()
        {
            power_pin = DriverGPIO::Create(static_cast<GPIOInstances>(BoardDefinitions::BELL_TEMP_SENSOR_PWR_GPIO),
                                           DriverGPIOParams{});

            power_pin->ConfPin(
                DriverGPIOPinParams{.dir      = DriverGPIOPinParams::Direction::Output,
                                    .irqMode  = DriverGPIOPinParams::InterruptMode::NoIntmode,
                                    .defLogic = 1,
                                    .pin      = static_cast<uint32_t>(BoardDefinitions::BELL_TEMP_SENSOR_PWR_PIN)});

            power_pin->WritePin(static_cast<uint32_t>(BoardDefinitions::BELL_TEMP_SENSOR_PWR_PIN), 1);
        }
        ~PowerEnable()
        {
            power_pin->WritePin(static_cast<uint32_t>(BoardDefinitions::BELL_TEMP_SENSOR_PWR_PIN), 0);
        }

      private:
        std::shared_ptr<drivers::DriverGPIO> power_pin;
    };

    class BellTemperatureSource : public AbstractTemperatureSource
    {
      public:
        BellTemperatureSource()
            : i2c{drivers::DriverI2C::Create(
                  static_cast<drivers::I2CInstances>(BoardDefinitions::BELL_TEMP_SENSOR_I2C),
                  {static_cast<std::uint32_t>(BoardDefinitions::BELL_TEMP_SENSOR_I2C_BAUDRATE)})},
              ct7117{ct7117_id, *i2c}
        {
            if (not ct7117.poll()) {
                throw std::runtime_error("CT7117 chip not present");
            }
            if (not ct7117.wakeup()) {
                throw std::runtime_error("Waking up CT7117 failed");
            }
        }
        Result read()
        {
            return ct7117.get_temperature();
        }

      private:
        static constexpr auto ct7117_id = (0x90 >> 1);
        PowerEnable power_enable;
        std::shared_ptr<drivers::DriverI2C> i2c;
        bsp::devices::temperature::CT7117::CT7117 ct7117;
    };

    std::shared_ptr<AbstractTemperatureSource> AbstractTemperatureSource::Factory::create()
    {
        return hal::impl::factory<BellTemperatureSource, AbstractTemperatureSource>();
    }
} // namespace hal::temperature