Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Generate time slots for stm32f1
- */
- #include <libopencm3/stm32/rcc.h>
- #include <libopencm3/stm32/gpio.h>
- #include <libopencm3/stm32/timer.h>
- #include <libopencm3/cm3/nvic.h>
- #define SLOT_LEN 10
- #define SMALLEST_SLOT_TIME 3
- static volatile uint32_t t_slot = 0;
- static volatile uint32_t ms[SLOT_LEN];
- static void sys_init(void) {
- rcc_clock_setup_in_hse_8mhz_out_72mhz(); // Use this for "blue pill"
- /* Enable GPIOA clock. */
- rcc_periph_clock_enable(RCC_GPIOA);
- rcc_periph_clock_enable(RCC_AFIO);
- /* Set GPIO1 (in GPIO port A) to 'output alternate function push-pull'. */
- gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO1);
- // timer init
- rcc_periph_clock_enable(RCC_TIM2);
- TIM2_SR = 0;
- TIM2_PSC = 17999;
- TIM2_ARR = ms[t_slot];
- TIM2_CCR2 = 1;
- // set up interrupt
- TIM2_DIER = TIM_DIER_UIE;
- nvic_enable_irq(NVIC_TIM2_IRQ);
- // set output to PWM1 mode
- TIM2_CCMR1 = TIM_CCMR1_OC2M_PWM1;
- TIM2_CCER = TIM_CCER_CC2E;
- }
- void tim2_isr() {
- TIM2_SR &= ~TIM_SR_UIF;
- if (t_slot==SLOT_LEN) t_slot = 0;
- TIM2_ARR = ms[t_slot++];
- }
- int main(void) {
- // set up time slot
- for (int i = 0; i < SLOT_LEN; i++)
- ms[i] = 4*i + SMALLEST_SLOT_TIME;
- sys_init();
- // enable timer
- TIM2_CR1 |= TIM_CR1_CEN;
- for (;;);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement