Advertisement
dtung

tasks

Oct 7th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. /*
  2. * Generate time slots for stm32f1
  3. */
  4. #include <libopencm3/stm32/rcc.h>
  5. #include <libopencm3/stm32/gpio.h>
  6. #include <libopencm3/stm32/timer.h>
  7. #include <libopencm3/cm3/nvic.h>
  8.  
  9. #define SLOT_LEN 10
  10. #define SMALLEST_SLOT_TIME 3
  11. static volatile uint32_t t_slot = 0;
  12. static volatile uint32_t ms[SLOT_LEN];
  13.  
  14. static void sys_init(void) {
  15. rcc_clock_setup_in_hse_8mhz_out_72mhz(); // Use this for "blue pill"
  16. /* Enable GPIOA clock. */
  17. rcc_periph_clock_enable(RCC_GPIOA);
  18. rcc_periph_clock_enable(RCC_AFIO);
  19. /* Set GPIO1 (in GPIO port A) to 'output alternate function push-pull'. */
  20. gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO1);
  21. // timer init
  22. rcc_periph_clock_enable(RCC_TIM2);
  23. TIM2_SR = 0;
  24. TIM2_PSC = 17;
  25. TIM2_ARR = ms[t_slot];
  26. TIM2_CCR2 = 1;
  27. // set up interrupt
  28. TIM2_DIER = TIM_DIER_UIE;
  29. nvic_enable_irq(NVIC_TIM2_IRQ);
  30. // set output to PWM1 mode
  31. TIM2_CCMR1 = TIM_CCMR1_OC2M_PWM1;
  32. TIM2_CCER = TIM_CCER_CC2E;
  33. }
  34.  
  35. void tim2_isr() {
  36. TIM2_SR &= ~TIM_SR_UIF;
  37. if (t_slot==SLOT_LEN) t_slot = 0;
  38. TIM2_ARR = ms[t_slot++];
  39. }
  40.  
  41. int main(void) {
  42. // set up time slot
  43. for (int i = 0; i < SLOT_LEN; i++)
  44. ms[i] = 4*i + SMALLEST_SLOT_TIME;
  45. sys_init();
  46. // enable timer
  47. TIM2_CR1 |= TIM_CR1_CEN;
  48. for (;;);
  49. return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement