3 * This provides a 1000Hz tick for the system.
5 * NB At 1000Hz, a U32 will roll over in approx 50 days.
6 * Therefore a todo to get rid of this at some stage.
13 #include "interrupts.h"
16 #include "nxt_motors.h"
19 extern volatile unsigned char gMakeRequest;
21 #define PIT_FREQ 1000 /* Hz */
23 #define LOW_PRIORITY_IRQ 10
25 static volatile U32 systick_sec;
26 static volatile U32 systick_sub_sec;
27 static volatile U32 systick_ms;
29 extern void systick_isr_entry(void);
30 extern void systick_low_priority_entry(void);
32 // Systick low priority
34 systick_low_priority_C(void)
36 *AT91C_AIC_ICCR = (1 << LOW_PRIORITY_IRQ);
37 // gMakeRequest = 1; // trigger Java tick request
38 nxt_avr_1kHz_update();
39 nxt_motor_1kHz_process();
48 /* Read status to confirm interrupt */
49 status = *AT91C_PITC_PIVR;
51 // systick_low_priority_C();
57 if (systick_sub_sec >= PIT_FREQ) {
61 // Trigger low priority task
62 *AT91C_AIC_ISCR = (1 << LOW_PRIORITY_IRQ);
70 // We're using a 32-bitter and can assume that we
71 // don't need to do any locking here.
77 systick_wait_ms(U32 ms)
79 volatile U32 final = ms + systick_ms;
81 while (systick_ms < final) {
87 systick_wait_ns(U32 ns)
89 volatile x = (ns >> 7) + 1;
99 int i_state = interrupts_get_and_disable();
101 aic_mask_off(LOW_PRIORITY_IRQ);
102 aic_set_vector(LOW_PRIORITY_IRQ, (1 << 5) /* positive internal edge */ |
103 AIC_INT_LEVEL_LOW, (U32) systick_low_priority_entry);
104 aic_mask_on(LOW_PRIORITY_IRQ);
106 aic_mask_off(AT91C_PERIPHERAL_ID_SYSIRQ);
107 aic_set_vector(AT91C_PERIPHERAL_ID_SYSIRQ, (1 << 5) /* positive internal edge */ |
108 AIC_INT_LEVEL_NORMAL, (U32) systick_isr_entry);
110 aic_mask_on(AT91C_PERIPHERAL_ID_SYSIRQ);
111 *AT91C_PITC_PIMR = ((CLOCK_FREQUENCY / 16 / PIT_FREQ) - 1) | 0x03000000; /* Enable, enable interrupts */
118 systick_get_time(U32 *sec, U32 *usec)
120 int istate = interrupts_get_and_disable();
125 *usec = systick_sub_sec * (1000000 / PIT_FREQ);
132 static U32 test_counter;
138 systick_wait_ms(2000);
142 void systick_suspend()
144 aic_mask_off(LOW_PRIORITY_IRQ);
147 void systick_resume()
149 aic_mask_on(LOW_PRIORITY_IRQ);