📄 freqgen.c
字号:
/********************************************************************* Description: freqgen.c* A HAL component that generates step pulses at a * specific frequency in software.** Author: John Kasunich* License: GPL Version 2* * Copyright (c) 2003 All rights reserved.** Last change: # $Revision: 1.14 $* $Author: jepler $* $Date: 2006/03/21 02:29:02 $********************************************************************//** This file, 'freqgen.c', is a HAL component that generates step pulses at a specific frequency in software. The maximum step rate will depend on the speed of the PC, but is expected to exceed 1KHz for even the slowest computers, and may reach 10KHz on fast ones. It is a realtime component. Note that this is _not_ the preferred step pulse generator for EMC2. This module simply generates a frequency (velocity). A separate PID or other loop is needed to turn EMC's position commands into velocity commands, and the PID loop needs to be tuned, which adds unneccessary complexity to the machine setup. For EMC and other applications that use position instead of velocity commands, the component "stepgen" is preferred. It contains a built in, pre-tuned position to velocity converter. This module is provided only for special cases where it is more appropriate. It supports up to 8 pulse generators. Each generator can produce several types of outputs in addition to step/dir, including quadrature, half- and full-step unipolar and bipolar, three phase, and five phase. A 32 bit feedback value is provided indicating the current position of the motor (assuming no lost steps). The number of step generators and type of outputs is determined by the insmod command line parameter 'step_type'. It accepts a comma separated (no spaces) list of up to 8 stepping types to configure up to 8 channels. So a command line like this: insmod freqgen step_type=0,0,1,2 will install four step generators, two using stepping type 0, one using type 1, and one using type 2. The driver exports three functions. 'freqgen.make-pulses', is responsible for actually generating the step pulses. It must be executed in a fast thread to reduce pulse jitter. The other two functions are normally called from a much slower thread. 'freqgen.update-freq' reads the frequency command and sets internal variables used by 'freqgen.make-pulses'. 'freqgen.capture-position' captures and scales the current values of the position feedback counters. Both 'update-freq' and 'capture-position' use floating point, 'make-pulses' does not. The component can optionally create two threads, one that supports floating point and one that does not. The 'period' parameter (at insmod time) determines the period of the non-FP thread, which is called 'freqgen.thread'. The 'fp_period' parameter sets the period of the floating point thread, which is called 'freqgen.threadFP' Polarity: All signals from this module have fixed polarity (active high in most cases). If the driver needs the opposite polarity, the signals can be inverted using parameters exported by the hardware driver(s) such as ParPort. Stepping Types: This module supports a number of stepping types, as follows: Type 0: Step and Direction _____ _____ _____ STEP ____________/ \_______/ \____________/ \___ | | | | |--(1)--|-(2)-|--(3)--| |--(4)--| ____|___________________________________|_____________ DIR ____X___________________________________X_____________ There are two output pins, STEP and DIR. Timing is controlled by HAL parameters. The parameters are (1): 'freqgen.n.dirsetup' minimum delay from a change on the DIR line to the beginning of a step pulse, (2): 'freqgen.n.steplen' length of the step pulse, (3): 'freqgen.n.stepspace', space between step pulses, and (4): 'freqgen.n.dirhold', minimum delay after step pulse before DIR may change. The default values for all four parameters are 1, which means 1 period of the thread. A positive frequency command results in DIR low, negative frequency command means DIR high. The minimum time between step pulses is 'steplen' + 'stepspace' periods, and the frequency command is clamped to avoid exceeding these limits. Type 1: Up/Down (aka Pseudo-PWM) There are two output pins, UP and DOWN. Whenever a step is required, either UP or DOWN is asserted for a single period. The frequency command is not clamped, so a step may be required every period, and the UP or DOWN line may be asserted for several periods in a row. (At the maximum freqency command, UP or DOWN will be constantly asserted.) This type of signal may be usefull with some stepper drives, but is primarily intended as a simple and cheap DAC. A filter and differential amp connected between UP and DOWN can produce a +/-10V signal, with bandwidth and resolution determined by the filter (in general, faster bandwidth gives lower resolution, and vice-versa.) All the remaining stepping types are simply different patterns of output states. For all of these types, a step can occur in every period. When a step occurs, the output state changes to the next (or previous) state in the state listings that follow. There are from two to five output pins, called 'PhaseA' thru 'PhaseE'. Type 2: Quadrature (aka Gray/Grey code) State Phase A Phase B 0 1 0 1 1 1 2 0 1 3 0 0 0 1 0 Type 3: Three Wire State Phase A Phase B Phase C 0 1 0 0 1 0 1 0 2 0 0 1 0 1 0 0 Type 4: Three Wire HalfStep State Phase A Phase B Phase C 0 1 0 0 1 1 1 0 2 0 1 0 3 0 1 1 4 0 0 1 5 1 0 1 0 1 0 0 Type 5: Unipolar Full Step (one winding on) State Phase A Phase B Phase C Phase D 0 1 0 0 0 1 0 1 0 0 2 0 0 1 0 3 0 0 0 1 0 1 0 0 0 Type 6: Unipolar Full Step (two windings on) State Phase A Phase B Phase C Phase D 0 1 1 0 0 1 0 1 1 0 2 0 0 1 1 3 1 0 0 1 0 1 1 0 0 Type 7: Bipolar Full Step (one winding on) State Phase A Phase B Phase C Phase D 0 1 0 0 0 1 1 1 1 0 2 0 1 1 1 3 0 0 0 1 0 1 0 0 0 Type 8: Bipolar Full Step (two windings on) State Phase A Phase B Phase C Phase D 0 1 0 1 0 1 0 1 1 0 2 0 1 0 1 3 1 0 0 1 0 1 0 1 0 Type 9: Unipolar Half Step State Phase A Phase B Phase C Phase D 0 1 0 0 0 1 1 1 0 0 2 0 1 0 0 3 0 1 1 0 4 0 0 1 0 5 0 0 1 1 6 0 0 0 1 7 1 0 0 1 0 1 0 0 0 Type 10: Bipolar Half Step State Phase A Phase B Phase C Phase D 0 1 0 0 0 1 1 0 1 0 2 1 1 1 0 3 0 1 1 0 4 0 1 1 1 5 0 1 0 1 6 0 0 0 1 7 1 0 0 1 0 1 0 0 0 Type 11: Five Wire Unipolar State Phase A Phase B Phase C Phase D Phase E 0 1 0 0 0 0 1 0 1 0 0 0 2 0 0 1 0 0 3 0 0 0 1 0 4 0 0 0 0 1 0 1 0 0 0 0 Type 12: Five Wire Wave State Phase A Phase B Phase C Phase D Phase E 0 1 1 0 0 0 1 0 1 1 0 0 2 0 0 1 1 0 3 0 0 0 1 1 4 1 0 0 0 1 0 1 1 0 0 0 Type 13: Five Wire Unipolar HalfStep State Phase A Phase B Phase C Phase D Phase E 0 1 0 0 0 0 1 1 1 0 0 0 2 0 1 0 0 0 3 0 1 1 0 0 4 0 0 1 0 0 5 0 0 1 1 0 6 0 0 0 1 0 7 0 0 0 1 1 8 0 0 0 0 1 9 1 0 0 0 1 0 1 0 0 0 0 Type 14: Five Wire Wave HalfStep State Phase A Phase B Phase C Phase D Phase E 0 1 1 0 0 0 1 1 1 1 0 0 2 0 1 1 0 0 3 0 1 1 1 0 4 0 0 1 1 0 5 0 0 1 1 1 6 0 0 0 1 1 7 1 0 0 1 1 8 1 0 0 0 1 9 1 1 0 0 1 0 1 1 0 0 0*//** Copyright (C) 2003 John Kasunich <jmkasunich AT users DOT sourceforge DOT net>*//** This program is free software; you can redistribute it and/or modify it under the terms of version 2.1 of the GNU General Public License as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA THE AUTHORS OF THIS LIBRARY ACCEPT ABSOLUTELY NO LIABILITY FOR ANY HARM OR LOSS RESULTING FROM ITS USE. IT IS _EXTREMELY_ UNWISE TO RELY ON SOFTWARE ALONE FOR SAFETY. Any machinery capable of harming persons must have provisions for completely removing power from all motors, etc, before persons enter any danger area. All machinery must be designed to comply with local and national safety codes, and the authors of this software can not, and do not, take any responsibility for such compliance. This code was written as part of the EMC HAL project. For more information, go to www.linuxcnc.org.*/#ifndef RTAPI#error This is a realtime component only!#endif#include <linux/ctype.h> /* isspace() */#include "rtapi.h" /* RTAPI realtime OS API */#include "rtapi_app.h" /* RTAPI realtime module decls */#include "hal.h" /* HAL public API decls */#define MAX_CHAN 8#ifdef MODULE/* module information */MODULE_AUTHOR("John Kasunich");MODULE_DESCRIPTION("Frequency Generator for EMC HAL");#ifdef MODULE_LICENSEMODULE_LICENSE("GPL");#endif /* MODULE_LICENSE */int step_type[MAX_CHAN] = { -1, -1, -1, -1, -1, -1, -1, -1 };MODULE_PARM(step_type, "1-8i");MODULE_PARM_DESC(step_type, "stepping types for up to 8 channels");static long period = 0; /* non-FP thread period, default = none */MODULE_PARM(period, "l");MODULE_PARM_DESC(period, "non-FP thread period (nsecs)");static long fp_period = 0; /* FP thread period, default = none */MODULE_PARM(fp_period, "l");MODULE_PARM_DESC(fp_period, "floating point thread period (nsecs)");#endif /* MODULE *//************************************************************************ STRUCTURES AND GLOBAL VARIABLES *************************************************************************//** These structures contains the runtime data for a single generator. The 'st0_t' struct has data needed for stepping type 0 only, and 'st2_t' has data needed for stepping types 2 and higher only. A union is used so the two structs can share space in the main 'freqgen_t' structure. This keeps the frequently accessed parts of the main structure smaller, allowing them to occupy fewer cache lines. This improves speed as well as conserving shared memory. Data is arranged in the structs in the order in which it will be accessed, so fetching one item will load the next item(s) into cache.*/typedef struct { unsigned char step_type; /* stepping type - see list above */ unsigned char need_step; /* non-zero if we need to step */ unsigned char setup_timer; /* timer for dir setup time */ unsigned char hold_timer; /* timer for dir hold time */ unsigned char space_timer; /* timer for pulse spacing */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -