Rename CLOCK_FREQ macro to CPU_FREQ: now clock frequency has to be set in the makefile.
[bertos.git] / bertos / hw / hw_stepper.h
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 Stepper hardware-specific definitions
34  *
35  * \version $Id$
36  *
37  * \author Daniele Basile <asterix@develer.com>
38  */
39
40
41 #ifndef HW_STEPPER_H
42 #define HW_STEPPER_H
43
44 #include <hw/hw_cpufreq.h>
45
46 #include <cfg/macros.h>
47
48 #include <drv/timer.h>
49
50 #warning TODO:This is an example implentation, you must implement it!
51
52 #define STEPPER_STROBE_INIT \
53 do { \
54                 /* put init code for strobe */ \
55 } while (0)
56
57
58 #define STEPPER_STROBE_ON       do { /* Implement me! */ } while(0)
59 #define STEPPER_STROBE_OFF      do { /* Implement me! */ } while(0)
60
61 /**
62  * CPU clock frequency is divided by 2^STEPPER_PRESCALER_LOG2 to
63  * obtain stepper clock.
64  */
65 #define STEPPER_PRESCALER_LOG2    1
66
67 /**
68  * Stepper timer clock frequency.
69  */
70 #define STEPPER_CLOCK ((CPU_FREQ) >> STEPPER_PRESCALER_LOG2)
71
72 /**
73  * us delay to reset a stepper motor.
74  * This is the time neccessary to reset
75  * the stepper controll chip. (see datasheet for more detail).
76  */
77 #define STEPPER_RESET_DELAY 1
78
79 /*
80  * Pins define for each stepper
81  */
82 #define STEPPER_1_CW_CCW_PIN             0
83 #define STEPPER_1_HALF_FULL_PIN          0
84 #define STEPPER_1_CONTROL_PIN            0
85 #define STEPPER_1_ENABLE_PIN             0
86 #define STEPPER_1_RESET_PIN              0
87
88 /* put here other stepper motor */
89
90 #define STEPPER_1_SET            do { /* Implement me! */ } while(0)
91 /* add here the set for other stepper motor */
92
93 #define STEPPER_1_CLEAR          do { /* Implement me! */ } while(0)
94 /* add here the clear for other stepper motor */
95
96 /*
97  * Generic macro definition
98  */
99
100 /*
101  * Stepper init macro
102  */
103 #define STEPPER_PIN_INIT_MACRO(port, index) do { \
104                 /* Add here init pin code */ \
105         } while (0)
106
107 /*
108  * Stepper commands macros
109  */
110 #define STEPPER_SET_CW(index)             do { /* Implement me! */ } while (0)
111 #define STEPPER_SET_CCW(index)            do { /* Implement me! */ } while (0)
112 #define STEPPER_SET_HALF(index)           do { /* Implement me! */ } while (0)
113 #define STEPPER_SET_FULL(index)           do { /* Implement me! */ } while (0)
114 #define STEPPER_SET_CONTROL_LOW(index)    do { /* Implement me! */ } while (0)
115 #define STEPPER_SET_CONTROL_HIGHT(index)  do { /* Implement me! */ } while (0)
116 #define STEPPER_SET_ENABLE(index)         do { /* Implement me! */ } while (0)
117 #define STEPPER_SET_DISABLE(index)        do { /* Implement me! */ } while (0)
118 #define STEPPER_SET_RESET_ENABLE(index)   do { /* Implement me! */ } while (0)
119 #define STEPPER_SET_RESET_DISABLE(index)  do { /* Implement me! */ } while (0)
120
121
122 /*
123  * Reset stepper macro
124  */
125
126 #define STEPPER_RESET_MACRO(index) do { \
127                 STEPPER_SET_RESET_ENABLE(index); \
128                 timer_udelay(STEPPER_RESET_DELAY); \
129                 STEPPER_SET_RESET_DISABLE(index); \
130         } while (0)
131
132 /*
133  * Set half or full step macro
134  */
135 #define STEPPER_SET_STEP_MODE_MACRO(index, flag) do { \
136                 if (flag) \
137                         STEPPER_SET_HALF(index); \
138                 else \
139                         STEPPER_SET_FULL(index); \
140         } while (0)
141
142 /*
143  * Set control status macro
144  */
145 #warning TODO: This macro is not implemented (see below)
146
147 #define STEPPER_SET_CONTROL_BIT_MACRO(index, flag) do { \
148                 /* if (flag) */ \
149                         /* WARNING This macros not implemented */ \
150                 /* else */ \
151                         /* WARNING This macros not implemented */ \
152         } while (0)
153
154 /*
155  * Set current power macro
156  */
157 #warning TODO: This macro is not implemented (see below)
158
159 #define STEPPER_SET_POWER_CURRENT_MACRO(index, flag) do { \
160                 /* if (flag) */ \
161                         /* WARNING This macrois not implemented */ \
162                 /* else */ \
163                         /* WARNING This macrois not implemented */ \
164         } while (0)
165
166 /*
167  * Set rotation of stepper motor
168  * - dir = 1: positive rotation
169  * - dir = 0: no motor moviment
170  * - dir = -1: negative rotation
171  *
172  */
173 #define STEPPER_SET_DIRECTION_MACRO(index, dir) do { \
174                 switch (dir) \
175                 { \
176                 case 1: \
177                         STEPPER_SET_CW(index); \
178                         break; \
179                 case -1: \
180                         STEPPER_SET_CCW(index); \
181                         break; \
182                 case 0: \
183                         break; \
184                 } \
185         } while (0)
186
187
188 /*
189  * Define macros for manage low level of stepper.
190  */
191
192 #define STEPPER_INIT()  do { \
193                 STEPPER_PIN_INIT_MACRO(A, 1); \
194                 /* Add here code for other stepper motor */ \
195         } while (0)
196
197
198 /*
199  * Enable select stepper motor
200  */
201 #define STEPPER_ENABLE(index) do { \
202                 switch (index) \
203                 { \
204                 case 1: \
205                         STEPPER_SET_ENABLE(1); \
206                         break; \
207                         /* Add here code for other stepper motor */ \
208                 } \
209         } while (0)
210
211 /*
212  * Enable all stepper connect to micro
213  */
214 #define STEPPER_ENABLE_ALL() do { \
215                 STEPPER_SET_ENABLE(1); \
216                 /* Add here code for other stepper motor */ \
217         } while (0)
218
219 /*
220  * Disable select stepper motor
221  */
222 #define STEPPER_DISABLE(index) do { \
223                 switch (index) \
224                 { \
225                 case 1: \
226                         STEPPER_SET_DISABLE(1); \
227                         break; \
228                         /* Add here code for other stepper motor */ \
229                 } \
230         } while (0)
231
232 /*
233  * Disable all stepper connect to micro
234  */
235 #define STEPPER_DISABLE_ALL() do { \
236                 STEPPER_SET_DISABLE(1); \
237                 /* Add here code for other stepper motor */ \
238         } while (0)
239
240 /*
241  * Reset selected stepper motor
242  */
243 #define STEPPER_RESET(index) do { \
244                 switch (index) \
245                 { \
246                 case 1: \
247                         STEPPER_RESET_MACRO(1); \
248                         break; \
249                         /* Add here code for other stepper motor */ \
250                 } \
251         } while (0)
252
253 /*
254  * Reset all stepper motor
255  */
256 #define STEPPER_RESET_ALL() do { \
257                 STEPPER_RESET_MACRO(1) \
258                 /* Add here code for other stepper motor */ \
259         } while (0)
260
261 // Set half/full step macros
262 #define STEPPER_SET_HALF_STEP(index, flag) do { \
263                 switch (index) \
264                 { \
265                 case 1: \
266                         STEPPER_SET_STEP_MODE_MACRO(1, flag); \
267                         break; \
268                         /* Add here code for other stepper motor */ \
269                 } \
270         } while (0)
271
272
273 // Control status
274 #define STEPPER_SET_CONTROL_BIT(index, flag) do { \
275                 switch (index) \
276                 { \
277                 case 1: \
278                         STEPPER_SET_CONTROL_BIT_MACRO(1, flag); \
279                         break; \
280                         /* Add here code for other stepper motor */ \
281                 } \
282         } while (0)
283
284
285 // Set stepper power current
286 #define STEPPER_SET_POWER_CURRENT(index, flag) do { \
287                 switch (index) \
288                 { \
289                 case 1: \
290                         STEPPER_SET_POWER_CURRENT_MACRO(1, flag); \
291                         break; \
292                         /* Add here code for other stepper motor */ \
293                 } \
294         } while (0)
295
296 // Set rotation dirction of stepper motor
297 #define STEPPER_SET_DIRECTION(index, dir) do { \
298                 switch (index) \
299                 { \
300                 case 1: \
301                         STEPPER_SET_DIRECTION_MACRO(1, dir); \
302                         break; \
303                         /* Add here code for other stepper motor */ \
304                 } \
305         } while (0)
306
307 #endif /* HW_STEPPER_H */
308
309