Specific the directory for all hw and cfg module. Use double quote for cfg and hw...
[bertos.git] / bertos / cpu / arm / drv / stepper_at91_hw_test.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  * \version $Id$
36  *
37  * \author Daniele Basile <asterix@develer.com>
38  */
39
40 #include "stepper_at91.h"
41
42 #include "cfg/cfg_stepper.h"
43 #include <cfg/macros.h>
44 #include <cfg/debug.h>
45
46 #include <cpu/types.h>
47 #include <cpu/irq.h>
48
49 #include <io/arm.h>
50
51
52 #warning FIXME:This test is incomplete.. you MUST review..
53
54 #if 0
55 static void stepper_test_irq_schedule(struct Stepper *motor, stepper_time_t delay)
56 {
57         stepper_tc_doPulse(motor->timer);
58         stepper_tc_setDelay(motor->timer, delay);
59 }
60
61 static void stepper_test_irq(struct Stepper *motor)
62 {
63
64         stepper_test_irq_schedule(motor, 300);
65 }
66 /*
67  * Test a timer couter driver
68  */
69 void stepper_timer_test_prestepper(struct Stepper *local_motor, struct StepperConfig *local_cfg, int index)
70 {
71         local_cfg->pulse = 300;
72
73         local_motor->cfg = local_cfg;
74         stepper_tc_init(index, &stepper_test_irq, local_motor);
75         stepper_tc_irq_enable(local_motor->timer);
76 }
77
78
79 bool su = true;
80 bool sub = true;
81 uint16_t periodo_st0 = 100;
82 uint16_t periodo_st1 = 233;
83
84 static void tc_irq(void) __attribute__ ((interrupt));
85 static void tc_irq(void)
86 {
87         uint32_t status_reg = TC2_SR & TC2_IMR;
88
89         if (status_reg & BV(TC_CPAS))
90         {
91                 TC2_CMR &= ~TC_ACPA_MASK;
92                 if (su)
93                 {
94                         TC2_CMR |= TC_ACPA_CLEAR_OUTPUT;
95                         TC2_RA += periodo_st0;
96                 }
97                 else
98                 {
99                         TC2_CMR |= TC_ACPA_SET_OUTPUT;
100                         TC2_RA += periodo_st1;
101                 }
102                 su = !su;
103         }
104         if (status_reg & BV(TC_CPBS))
105         {
106                 TC2_CMR &= ~TC_BCPB_MASK ;
107                 if (sub)
108                 {
109                         TC2_CMR |= TC_BCPB_CLEAR_OUTPUT;
110                         TC2_RB += periodo_st0;
111                 }
112                 else
113                 {
114                         TC2_CMR |= TC_BCPB_SET_OUTPUT;
115                         TC2_RB += periodo_st1;
116                 }
117                 sub = !sub;
118         }
119         /* Inform hw that we have served the IRQ */
120         AIC_EOICR = 0;
121 }
122
123 /*
124  * Test a timer couter hardware
125  */
126 void stepper_timer_test_brute(void)
127 {
128         PIOA_PDR |= BV(26) | BV(27);
129         PIOA_BSR |= BV(26) | BV(27);
130
131         // Power on TCLK0
132         PMC_PCER |= BV(TC2_ID);// | BV(TC1_ID) | BV(TC2_ID);
133
134         TC_BCR = 1;
135         TC_BMR |= TC_NONEXC2;
136
137         // Select waveform mode
138         TC2_CMR = BV(TC_WAVE);
139
140         TC2_CMR |= TC_EEVT_XC2;
141         TC2_CMR |= TC_WAVSEL_UP;
142         TC2_CMR |= TC_CLKS_MCK8;
143
144         //Set waveform on TIOA and TIOB
145         TC2_CMR |= TC_ACPA_SET_OUTPUT;
146         TC2_CMR |= TC_BCPB_SET_OUTPUT;
147
148
149         //Reset all comp_reg register
150         TC2_RA = 0;
151         TC2_RB = 0;
152
153         cpuflags_t flags;
154         IRQ_SAVE_DISABLE(flags);
155
156         /* Set the vector. */
157         AIC_SVR(TC2_ID) = tc_irq;
158         /* Initialize to edge triggered with defined priority. */
159         AIC_SMR(TC2_ID) = AIC_SRCTYPE_INT_EDGE_TRIGGERED;
160         /* Enable the USART IRQ */
161         AIC_IECR = BV(TC2_ID);
162
163         IRQ_RESTORE(flags);
164
165         // Disable all interrupt
166         TC2_IDR = 0xFFFFFFFF;
167
168         //Enable interrupt on RA, RB
169         TC2_IER = BV(TC_CPAS) | BV(TC_CPBS);
170
171         //Enable timer and trig it
172         TC2_CCR = BV(TC_CLKEN) | BV(TC_SWTRG);
173 }
174 #endif
175