Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <libopencm3/stm32/rcc.h>
- #include <libopencm3/stm32/gpio.h>
- #include <libopencm3/stm32/timer.h>
- #include <libopencm3/cm3/nvic.h>
- #include <libopencm3/stm32/exti.h>
- #include <libopencm3/cm3/cortex.h>
- //#include <stm32f2xx_rcc.h>
- static void gpio_setup ( void ) {
- /* Enable GPIOC clock. */
- rcc_peripheral_enable_clock ( &RCC_AHB1ENR, RCC_AHB1ENR_IOPCEN );
- /* Set GPIO3 to 'output push-pull'. */
- gpio_mode_setup ( GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO3 );
- }
- static void nvic_setup(void) {
- /* Without this the timer interrupt routine will never be called. */
- nvic_enable_irq(NVIC_TIM2_IRQ);
- //nvic_set_priority(NVIC_TIM2_IRQ, 1);
- }
- static void timer2_setup ( void ) {
- /* Set timer start value. */
- TIM_CNT(TIM2) = 1;
- /* Set timer prescaler. 72MHz/1440 => 50000 counts per second. */
- TIM_PSC(TIM2) = 20000; // 120M/2000 = 60k/second
- /* End timer value. If this is reached an interrupt is generated. */
- TIM_ARR(TIM2) = 600;
- /* Update interrupt enable. */
- TIM_DIER(TIM2) |= TIM_DIER_UIE;
- //timer_set_repetition_counter ( TIM2, 100 );
- /* Start timer. */
- TIM_CR1(TIM2) |= TIM_CR1_CEN;
- return;
- }
- void tim2_isr ( void ) {
- //TIM2_SR &= ~TIM_SR_UIF; //clearing update interrupt flag
- TIM_SR(TIM2) &= ~TIM_SR_UIF; /* Clear interrrupt flag. */
- __asm__("nop");
- gpio_toggle(GPIOC, GPIO3); /* LED on/off. */
- }
- int main ( void ) {
- #if 1 // go for 120MHz, built into libopencm3
- // requires: external 8MHz crystal on pin5/6 with associated caps to ground
- rcc_clock_setup_hse_3v3 ( &hse_8mhz_3v3 [ CLOCK_3V3_120MHZ ] );
- #endif
- gpio_setup();
- gpio_set ( GPIOC, GPIO3 );
- /* Enable TIM2 clock. */
- rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_TIM2EN);
- //__enable_irq();
- //cm_enable_interrupts();
- nvic_setup();
- timer2_setup();
- TIM_SR(TIM2) &= ~TIM_SR_UIF; /* Clear interrrupt flag. */
- while ( 1 ) {
- __asm__("nop");
- } // while forever
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment