83 lines
1.9 KiB
C++
83 lines
1.9 KiB
C++
/*
|
||
* timer.cpp
|
||
*
|
||
* Created on: 10 мар. 2023 г.
|
||
* Author: sychev
|
||
*/
|
||
|
||
#include "free_rtos/timer/timer.hpp"
|
||
|
||
#include <kernel/dpl/TimerP.h>
|
||
#include <drivers/soc.h>
|
||
#include <kernel/dpl/AddrTranslateP.h>
|
||
|
||
using namespace free_rtos;
|
||
|
||
void free_rtos::timer_isr_callback(void * arg)
|
||
{
|
||
Timer * const p_timer = (Timer * const)arg;
|
||
|
||
p_timer->sem_.post();
|
||
++p_timer->tick_counter_;
|
||
|
||
TimerP_clearOverflowInt(p_timer->base_addr_);
|
||
HwiP_clearInt(p_timer->int_num_);
|
||
}
|
||
|
||
bool free_rtos::Timer::Init(Settings& sett)
|
||
{
|
||
TimerP_Params timerParams;
|
||
HwiP_Params timerHwiParams;
|
||
int32_t status;
|
||
|
||
/* set timer clock source */
|
||
SOC_controlModuleUnlockMMR(SOC_DOMAIN_ID_MAIN, 2);
|
||
*(volatile uint32_t*)AddrTranslateP_getLocalAddr(sett.clock_src_mux_addr) = 0; /// CLOCK_SRC_MCU_HFOSC0
|
||
SOC_controlModuleLockMMR(SOC_DOMAIN_ID_MAIN, 2);
|
||
|
||
base_addr_ = (uint32_t)AddrTranslateP_getLocalAddr(sett.base_address);
|
||
|
||
TimerP_Params_init(&timerParams);
|
||
timerParams.inputPreScaler = 1;
|
||
timerParams.inputClkHz = sett.input_clk_Hz;
|
||
timerParams.periodInNsec = sett.period_us * 1000;
|
||
timerParams.oneshotMode = 0;
|
||
timerParams.enableOverflowInt = 1;
|
||
timerParams.enableDmaTrigger = 0;
|
||
|
||
TimerP_setup(base_addr_, &timerParams);
|
||
|
||
int_num_ = sett.int_num;
|
||
|
||
HwiP_Params_init(&timerHwiParams);
|
||
|
||
timerHwiParams.intNum = int_num_;
|
||
timerHwiParams.callback = free_rtos::timer_isr_callback;
|
||
timerHwiParams.args = (void*)this;
|
||
timerHwiParams.isPulse = 0;
|
||
timerHwiParams.priority = sett.int_priority;
|
||
|
||
status = HwiP_construct(&hwi_obj_, &timerHwiParams);
|
||
|
||
return (status == SystemP_SUCCESS);
|
||
}
|
||
|
||
uint32_t free_rtos::Timer::Wait() {
|
||
sem_.pend();
|
||
|
||
return tick_counter_;
|
||
}
|
||
|
||
void free_rtos::Timer::Start() {
|
||
TimerP_start(base_addr_);
|
||
}
|
||
|
||
void free_rtos::Timer::Stop() {
|
||
TimerP_stop(base_addr_);
|
||
}
|
||
|
||
|
||
|
||
|
||
|