Guest User

Untitled

a guest
Jan 15th, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.39 KB | None | 0 0
  1. #include <libopencm3/stm32/rcc.h>
  2. #include <libopencm3/stm32/gpio.h>
  3. #include <libopencm3/stm32/timer.h>
  4.  
  5. #include <libopencm3/cm3/nvic.h>
  6. #include <libopencm3/stm32/exti.h>
  7.  
  8. #include <libopencm3/cm3/cortex.h>
  9.  
  10. //#include <stm32f2xx_rcc.h>                                                                                                                                                            
  11.  
  12. static void gpio_setup ( void ) {
  13.  
  14.   /* Enable GPIOC clock. */
  15.   rcc_peripheral_enable_clock ( &RCC_AHB1ENR, RCC_AHB1ENR_IOPCEN );
  16.  
  17.   /* Set GPIO3 to 'output push-pull'. */
  18.   gpio_mode_setup ( GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO3 );
  19.  
  20. }
  21.  
  22. static void nvic_setup(void) {
  23.   /* Without this the timer interrupt routine will never be called. */
  24.   nvic_enable_irq(NVIC_TIM2_IRQ);
  25.   //nvic_set_priority(NVIC_TIM2_IRQ, 1);                                                                                                                                                
  26. }
  27.  
  28. static void timer2_setup ( void ) {
  29.  
  30.   /* Set timer start value. */
  31.   TIM_CNT(TIM2) = 1;
  32.  
  33.   /* Set timer prescaler. 72MHz/1440 => 50000 counts per second. */
  34.   TIM_PSC(TIM2) = 20000; // 120M/2000 = 60k/second                                                                                                                                      
  35.  
  36.   /* End timer value. If this is reached an interrupt is generated. */
  37.   TIM_ARR(TIM2) = 600;
  38.  
  39.   /* Update interrupt enable. */
  40.   TIM_DIER(TIM2) |= TIM_DIER_UIE;
  41.  
  42.   //timer_set_repetition_counter ( TIM2, 100 );                                                                                                                                        
  43.  
  44.   /* Start timer. */
  45.   TIM_CR1(TIM2) |= TIM_CR1_CEN;
  46.  
  47.   return;
  48. }
  49.  
  50. void tim2_isr ( void ) {
  51.  
  52.   //TIM2_SR &= ~TIM_SR_UIF;    //clearing update interrupt flag                                                                                                                        
  53.   TIM_SR(TIM2) &= ~TIM_SR_UIF; /* Clear interrrupt flag. */
  54.  
  55.   __asm__("nop");
  56.  
  57.   gpio_toggle(GPIOC, GPIO3); /* LED on/off. */
  58.  
  59. }
  60.  
  61.  
  62. int main ( void ) {
  63.  
  64. #if 1 // go for 120MHz, built into libopencm3                                                                                                                                          
  65.   // requires: external 8MHz crystal on pin5/6 with associated caps to ground                                                                                                          
  66.   rcc_clock_setup_hse_3v3 ( &hse_8mhz_3v3 [ CLOCK_3V3_120MHZ ] );
  67. #endif
  68.  
  69.   gpio_setup();
  70.   gpio_set ( GPIOC, GPIO3 );
  71.  
  72.   /* Enable TIM2 clock. */
  73.   rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_TIM2EN);
  74.  
  75.   //__enable_irq();                                                                                                                                                                    
  76.   //cm_enable_interrupts();                                                                                                                                                            
  77.  
  78.   nvic_setup();
  79.  
  80.   timer2_setup();
  81.  
  82.   TIM_SR(TIM2) &= ~TIM_SR_UIF; /* Clear interrrupt flag. */
  83.  
  84.   while ( 1 ) {
  85.     __asm__("nop");
  86.   } // while forever                                                                                                                                                                    
  87.  
  88.   return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment