stepper_at91.c

Go to the documentation of this file.
00001 
00056 #include "stepper_at91.h"
00057 
00058 #include "cfg/cfg_stepper.h"
00059 #include <cfg/macros.h>
00060 #include <cfg/debug.h>
00061 
00062 #include <cpu/types.h>
00063 #include <cpu/irq.h>
00064 
00065 #include <io/arm.h>
00066 
00067 
00068 /*
00069  * Delay to set C compare to clear output
00070  * on select TIO output
00071  */
00072 #define STEPPER_DELAY_ON_COMPARE_C 20
00073 
00074 /*
00075  * Forward declaration for interrupt handler
00076  */
00077 static void stepper_tc0_irq(void);
00078 static void stepper_tc1_irq(void);
00079 static void stepper_tc2_irq(void);
00080 
00082 static struct TimerCounter stepper_timers[CONFIG_TC_STEPPER_MAX_NUM] =
00083 {
00084     { //Timer Counter settings for TIOA0 output pin
00085         .timer_id = TC0_ID,
00086         .blk_ctrl_set = TC_NONEXC0,
00087         .chl_mode_reg = &TC0_CMR,
00088         .chl_ctrl_reg = &TC0_CCR,
00089         .comp_effect_mask = TC_ACPA_MASK,
00090         .comp_effect_set = TC_ACPA_SET_OUTPUT,
00091         .comp_effect_clear = TC_ACPA_CLEAR_OUTPUT,
00092         .comp_effect_c_mask = TC_ACPC_MASK,
00093         .comp_effect_c_clear = TC_ACPC_CLEAR_OUTPUT,
00094         .ext_event_set = TC_EEVT_XC0,
00095         .comp_reg = &TC0_RA,
00096         .comp_c_reg = &TC0_RC,
00097         .count_val_reg = &TC0_CV,
00098         .irq_enable_reg = &TC0_IER,
00099         .irq_disable_reg = &TC0_IDR,
00100         .irq_set_mask = BV(TC_CPAS),
00101         .irq_mask_reg = &TC0_IMR,
00102         .isr = stepper_tc0_irq,
00103         .status_reg = &TC0_SR,
00104         .tio_pin = TIOA0,
00105         .callback = NULL,
00106         .motor = NULL,
00107     },
00108     { //Timer Counter settings for TIOB0 output pin
00109         .timer_id = TC0_ID,
00110         .blk_ctrl_set = TC_NONEXC0,
00111         .chl_mode_reg = &TC0_CMR,
00112         .chl_ctrl_reg = &TC0_CCR,
00113         .comp_reg = &TC0_RB,
00114         .comp_c_reg = &TC0_RC,
00115         .count_val_reg = &TC0_CV,
00116         .comp_effect_mask = TC_BCPB_MASK,
00117         .comp_effect_set = TC_BCPB_SET_OUTPUT,
00118         .comp_effect_clear = TC_BCPB_CLEAR_OUTPUT,
00119         .comp_effect_c_mask = TC_BCPC_MASK,
00120         .comp_effect_c_clear = TC_BCPC_CLEAR_OUTPUT,
00121         .ext_event_set = TC_EEVT_XC0,
00122         .irq_enable_reg = &TC0_IER,
00123         .irq_disable_reg = &TC0_IDR,
00124         .irq_set_mask = BV(TC_CPBS),
00125         .irq_mask_reg = &TC0_IMR,
00126         .isr = stepper_tc0_irq,
00127         .status_reg = &TC0_SR,
00128         .tio_pin = TIOB0,
00129         .callback = NULL,
00130         .motor = NULL,
00131     },
00132     { //Timer Counter settings for TIOA1 output pin
00133         .timer_id = TC1_ID,
00134         .blk_ctrl_set = TC_NONEXC1,
00135         .chl_mode_reg = &TC1_CMR,
00136         .chl_ctrl_reg = &TC1_CCR,
00137         .comp_reg = &TC1_RA,
00138         .comp_c_reg = &TC1_RC,
00139         .count_val_reg = &TC1_CV,
00140         .comp_effect_mask = TC_ACPA_MASK,
00141         .comp_effect_set = TC_ACPA_SET_OUTPUT,
00142         .comp_effect_clear = TC_ACPA_CLEAR_OUTPUT,
00143         .comp_effect_c_mask = TC_ACPC_MASK,
00144         .comp_effect_c_clear = TC_ACPC_CLEAR_OUTPUT,
00145         .ext_event_set = TC_EEVT_XC1,
00146         .irq_enable_reg = &TC1_IER,
00147         .irq_disable_reg = &TC1_IDR,
00148         .irq_set_mask = BV(TC_CPAS),
00149         .irq_mask_reg = &TC1_IMR,
00150         .isr = stepper_tc1_irq,
00151         .status_reg = &TC1_SR,
00152         .tio_pin = TIOA1,
00153         .callback = NULL,
00154         .motor = NULL,
00155     },
00156     { //Timer Counter settings for TIOB1 output pin
00157         .timer_id = TC1_ID,
00158         .blk_ctrl_set = TC_NONEXC1,
00159         .chl_mode_reg = &TC1_CMR,
00160         .chl_ctrl_reg = &TC1_CCR,
00161         .comp_reg = &TC1_RB,
00162         .comp_c_reg = &TC1_RC,
00163         .count_val_reg = &TC1_CV,
00164         .comp_effect_mask = TC_BCPB_MASK,
00165         .comp_effect_set = TC_BCPB_SET_OUTPUT,
00166         .comp_effect_clear = TC_BCPB_CLEAR_OUTPUT,
00167         .comp_effect_c_mask = TC_BCPC_MASK,
00168         .comp_effect_c_clear = TC_BCPC_CLEAR_OUTPUT,
00169         .ext_event_set = TC_EEVT_XC1,
00170         .irq_enable_reg = &TC1_IER,
00171         .irq_disable_reg = &TC1_IDR,
00172         .irq_set_mask = BV(TC_CPBS),
00173         .irq_mask_reg = &TC1_IMR,
00174         .isr = stepper_tc1_irq,
00175         .status_reg = &TC1_SR,
00176         .tio_pin = TIOB1,
00177         .callback = NULL,
00178         .motor = NULL,
00179     },
00180     { //Timer Counter settings for TIOA2 output pin
00181         .timer_id = TC2_ID,
00182         .blk_ctrl_set = TC_NONEXC2,
00183         .chl_mode_reg = &TC2_CMR,
00184         .chl_ctrl_reg = &TC2_CCR,
00185         .comp_reg = &TC2_RA,
00186         .comp_c_reg = &TC2_RC,
00187         .count_val_reg = &TC2_CV,
00188         .comp_effect_mask = TC_ACPA_MASK,
00189         .comp_effect_set = TC_ACPA_SET_OUTPUT,
00190         .comp_effect_clear = TC_ACPA_CLEAR_OUTPUT,
00191         .comp_effect_c_mask = TC_ACPC_MASK,
00192         .comp_effect_c_clear = TC_ACPC_CLEAR_OUTPUT,
00193         .ext_event_set = TC_EEVT_XC2,
00194         .irq_enable_reg = &TC2_IER,
00195         .irq_disable_reg = &TC2_IDR,
00196         .irq_set_mask = BV(TC_CPAS),
00197         .irq_mask_reg = &TC2_IMR,
00198         .isr = stepper_tc2_irq,
00199         .status_reg = &TC2_SR,
00200         .tio_pin = TIOA2,
00201         .callback = NULL,
00202         .motor = NULL,
00203     },
00204     { //Timer Counter settings for TIOB2 output pin
00205         .timer_id = TC2_ID,
00206         .blk_ctrl_set = TC_NONEXC2,
00207         .chl_mode_reg = &TC2_CMR,
00208         .chl_ctrl_reg = &TC2_CCR,
00209         .comp_reg = &TC2_RB,
00210         .comp_c_reg = &TC2_RC,
00211         .count_val_reg = &TC2_CV,
00212         .comp_effect_mask = TC_BCPB_MASK,
00213         .comp_effect_set = TC_BCPB_SET_OUTPUT,
00214         .comp_effect_clear = TC_BCPB_CLEAR_OUTPUT,
00215         .comp_effect_c_mask = TC_BCPC_MASK,
00216         .comp_effect_c_clear = TC_BCPC_CLEAR_OUTPUT,
00217         .ext_event_set = TC_EEVT_XC2,
00218         .irq_enable_reg = &TC2_IER,
00219         .irq_disable_reg = &TC2_IDR,
00220         .irq_set_mask = BV(TC_CPBS),
00221         .irq_mask_reg = &TC2_IMR,
00222         .isr = stepper_tc2_irq,
00223         .status_reg = &TC2_SR,
00224         .tio_pin = TIOB2,
00225         .callback = NULL,
00226         .motor = NULL,
00227     }
00228 };
00229 
00233 INLINE void stepper_tc_tio_irq(struct TimerCounter * t)
00234 {
00235     //
00236     *t->chl_mode_reg &= ~t->comp_effect_c_mask;
00237     *t->chl_mode_reg |= t->comp_effect_c_clear;
00238 
00239     /*
00240      * Cleat TIO output on c register compare.
00241      * This generate an pulse with variable lenght, this
00242      * depend to delay that interrupt is realy service.
00243      */
00244     *t->comp_c_reg = *t->count_val_reg + STEPPER_DELAY_ON_COMPARE_C;
00245 
00246     //Call the associate callback
00247     t->callback(t->motor);
00248 
00249     *t->chl_mode_reg &= ~t->comp_effect_c_mask;
00250 }
00251 
00252 
00253 /*
00254  * Interrupt handler for timer counter TCKL0
00255  */
00256 static void ISR_FUNC stepper_tc0_irq(void)
00257 {
00258     /*
00259      * Warning: when we read the status_reg register, we reset it.
00260      * That mean if is occur an interrupt event we can read only
00261      * the last that has been occur. To not miss an interrupt event
00262      * we save the status_reg register and then we read it.
00263      */
00264     uint32_t  status_reg = TC0_SR & TC0_IMR;
00265 
00266     if (status_reg & BV(TC_CPAS))
00267         stepper_tc_tio_irq(&stepper_timers[TC_TIOA0]);
00268 
00269     if (status_reg & BV(TC_CPBS))
00270         stepper_tc_tio_irq(&stepper_timers[TC_TIOB0]);
00271 
00272     /* Inform hw that we have served the IRQ */
00273     AIC_EOICR = 0;
00274 
00275 }
00276 
00277 /*
00278  * Interrupt handler for timer counter TCKL1
00279  */
00280 static void ISR_FUNC stepper_tc1_irq(void)
00281 {
00282     /*
00283      * Warning: when we read the status_reg register, we reset it.
00284      * That mean if is occur an interrupt event we can read only
00285      * the last that has been occur. To not miss an interrupt event
00286      * we save the status_reg register and then we read it.
00287      */
00288     uint32_t  status_reg = TC1_SR & TC1_IMR;
00289 
00290     if (status_reg & BV(TC_CPAS))
00291         stepper_tc_tio_irq(&stepper_timers[TC_TIOA1]);
00292 
00293     if (status_reg & BV(TC_CPBS))
00294         stepper_tc_tio_irq(&stepper_timers[TC_TIOB1]);
00295 
00296 
00297     /* Inform hw that we have served the IRQ */
00298     AIC_EOICR = 0;
00299 }
00300 
00301 
00302 /*
00303  * Interrupt handler for timer counter TCKL2
00304  */
00305 static void ISR_FUNC stepper_tc2_irq(void)
00306 {
00307 
00308     /*
00309      * Warning: when we read the status_reg register, we reset it.
00310      * That mean if is occur an interrupt event we can read only
00311      * the last that has been occur. To not miss an interrupt event
00312      * we save the status_reg register and then we read it.
00313      */
00314     uint32_t  status_reg = TC2_SR & TC2_IMR;
00315 
00316     if (status_reg & BV(TC_CPAS))
00317         stepper_tc_tio_irq(&stepper_timers[TC_TIOA2]);
00318 
00319     if (status_reg & BV(TC_CPBS))
00320         stepper_tc_tio_irq(&stepper_timers[TC_TIOB2]);
00321 
00322     /* Inform hw that we have served the IRQ */
00323     AIC_EOICR = 0;
00324 
00325 }
00326 
00333 void stepper_tc_setup(int index, stepper_isr_t callback, struct Stepper *motor)
00334 {
00335     ASSERT(index < CONFIG_TC_STEPPER_MAX_NUM);
00336 
00337     motor->timer = &stepper_timers[index];
00338 
00339     //Disable PIO controller and enable TIO function
00340     TIO_PIO_PDR = BV(motor->timer->tio_pin);
00341     TIO_PIO_ABSR = BV(motor->timer->tio_pin);
00342 
00343     /*
00344      * Sets timer counter in waveform mode.
00345      * We set as default:
00346      * - Waveform mode 00 (see datasheet for more detail.)
00347      * - Master clock prescaler to STEPPER_MCK_PRESCALER
00348      * - Set none external event
00349      * - Clear pin output on comp_reg
00350      * - None effect on reg C compare
00351      */
00352     *motor->timer->chl_mode_reg = BV(TC_WAVE);
00353     *motor->timer->chl_mode_reg |= motor->timer->ext_event_set;
00354     *motor->timer->chl_mode_reg &= ~TC_WAVSEL_MASK;
00355     *motor->timer->chl_mode_reg |= TC_WAVSEL_UP;
00356     *motor->timer->chl_mode_reg |= STEPPER_MCK_PRESCALER;
00357     *motor->timer->chl_mode_reg |= motor->timer->comp_effect_clear;
00358     *motor->timer->chl_mode_reg &= ~motor->timer->comp_effect_c_mask;
00359 
00360     //Reset comp_reg and C compare register
00361     *motor->timer->comp_reg = 0;
00362     *motor->timer->comp_c_reg = 0;
00363 
00364     //Register interrupt vector
00365     cpu_flags_t flags;
00366     IRQ_SAVE_DISABLE(flags);
00367 
00368     /*
00369      * Warning: To guarantee a correct management of interrupt event, we must
00370      * trig the interrupt on level sensitive. This becouse, we have only a common
00371      * line for interrupt request, and if we have at the same time two interrupt
00372      * request could be that the is service normaly but the second will never
00373      *  been detected and interrupt will stay active but never serviced.
00374      */
00375     AIC_SVR(motor->timer->timer_id) = motor->timer->isr;
00376     AIC_SMR(motor->timer->timer_id) = AIC_SRCTYPE_INT_LEVEL_SENSITIVE;
00377     AIC_IECR = BV(motor->timer->timer_id);
00378 
00379     // Disable interrupt on select timer counter
00380     stepper_tc_irq_disable(motor->timer);
00381 
00382     IRQ_RESTORE(flags);
00383 
00384     //Register callback
00385     motor->timer->callback = callback;
00386     motor->timer->motor = motor;
00387 }
00388 
00392 void stepper_tc_init(void)
00393 {
00394     STEPPER_STROBE_INIT;
00395 
00396     ASSERT(CONFIG_NUM_STEPPER_MOTORS <= CONFIG_TC_STEPPER_MAX_NUM);
00397 
00398     /*
00399      * Enable timer counter:
00400      * - power on all timer counter
00401      * - disable all interrupt
00402      * - disable all external event/timer source
00403      */
00404     for (int i = 0; i < CONFIG_TC_STEPPER_MAX_NUM; i++)
00405     {
00406         PMC_PCER = BV(stepper_timers[i].timer_id);
00407         *stepper_timers[i].irq_disable_reg = 0xFFFFFFFF;
00408         TC_BMR = stepper_timers[i].blk_ctrl_set;
00409     }
00410 
00411     /*
00412      * Enable timer counter and start it.
00413      */
00414     for (int i = 0; i < CONFIG_TC_STEPPER_MAX_NUM; i++)
00415         *stepper_timers[i].chl_ctrl_reg = (BV(TC_CLKEN) | BV(TC_SWTRG));
00416 
00417 }
00418