2019-12-01 22:57:02 +01:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017-2019 by Löwenware Ltd
|
|
|
|
* Please, refer LICENSE file for legal information
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file timer.c
|
|
|
|
* @author Ilja Kartašov <ik@lowenware.com>
|
|
|
|
* @brief
|
|
|
|
*
|
|
|
|
* @see https://lowenware.com/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <aarch64/aarch64.h>
|
2020-01-17 10:13:01 +01:00
|
|
|
#include <drivers/soc/bcm2837/bcm2837.h>
|
|
|
|
#include <leos/log.h>
|
2019-12-01 22:57:02 +01:00
|
|
|
#include "timer.h"
|
|
|
|
|
2020-01-29 12:01:32 +01:00
|
|
|
#if CONFIG_ARM_TIMER == 1
|
|
|
|
const unsigned int m_interval = 500000000;
|
|
|
|
#else
|
|
|
|
const unsigned int m_interval = 200000;
|
2020-01-17 10:13:01 +01:00
|
|
|
#endif
|
2020-01-29 12:01:32 +01:00
|
|
|
unsigned int m_current = 0;
|
2020-01-17 10:13:01 +01:00
|
|
|
|
2020-01-20 09:14:13 +01:00
|
|
|
#define TIMER_ENABLE ((1 << 31) | (1 << 29) | (1 << 28))
|
2020-01-29 12:01:32 +01:00
|
|
|
#define TIMER_RELOAD 5000000
|
2020-01-20 09:14:13 +01:00
|
|
|
|
2019-12-01 22:57:02 +01:00
|
|
|
void
|
2020-01-17 10:13:01 +01:00
|
|
|
Timer_init(void)
|
2019-12-01 22:57:02 +01:00
|
|
|
{
|
2020-01-17 10:13:01 +01:00
|
|
|
#if CONFIG_ARM_TIMER == 1
|
2020-01-20 09:14:13 +01:00
|
|
|
/* Route local timer to a core0 */
|
|
|
|
AARCH64_LOCAL_INT_ROUTING &= ~0x03;
|
|
|
|
/* Set up timer status control register */
|
|
|
|
AARCH64_LOCAL_TIMER_STATCTL = TIMER_ENABLE | TIMER_RELOAD;
|
|
|
|
/* clear interrupt flag and reload timer */
|
|
|
|
AARCH64_LOCAL_TIMER_RECLR |= (1 << 31) | (1 << 30);
|
|
|
|
/* Set timer interrupt control register */
|
|
|
|
AARCH64_CORE0_TIMER_IRQCTL = (1 << 1); /* nCNTPNSIR1 - Non Secure*/
|
|
|
|
|
2020-01-17 10:13:01 +01:00
|
|
|
#else
|
2020-01-29 12:01:32 +01:00
|
|
|
m_current = AArch64_getReg32(TIMER_CLO);
|
2019-12-01 22:57:02 +01:00
|
|
|
m_current += m_interval;
|
2020-01-29 12:01:32 +01:00
|
|
|
AArch64_setReg32(TIMER_C1, m_current);
|
2020-01-17 10:13:01 +01:00
|
|
|
#endif
|
2019-12-01 22:57:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-01-17 10:13:01 +01:00
|
|
|
Timer_incFromISR(void)
|
2019-12-01 22:57:02 +01:00
|
|
|
{
|
2020-01-29 12:01:32 +01:00
|
|
|
m_current += m_interval;
|
2020-01-17 10:13:01 +01:00
|
|
|
#if CONFIG_ARM_TIMER == 1
|
2020-01-20 09:14:13 +01:00
|
|
|
/* clear interrupt flag and reload timer */
|
2020-01-29 12:01:32 +01:00
|
|
|
// AARCH64_LOCAL_TIMER_STATCTL |= TIMER_RELOAD;
|
2020-01-20 09:14:13 +01:00
|
|
|
AARCH64_LOCAL_TIMER_RECLR |= (1 << 31) | (1 << 30);
|
2020-01-17 10:13:01 +01:00
|
|
|
#else
|
2020-01-29 12:01:32 +01:00
|
|
|
AArch64_setReg32(TIMER_C1, m_current);
|
|
|
|
AArch64_setReg32(TIMER_CS, TIMER_CS_M1);
|
2020-01-17 10:13:01 +01:00
|
|
|
#endif
|
2019-12-01 22:57:02 +01:00
|
|
|
}
|
2020-01-17 10:13:01 +01:00
|
|
|
|
2020-01-20 09:14:13 +01:00
|
|
|
uint32_t
|
|
|
|
Timer_getTicks(void)
|
|
|
|
{
|
|
|
|
return m_current;
|
|
|
|
}
|