~aleteoryx/muditaos

ref: 70d3ba4c03655d7b1e90cbeab88f116bd892e600 muditaos/module-bsp/board/rt1051/bsp/pit/pit.cpp -rw-r--r-- 2.1 KiB
70d3ba4c — Dawid Wojtas [BH-1935] Fix the backlight in pre-wake up 1 year, 10 months 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "pit.hpp"
#include <board.h>
#include <board/clock_config.h>
#include <fsl_pit.h>
#include <board/pin_mux.h>
#include <stdint.h>

struct PIT_t
{
    uint32_t usec        = 1000000;
    xQueueHandle qhandle = nullptr;
    std::function<void()> irq_cb;
};

static PIT_t LPIT;

static void pit_init(xQueueHandle qhandle);
static void pit_start(uint32_t usec, std::function<void(void)> cb);
static void pit_stop();

namespace bsp
{
    namespace pit
    {
        void init(xQueueHandle qhandle)
        {
            pit_init(qhandle);
        }

        void start(uint32_t usec, std::function<void(void)> cb)
        {
            pit_start(usec, cb);
        }

        void stop()
        {
            pit_stop();
        }

    }; // namespace pit
};     // namespace bsp

/// bsp

static void pit_init(xQueueHandle qhandle)
{
    LPIT.qhandle = qhandle;
    pit_config_t pitConfig;
    PIT_GetDefaultConfig(&pitConfig);
    PIT_Init(PIT, &pitConfig);
}

static void pit_start(uint32_t usec, std::function<void(void)> cb)
{
    LPIT.usec = usec;
    DisableIRQ(PIT_IRQn);
    PIT_DisableInterrupts(PIT, kPIT_Chnl_0, kPIT_TimerInterruptEnable);

    PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, USEC_TO_COUNT(usec, CLOCK_GetFreq(kCLOCK_OscClk)));
    LPIT.irq_cb = cb;

    PIT_EnableInterrupts(PIT, kPIT_Chnl_0, kPIT_TimerInterruptEnable);
    EnableIRQ(PIT_IRQn);

    PIT_StartTimer(PIT, kPIT_Chnl_0);
}

static void pit_stop()
{
    PIT_StopTimer(PIT, kPIT_Chnl_0);
}

extern "C"
{
    void PIT_IRQHandler(void)
    {
        BaseType_t xHigherPriorityTaskWoken = pdFALSE;
        if (LPIT.qhandle) {
            auto val = bsp::pit::Event::Overflow;
            xQueueSendFromISR(LPIT.qhandle, &val, &xHigherPriorityTaskWoken);
        }
        PIT_ClearStatusFlags(PIT, kPIT_Chnl_0, kPIT_TimerFlag);
        __DSB();
        /// stop timer - we are interested in one time run
        pit_stop();
        if (LPIT.irq_cb)
            LPIT.irq_cb();
        portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);
    }
};