From 52c7fb8c6cf6924a4de2826928a18a74e08a5b97 Mon Sep 17 00:00:00 2001 From: andypugh Date: Thu, 27 Nov 2025 21:43:28 +0000 Subject: [PATCH 1/2] Expand hardware drivers to 64 bits (address issue #2331) --- debian/copyright | 1 - src/Makefile | 3 - src/Makefile.inc.in | 1 - src/hal/drivers/hal_evoreg.c | 4 +- src/hal/drivers/hal_gm.c | 40 +++-- src/hal/drivers/hal_motenc.c | 21 ++- src/hal/drivers/hal_ppmc.c | 24 +-- src/hal/drivers/hal_stg.c | 35 +++-- src/hal/drivers/hal_tiro.c | 277 ----------------------------------- 9 files changed, 80 insertions(+), 326 deletions(-) delete mode 100644 src/hal/drivers/hal_tiro.c diff --git a/debian/copyright b/debian/copyright index 736f69ef1de..31ef1e2e64b 100644 --- a/debian/copyright +++ b/debian/copyright @@ -247,7 +247,6 @@ Files: src/hal/drivers/hal_speaker.c src/hal/drivers/hal_stg.c src/hal/drivers/hal_stg.h - src/hal/drivers/hal_tiro.c src/hal/drivers/hal_vti.c src/hal/drivers/hal_vti.h src/hal/drivers/motenc.h diff --git a/src/Makefile b/src/Makefile index 15c9000d13c..df3fe6ba745 100644 --- a/src/Makefile +++ b/src/Makefile @@ -931,8 +931,6 @@ endif ifneq ($(BUILD_SYS),uspace) obj-$(CONFIG_PCI_8255) += pci_8255.o pci_8255-objs := hal/drivers/pci_8255.o -obj-$(CONFIG_HAL_TIRO) += hal_tiro.o -hal_tiro-objs := hal/drivers/hal_tiro.o $(MATHSTUB) obj-$(CONFIG_HAL_STG) += hal_stg.o hal_stg-objs := hal/drivers/hal_stg.o $(MATHSTUB) obj-$(CONFIG_HAL_VTI) += hal_vti.o @@ -1331,7 +1329,6 @@ endif #../rtlib/uparport$(MODULE_EXT): $(addprefix objects/rt,$(uparport-objs)) ifneq ($(BUILD_SYS),uspace) ../rtlib/pci_8255$(MODULE_EXT): $(addprefix objects/rt,$(pci_8255-objs)) -../rtlib/hal_tiro$(MODULE_EXT): $(addprefix objects/rt,$(hal_tiro-objs)) ../rtlib/hal_stg$(MODULE_EXT): $(addprefix objects/rt,$(hal_stg-objs)) ../rtlib/hal_vti$(MODULE_EXT): $(addprefix objects/rt,$(hal_vti-objs)) ../rtlib/hal_evoreg$(MODULE_EXT): $(addprefix objects/rt,$(hal_evoreg-objs)) diff --git a/src/Makefile.inc.in b/src/Makefile.inc.in index 223062d996a..f1217643b2b 100644 --- a/src/Makefile.inc.in +++ b/src/Makefile.inc.in @@ -208,7 +208,6 @@ CONFIG_SAMPLER=m CONFIG_UPARPORT=m CONFIG_HAL_PARPORT=m CONFIG_PROBE_PARPORT=m -CONFIG_HAL_TIRO=m CONFIG_HAL_EVOREG=m CONFIG_HAL_MOTENC=m CONFIG_HAL_SKELETON=m diff --git a/src/hal/drivers/hal_evoreg.c b/src/hal/drivers/hal_evoreg.c index 5bc2ee34287..d912f44e1e1 100644 --- a/src/hal/drivers/hal_evoreg.c +++ b/src/hal/drivers/hal_evoreg.c @@ -103,7 +103,7 @@ typedef struct { hal_bit_t *digital_in[47]; /* ptrs for digital input pins 0 - 45 */ hal_bit_t *digital_out[25]; /* ptrs for digital output pins 0 - 20 */ __u16 raw_counts_old[3]; - __s32 counts[3]; + __s64 counts[3]; hal_float_t pos_scale; /*! \todo scale for position command FIXME should be one per axis */ } evoreg_t; @@ -293,7 +293,7 @@ static void update_port(void *arg, long period) writew((*(port->dac_out[1])/10 * 0x7fff), (char *)port->io_base + 0x80); writew((*(port->dac_out[2])/10 * 0x7fff), (char *)port->io_base + 0xa0); -/* Read Encoders, improve the 16bit hardware counters to 32bit and scale the values */ +/* Read Encoders, improve the 16bit hardware counters to 64bit and scale the values */ raw_counts[0] = (__u16) readw(port->io_base); raw_counts[1] = (__u16) readw((char *)port->io_base + 0x08 ); raw_counts[2] = (__u16) readw((char *)port->io_base + 0x10 ); diff --git a/src/hal/drivers/hal_gm.c b/src/hal/drivers/hal_gm.c index ab4bf7cf024..5a0becf1b61 100644 --- a/src/hal/drivers/hal_gm.c +++ b/src/hal/drivers/hal_gm.c @@ -1,9 +1,10 @@ #include #include -#include // RTAPI realtime OS API -#include // RTAPI realtime module decls -#include // HAL public API decls +#include "rtapi.h" // RTAPI realtime OS API +#include "rtapi_app.h" // RTAPI realtime module decls +#include "rtapi_stdint.h" // portable ints +#include "hal.h" // HAL public API decls #include "gm.h" // Hardware dependent defines #include @@ -16,9 +17,11 @@ typedef struct { //encoder_t // Pins hal_bit_t *reset; hal_s32_t *counts; + hal_s64_t *counts64; hal_float_t *position; hal_float_t *velocity; hal_s32_t *rawcounts; + hal_s64_t *rawcounts64; hal_bit_t *index_enable; // Parameters @@ -30,11 +33,11 @@ typedef struct { //encoder_t hal_float_t min_speed_estimate; // Private data - hal_s32_t raw_offset; - hal_s32_t index_offset; - hal_s32_t last_index_latch; + rtapi_s64 index_offset; + rtapi_s64 last_index_latch; hal_bit_t first_index; hal_bit_t module_exist; + rtapi_s32 old_counts; } encoder_t; typedef struct { //switches_t @@ -239,19 +242,19 @@ typedef struct { //stepgen_t hal_float_t *position_cmd; hal_float_t *velocity_cmd; hal_float_t *position_fb; - hal_s32_t *count_fb; + hal_s64_t *count_fb; hal_bit_t *enable; // Parameters hal_u32_t step_type; //0: StepDir, 1: UpDown, 2: Quadrature - hal_bit_t control_type; //0: position, 1: velocity + hal_bit_t control_type; //0: position, 1: velocity hal_u32_t steplen; hal_u32_t stepspace; hal_u32_t dirdelay; hal_float_t maxaccel; hal_float_t maxvel; hal_bit_t polarity_A; - hal_bit_t polarity_B; + hal_bit_t polarity_B; hal_float_t position_scale; //Saved Parameters @@ -259,7 +262,7 @@ typedef struct { //stepgen_t hal_u32_t curr_stepspace; hal_u32_t curr_dirdelay; hal_float_t curr_maxaccel; - hal_float_t curr_maxvel; + hal_float_t curr_maxvel; hal_float_t curr_position_scale; // Private data @@ -623,9 +626,11 @@ ExportEncoder(void *arg, int comp_id, int version) //Export Pins if(error == 0) error = hal_pin_bit_newf(HAL_IN, &(device->encoder[i].reset), comp_id, "gm.%1d.encoder.%1d.reset", boardId, i); if(error == 0) error = hal_pin_s32_newf(HAL_OUT, &(device->encoder[i].counts), comp_id, "gm.%1d.encoder.%1d.counts", boardId, i); + if(error == 0) error = hal_pin_s64_newf(HAL_OUT, &(device->encoder[i].counts64), comp_id, "gm.%1d.encoder.%1d.counts64", boardId, i); if(error == 0) error = hal_pin_float_newf(HAL_OUT, &(device->encoder[i].position), comp_id, "gm.%1d.encoder.%1d.position", boardId, i); if(error == 0) error = hal_pin_float_newf(HAL_OUT, &(device->encoder[i].velocity), comp_id, "gm.%1d.encoder.%1d.velocity", boardId, i); if(error == 0) error = hal_pin_s32_newf(HAL_OUT, &(device->encoder[i].rawcounts), comp_id, "gm.%1d.encoder.%1d.rawcounts", boardId, i); + if(error == 0) error = hal_pin_s64_newf(HAL_OUT, &(device->encoder[i].rawcounts64), comp_id, "gm.%1d.encoder.%1d.rawcounts", boardId, i); if(error == 0) error = hal_pin_bit_newf(HAL_IO, &(device->encoder[i].index_enable), comp_id, "gm.%1d.encoder.%1d.index-enable", boardId, i); //Export Parameters @@ -637,7 +642,7 @@ ExportEncoder(void *arg, int comp_id, int version) if(error == 0) error = hal_param_float_newf(HAL_RW, &(device->encoder[i].min_speed_estimate), comp_id, "gm.%1d.encoder.%1d.min-speed-estimate", boardId, i); //Init parameters - device->encoder[i].raw_offset = pCard->ENC_counter[i]; + device->encoder[i].old_counts = pCard->ENC_counter[i]; device->encoder[i].index_offset = 0; device->encoder[i].last_index_latch = pCard->ENC_index_latch[i]; device->encoder[i].first_index = 1; @@ -679,7 +684,7 @@ ExportStepgen(void *arg, int comp_id, int version) if(error == 0) error = hal_pin_float_newf(HAL_IN, &(device->stepgen[i].position_cmd), comp_id, "gm.%1d.stepgen.%1d.position-cmd", boardId, i); if(error == 0) error = hal_pin_float_newf(HAL_OUT, &(device->stepgen[i].position_fb), comp_id, "gm.%1d.stepgen.%1d.position-fb", boardId, i); if(error == 0) error = hal_pin_float_newf(HAL_IN, &(device->stepgen[i].velocity_cmd), comp_id, "gm.%1d.stepgen.%1d.velocity-cmd", boardId, i); - if(error == 0) error = hal_pin_s32_newf(HAL_OUT, &(device->stepgen[i].count_fb), comp_id, "gm.%1d.stepgen.%1d.count-fb", boardId, i); + if(error == 0) error = hal_pin_s64_newf(HAL_OUT, &(device->stepgen[i].count_fb), comp_id, "gm.%1d.stepgen.%1d.count-fb", boardId, i); if(error == 0) error = hal_pin_bit_newf(HAL_IN, &(device->stepgen[i].enable), comp_id, "gm.%1d.stepgen.%1d.enable", boardId, i); //Export Parameters. @@ -1472,7 +1477,7 @@ encoder(void *arg, long period) card *pCard = device->pCard; int i; - hal_s32_t temp1 = 0, temp2; + rtapi_s32 temp1 = 0, temp2, delta_counts; hal_float_t vel; //Update parameters @@ -1529,12 +1534,15 @@ encoder(void *arg, long period) } } device->encoder[i].last_index_latch = temp2; - - *(device->encoder[i].rawcounts) = temp1 - device->encoder[i].raw_offset; + delta_counts = temp1 - device->encoder[i].old_counts; + device->encoder[i].old_counts = temp1; + *(device->encoder[i].rawcounts) += delta_counts; + *(device->encoder[i].rawcounts64) += delta_counts; *(device->encoder[i].counts) = *(device->encoder[i].rawcounts) - device->encoder[i].index_offset; + *(device->encoder[i].counts64) = *(device->encoder[i].rawcounts64) - device->encoder[i].index_offset; if((device->encoder[i].position_scale < 0.000001) && (device->encoder[i].position_scale > -0.000001)) device->encoder[i].position_scale = 1; //Don't like to divide by 0 - *(device->encoder[i].position) = (hal_float_t) *(device->encoder[i].counts) / device->encoder[i].position_scale; + *(device->encoder[i].position) = (hal_float_t) *(device->encoder[i].counts64) / device->encoder[i].position_scale; vel = (hal_float_t) pCard->ENC_period[i]; if(vel == 0) vel = 1; diff --git a/src/hal/drivers/hal_motenc.c b/src/hal/drivers/hal_motenc.c index b8e4a97d9c6..dbb33aa37b6 100644 --- a/src/hal/drivers/hal_motenc.c +++ b/src/hal/drivers/hal_motenc.c @@ -25,6 +25,7 @@ * * Pins: * s32 motenc..enc--count +* s64 motenc..enc--count64 * float motenc..enc--position * bit motenc..enc--index * bit motenc..enc--index-enable @@ -124,9 +125,10 @@ #include -#include // RTAPI realtime OS API. -#include // RTAPI realtime module decls. -#include // HAL public API decls. +#include "rtapi.h" // RTAPI realtime OS API. +#include "rtapi_app.h" // RTAPI realtime module decls. +#include "rtapi_stdint.h" // rtapi_s32 etc +#include "hal.h" // HAL public API decls. #include "motenc.h" // Hardware dependent defines. // Module information. @@ -146,6 +148,7 @@ MODULE_LICENSE("GPL"); typedef struct { // Pins. + hal_s64_t *pCount64; hal_s32_t *pCount; // Captured binary count value. hal_float_t *pPosition; // Scaled position (floating point). hal_bit_t *pIndex; // Current state of index. @@ -161,6 +164,7 @@ typedef struct { // Private data. double oldScale; // Stored scale value. double scaleRecip; // Reciprocal value used for scaling. + rtapi_s32 pCount_old; // Previous counts, for bit width extending } EncoderPinsParams; typedef struct { @@ -494,6 +498,10 @@ Device_ExportEncoderPinsParametersFunctions(Device *this, int componentId, int b componentId, "motenc.%d.enc-%02d-count", boardId, channel)) != 0) break; + if((halError = hal_pin_s64_newf(HAL_OUT, &(this->encoder[channel].pCount64), + componentId, "motenc.%d.enc-%02d-count64", boardId, channel)) != 0) + break; + if((halError = hal_pin_float_newf(HAL_OUT, &(this->encoder[channel].pPosition), componentId, "motenc.%d.enc-%02d-position", boardId, channel)) != 0) break; @@ -517,6 +525,8 @@ Device_ExportEncoderPinsParametersFunctions(Device *this, int componentId, int b // Init encoder. *(this->encoder[channel].pCount) = 0; + *(this->encoder[channel].pCount64) = 0; + this->encoder[channel].pCount_old = 0; *(this->encoder[channel].pPosition) = 0.0; *(this->encoder[channel].pIndex) = 0; *(this->encoder[channel].pIndexEnable) = 0; @@ -794,6 +804,9 @@ Device_EncoderRead(void *arg, long period) // Read encoder counts. *(pEncoder->pCount) = pCard->fpga[i].encoderCount[j]; + // Extend counts to 64 bits + *(pEncoder->pCount64) += *(pEncoder->pCount) - pEncoder->pCount_old; + pEncoder->pCount_old = *(pEncoder->pCount); // Check for change in scale value. if ( pEncoder->scale != pEncoder->oldScale ) { @@ -811,7 +824,7 @@ Device_EncoderRead(void *arg, long period) } // Scale count to make floating point position. - *(pEncoder->pPosition) = *(pEncoder->pCount) * pEncoder->scaleRecip; + *(pEncoder->pPosition) = *(pEncoder->pCount64) * pEncoder->scaleRecip; } } } diff --git a/src/hal/drivers/hal_ppmc.c b/src/hal/drivers/hal_ppmc.c index 6f357b78aef..f97b278cc38 100644 --- a/src/hal/drivers/hal_ppmc.c +++ b/src/hal/drivers/hal_ppmc.c @@ -73,10 +73,11 @@ #include /* kmalloc() */ #include /* kmalloc() */ -#include /* RTAPI realtime OS API */ -#include /* RTAPI realtime module decls */ -#include /* HAL public API decls */ -#include +#include "rtapi.h" /* RTAPI realtime OS API */ +#include "rtapi_app.h" /* RTAPI realtime module decls */ +#include "rtapi_stdint.h" // portable ints +#include "hal.h" /* HAL public API decls */ +#include "hal_parport.h" #define MAX_BUS 3 /* max number of parports (EPP busses) */ @@ -293,12 +294,13 @@ typedef union { typedef struct { hal_float_t *position; /* output: scaled position pointer */ hal_s32_t *count; /* output: unscaled encoder counts */ + hal_s64_t *count64; /* 64 bit version to avoid wrapping */ hal_s32_t *delta; /* output: delta counts since last read */ hal_s32_t prevdir; /* local: previous direction */ hal_float_t scale; /* parameter: scale factor */ hal_bit_t *index; /* output: index flag */ hal_bit_t *index_enable; /* enable index pulse to reset encoder count */ - hal_s32_t oldreading; /* used to detect overflow / underflow of the counter JE001 */ + rtapi_s64 oldreading; /* used to detect overflow / underflow of the counter JE001 AP001*/ unsigned int indres; /* copy of reset-on-index register bits (only valid on 1st encoder of board)*/ unsigned int indrescnt; /* counts servo cycles since index reset was turned on */ hal_float_t *vel; /* output: scaled velocity */ @@ -1076,7 +1078,7 @@ static void read_encoders(slot_data_t *slot) int i, byteindex, byteindx2; double vel; // local temporary velocity union pos_tag { - hal_s32_t l; // JE001 + rtapi_s64 l; // JE001 AP001 struct byte_tag { signed char b0; signed char b1; @@ -1106,16 +1108,16 @@ static void read_encoders(slot_data_t *slot) for (i = 0; i < 4; i++) { slot->encoder[i].indrescnt++; /* increment counter each servo cycle */ oldpos.l = slot->encoder[i].oldreading; + pos.l = *(slot->encoder[i].count); // init the higher-order bytes pos.byte.b0 = (signed char)slot->rd_buf[byteindex++]; pos.byte.b1 = (signed char)slot->rd_buf[byteindex++]; pos.byte.b2 = (signed char)slot->rd_buf[byteindex++]; - pos.byte.b3 = oldpos.byte.b3; /* check for - to + transition */ if ((oldpos.byte.b2 & 0xc0) == 0xc0 && (pos.byte.b2 == 0)) - pos.byte.b3++; + pos.l += 0x01000000; else if ((oldpos.byte.b2 == 0) && (pos.byte.b2 & 0xc0) == 0xc0) - pos.byte.b3--; + pos.l -= 0x01000000; *(slot->encoder[i].delta) = pos.l - slot->encoder[i].oldreading; vel = (pos.l - slot->encoder[i].oldreading) / (read_period * 1e-9 * slot->encoder[i].scale); @@ -1135,9 +1137,9 @@ static void read_encoders(slot_data_t *slot) /* need to properly set the 24->32 bit extension byte */ if ( pos.byte.b2 < 0 ) { /* going backwards */ - pos.byte.b3 = 0xFF; + pos.l |= 0xFFFFFFFFFF000000; } else { - pos.byte.b3 = 0; + pos.l &= 0x0000000000FFFFFF; } oldpos.byte.b3 = pos.byte.b3; } diff --git a/src/hal/drivers/hal_stg.c b/src/hal/drivers/hal_stg.c index ec0357883de..d4837304cc9 100644 --- a/src/hal/drivers/hal_stg.c +++ b/src/hal/drivers/hal_stg.c @@ -171,12 +171,14 @@ typedef struct { typedef struct { /* counter data */ hal_s32_t *count[MAX_CHANS]; /* captured binary count value */ - hal_s32_t offset[MAX_CHANS]; /* offset to hold latched position from index pulse */ + rtapi_s32 old_count[MAX_CHANS]; /* used for bit-extension to 32 bits */ + hal_s64_t *count64[MAX_CHANS]; /* 64-bit counts */ + rtapi_s64 offset[MAX_CHANS]; /* offset to hold latched position from index pulse */ hal_float_t *pos[MAX_CHANS]; /* scaled position (floating point) */ hal_float_t pos_scale[MAX_CHANS]; /* parameter: scaling factor for pos */ hal_bit_t *index_enable[MAX_CHANS]; /* pins for index homing */ - hal_bit_t *index_latch[MAX_CHANS]; /* value of the index latch for the axis */ -// hal_s32_t check_index[MAX_CHANS]; /* internal marker for two stage index pulse check */ + hal_bit_t *index_latch[MAX_CHANS]; /* value of the index latch for the axis */ +// hal_s32_t check_index[MAX_CHANS]; /* internal marker for two stage index pulse check */ hal_bit_t *index_polarity[MAX_CHANS]; /* Polarity of index pulse */ /* dac data */ @@ -528,8 +530,9 @@ static void stg_counter_capture(void *arg, long period) // read the value without latching, latching was done on index // remember this as an offset, it will be substracted from nominal stg->offset[n] = stg_counter_read(n); - /* set index-enable false, so outside knows we found the index, and reset the position */ - *(stg->index_enable[n]) = 0; + stg->counts64[n] = 0 - stg->offset[n]; // atp #2331 14/11/25 + /* set index-enable false, so outside knows we found the index, and reset the position */ + *(stg->index_enable[n]) = 0; /* msg = rtapi_get_msg_level(); @@ -576,10 +579,11 @@ static void stg_counter_capture(void *arg, long period) if ( *(stg->index_enable[n]) == 1 ) { // read the value without latching, latching was done on index - // remember this as an offset, it will be substracted from nominal + // remember this as an offset, it will be substracted from nominal stg->offset[n] = stg_counter_read(n); - /* set index-enable false, so outside knows we found the index, and reset the position */ - *(stg->index_enable[n]) = 0; + stg->counts64[n] = 0 - stg->offset[n]; // atp #2331 14/11/25 + /* set index-enable false, so outside knows we found the index, and reset the position */ + *(stg->index_enable[n]) = 0; /* msg = rtapi_get_msg_level(); @@ -603,10 +607,14 @@ static void stg_counter_capture(void *arg, long period) for (n = 0; n < num_chan; n++) { + rtapi_s32 delta; /* capture raw counts to latches */ stg_counter_latch(n); /* read raw count, and substract the offset (determined by indexed homing) */ - *(stg->count[n]) = stg_counter_read(n) - stg->offset[n]; + *(stg->count[n]) = stg_counter_read(n) - stg->offset[n]; + // 64-bit index is handled differently + delta = (stg_counter_read(n) - (stg->old_count[n]); + *(stg->count64[n]) += delta; /* make sure scale isn't zero or tiny to avoid divide error */ if (stg->pos_scale[n] < 0.0) { if (stg->pos_scale[n] > -EPSILON) @@ -616,7 +624,7 @@ static void stg_counter_capture(void *arg, long period) stg->pos_scale[n] = 1.0; } /* scale count to make floating point position */ - *(stg->pos[n]) = *(stg->count[n]) / stg->pos_scale[n]; + *(stg->pos[n]) = *(stg->count64[n]) / stg->pos_scale[n]; } /* done */ return; @@ -1435,12 +1443,17 @@ static int export_counter(int num, stg_struct *addr) msg = rtapi_get_msg_level(); rtapi_set_msg_level( STG_MSG_LEVEL ); - /* export pin for counts captured by update() */ + /* export pins for counts captured by update() */ retval = hal_pin_s32_newf(HAL_OUT, &addr->count[num], comp_id, "stg.%d.counts", num); if (retval != 0) { return retval; } + retval = hal_pin_s64_newf(HAL_OUT, &addr->count64[num], + comp_id, "stg.%d.counts64", num); + if (retval != 0) { + return retval; + } /* export pin for scaled position captured by update() */ retval = hal_pin_float_newf(HAL_OUT, &addr->pos[num], comp_id, "stg.%d.position", num); diff --git a/src/hal/drivers/hal_tiro.c b/src/hal/drivers/hal_tiro.c deleted file mode 100644 index e79dedde708..00000000000 --- a/src/hal/drivers/hal_tiro.c +++ /dev/null @@ -1,277 +0,0 @@ -/******************************************************************** -* Description: hal_tiro.c -* This is the driver for an ISA (PC104) encoder reading -* board. -* -* Author: Alex Joni -* License: GPL Version 2 -* -* Copyright (c) 2003 All rights reserved. -* -* Last change: -********************************************************************/ - -/** This is the driver for an ISA (PC104) encoder reading board. - The board includes up to 4 channels of LS7166 chips for counting - quadrature encoders. Schematics of the board will be included on - my webpage www.juve.ro. - - Installation of the driver only realtime: - - insmod hal_tiro base=0x300 num_chan=4 - - This code can to some extent be used for the DRO board (with minor - adjustments). If it is required a DRO driver will follow. -*/ - -/** Copyright (C) 2004 Alex Joni - -*/ - -/** Copyright (C) 2003 John Kasunich - -*/ - -/** This program is free software; you can redistribute it and/or - modify it under the terms of version 2 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. -*/ - -#include /* RTAPI realtime OS API */ -#include /* RTAPI realtime module decls */ -#include /* HAL public API decls */ - -#define FASTIO - -#ifdef FASTIO -#define rtapi_inb inb -#define rtapi_outb outb -#include -#endif - -/* module information */ -MODULE_AUTHOR("Alex Joni"); -MODULE_DESCRIPTION("Driver for Tiro-PC104 board for EMC HAL"); -MODULE_LICENSE("GPL"); -static int base = 0x300; /* board base address */ -RTAPI_MP_INT(base, "board base address"); -static int num_chan = 4; /* number of channels - default = 4 */ -RTAPI_MP_INT(num_chan, "number of channels"); - -/*********************************************************************** -* STRUCTURES AND GLOBAL VARIABLES * -************************************************************************/ - -/* this structure contains the runtime data for a single counter */ - -typedef struct { - 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 */ -} counter_t; - -/* pointer to array of counter_t structs in shmem, 1 per counter */ -static counter_t *counter_array; - -/* other globals */ -static int comp_id; /* component ID */ - -#define DATA(x) (base + (2 * x)) /* Address of Data register */ -#define CTRL(x) (base + (2 * x) + 1) /* Address of Control register */ - -/*********************************************************************** -* LOCAL FUNCTION DECLARATIONS * -************************************************************************/ - -static int export_counter(int num, counter_t * addr); -static void capture(void *arg, long period); -static int LS7166Init(int ch); -static long LS7166Read(int i); - -/*********************************************************************** -* INIT AND EXIT CODE * -************************************************************************/ - -#define MAX_CHAN 4 - -int rtapi_app_main(void) -{ - int n, retval; - - /* test for number of channels */ - if ((num_chan <= 0) || (num_chan > MAX_CHAN)) { - rtapi_print_msg(RTAPI_MSG_ERR, - "TIRO: ERROR: invalid num_chan: %d\n", num_chan); - return -1; - } - /* have good config info, connect to the HAL */ - comp_id = hal_init("hal_tiro"); - if (comp_id < 0) { - rtapi_print_msg(RTAPI_MSG_ERR, "TIRO: ERROR: hal_init() failed\n"); - return -1; - } - /* allocate shared memory for counter data */ - counter_array = hal_malloc(num_chan * sizeof(counter_t)); - if (counter_array == 0) { - rtapi_print_msg(RTAPI_MSG_ERR, - "TIRO: ERROR: hal_malloc() failed\n"); - hal_exit(comp_id); - return -1; - } - /* export all the variables for each counter */ - for (n = 0; n < num_chan; n++) { - /* export all vars */ - retval = export_counter(n, &(counter_array[n])); - if (retval != 0) { - rtapi_print_msg(RTAPI_MSG_ERR, - "TIRO: ERROR: counter %d var export failed\n", n + 1); - hal_exit(comp_id); - return -1; - } - /* init counter */ - *(counter_array[n].count) = 0; - *(counter_array[n].pos) = 0.0; - counter_array[n].pos_scale = 1.0; - - /* init counter chip */ - LS7166Init(n); - } - /* export functions */ - retval = hal_export_funct("tiro.capture-position", capture, - counter_array, 1, 0, comp_id); - if (retval != 0) { - rtapi_print_msg(RTAPI_MSG_ERR, - "TIRO: ERROR: capture funct export failed\n"); - hal_exit(comp_id); - return -1; - } - rtapi_print_msg(RTAPI_MSG_INFO, - "TIRO: installed %d encoder counters\n", num_chan); - hal_ready(comp_id); - return 0; -} - -void rtapi_app_exit(void) -{ - hal_exit(comp_id); -} - -/*********************************************************************** -* REALTIME ENCODER COUNTING AND UPDATE FUNCTIONS * -************************************************************************/ - -static void capture(void *arg, long period) -{ - counter_t *cntr; - int n; - - cntr = arg; - for (n = 0; n < num_chan; n++) { - - /* capture raw counts to latches */ - *(cntr->count) = LS7166Read(n); - /* scale count to make floating point position */ - *(cntr->pos) = *(cntr->count) * cntr->pos_scale; - /* move on to next channel */ - cntr++; - } - /* done */ -} - - -/*********************************************************************** -* BOARD SPECIFIC FUNCTIONS * -************************************************************************/ -/* - LS7166Init() - Initializes the channel -*/ - -int LS7166Init(int ch) -{ - rtapi_outb(CTRL(ch), 0x49); - rtapi_outb(CTRL(ch), 0xC3); - rtapi_outb(CTRL(ch), 0x80); - return 0; -} - - -/* - LS7166Read() - reads one channel -*/ -long LS7166Read(int i) -{ - union pos_tag { - long l; - struct byte_tag { char b0; char b1; char b2; char b3;} byte; - } pos; - - rtapi_outb(CTRL(i), 0x03); - pos.byte.b0=rtapi_inb(DATA(i)); - pos.byte.b1=rtapi_inb(DATA(i)); - pos.byte.b2=rtapi_inb(DATA(i)); - if (pos.byte.b2 < 0) { - pos.byte.b3 = -1; - } - else { - pos.byte.b3 = 0; - } - return pos.l; -} - -/*********************************************************************** -* LOCAL FUNCTION DEFINITIONS * -************************************************************************/ - -static int export_counter(int num, counter_t * addr) -{ - int retval, msg; - - /* This function exports a lot of stuff, which results in a lot of - logging if msg_level is at INFO or ALL. So we save the current value - of msg_level and restore it later. If you actually need to log this - function's actions, change the second line below */ - msg = rtapi_get_msg_level(); - rtapi_set_msg_level(RTAPI_MSG_WARN); - - /* export pin for counts captured by update() */ - retval = hal_pin_s32_newf(HAL_OUT, &(addr->count), comp_id, - "tiro.%d.counts", num); - if (retval != 0) { - return retval; - } - /* export pin for scaled position captured by update() */ - retval = hal_pin_float_newf(HAL_OUT, &(addr->pos), comp_id, - "tiro.%d.position", num); - if (retval != 0) { - return retval; - } - /* export parameter for scaling */ - retval = hal_param_float_newf(HAL_RW, &(addr->pos_scale), comp_id, - "tiro.%d.position-scale", num); - if (retval != 0) { - return retval; - } - /* restore saved message level */ - rtapi_set_msg_level(msg); - return 0; -} From 88935139726b2af4868fb961de4349555b4e8af3 Mon Sep 17 00:00:00 2001 From: andypugh Date: Sun, 12 Apr 2026 23:52:30 +0100 Subject: [PATCH 2/2] Fix merge coommit oversight --- src/hal/drivers/hal_gm.c | 8 ++++---- src/hal/drivers/hal_motenc.c | 8 ++++---- src/hal/drivers/hal_ppmc.c | 10 +++++----- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/hal/drivers/hal_gm.c b/src/hal/drivers/hal_gm.c index 5a0becf1b61..7ac656b6e83 100644 --- a/src/hal/drivers/hal_gm.c +++ b/src/hal/drivers/hal_gm.c @@ -1,10 +1,10 @@ #include #include -#include "rtapi.h" // RTAPI realtime OS API -#include "rtapi_app.h" // RTAPI realtime module decls -#include "rtapi_stdint.h" // portable ints -#include "hal.h" // HAL public API decls +#include // RTAPI realtime OS API +#include // RTAPI realtime module decls +#include // portable ints +#include // HAL public API decls #include "gm.h" // Hardware dependent defines #include diff --git a/src/hal/drivers/hal_motenc.c b/src/hal/drivers/hal_motenc.c index dbb33aa37b6..8406c9452a1 100644 --- a/src/hal/drivers/hal_motenc.c +++ b/src/hal/drivers/hal_motenc.c @@ -125,10 +125,10 @@ #include -#include "rtapi.h" // RTAPI realtime OS API. -#include "rtapi_app.h" // RTAPI realtime module decls. -#include "rtapi_stdint.h" // rtapi_s32 etc -#include "hal.h" // HAL public API decls. +#include // RTAPI realtime OS API. +#include // RTAPI realtime module decls. +#include // portable ints +#include // HAL public API decls. #include "motenc.h" // Hardware dependent defines. // Module information. diff --git a/src/hal/drivers/hal_ppmc.c b/src/hal/drivers/hal_ppmc.c index f97b278cc38..76dd18bfc4c 100644 --- a/src/hal/drivers/hal_ppmc.c +++ b/src/hal/drivers/hal_ppmc.c @@ -73,11 +73,11 @@ #include /* kmalloc() */ #include /* kmalloc() */ -#include "rtapi.h" /* RTAPI realtime OS API */ -#include "rtapi_app.h" /* RTAPI realtime module decls */ -#include "rtapi_stdint.h" // portable ints -#include "hal.h" /* HAL public API decls */ -#include "hal_parport.h" +#include /* RTAPI realtime OS API */ +#include /* RTAPI realtime module decls */ +#include // portable ints +#include /* HAL public API decls */ +#include #define MAX_BUS 3 /* max number of parports (EPP busses) */