Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "at91sam9263.h"
- #define LEFT_LED (1 << 8)
- #define CYCLES_TO_MS 6250 // 100MHz main clock -> divided by the prescaler by 16 -> 6.25Mhz of the PIT clock
- #define SEC_TO_MILISEC 1000
- #define INTERRUPT_CHECK_IN_MS 100 // minimum value of interrupt, the bigger - the better performance, but worse precission
- // CONFIG
- #define DELAY 1
- // CONFIG
- void dbgu_print_ascii(const char *a) {}
- void setOutputLED1();
- void toggleLeftLed();
- void interruptHandler();
- void PIT_init();
- int main() {
- PIT_init();
- setOutputLED1();
- while(1){
- }
- return 0;
- }
- void setOutputLED1() {
- AT91C_BASE_PIOB->PIO_PER = LEFT_LED;
- AT91C_BASE_PIOB->PIO_OER = LEFT_LED;
- AT91C_BASE_PIOB->PIO_SODR = LEFT_LED; // turn off the LED1
- }
- void toggleLeftLed() {
- if (!(AT91C_BASE_PIOB->PIO_ODSR & LEFT_LED)) {
- AT91C_BASE_PIOB->PIO_SODR = LEFT_LED;
- } else {
- AT91C_BASE_PIOB->PIO_CODR = LEFT_LED ;
- }
- }
- void interruptHandler() {
- //static int cyclesToGo = 0;
- if (AT91C_BASE_PITC->PITC_PIMR & AT91C_PITC_PITIEN) /* interrupt enabled */ {
- if (AT91C_BASE_PITC->PITC_PISR & AT91C_PITC_PITS) /* timer requested int */ {
- AT91C_BASE_PITC->PITC_PIVR; /* delay ~100 ms */
- toggleLeftLed();
- //if (!cyclesToGo) {
- // toggleLeftLed(); /* actual delayed function */
- // cyclesToGo = DELAY * SEC_TO_MILISEC / INTERRUPT_CHECK_IN_MS; // reload cyclesToGo value
- //}
- //--cyclesToGo;
- }
- }
- // else /* another device requested interrupts */ {
- // check which device requested INT,
- // process INT, clear INT flag,
- // if unknown device, just increase counter of unknown interrupts
- // }
- }
- void PIT_init() {
- AT91C_BASE_AIC->AIC_IDCR = 1 << AT91C_ID_SYS; // 2nd step
- AT91C_BASE_AIC->AIC_SVR[AT91C_ID_SYS] = (unsigned int) interruptHandler; // 3rd step
- 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 */
- AT91C_BASE_AIC->AIC_ICCR = 1 << AT91C_ID_SYS; // 5th step
- AT91C_BASE_AIC->AIC_IECR = 1 << AT91C_ID_SYS; // 6th step
- AT91C_BASE_PITC->PITC_PIMR = AT91C_PITC_PITIEN /* 7th step */ | AT91C_PITC_PITEN /* 8th step */ | (0x000FFFFFU & (INTERRUPT_CHECK_IN_MS*CYCLES_TO_MS));
- // <TO DO> 9th step
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement