+void ResetSspClk(void) {
+ //enable clock of timer and software trigger
+ AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
+ AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
+ AT91C_BASE_TC2->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
+ while (AT91C_BASE_TC2->TC_CV > 0);
+}
+
+
+uint32_t GetCountSspClk(){
+ uint32_t hi, lo;
+
+ do {
+ hi = AT91C_BASE_TC2->TC_CV;
+ lo = AT91C_BASE_TC0->TC_CV;
+ } while(hi != AT91C_BASE_TC2->TC_CV);
+
+ return (hi << 16) | lo;
+}
+
+
+// -------------------------------------------------------------------------
+// Timer for bitbanging, or LF stuff when you need a very precis timer
+// 1us = 1.5ticks
+// -------------------------------------------------------------------------
+void StartTicks(void){
+ // initialization of the timer
+ AT91C_BASE_PMC->PMC_PCER |= (1 << AT91C_ID_TC0) | (1 << AT91C_ID_TC1);
+ AT91C_BASE_TCB->TCB_BMR = AT91C_TCB_TC0XC0S_NONE | AT91C_TCB_TC1XC1S_TIOA0 | AT91C_TCB_TC2XC2S_NONE;
+
+ // disable TC0 and TC1 for re-configuration
+ AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
+ AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS;
+
+ // first configure TC1 (higher, 0xFFFF0000) 16 bit counter
+ AT91C_BASE_TC1->TC_CMR = AT91C_TC_CLKS_XC1; // just connect to TIOA0 from TC0
+ AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; // re-enable timer and wait for TC0
+
+ // second configure TC0 (lower, 0x0000FFFF) 16 bit counter
+ AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK | // MCK(48MHz) / 32
+ AT91C_TC_WAVE | AT91C_TC_WAVESEL_UP_AUTO |
+ AT91C_TC_ACPA_CLEAR | // RA comperator clears TIOA (carry bit)
+ AT91C_TC_ACPC_SET | // RC comperator sets TIOA (carry bit)
+ AT91C_TC_ASWTRG_SET; // SWTriger sets TIOA (carry bit)
+ AT91C_BASE_TC0->TC_RC = 0; // set TIOA (carry bit) on overflow, return to zero
+ AT91C_BASE_TC0->TC_RA = 1; // clear carry bit on next clock cycle
+ AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; // reset and re-enable timer
+
+ // synchronized startup procedure
+ while (AT91C_BASE_TC0->TC_CV > 0); // wait until TC0 returned to zero
+ while (AT91C_BASE_TC0->TC_CV < 2); // and has started (TC_CV > TC_RA, now TC1 is cleared)
+
+ // return to zero
+ AT91C_BASE_TC1->TC_CCR = AT91C_TC_SWTRG;
+ AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG;
+ while (AT91C_BASE_TC0->TC_CV > 0);