Remove \version svn tag.
[bertos.git] / bertos / cpu / arm / drv / stepper_at91_hwtest.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2008 Develer S.r.l. (http://www.develer.com/)
30  * All Rights Reserved.
31  * -->
32  *
33  * \brief Low level test for stepper driver interface implementation.
34  *
35  *
36  * \author Daniele Basile <asterix@develer.com>
37  */
38
39 #include "stepper_at91.h"
40
41 #include "cfg/cfg_stepper.h"
42 #include <cfg/macros.h>
43 #include <cfg/debug.h>
44
45 #include <cpu/types.h>
46 #include <cpu/irq.h>
47
48 #include <io/arm.h>
49
50
51 #warning FIXME:This test is incomplete.. you MUST review..
52
53 #if 0
54 static void stepper_test_irq_schedule(struct Stepper *motor, stepper_time_t delay)
55 {
56         stepper_tc_doPulse(motor->timer);
57         stepper_tc_setDelay(motor->timer, delay);
58 }
59
60 static void stepper_test_irq(struct Stepper *motor)
61 {
62
63         stepper_test_irq_schedule(motor, 300);
64 }
65 /*
66  * Test a timer couter driver
67  */
68 void stepper_timer_test_prestepper(struct Stepper *local_motor, struct StepperConfig *local_cfg, int index)
69 {
70         local_cfg->pulse = 300;
71
72         local_motor->cfg = local_cfg;
73         stepper_tc_init(index, &stepper_test_irq, local_motor);
74         stepper_tc_irq_enable(local_motor->timer);
75 }
76
77
78 bool su = true;
79 bool sub = true;
80 uint16_t periodo_st0 = 100;
81 uint16_t periodo_st1 = 233;
82
83 static void tc_irq(void) __attribute__ ((interrupt));
84 static void tc_irq(void)
85 {
86         uint32_t status_reg = TC2_SR & TC2_IMR;
87
88         if (status_reg & BV(TC_CPAS))
89         {
90                 TC2_CMR &= ~TC_ACPA_MASK;
91                 if (su)
92                 {
93                         TC2_CMR |= TC_ACPA_CLEAR_OUTPUT;
94                         TC2_RA += periodo_st0;
95                 }
96                 else
97                 {
98                         TC2_CMR |= TC_ACPA_SET_OUTPUT;
99                         TC2_RA += periodo_st1;
100                 }
101                 su = !su;
102         }
103         if (status_reg & BV(TC_CPBS))
104         {
105                 TC2_CMR &= ~TC_BCPB_MASK ;
106                 if (sub)
107                 {
108                         TC2_CMR |= TC_BCPB_CLEAR_OUTPUT;
109                         TC2_RB += periodo_st0;
110                 }
111                 else
112                 {
113                         TC2_CMR |= TC_BCPB_SET_OUTPUT;
114                         TC2_RB += periodo_st1;
115                 }
116                 sub = !sub;
117         }
118         /* Inform hw that we have served the IRQ */
119         AIC_EOICR = 0;
120 }
121
122 /*
123  * Test a timer couter hardware
124  */
125 void stepper_timer_test_brute(void)
126 {
127         PIOA_PDR |= BV(26) | BV(27);
128         PIOA_BSR |= BV(26) | BV(27);
129
130         // Power on TCLK0
131         PMC_PCER |= BV(TC2_ID);// | BV(TC1_ID) | BV(TC2_ID);
132
133         TC_BCR = 1;
134         TC_BMR |= TC_NONEXC2;
135
136         // Select waveform mode
137         TC2_CMR = BV(TC_WAVE);
138
139         TC2_CMR |= TC_EEVT_XC2;
140         TC2_CMR |= TC_WAVSEL_UP;
141         TC2_CMR |= TC_CLKS_MCK8;
142
143         //Set waveform on TIOA and TIOB
144         TC2_CMR |= TC_ACPA_SET_OUTPUT;
145         TC2_CMR |= TC_BCPB_SET_OUTPUT;
146
147
148         //Reset all comp_reg register
149         TC2_RA = 0;
150         TC2_RB = 0;
151
152         cpuflags_t flags;
153         IRQ_SAVE_DISABLE(flags);
154
155         /* Set the vector. */
156         AIC_SVR(TC2_ID) = tc_irq;
157         /* Initialize to edge triggered with defined priority. */
158         AIC_SMR(TC2_ID) = AIC_SRCTYPE_INT_EDGE_TRIGGERED;
159         /* Enable the USART IRQ */
160         AIC_IECR = BV(TC2_ID);
161
162         IRQ_RESTORE(flags);
163
164         // Disable all interrupt
165         TC2_IDR = 0xFFFFFFFF;
166
167         //Enable interrupt on RA, RB
168         TC2_IER = BV(TC_CPAS) | BV(TC_CPBS);
169
170         //Enable timer and trig it
171         TC2_CCR = BV(TC_CLKEN) | BV(TC_SWTRG);
172 }
173 #endif
174