Advertisement
ST4SH3R

test szajs

Nov 22nd, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1. #include "at91sam9263.h"
  2.  
  3. #define LEFT_LED (1 << 8)
  4.  
  5. #define CYCLES_TO_MS 6250 // 100MHz main clock -> divided by the prescaler by 16 -> 6.25Mhz of the PIT clock
  6. #define SEC_TO_MILISEC 1000
  7. #define INTERRUPT_CHECK_IN_MS 100 // minimum value of interrupt, the bigger - the better performance, but worse precission
  8.  
  9. // CONFIG
  10.  
  11. #define DELAY 1
  12.  
  13. // CONFIG
  14.  
  15. void dbgu_print_ascii(const char *a) {}
  16. void setOutputLED1();
  17. void toggleLeftLed();
  18. void interruptHandler();
  19. void PIT_init();
  20.  
  21.  
  22. int main() {
  23.     PIT_init();
  24.     setOutputLED1();
  25.  
  26.     while(1){
  27.  
  28.     }
  29.  
  30.     return 0;
  31. }
  32.  
  33.  
  34. void setOutputLED1() {
  35.     AT91C_BASE_PIOB->PIO_PER = LEFT_LED;
  36.     AT91C_BASE_PIOB->PIO_OER = LEFT_LED;
  37.     AT91C_BASE_PIOB->PIO_SODR = LEFT_LED; // turn off the LED1
  38. }
  39.  
  40. void toggleLeftLed() {
  41.     if (!(AT91C_BASE_PIOB->PIO_ODSR & LEFT_LED)) {
  42.         AT91C_BASE_PIOB->PIO_SODR = LEFT_LED;
  43.     } else {
  44.         AT91C_BASE_PIOB->PIO_CODR = LEFT_LED ;
  45.     }
  46. }
  47.  
  48. void interruptHandler() {
  49.     //static int cyclesToGo = 0;
  50.  
  51.     if (AT91C_BASE_PITC->PITC_PIMR & AT91C_PITC_PITIEN) /* interrupt enabled */ {
  52.  
  53.         if (AT91C_BASE_PITC->PITC_PISR & AT91C_PITC_PITS) /* timer requested int */ {
  54.             AT91C_BASE_PITC->PITC_PIVR; /* delay ~100 ms */
  55.             toggleLeftLed();
  56.             //if (!cyclesToGo) {
  57.             //  toggleLeftLed(); /* actual delayed function */
  58.             //  cyclesToGo = DELAY * SEC_TO_MILISEC / INTERRUPT_CHECK_IN_MS; // reload cyclesToGo value        
  59.             //}
  60.             //--cyclesToGo;
  61.         }
  62.     }
  63. //  else /* another device requested interrupts */ {
  64. //      check which device requested INT,
  65. //      process INT, clear INT flag,
  66. //      if unknown device, just increase counter of unknown interrupts
  67. //  }
  68. }
  69.  
  70. void PIT_init() {
  71.     AT91C_BASE_AIC->AIC_IDCR = 1 << AT91C_ID_SYS; // 2nd step
  72.     AT91C_BASE_AIC->AIC_SVR[AT91C_ID_SYS] = (unsigned int) interruptHandler; // 3rd step
  73.    
  74.     AT91C_BASE_AIC->AIC_SMR[AT91C_ID_SYS] = (AT91C_AIC_SRCTYPE & AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE) /* 4th step - edge */ | (AT91C_AIC_PRIOR & AT91C_AIC_PRIOR_LOWEST); /* 4th step - priority */
  75.     AT91C_BASE_AIC->AIC_ICCR = 1 << AT91C_ID_SYS; // 5th step
  76.     AT91C_BASE_AIC->AIC_IECR = 1 << AT91C_ID_SYS; // 6th step
  77.     AT91C_BASE_PITC->PITC_PIMR = AT91C_PITC_PITIEN /* 7th step */ | AT91C_PITC_PITEN /* 8th step */ | (0x000FFFFFU & (INTERRUPT_CHECK_IN_MS*CYCLES_TO_MS));
  78.     // <TO DO> 9th step
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement