diff --git a/include/darwin_sch.h b/include/darwin_sch.h new file mode 100644 index 0000000000000000000000000000000000000000..21ce2fe1eb96001d08885159bb470b35522dfbeb --- /dev/null +++ b/include/darwin_sch.h @@ -0,0 +1,10 @@ +#ifndef _DARWIN_SCH_H +#define _DARWIN_SCH_H + +#include "stm32f1xx.h" +#include "scheduler.h" + +TScheduler *darwin_sch_init(void); + +#endif + diff --git a/include/scheduler.h b/include/scheduler.h deleted file mode 100644 index 2970a430412faae59d7b903aa320ac604d960600..0000000000000000000000000000000000000000 --- a/include/scheduler.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef _SCHEDULER_H -#define _SCHEDULER_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include "stm32f1xx.h" - -typedef enum {SCH_CH1=0,SCH_CH2=1,SCH_CH3=2,SCH_CH4=3} sch_ch_t; - -void scheduler_init(void); -void scheduler_set_period(sch_ch_t channel_id, uint16_t period_ms); -void scheduler_set_one_shot(sch_ch_t channel_id, uint16_t time_ms); -void scheduler_set_function(sch_ch_t channel_id, void (*function)(void)); -void scheduler_start(sch_ch_t channel_id); -void scheduler_stop(sch_ch_t channel_id); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/darwin_sch.c b/src/darwin_sch.c new file mode 100644 index 0000000000000000000000000000000000000000..f27bee62d274c22231afdaa561f2795f422ed73b --- /dev/null +++ b/src/darwin_sch.c @@ -0,0 +1,128 @@ +#include "darwin_sch.h" + +/* private variables */ +TIM_HandleTypeDef darwin_sch_timer_handle; +TScheduler darwin_scheduler; + +/* interrupt handlers */ +void TIM1_CC_IRQHandler(void) +{ + /* Capture compare 1 event */ + if(__HAL_TIM_GET_FLAG(&darwin_sch_timer_handle, TIM_FLAG_CC1) != RESET) + { + if(__HAL_TIM_GET_IT_SOURCE(&darwin_sch_timer_handle, TIM_IT_CC1) !=RESET) + { + __HAL_TIM_CLEAR_IT(&darwin_sch_timer_handle, TIM_IT_CC1); + scheduler_interrupt(&darwin_scheduler,SCHED_CH1); + } + } + /* Capture compare 2 event */ + if(__HAL_TIM_GET_FLAG(&darwin_sch_timer_handle, TIM_FLAG_CC2) != RESET) + { + if(__HAL_TIM_GET_IT_SOURCE(&darwin_sch_timer_handle, TIM_IT_CC2) !=RESET) + { + __HAL_TIM_CLEAR_IT(&darwin_sch_timer_handle, TIM_IT_CC2); + scheduler_interrupt(&darwin_scheduler,SCHED_CH2); + } + } + /* Capture compare 3 event */ + if(__HAL_TIM_GET_FLAG(&darwin_sch_timer_handle, TIM_FLAG_CC3) != RESET) + { + if(__HAL_TIM_GET_IT_SOURCE(&darwin_sch_timer_handle, TIM_IT_CC3) !=RESET) + { + __HAL_TIM_CLEAR_IT(&darwin_sch_timer_handle, TIM_IT_CC3); + scheduler_interrupt(&darwin_scheduler,SCHED_CH3); + } + } + /* Capture compare 4 event */ + if(__HAL_TIM_GET_FLAG(&darwin_sch_timer_handle, TIM_FLAG_CC4) != RESET) + { + if(__HAL_TIM_GET_IT_SOURCE(&darwin_sch_timer_handle, TIM_IT_CC4) !=RESET) + { + __HAL_TIM_CLEAR_IT(&darwin_sch_timer_handle, TIM_IT_CC4); + scheduler_interrupt(&darwin_scheduler,SCHED_CH4); + } + } +} + +/* private functions */ +void darwin_sch_start(unsigned short int channel_id) +{ + HAL_TIM_OC_Start_IT(&darwin_sch_timer_handle,channel_id); +} + +void darwin_sch_stop(unsigned short int channel_id) +{ + HAL_TIM_OC_Stop_IT(&darwin_sch_timer_handle,channel_id); +} + +void darwin_sch_set_pulse(unsigned short int channel_id,unsigned short int pulse, unsigned char running) +{ + TIM_OC_InitTypeDef out_comp_config; + unsigned short int capture; + + if(running==0x00) + { + if(darwin_scheduler.channels[channel_id].enabled==0x00) + { + out_comp_config.OCMode = TIM_OCMODE_TIMING; + capture = HAL_TIM_ReadCapturedValue(&darwin_sch_timer_handle,channel_id); + out_comp_config.Pulse = capture+pulse; + out_comp_config.OCPolarity = TIM_OCPOLARITY_HIGH; + out_comp_config.OCFastMode = TIM_OCFAST_DISABLE; + HAL_TIM_OC_ConfigChannel(&darwin_sch_timer_handle, &out_comp_config, channel_id); + } + } + else + { + capture = HAL_TIM_ReadCapturedValue(&darwin_sch_timer_handle, channel_id); + __HAL_TIM_SetCompare(&darwin_sch_timer_handle,channel_id, capture+pulse); + } +} + +/* public functions */ +TScheduler *darwin_sch_init(void) +{ + TIM_ClockConfigTypeDef sClockSourceConfig; + TIM_MasterConfigTypeDef sMasterConfig; + TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig; + + __HAL_RCC_TIM1_CLK_ENABLE(); + + darwin_sch_timer_handle.Instance = TIM1; + darwin_sch_timer_handle.Init.Prescaler = 168; + darwin_sch_timer_handle.Init.CounterMode = TIM_COUNTERMODE_UP; + darwin_sch_timer_handle.Init.Period = 0xFFFF; + darwin_sch_timer_handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + darwin_sch_timer_handle.Init.RepetitionCounter = 0; + HAL_TIM_Base_Init(&darwin_sch_timer_handle); + + sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; + HAL_TIM_ConfigClockSource(&darwin_sch_timer_handle, &sClockSourceConfig); + + HAL_TIM_OC_Init(&darwin_sch_timer_handle); + + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + HAL_TIMEx_MasterConfigSynchronization(&darwin_sch_timer_handle, &sMasterConfig); + + sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; + sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; + sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; + sBreakDeadTimeConfig.DeadTime = 0; + sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; + sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; + sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; + HAL_TIMEx_ConfigBreakDeadTime(&darwin_sch_timer_handle, &sBreakDeadTimeConfig); + + /* Peripheral interrupt init */ + HAL_NVIC_SetPriority(TIM1_CC_IRQn, 2, 0); + HAL_NVIC_EnableIRQ(TIM1_CC_IRQn); + + scheduler_init(&darwin_scheduler,4,168); + darwin_scheduler.start=darwin_sch_start; + darwin_scheduler.stop=darwin_sch_stop; + darwin_scheduler.set_pulse=darwin_sch_set_pulse; + + return &darwin_scheduler; +} diff --git a/src/scheduler.c b/src/scheduler.c deleted file mode 100644 index b8e0d999b713a1120c8a22453d93bec19e06bc0d..0000000000000000000000000000000000000000 --- a/src/scheduler.c +++ /dev/null @@ -1,58 +0,0 @@ -#include "scheduler.h" - -#define SCHEDULER_TIMER TIM1 -#define SCHEDULER_TIMER_IRQn TIM1_IRQn -#define SCHEDULER_TIMER_IRQHandler TIM1_IRQHandler -#define SCHEDULER_TIMER_CLK __HAL_RCC_TIM1_CLK_ENABLE() - -typedef void (*scheduler_function)(void); - -scheduler_function sch_ch1_fnct; -scheduler_function sch_ch2_fnct; -scheduler_function sch_ch3_fnct; -scheduler_function sch_ch4_fnct; -uint16_t sch_ch1_period; -uint16_t sch_ch2_period; -uint16_t sch_ch3_period; -uint16_t sch_ch4_period; - -void scheduler_init(void) -{ - /* initialize internal variables */ - sch_ch1_fnct=0; - sch_ch2_fnct=0; - sch_ch3_fnct=0; - sch_ch4_fnct=0; - sch_ch1_period=0; - sch_ch2_period=0; - sch_ch3_period=0; - sch_ch4_period=0; - /* initialize timer 1 */ - -} - -void scheduler_set_period(sch_ch_t channel_id, uint16_t period_ms) -{ - -} - -void scheduler_set_one_shot(sch_ch_t channel_id, uint16_t time_ms) -{ - -} - -void scheduler_set_function(sch_ch_t channel_id, void (*function)(void)) -{ - -} - -void scheduler_start(sch_ch_t channel_id) -{ - -} - -void scheduler_stop(sch_ch_t channel_id) -{ - -} -