stepper_at91_hwtest.c

Go to the documentation of this file.
00001 
00040 #include "stepper_at91.h"
00041 
00042 #include "cfg/cfg_stepper.h"
00043 #include <cfg/macros.h>
00044 #include <cfg/debug.h>
00045 
00046 #include <cpu/types.h>
00047 #include <cpu/irq.h>
00048 
00049 #include <io/arm.h>
00050 
00051 
00052 #warning FIXME:This test is incomplete.. you MUST review..
00053 
00054 #if 0
00055 static void stepper_test_irq_schedule(struct Stepper *motor, stepper_time_t delay)
00056 {
00057     stepper_tc_doPulse(motor->timer);
00058     stepper_tc_setDelay(motor->timer, delay);
00059 }
00060 
00061 static void stepper_test_irq(struct Stepper *motor)
00062 {
00063 
00064     stepper_test_irq_schedule(motor, 300);
00065 }
00066 /*
00067  * Test a timer couter driver
00068  */
00069 void stepper_timer_test_prestepper(struct Stepper *local_motor, struct StepperConfig *local_cfg, int index)
00070 {
00071     local_cfg->pulse = 300;
00072 
00073     local_motor->cfg = local_cfg;
00074     stepper_tc_init(index, &stepper_test_irq, local_motor);
00075     stepper_tc_irq_enable(local_motor->timer);
00076 }
00077 
00078 
00079 bool su = true;
00080 bool sub = true;
00081 uint16_t periodo_st0 = 100;
00082 uint16_t periodo_st1 = 233;
00083 
00084 static void tc_irq(void) __attribute__ ((interrupt));
00085 static void tc_irq(void)
00086 {
00087     uint32_t status_reg = TC2_SR & TC2_IMR;
00088 
00089     if (status_reg & BV(TC_CPAS))
00090     {
00091         TC2_CMR &= ~TC_ACPA_MASK;
00092         if (su)
00093         {
00094             TC2_CMR |= TC_ACPA_CLEAR_OUTPUT;
00095             TC2_RA += periodo_st0;
00096         }
00097         else
00098         {
00099             TC2_CMR |= TC_ACPA_SET_OUTPUT;
00100             TC2_RA += periodo_st1;
00101         }
00102         su = !su;
00103     }
00104     if (status_reg & BV(TC_CPBS))
00105     {
00106         TC2_CMR &= ~TC_BCPB_MASK ;
00107         if (sub)
00108         {
00109             TC2_CMR |= TC_BCPB_CLEAR_OUTPUT;
00110             TC2_RB += periodo_st0;
00111         }
00112         else
00113         {
00114             TC2_CMR |= TC_BCPB_SET_OUTPUT;
00115             TC2_RB += periodo_st1;
00116         }
00117         sub = !sub;
00118     }
00119     /* Inform hw that we have served the IRQ */
00120     AIC_EOICR = 0;
00121 }
00122 
00123 /*
00124  * Test a timer couter hardware
00125  */
00126 void stepper_timer_test_brute(void)
00127 {
00128     PIOA_PDR |= BV(26) | BV(27);
00129     PIOA_BSR |= BV(26) | BV(27);
00130 
00131     // Power on TCLK0
00132     PMC_PCER |= BV(TC2_ID);// | BV(TC1_ID) | BV(TC2_ID);
00133 
00134     TC_BCR = 1;
00135     TC_BMR |= TC_NONEXC2;
00136 
00137     // Select waveform mode
00138     TC2_CMR = BV(TC_WAVE);
00139 
00140     TC2_CMR |= TC_EEVT_XC2;
00141     TC2_CMR |= TC_WAVSEL_UP;
00142     TC2_CMR |= TC_CLKS_MCK8;
00143 
00144     //Set waveform on TIOA and TIOB
00145     TC2_CMR |= TC_ACPA_SET_OUTPUT;
00146     TC2_CMR |= TC_BCPB_SET_OUTPUT;
00147 
00148 
00149     //Reset all comp_reg register
00150     TC2_RA = 0;
00151     TC2_RB = 0;
00152 
00153     cpuflags_t flags;
00154     IRQ_SAVE_DISABLE(flags);
00155 
00156     /* Set the vector. */
00157     AIC_SVR(TC2_ID) = tc_irq;
00158     /* Initialize to edge triggered with defined priority. */
00159     AIC_SMR(TC2_ID) = AIC_SRCTYPE_INT_EDGE_TRIGGERED;
00160     /* Enable the USART IRQ */
00161     AIC_IECR = BV(TC2_ID);
00162 
00163     IRQ_RESTORE(flags);
00164 
00165     // Disable all interrupt
00166     TC2_IDR = 0xFFFFFFFF;
00167 
00168     //Enable interrupt on RA, RB
00169     TC2_IER = BV(TC_CPAS) | BV(TC_CPBS);
00170 
00171     //Enable timer and trig it
00172     TC2_CCR = BV(TC_CLKEN) | BV(TC_SWTRG);
00173 }
00174 #endif
00175