You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.8 KiB
94 lines
2.8 KiB
#include "clock.hpp"
|
|
|
|
#include "chip_helper.hpp"
|
|
extern "C" {
|
|
|
|
static zchip_tim_t* m_usdleaytim;
|
|
|
|
uint32_t zchip_clock_init(zchip_clock_cfg_t* cfg) {
|
|
m_usdleaytim = cfg->usdleaytim;
|
|
|
|
uint32_t freq = chip_get_timer_clock_sorce_freq(cfg->usdleaytim);
|
|
uint32_t prescaler = freq / 1000000 - 1; // 1us
|
|
uint32_t autoreload = 65535;
|
|
|
|
HAL_TIM_Base_DeInit(cfg->usdleaytim);
|
|
|
|
cfg->usdleaytim->Init.Prescaler = prescaler;
|
|
cfg->usdleaytim->Init.CounterMode = TIM_COUNTERMODE_UP;
|
|
cfg->usdleaytim->Init.Period = autoreload;
|
|
cfg->usdleaytim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
|
HAL_TIM_Base_Init(cfg->usdleaytim);
|
|
|
|
return 0;
|
|
}
|
|
//
|
|
uint32_t zchip_clock_get_ticket(void) { return HAL_GetTick(); }
|
|
uint32_t zchip_clock_hasspassed(uint32_t ticket) {
|
|
uint32_t nowticket = HAL_GetTick();
|
|
if (nowticket >= ticket) {
|
|
return nowticket - ticket;
|
|
}
|
|
return UINT32_MAX - ticket + nowticket;
|
|
}
|
|
|
|
static HAL_StatusTypeDef _HAL_TIM_Base_Start(TIM_HandleTypeDef* htim) __attribute__((optimize("O2")));
|
|
static HAL_StatusTypeDef _HAL_TIM_Base_Stop(TIM_HandleTypeDef* htim) __attribute__((optimize("O2")));
|
|
|
|
static HAL_StatusTypeDef _HAL_TIM_Base_Start(TIM_HandleTypeDef* htim) {
|
|
uint32_t tmpsmcr;
|
|
/* Check the TIM state */
|
|
if (htim->State != HAL_TIM_STATE_READY) {
|
|
return HAL_ERROR;
|
|
}
|
|
htim->State = HAL_TIM_STATE_BUSY;
|
|
if (IS_TIM_SLAVE_INSTANCE(htim->Instance)) {
|
|
tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
|
|
if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr)) {
|
|
__HAL_TIM_ENABLE(htim);
|
|
}
|
|
} else {
|
|
__HAL_TIM_ENABLE(htim);
|
|
}
|
|
return HAL_OK;
|
|
}
|
|
static HAL_StatusTypeDef _HAL_TIM_Base_Stop(TIM_HandleTypeDef* htim) {
|
|
/* Disable the Peripheral */
|
|
__HAL_TIM_DISABLE(htim);
|
|
/* Set the TIM state */
|
|
htim->State = HAL_TIM_STATE_READY;
|
|
/* Return function status */
|
|
return HAL_OK;
|
|
}
|
|
|
|
void __zchip_clock_early_delayus(uint32_t n) {
|
|
volatile uint32_t counter = 0;
|
|
__HAL_TIM_SET_COUNTER(m_usdleaytim, 0);
|
|
_HAL_TIM_Base_Start(m_usdleaytim);
|
|
while (counter < n) {
|
|
counter = __HAL_TIM_GET_COUNTER(m_usdleaytim);
|
|
}
|
|
_HAL_TIM_Base_Stop(m_usdleaytim);
|
|
}
|
|
void zchip_clock_early_delayus(uint32_t n) {
|
|
uint32_t us = n % 1000;
|
|
uint32_t ms = n / 1000;
|
|
if (us > 0) {
|
|
__zchip_clock_early_delayus(us);
|
|
}
|
|
for (uint32_t i = 0; i < ms; i++) {
|
|
__zchip_clock_early_delayus(1000);
|
|
}
|
|
}
|
|
}
|
|
|
|
void zchip_clock_early_delayus2(uint32_t us) { __zchip_clock_early_delayus(us); }
|
|
void zchip_clock_early_delayus_timer_start() {
|
|
__HAL_TIM_SET_COUNTER(m_usdleaytim, 0);
|
|
_HAL_TIM_Base_Start(m_usdleaytim);
|
|
}
|
|
uint32_t zchip_clock_early_delayus_timer_haspassed() {
|
|
uint32_t counter = __HAL_TIM_GET_COUNTER(m_usdleaytim);
|
|
return counter;
|
|
}
|
|
void zchip_clock_early_delayus_timer_stop() { _HAL_TIM_Base_Stop(m_usdleaytim); }
|