From: <jmk...@us...> - 2003-09-29 02:58:31
|
Update of /cvsroot/emc/rtapi/examples/hal In directory sc8-pr-cvs1:/tmp/cvs-serv26769/examples/hal Modified Files: Makefile encoder.c Added Files: stepgen.c Log Message: started work on step pulse generator - not ready for testing yet --- NEW FILE: stepgen.c --- /** This file, 'stepgen.c', is a HAL component that provides software based step pulse generation. 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. 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 counters is set by the module parameter 'num_chan' when the component is insmod'ed. The driver exports two functions. The first, 'StepGen.MakePulses', is responsible for actually generating the step pulses. It must be executed in a fast thread to reduce pulse jitter. For proper frequency scaling, that thread should be 'StepGen.FastThread', which is created by this component. The 'period' parameter (at insmod time) determines the period of 'StepGen.FastThread' and also is used for scaling inside the module. Other functions may be executed before or after 'StepGen.MakePulses' in that thread to read and/or write outputs, especially the step outputs. The second function, 'StepGen.Update', is normally called from a much slower thread, and is responsible for reading the frequency command and setting internal variables used by the first function. 'StepGen.Update' uses floating point, 'StepGen.MakePulses' does not. 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_____________ Timing is controlled by exported HAL parameters. The parameters are (1): 'StepGen.n.DirSetup' minimum delay from a change on the DIR line to the beginning of a step pulse, (2): 'StepGen.n.StepLen' length of the step pulse, (3): 'StepGen.n.StepSpace', space between step pulses, and (4):'StepGen.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 'PulseLen' + 'PulseSpace' periods, and the frequency command is clamped to avoid exceeding these limits. Type 1: Up/Down (aka Pseudo-PWM) There are two output bits, 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. Type 2: Quadrature (aka Gray/Grey code) State Phase A Phase B 0 0 0 1 0 1 2 1 1 3 1 0 0 0 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 */ #ifdef MODULE /* module information */ MODULE_AUTHOR("John Kasunich"); MODULE_DESCRIPTION("Step Pulse Generator for EMC HAL"); #ifdef MODULE_LICENSE MODULE_LICENSE("GPL"); #endif /* MODULE_LICENSE */ #if 0 static int num_chan = 3; /* number of channels, default = 3 */ MODULE_PARM(num_chan, "i"); MODULE_PARM_DESC(cfg, "number of channels"); #endif static char *cfg = "0 0 0"; /* config string, default = 3 x step/dir */ MODULE_PARM(cfg, "s"); MODULE_PARM_DESC(cfg, "config string"); static long period = 50000; /* thread period, default = 50uS */ MODULE_PARM(period, "l"); MODULE_PARM_DESC(period, "thread period (nsecs)"); #endif /* MODULE */ /*********************************************************************** * STRUCTURES AND GLOBAL VARIABLES * ************************************************************************/ /** These structures contains the runtime data for a single counter. 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 'stepgen_t' structure. This keeps the frequently accessed parts of the main structure small enough to fit in two cache lines, and therefore 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 */ unsigned char len_timer; /* timer for pulse length */ hal_u8_t dir_setup; /* parameter: direction setup time */ hal_u8_t dir_hold; /* parameter: direction hold time */ hal_u8_t step_len; /* parameter: step pulse length */ hal_u8_t step_space; /* parameter: min step pulse spacing */ } st0_t; typedef struct { unsigned char step_type; /* stepping type - see list above */ unsigned char state; /* current position in state table */ unsigned char cycle_max; /* cycle length for step types 2 and up */ unsigned char num_phases; /* number of phases for types 2 and up */ unsigned char *lut; /* pointer to lookup table */ } st2_t; typedef struct { unsigned long accum; /* frequency generator accumulator */ signed long addval; /* frequency generator add value */ union { st0_t st0; /* working data for step type 0 */ st2_t st2; /* working data for step types 2 and up */ } wd; hal_bit_t phase[5]; /* source vars for output signals */ hal_s32_t raw_count; /* current position (for feedback) */ hal_s32_t count; /* captured binary count value */ hal_float_t pos; /* scaled position (floating point) */ hal_float_t pos_scale; /* parameter: scaling factor for pos */ hal_float_t *freq; /* dest var for frequency command */ hal_float_t freq_scale; /* parameter: scaling factor for freq */ } stepgen_t; #define STEP_PIN 0 /* output phase used for STEP signal */ #define DIR_PIN 1 /* output phase used for DIR signal */ #define UP_PIN 0 /* output phase used for UP signal */ #define DOWN_PIN 1 /* output phase used for DOWN signal */ /* lookup tables for stepping types 2 and higher - phase A is the LSB */ const unsigned char master_lut[][10] = { { 0, 2, 3, 1, 0, 0, 0, 0, 0, 0 }, /* Quadrature */ { 1, 2, 4, 0, 0, 0, 0, 0, 0, 0 }, /* Three Wire */ { 1, 3, 2, 6, 4, 5, 0, 0, 0, 0 }, /* Three Wire Half Step */ { 1, 2, 4, 8, 0, 0, 0, 0, 0, 0 }, /* Unipolar Full Step 1 */ { 3, 6, 12, 9, 0, 0, 0, 0, 0, 0 }, /* Unipoler Full Step 2 */ { 1, 7, 14, 8, 0, 0, 0, 0, 0, 0 }, /* Bipolar Full Step 1 */ { 5, 6, 10, 9, 0, 0, 0, 0, 0, 0 }, /* Bipoler Full Step 2 */ { 1, 3, 2, 6, 4, 12, 8, 9, 0, 0 }, /* Unipolar Half Step */ { 1, 5, 7, 6, 14, 10, 8, 9, 0, 0 }, /* Bipolar Half Step */ { 1, 2, 4, 8, 16, 0, 0, 0, 0, 0 }, /* Five Wire Unipolar */ { 3, 6, 12, 24, 17, 0, 0, 0, 0, 0 }, /* Five Wire Wave */ { 1, 3, 2, 6, 4, 12, 8, 24, 16, 17 }, /* Five Wire Uni Half */ { 3, 7, 6, 14, 12, 28, 24, 25, 17, 19 } /* Five Wire Wave Half */ }; const unsigned char cycle_len_lut[] = { 4, 3, 6, 4, 4, 4, 4, 8, 8, 5, 5, 10, 10 }; const unsigned char num_phases_lut[] = { 2, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, }; #define MAX_STEP_TYPE 14 /* pointer to array of counter_t structs in shared memory, 1 per port */ stepgen_t *stepgen_array; /* other globals */ int comp_id; /* component ID */ int num_chan; /* number of step generators configured */ /*********************************************************************** * LOCAL FUNCTION DECLARATIONS * ************************************************************************/ static int parse_step_type(char *cp); static int export_stepgen(int num, stepgen_t * addr, int steptype); static void make_pulses(void *arg); static void update(void *arg); /*********************************************************************** * INIT AND EXIT CODE * ************************************************************************/ #define MAX_CHAN 8 #define MAX_TOK (MAX_CHAN) int rtapi_app_main(void) { char *cp; char *tokens[MAX_TOK]; int step_type[MAX_CHAN]; int n, retval; /* test for config string */ if (cfg == 0) { rtapi_print_msg(RTAPI_MSG_ERR, "STEPGEN: ERROR: no config string\n"); return -1; } /* point to config string */ cp = cfg; /* break it into tokens */ for (n = 0; n < MAX_TOK; n++) { /* strip leading whitespace */ while ((*cp != '\0') && (isspace(*cp))) cp++; /* mark beginning of token */ tokens[n] = cp; /* find end of token */ while ((*cp != '\0') && (!isspace(*cp))) cp++; /* mark end of this token, prepare to search for next one */ if (*cp != '\0') { *cp = '\0'; cp++; } } /* mark all channels unused */ for (n = 0; n < MAX_CHAN; n++) { step_type[n] = -1; } /* parse config string, results in step_type[] array */ num_chan = 0; n = 0; while ((num_chan < MAX_CHAN) && (n < MAX_TOK)) { if (tokens[n][0] != '\0') { /* something here, is it a valid stepping type? */ step_type[num_chan] = parse_step_type(tokens[n]); if ((step_type[num_chan] < 0 ) || (step_type[num_chan] > MAX_STEP_TYPE)) { rtapi_print_msg(RTAPI_MSG_ERR, "STEPGEN: ERROR: bad stepping type '%s'\n", tokens[n]); return -1; } num_chan++; } n++; } /* OK, now we've parsed everything */ if (num_chan == 0) { rtapi_print_msg(RTAPI_MSG_ERR, "STEPGEN: ERROR: no channels configured\n"); return -1; } /* test for 'period' */ if ((period < 5000) || (period > 500000)) { rtapi_print_msg(RTAPI_MSG_ERR, "STEPGEN: ERROR: invalid period: %d\n", period); return -1; } /* have good config info, connect to the HAL */ comp_id = hal_init("StepGen"); if (comp_id < 0) { rtapi_print_msg(RTAPI_MSG_ERR, "STEPGEN: ERROR: hal_init() failed\n"); return -1; } /* allocate shared memory for counter data */ stepgen_array = hal_malloc(num_chan * sizeof(stepgen_t)); if (stepgen_array == 0) { rtapi_print_msg(RTAPI_MSG_ERR, "STEPGEN: ERROR: hal_malloc() failed\n"); hal_exit(comp_id); return -1; } /* export all the variables for each pulse generator */ for (n = 0; n < num_chan; n++) { /* export all vars */ retval = export_stepgen(n + 1, &(stepgen_array[n]), step_type[n]); if (retval != 0) { rtapi_print_msg(RTAPI_MSG_ERR, "STEPGEN: ERROR: stepgen %d var export failed\n", n + 1); hal_exit(comp_id); return -1; } } /* export functions */ retval = hal_export_funct("StepGen.MakePulses", make_pulses, stepgen_array, 0, 0, comp_id); if (retval != 0) { rtapi_print_msg(RTAPI_MSG_ERR, "STEPGEN: ERROR: makepulses funct export failed\n", n + 1); hal_exit(comp_id); return -1; } retval = hal_export_funct("StepGen.Update", update, stepgen_array, 1, 0, comp_id); if (retval != 0) { rtapi_print_msg(RTAPI_MSG_ERR, "STEPGEN: ERROR: update funct export failed\n", n + 1); hal_exit(comp_id); return -1; } rtapi_print_msg(RTAPI_MSG_INFO, "STEPGEN: installed %d step pulse generators\n", num_chan); /* create a thread */ retval = hal_create_thread("StepGen.FastThread", period, 0, comp_id); if (retval < 0) { rtapi_print_msg(RTAPI_MSG_ERR, "STEPGEN: ERROR: could not create thread\n"); hal_exit(comp_id); return -1; } else { rtapi_print_msg(RTAPI_MSG_INFO, "STEPGEN: created %d uS thread\n", period / 1000); } return 0; } void rtapi_app_exit(void) { hal_exit(comp_id); } /*********************************************************************** * REALTIME PORT READ AND WRITE FUNCTIONS * ************************************************************************/ /** The frequency generator works by adding a signed value proportional to frequency to an accumulator. When the accumulator overflows (or underflows), it is time to generate an up (or down) output pulse. The add value is limited to +/-2^30, and overflows are detected at bit 30, not bit 31. This means that with add_val at it's max (or min) value, and overflow (or underflow) occurs on every cycle. NOTE: step/dir outputs cannot generate a step every cycle. The maximum steprate is determined by the timing parameters. The time between pulses must be at least (StepLen + StepSpace) cycles, and the time between direction changes must be at least (DirSetup + StepLen + DirHold) cycles. */ static void make_pulses(void *arg) { stepgen_t *stepgen; int n, p; unsigned char tmp_step, tmp_dir, state; stepgen = arg; for (n = 0; n < num_chan; n++) { /* get current value of bit 31 */ tmp_step = stepgen->accum >> 31; /* update the accumulator */ stepgen->accum += stepgen->addval; /* test for overflow/underflow (change in bit 31) */ tmp_step ^= stepgen->accum >> 31; /* get direction bit, 1 if negative, 0 if positive */ tmp_dir = stepgen->addval >> 31; /* generate output, based on stepping type */ if ( stepgen->wd.st0.step_type == 0 ) { /* step/dir output, using working data in stepgen->wd.st0.xxx */ stepgen->wd.st0.need_step |= tmp_step; /* update timers */ if ( stepgen->wd.st0.setup_timer ) { stepgen->wd.st0.setup_timer--; } if ( stepgen->wd.st0.hold_timer ) { stepgen->wd.st0.hold_timer--; } if ( stepgen->wd.st0.space_timer ) { stepgen->wd.st0.space_timer--; } if ( stepgen->wd.st0.len_timer ) { if ( --stepgen->wd.st0.len_timer == 0 ) { /* terminate step pulse */ stepgen->phase[STEP_PIN] = 0; /* begin timing pulse space */ stepgen->wd.st0.space_timer = stepgen->wd.st0.step_space; } } /* is direction OK? */ if ( tmp_dir != stepgen->phase[DIR_PIN] ) { /* need to change direction bit, can we? */ if (( stepgen->wd.st0.hold_timer == 0 ) && ( stepgen->wd.st0.setup_timer == 0 )) { /* set direction output */ stepgen->phase[DIR_PIN] = tmp_dir; /* start setup timer */ stepgen->wd.st0.setup_timer = stepgen->wd.st0.dir_setup; } } /* do we need to step? */ if ( stepgen->wd.st0.need_step ) { /* yes, can we? */ if (( stepgen->wd.st0.space_timer == 0 ) && ( stepgen->wd.st0.setup_timer == 0 ) && ( stepgen->phase[DIR_PIN] == tmp_dir )) { /* yes, start step pulse */ stepgen->phase[STEP_PIN] = 1; /* start pulse length and dir hold timers */ stepgen->wd.st0.len_timer = stepgen->wd.st0.step_len; stepgen->wd.st0.hold_timer = stepgen->wd.st0.step_len + stepgen->wd.st0.dir_hold; /* don't need a step any more */ stepgen->wd.st0.need_step = 0; /* count the step */ if ( tmp_dir ) { stepgen->raw_count--; } else { stepgen->raw_count++; } } } } else if ( stepgen->wd.st0.step_type == 1 ) { /* pesudo-PWM */ stepgen->phase[UP_PIN] = tmp_step & ~tmp_dir; stepgen->phase[DOWN_PIN] = tmp_step & tmp_dir; /* count the step for feedback */ if ( tmp_step ) { if ( tmp_dir ) { stepgen->raw_count--; } else { stepgen->raw_count++; } } } else { /* step type 2 or greater */ /* update step cycle counter */ if ( tmp_step ) { if ( tmp_dir ) { if ( stepgen->wd.st2.state-- == 0 ) { stepgen->wd.st2.state = stepgen->wd.st2.cycle_max; } /* count the step for feedback */ stepgen->raw_count--; } else { if ( ++stepgen->wd.st2.state > stepgen->wd.st2.cycle_max ) { stepgen->wd.st2.state = 0; } /* count the step for feedback */ stepgen->raw_count++; } /* look up correct output pattern */ state = (stepgen->wd.st2.lut)[stepgen->wd.st2.state]; /* now output the phase bits */ for ( p = 0 ; p < stepgen->wd.st2.num_phases ; p++ ) { /* output one phase */ stepgen->phase[p] = state & 1; /* move to the next phase */ state >>= 1; } } } /* move on to next step generator */ stepgen++; } /* done */ } static void update(void *arg) { stepgen_t *stepgen; int n; stepgen = arg; for (n = 0; n < num_chan; n++) { /* update this channel */ /* move on to next channel */ stepgen++; } /* done */ } /*********************************************************************** * LOCAL FUNCTION DEFINITIONS * ************************************************************************/ static int parse_step_type(char *cp) { int result; if ( *cp == '\0' ) { return -1; } /* initial value */ result = 0; /* parse digits */ while (*cp != '\0') { /* if char is a decimal digit, add it to result */ if ((*cp >= '0') && (*cp <= '9')) { result *= 10; result += *cp - '0'; } else { /* not a valid digit */ return -1; } /* next char */ cp++; } return result; } static int export_stepgen(int num, stepgen_t * addr, int step_type) { int n, retval; char buf[HAL_NAME_LEN + 2]; /* first init & export stuff that is independent of step type */ addr->accum = 1 << 30; addr->addval = 0; addr->raw_count = 0; addr->count = 0; addr->pos = 0.0; for ( n = 0 ; n < 5 ; n++ ) { addr->phase[n] = 0; } /* export src variable for raw counts */ rtapi_snprintf(buf, HAL_NAME_LEN, "StepGen.%d.RawCounts", num); retval = hal_src_s32(buf, &(addr->raw_count), comp_id); if (retval != 0) { return retval; } /* export src variable for counts captured by update() */ rtapi_snprintf(buf, HAL_NAME_LEN, "StepGen.%d.Counts", num); retval = hal_src_s32(buf, &(addr->count), comp_id); if (retval != 0) { return retval; } /* export src variable for scaled position captured by update() */ rtapi_snprintf(buf, HAL_NAME_LEN, "StepGen.%d.Position", num); retval = hal_src_float(buf, &(addr->pos), comp_id); if (retval != 0) { return retval; } /* export parameter for position scaling */ rtapi_snprintf(buf, HAL_NAME_LEN, "StepGen.%d.PosScale", num); retval = hal_param_float(buf, &(addr->pos_scale), comp_id); if (retval != 0) { return retval; } /* export dest variable for frequency command */ rtapi_snprintf(buf, HAL_NAME_LEN, "StepGen.%d.FreqIn", num); retval = hal_dst_float(buf, &(addr->freq), comp_id); if (retval != 0) { return retval; } /* export parameter for frequency scaling */ rtapi_snprintf(buf, HAL_NAME_LEN, "StepGen.%d.FreqScale", num); retval = hal_param_float(buf, &(addr->freq_scale), comp_id); if (retval != 0) { return retval; } addr->wd.st0.step_type = step_type; if ( step_type == 0 ) { /* setup for stepping type 0 - step/dir */ addr->wd.st0.need_step = 0; addr->wd.st0.setup_timer = 0; addr->wd.st0.hold_timer = 0; addr->wd.st0.space_timer = 0; addr->wd.st0.len_timer = 0; /* export parameters for step/dir pulse timing */ rtapi_snprintf(buf, HAL_NAME_LEN, "StepGen.%d.DirSetup", num); retval = hal_param_u8(buf, &(addr->wd.st0.dir_setup), comp_id); if (retval != 0) { return retval; } rtapi_snprintf(buf, HAL_NAME_LEN, "StepGen.%d.DirHold", num); retval = hal_param_u8(buf, &(addr->wd.st0.dir_hold), comp_id); if (retval != 0) { return retval; } rtapi_snprintf(buf, HAL_NAME_LEN, "StepGen.%d.StepLen", num); retval = hal_param_u8(buf, &(addr->wd.st0.step_len), comp_id); if (retval != 0) { return retval; } rtapi_snprintf(buf, HAL_NAME_LEN, "StepGen.%d.StepSpace", num); retval = hal_param_u8(buf, &(addr->wd.st0.step_space), comp_id); if (retval != 0) { return retval; } /* init the parameters */ addr->wd.st0.dir_setup = 1; addr->wd.st0.dir_hold = 1; addr->wd.st0.step_len = 1; addr->wd.st0.step_space = 1; /* export src variables for step and direction */ rtapi_snprintf(buf, HAL_NAME_LEN, "StepGen.%d.STEP", num); retval = hal_src_bit(buf, &(addr->phase[STEP_PIN]), comp_id); if (retval != 0) { return retval; } rtapi_snprintf(buf, HAL_NAME_LEN, "StepGen.%d.DIR", num); retval = hal_src_bit(buf, &(addr->phase[DIR_PIN]), comp_id); if (retval != 0) { return retval; } } else if ( step_type == 1 ) { /* setup for stepping type 1 - pseudo-PWM */ /* export src variables for up and down */ rtapi_snprintf(buf, HAL_NAME_LEN, "StepGen.%d.UP", num); retval = hal_src_bit(buf, &(addr->phase[UP_PIN]), comp_id); if (retval != 0) { return retval; } rtapi_snprintf(buf, HAL_NAME_LEN, "StepGen.%d.DOWN", num); retval = hal_src_bit(buf, &(addr->phase[DOWN_PIN]), comp_id); if (retval != 0) { return retval; } } else { /* setup for stepping types 2 and higher */ addr->wd.st2.state = 0; addr->wd.st2.cycle_max = cycle_len_lut[step_type-2] - 1; addr->wd.st2.num_phases = num_phases_lut[step_type-2]; /* export source variables for output phases */ for ( n = 0 ; n < addr->wd.st2.num_phases ; n++ ) { rtapi_snprintf(buf, HAL_NAME_LEN, "StepGen.%d.Phase%c", num, n+'A'); retval = hal_src_bit(buf, &(addr->phase[n]), comp_id); if (retval != 0) { return retval; } } } return 0; } Index: Makefile =================================================================== RCS file: /cvsroot/emc/rtapi/examples/hal/Makefile,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** Makefile 25 Sep 2003 05:44:45 -0000 1.5 --- Makefile 29 Sep 2003 02:58:11 -0000 1.6 *************** *** 4,8 **** include ../../Makefile.inc ! SRCS = hal_lib.c test_comp.c halcmd.c parport.c encoder.c OBJS = \ --- 4,8 ---- include ../../Makefile.inc ! SRCS = hal_lib.c test_comp.c halcmd.c parport.c encoder.c stepgen.c OBJS = \ *************** *** 11,14 **** --- 11,15 ---- $(RTLIB_DIR)/parport.o \ $(RTLIB_DIR)/encoder.o \ + $(RTLIB_DIR)/stepgen.o \ $(LIB_DIR)/halcmd.o *************** *** 67,70 **** --- 68,74 ---- /home/John/emcdev/rtapi/lib/encoder.o: /home/John/emcdev/rtapi/include/rtapi_app.h /home/John/emcdev/rtapi/lib/encoder.o: hal.h + /home/John/emcdev/rtapi/lib/stepgen.o: /home/John/emcdev/rtapi/include/rtapi.h + /home/John/emcdev/rtapi/lib/stepgen.o: /home/John/emcdev/rtapi/include/rtapi_app.h + /home/John/emcdev/rtapi/lib/stepgen.o: hal.h /home/John/emcdev/rtapi/rtlib/hal_lib.o: /home/John/emcdev/rtapi/include/rtapi.h *************** *** 80,81 **** --- 84,88 ---- /home/John/emcdev/rtapi/rtlib/encoder.o: /home/John/emcdev/rtapi/include/rtapi_app.h /home/John/emcdev/rtapi/rtlib/encoder.o: hal.h + /home/John/emcdev/rtapi/rtlib/stepgen.o: /home/John/emcdev/rtapi/include/rtapi.h + /home/John/emcdev/rtapi/rtlib/stepgen.o: /home/John/emcdev/rtapi/include/rtapi_app.h + /home/John/emcdev/rtapi/rtlib/stepgen.o: hal.h Index: encoder.c =================================================================== RCS file: /cvsroot/emc/rtapi/examples/hal/encoder.c,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** encoder.c 25 Sep 2003 05:44:45 -0000 1.1 --- encoder.c 29 Sep 2003 02:58:11 -0000 1.2 *************** *** 52,56 **** #endif - #include <linux/ctype.h> /* isspace() */ #include <rtapi.h> /* RTAPI realtime OS API */ #include <rtapi_app.h> /* RTAPI realtime module decls */ --- 52,55 ---- *************** *** 65,72 **** MODULE_LICENSE("GPL"); #endif /* MODULE_LICENSE */ ! static int num_chan = 0; /* number of channels */ MODULE_PARM(num_chan, "i"); MODULE_PARM_DESC(cfg, "number of channels"); ! static long period = 0; /* thread period */ MODULE_PARM(period, "l"); MODULE_PARM_DESC(period, "thread period (nsecs)"); --- 64,71 ---- MODULE_LICENSE("GPL"); #endif /* MODULE_LICENSE */ ! static int num_chan = 3; /* number of channels - default = 3 */ MODULE_PARM(num_chan, "i"); MODULE_PARM_DESC(cfg, "number of channels"); ! static long period = 0; /* thread period - default = no thread */ MODULE_PARM(period, "l"); MODULE_PARM_DESC(period, "thread period (nsecs)"); |