interrupt_timer.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //*----------------------------------------------------------------------------
  2. //* ATMEL Microcontroller Software Support - ROUSSET -
  3. //*----------------------------------------------------------------------------
  4. //* The software is delivered "AS IS" without warranty or condition of any
  5. //* kind, either express, implied or statutory. This includes without
  6. //* limitation any warranty or condition with respect to merchantability or
  7. //* fitness for any particular purpose, or against the infringements of
  8. //* intellectual property rights of others.
  9. //*----------------------------------------------------------------------------
  10. //* File Name : interrupt_timer.c
  11. //* Object : Timer interrupt management
  12. //* : Use LED7 & LED8 for status interrupt
  13. //*
  14. //* 1.0 24/Jun/04 JPP : Creation
  15. //*----------------------------------------------------------------------------
  16. // Include Standard LIB files
  17. #include "Board.h"
  18. //* Global variable
  19. int count_timer0_interrupt;
  20. int count_timer1_interrupt;
  21. #define TIMER0_INTERRUPT_LEVEL 1
  22. #define TIMER1_INTERRUPT_LEVEL 4
  23. /*-----------------*/
  24. /* Clock Selection */
  25. /*-----------------*/
  26. #define TC_CLKS 0x7
  27. #define TC_CLKS_MCK2 0x0
  28. #define TC_CLKS_MCK8 0x1
  29. #define TC_CLKS_MCK32 0x2
  30. #define TC_CLKS_MCK128 0x3
  31. #define TC_CLKS_MCK1024 0x4
  32. //*------------------------- Internal Function --------------------------------
  33. //*----------------------------------------------------------------------------
  34. //* Function Name : AT91F_TC_Open
  35. //* Object : Initialize Timer Counter Channel and enable is clock
  36. //* Input Parameters : <tc_pt> = TC Channel Descriptor Pointer
  37. //* <mode> = Timer Counter Mode
  38. //* : <TimerId> = Timer peripheral ID definitions
  39. //* Output Parameters : None
  40. //*----------------------------------------------------------------------------
  41. void AT91F_TC_Open ( AT91PS_TC TC_pt, unsigned int Mode, unsigned int TimerId)
  42. //* Begin
  43. {
  44. unsigned int dummy;
  45. //* First, enable the clock of the TIMER
  46. AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1<< TimerId ) ;
  47. //* Disable the clock and the interrupts
  48. TC_pt->TC_CCR = AT91C_TC_CLKDIS ;
  49. TC_pt->TC_IDR = 0xFFFFFFFF ;
  50. //* Clear status bit
  51. dummy = TC_pt->TC_SR;
  52. //* Suppress warning variable "dummy" was set but never used
  53. dummy = dummy;
  54. //* Set the Mode of the Timer Counter
  55. TC_pt->TC_CMR = Mode ;
  56. //* Enable the clock
  57. TC_pt->TC_CCR = AT91C_TC_CLKEN ;
  58. //* End
  59. }
  60. //*------------------------- Interrupt Function -------------------------------
  61. //*----------------------------------------------------------------------------
  62. //* Function Name : timer0_c_irq_handler
  63. //* Object : C handler interrupt function called by the interrupts
  64. //* assembling routine
  65. //* Output Parameters : increment count_timer0_interrupt
  66. //*----------------------------------------------------------------------------
  67. void timer0_c_irq_handler(void)
  68. {
  69. AT91PS_TC TC_pt = AT91C_BASE_TC0;
  70. unsigned int dummy;
  71. //* Acknowledge interrupt status
  72. dummy = TC_pt->TC_SR;
  73. //* Suppress warning variable "dummy" was set but never used
  74. dummy = dummy;
  75. count_timer0_interrupt++;
  76. //* Read the output state
  77. if ( (AT91F_PIO_GetInput(AT91C_BASE_PIOA) & LED3 ) == LED3 )
  78. {
  79. AT91F_PIO_ClearOutput( AT91C_BASE_PIOA, LED3 );
  80. }
  81. else
  82. {
  83. AT91F_PIO_SetOutput( AT91C_BASE_PIOA, LED3 );
  84. }
  85. }
  86. //*----------------------------------------------------------------------------
  87. //* Function Name : timer1_c_irq_handler
  88. //* Object : C handler interrupt function called by the interrupts
  89. //* assembling routine
  90. //* Output Parameters : increment count_timer1_interrupt
  91. //*----------------------------------------------------------------------------
  92. void timer1_c_irq_handler(void)
  93. {
  94. AT91PS_TC TC_pt = AT91C_BASE_TC1;
  95. unsigned int dummy;
  96. //* Acknowledge interrupt status
  97. dummy = TC_pt->TC_SR;
  98. //* Suppress warning variable "dummy" was set but never used
  99. dummy = dummy;
  100. count_timer1_interrupt++;
  101. //* Read the output state
  102. if ( (AT91F_PIO_GetInput(AT91C_BASE_PIOA) & LED4 ) == LED4 )
  103. {
  104. AT91F_PIO_ClearOutput( AT91C_BASE_PIOA, LED4 );
  105. }
  106. else
  107. {
  108. AT91F_PIO_SetOutput( AT91C_BASE_PIOA, LED4 );
  109. }
  110. }
  111. //*-------------------------- External Function -------------------------------
  112. //*----------------------------------------------------------------------------
  113. //* Function Name : timer_init
  114. //* Object : Init timer counter
  115. //* Input Parameters : none
  116. //* Output Parameters : TRUE
  117. //*----------------------------------------------------------------------------
  118. void timer_init ( void )
  119. //* Begin
  120. {
  121. //init the timer interrupt counter
  122. count_timer0_interrupt=0;
  123. count_timer1_interrupt=0;
  124. //* Open timer0
  125. AT91F_TC_Open(AT91C_BASE_TC0,TC_CLKS_MCK1024,AT91C_ID_TC0);
  126. //* Open Timer 0 interrupt
  127. AT91F_AIC_ConfigureIt ( AT91C_BASE_AIC, AT91C_ID_TC0, TIMER0_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE, timer0_c_irq_handler);
  128. AT91C_BASE_TC0->TC_IER = AT91C_TC_CPCS; // IRQ enable CPC
  129. AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_TC0);
  130. //* Open timer1
  131. AT91F_TC_Open(AT91C_BASE_TC1,TC_CLKS_MCK128,AT91C_ID_TC1);
  132. //* Open Timer 1 interrupt
  133. AT91F_AIC_ConfigureIt ( AT91C_BASE_AIC, AT91C_ID_TC1, TIMER1_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE, timer1_c_irq_handler);
  134. AT91C_BASE_TC1->TC_IER = AT91C_TC_CPCS; // IRQ enable CPC
  135. AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_TC1);
  136. //* Generate interrupt by software
  137. AT91F_AIC_Trig (AT91C_BASE_AIC,AT91C_ID_TC0) ;
  138. AT91F_AIC_Trig (AT91C_BASE_AIC,AT91C_ID_TC1) ;
  139. //* Start timer0
  140. AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG ;
  141. //* Start timer1
  142. AT91C_BASE_TC1->TC_CCR = AT91C_TC_SWTRG ;
  143. //* End
  144. }