4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2008 Develer S.r.l. (http://www.develer.com/)
30 * All Rights Reserved.
33 * \brief Stepper hardware-specific definitions
36 * \author Daniele Basile <asterix@develer.com>
40 #include <cfg/compiler.h>
41 #include <cfg/macros.h>
42 #include <drv/stepper.h>
46 * Setting master clock prescaler for all timer couter
48 * You could choise one of these:
49 * - TC_CLKS_MCK2: Selects MCK / 2
50 * - TC_CLKS_MCK8: Selects MCK / 8
51 * - TC_CLKS_MCK32: Selects MCK / 32
52 * - TC_CLKS_MCK128: Selects MCK / 128
53 * - TC_CLKS_MCK1024: Selects MCK / 1024
55 #if STEPPER_PRESCALER_LOG2 == 1
56 #define STEPPER_MCK_PRESCALER TC_CLKS_MCK2
57 #elif STEPPER_PRESCALER_LOG2 == 3
58 #define STEPPER_MCK_PRESCALER TC_CLKS_MCK8
59 #elif STEPPER_PRESCALER_LOG2 == 5
60 #define STEPPER_MCK_PRESCALER TC_CLKS_MCK32
61 #elif STEPPER_PRESCALER_LOG2 == 7
62 #define STEPPER_MCK_PRESCALER TC_CLKS_MCK128
63 #elif STEPPER_PRESCALER_LOG2 == 10
64 #define STEPPER_MCK_PRESCALER TC_CLKS_MCK1024
66 #error Unsupported stepper prescaler value.
70 * Timer counter hw enumeration.
85 * IRQ callback function type definition.
87 typedef void (*irq_t)(void);
90 * Timer contex structure.
92 typedef struct TimerCounter
94 int timer_id; ///< Timer counter ID
95 uint32_t blk_ctrl_set; ///< Control block setting for this timer
96 reg32_t *chl_mode_reg; ///< Channel mode register
97 reg32_t *chl_ctrl_reg; ///< Channel control register
98 reg32_t *comp_reg; ///< Compare register
99 reg32_t *comp_c_reg; ///< C compare register
100 reg32_t *count_val_reg; ///< Current timer counter value
101 uint32_t comp_effect_mask; ///< Bit mask for TIO register compare effects
102 uint32_t comp_effect_set; ///< Set TIO on register compare event
103 uint32_t comp_effect_clear; ///< Clear TIO on register compare event
104 uint32_t comp_effect_c_mask; ///< Bit mask for TIO on C register compare effects
105 uint32_t comp_effect_c_clear; ///< Clear TIO on C register compare event
106 uint32_t ext_event_set; ///< Setting for extern event trigger for TIOB
107 reg32_t *irq_enable_reg; ///< Enable interrupt register
108 reg32_t *irq_disable_reg; ///< Disable interrupt register
109 uint32_t irq_set_mask; ///< IRQ flag bit for select TIO
110 reg32_t *irq_mask_reg; ///< IRQ mask register
111 irq_t isr; ///< IRQ handler
112 reg32_t *status_reg; ///< Timer status register
113 int tio_pin; ///< Timer I/O pin
114 stepper_isr_t callback; ///< Interrupt callback pointer
115 struct Stepper *motor; ///< Stepper context structure
120 * Enable interrupt for timer counter compare event.
122 INLINE void stepper_tc_irq_enable(struct TimerCounter *timer)
124 *timer->irq_enable_reg = timer->irq_set_mask;
128 * Disable interrupt for timer counter compare event.
130 INLINE void stepper_tc_irq_disable(struct TimerCounter *timer)
132 *timer->irq_disable_reg = timer->irq_set_mask;
136 * Set delay for next interrupt compare event.
138 INLINE void stepper_tc_setDelay(struct TimerCounter *timer, stepper_time_t delay)
140 *timer->comp_reg += delay;
145 * Set delay for next interrupt compare event.
147 INLINE void stepper_tc_resetTimer(struct TimerCounter *timer)
149 *timer->comp_reg = 0;
153 * Programm timer counter to generate a pulse on select TIO output.
155 INLINE void FAST_FUNC stepper_tc_doPulse(struct TimerCounter *timer)
157 *timer->chl_mode_reg &= ~timer->comp_effect_mask;
158 *timer->chl_mode_reg |= timer->comp_effect_set;
162 * Programm timer counter to not generate a pulse on select TIO output.
164 INLINE void FAST_FUNC stepper_tc_skipPulse(struct TimerCounter *timer)
166 *timer->chl_mode_reg &= ~timer->comp_effect_mask;
169 void stepper_tc_setup(int index, stepper_isr_t callback, struct Stepper *motor);
170 void stepper_tc_init(void);
173 * Test the hardware timer counter on board.
174 * This test generate a square waveform through irq, setting
175 * the timer register.
177 void stepper_timer_test_brute(void);
179 * Test the timer driver structure.
180 * This test generate a square waveform through irq.
181 * The irq callback is programmable, and all timer setting
182 * are save in one data structure. Every pulse is generate through
183 * a call of this irq callback.
185 void stepper_timer_test_prestepper(struct Stepper *local_motor, struct StepperConfig *local_cfg, int index);