<commit_before>/**
 * @file
 *
 * @date   12.11.2020
 * @author <NAME>
 */

#include <assert.h>
#include <hal/clock.h>
#include <hal/reg.h>
#include <kernel/irq.h>
#include <kernel/time/clock_source.h>
#include "stm32_cube_tim_base.h"

int stm32_cube_tim_base_set_oneshot(struct clock_source *cs) {
	TIM_HandleTypeDef *htim = cs->driver_priv_data;

	HAL_TIM_Base_Stop(htim);

	htim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
	TIM_Base_SetConfig(htim->Instance, &htim->Init);

	return 0;
}

int stm32_cube_tim_base_set_periodic(struct clock_source *cs) {
	TIM_HandleTypeDef *htim = cs->driver_priv_data;

	HAL_TIM_Base_Stop(htim);

	htim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
	TIM_Base_SetConfig(htim->Instance, &htim->Init);

	return 0;
}

int stm32_cube_tim_base_set_next_event(struct clock_source *cs,
		uint32_t next_event) {
	TIM_HandleTypeDef *htim = cs->driver_priv_data;

	htim->Init.Period = next_event - 1;
	TIM_Base_SetConfig(htim->Instance, &htim->Init);

	HAL_TIM_Base_Start(htim);

	return 0;
}

cycle_t stm32_cube_tim_base_read(struct clock_source *cs) {
	TIM_HandleTypeDef *htim = cs->driver_priv_data;

	return htim->Instance->ARR - htim->Instance->CNT;
}

irq_return_t stm32_cube_time_base_irq_handler(unsigned int irq_nr, void *data) {
	clock_tick_handler(data);

	return IRQ_HANDLED;
}<commit_msg>clocks: Fix tim_base stm32 clock<commit_after>/**
 * @file
 *
 * @date   12.11.2020
 * @author <NAME>
 */

#include <assert.h>
#include <hal/clock.h>
#include <hal/reg.h>
#include <kernel/irq.h>
#include <kernel/time/clock_source.h>
#include "stm32_cube_tim_base.h"

int stm32_cube_tim_base_set_oneshot(struct clock_source *cs) {
	TIM_HandleTypeDef *htim = cs->driver_priv_data;

	HAL_TIM_Base_Stop_IT(htim);

	return 0;
}

int stm32_cube_tim_base_set_periodic(struct clock_source *cs) {
	/* Same as oneshot mode. */
	return stm32_cube_tim_base_set_oneshot(cs);
}

int stm32_cube_tim_base_set_next_event(struct clock_source *cs,
		uint32_t next_event) {
	TIM_HandleTypeDef *htim = cs->driver_priv_data;

	htim->Instance->CR1 |= (1 << TIM_CR1_URS_Pos);
	htim->Instance->ARR = next_event - 1;
	htim->Instance->EGR = TIM_EGR_UG;

	HAL_TIM_Base_Start_IT(htim);

	return 0;
}

cycle_t stm32_cube_tim_base_read(struct clock_source *cs) {
	TIM_HandleTypeDef *htim = cs->driver_priv_data;

	if (!(htim->Instance->CR1 & (1 << TIM_CR1_CEN_Pos))) {
		return htim->Instance->ARR;
	}

	return htim->Instance->ARR - htim->Instance->CNT;
}

irq_return_t stm32_cube_time_base_irq_handler(unsigned int irq_nr, void *data) {
	struct clock_source *cs = data;
	TIM_HandleTypeDef *htim = cs->driver_priv_data;

	HAL_TIM_IRQHandler(htim);

	if (cs->event_device->flags & CLOCK_EVENT_ONESHOT_MODE) {
		HAL_TIM_Base_Stop_IT(htim);
	}

	clock_tick_handler(data);

	return IRQ_HANDLED;
}
{'fraction_non_alphanumeric': 0.1255780860903593, 'fraction_numerical': 0.02170046246887229, 'mean_word_length': 5.808716707021792, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '928661', 'n_tokens_mistral': 1243, 'n_tokens_neox': 1166, 'n_words': 181}