SHARE
TWEET

Untitled

a guest Apr 21st, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include "system.h"
  4. #include "sys/alt_irq.h"
  5. #include "altera_avalon_pio_regs.h"
  6. #include "stdio.h"
  7. #include <io.h>
  8. #include "altera_avalon_timer_regs.h"
  9.  
  10. volatile alt_u8 count = 0;
  11. volatile alt_u8 FLAG= 0;
  12.  
  13.  
  14. volatile int edge_capture = 0;
  15. static void handle_key_interrupts(void* context);
  16.  
  17. static void init_pio_key_interrupts()
  18. {
  19. #ifdef KEY_BASE
  20.     void *edge_capture_ptr;
  21.     edge_capture_ptr = (void*) &edge_capture;
  22.  
  23.     IOWR_ALTERA_AVALON_PIO_IRQ_MASK(KEYS_BASE, 0x1);
  24.  
  25.     IOWR_ALTER_AVALON_PIO_EDGE_CAP(KEY_BASE, 0xf);
  26.  
  27.     alt_ic_isr_register(KEYS_IRQ_INTERRUPT_CONTROLLER_ID,
  28.             KEYS_IRQ,
  29.             handle_key_interrupts,
  30.             edge_capture_ptr, 0x0);
  31.  
  32.     printf("interrupt initialization status: Set \n");
  33.  
  34. #else
  35.     printf("interrupt initialization status: Error \n");
  36. #endif
  37. }
  38.  
  39. static void handle_key_interrupts(void* context);
  40. {
  41.     FLAG = 0;
  42.  
  43.     volatile int *edge_capture_ptr = (volatile int*) context;
  44.     *edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(KEYS_BASE);
  45.  
  46.     IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEYS_BASE, 0xf);
  47. }
  48.  
  49. static void init_timer()
  50. {
  51.     unsigned int val;
  52. #ifdef TIMER_0_BASE
  53.     IOWR_ALTERA_AVALON_TIMER_PERIODH(TIMER_0_BASE, 0x02FA);
  54.     IOWR_ALTERA_AVALON_TIMER_PERIODL(TIMER_0_BASE, 0xF080);
  55.  
  56.     IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_0_BASE, 0x2 | 0x4);
  57.     IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_0_BASE, 0);
  58.  
  59.     val = IORD_ALTERA_AVALON_TIMER_PERIODH(TIMER_0_BASE);
  60.     val = val << 16;
  61.     val = val | IORD_ALTERA_AVALON_TIMER_PERIODL(TIMER_0_BASE);
  62.     val = (val * 20) / 1000000000;
  63.  
  64.     printf("timer status: Set = %d [s] \n", val);
  65. #else
  66.     printf("timer status: Error \n");
  67. #endif
  68.  
  69.  
  70. }
  71.  
  72. int main()
  73. {
  74.     printf("Start \n");
  75.     unsigned int status = 0;
  76.     init_pio_key_interrupts();
  77.     init_timer();
  78.  
  79.     while(1)
  80.     {
  81.  
  82.         status = IORD_ALTERA_AVALON_TIMER_STATUS(TIMER_0_BASE);
  83.         if(FLAG == 0)
  84.         {
  85.             count = 0;
  86.             FLAG = 1;
  87.         }
  88.         else if (status & 0x01)
  89.         {
  90.             IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_0_BASE, 0);
  91.             count += 1;
  92.         }
  93.  
  94.  
  95.         IOWR(LEDSG_BASE,0,count);
  96.     }
  97.     return 0;
  98. }
RAW Paste Data
Want to get better at C?
Learn to code C in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top