HAL_COMP(pwm);

HAL_PIN(u) = 100.0;
HAL_PIN(v) = 200.0;
HAL_PIN(w) = 300.0;
HAL_PIN(enable) = 0.0;

INIT(
   //W PA8
   //V PA9
   //U PA10
   //SD PB1 PB2 PB3
   
   //SD pins
   rcc_periph_clock_enable(RCC_GPIOB);
   gpio_mode_setup(GPIOB, GPIO_MODE_OUTPUT,GPIO_PUPD_NONE, GPIO1 | GPIO2 | GPIO3);
   
   //pwm
   rcc_periph_clock_enable(RCC_GPIOA);
   rcc_periph_clock_enable(RCC_TIM1);
   gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO8 | GPIO9 | GPIO10);
   gpio_set_output_options(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ,  GPIO8 | GPIO9 | GPIO10);
   gpio_set_af(GPIOA, GPIO_AF6, GPIO8 | GPIO9 | GPIO10);
   timer_reset(TIM1);
   timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_CENTER_3, TIM_CR1_DIR_UP);
   timer_set_prescaler(TIM1, 0);
   timer_set_period(TIM1,2400);
   timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_PWM1);
   timer_set_oc_mode(TIM1, TIM_OC2, TIM_OCM_PWM1);
   timer_set_oc_mode(TIM1, TIM_OC3, TIM_OCM_PWM1);
   timer_set_oc_polarity_low(TIM1, TIM_OC1);
   timer_set_oc_polarity_low(TIM1, TIM_OC2);
   timer_set_oc_polarity_low(TIM1, TIM_OC3);
   timer_enable_oc_output(TIM1, TIM_OC1);
   timer_enable_oc_output(TIM1, TIM_OC2);
   timer_enable_oc_output(TIM1, TIM_OC3);
   timer_enable_break_main_output(TIM1);
   timer_set_oc_value(TIM1, TIM_OC1, 2);
   timer_set_oc_value(TIM1, TIM_OC2, 2);
   timer_set_oc_value(TIM1, TIM_OC3, 2);
   timer_update_on_overflow(TIM1);
   //timer_enable_update_event(TIM1);
   timer_set_repetition_counter(TIM1, 1);
   //timer_set_master_mode(TIM1, TIM_CR2_MMS_UPDATE);
   timer_enable_preload(TIM1);
   timer_enable_counter(TIM1);
);

RT(
   if(PIN(enable) > 0)
      gpio_set(GPIOB, GPIO1 | GPIO2 | GPIO3);
   else
      gpio_clear(GPIOB, GPIO1 | GPIO2 | GPIO3);

   timer_set_oc_value(TIM1, TIM_OC1, PIN(w));
   timer_set_oc_value(TIM1, TIM_OC2, PIN(v));
   timer_set_oc_value(TIM1, TIM_OC3, PIN(u));
);

ENDCOMP;