Skip to content
Snippets Groups Projects
Commit 055e4fbb authored by Sergi Hernandez's avatar Sergi Hernandez
Browse files

Used the scheduler from the library.

parent cfee3b23
No related branches found
No related tags found
1 merge request!5Dynamixel manager
#ifndef _DARWIN_SCH_H
#define _DARWIN_SCH_H
#include "stm32f1xx.h"
#include "scheduler.h"
TScheduler *darwin_sch_init(void);
#endif
#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
#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;
}
#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)
{
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment