Skip to content

Commit 52c7fb8

Browse files
committed
Expand hardware drivers to 64 bits (address issue #2331)
1 parent a3836b3 commit 52c7fb8

9 files changed

Lines changed: 80 additions & 326 deletions

File tree

debian/copyright

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ Files:
247247
src/hal/drivers/hal_speaker.c
248248
src/hal/drivers/hal_stg.c
249249
src/hal/drivers/hal_stg.h
250-
src/hal/drivers/hal_tiro.c
251250
src/hal/drivers/hal_vti.c
252251
src/hal/drivers/hal_vti.h
253252
src/hal/drivers/motenc.h

src/Makefile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -931,8 +931,6 @@ endif
931931
ifneq ($(BUILD_SYS),uspace)
932932
obj-$(CONFIG_PCI_8255) += pci_8255.o
933933
pci_8255-objs := hal/drivers/pci_8255.o
934-
obj-$(CONFIG_HAL_TIRO) += hal_tiro.o
935-
hal_tiro-objs := hal/drivers/hal_tiro.o $(MATHSTUB)
936934
obj-$(CONFIG_HAL_STG) += hal_stg.o
937935
hal_stg-objs := hal/drivers/hal_stg.o $(MATHSTUB)
938936
obj-$(CONFIG_HAL_VTI) += hal_vti.o
@@ -1331,7 +1329,6 @@ endif
13311329
#../rtlib/uparport$(MODULE_EXT): $(addprefix objects/rt,$(uparport-objs))
13321330
ifneq ($(BUILD_SYS),uspace)
13331331
../rtlib/pci_8255$(MODULE_EXT): $(addprefix objects/rt,$(pci_8255-objs))
1334-
../rtlib/hal_tiro$(MODULE_EXT): $(addprefix objects/rt,$(hal_tiro-objs))
13351332
../rtlib/hal_stg$(MODULE_EXT): $(addprefix objects/rt,$(hal_stg-objs))
13361333
../rtlib/hal_vti$(MODULE_EXT): $(addprefix objects/rt,$(hal_vti-objs))
13371334
../rtlib/hal_evoreg$(MODULE_EXT): $(addprefix objects/rt,$(hal_evoreg-objs))

src/Makefile.inc.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ CONFIG_SAMPLER=m
208208
CONFIG_UPARPORT=m
209209
CONFIG_HAL_PARPORT=m
210210
CONFIG_PROBE_PARPORT=m
211-
CONFIG_HAL_TIRO=m
212211
CONFIG_HAL_EVOREG=m
213212
CONFIG_HAL_MOTENC=m
214213
CONFIG_HAL_SKELETON=m

src/hal/drivers/hal_evoreg.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ typedef struct {
103103
hal_bit_t *digital_in[47]; /* ptrs for digital input pins 0 - 45 */
104104
hal_bit_t *digital_out[25]; /* ptrs for digital output pins 0 - 20 */
105105
__u16 raw_counts_old[3];
106-
__s32 counts[3];
106+
__s64 counts[3];
107107
hal_float_t pos_scale; /*! \todo scale for position command FIXME should be one per axis */
108108
} evoreg_t;
109109

@@ -293,7 +293,7 @@ static void update_port(void *arg, long period)
293293
writew((*(port->dac_out[1])/10 * 0x7fff), (char *)port->io_base + 0x80);
294294
writew((*(port->dac_out[2])/10 * 0x7fff), (char *)port->io_base + 0xa0);
295295

296-
/* Read Encoders, improve the 16bit hardware counters to 32bit and scale the values */
296+
/* Read Encoders, improve the 16bit hardware counters to 64bit and scale the values */
297297
raw_counts[0] = (__u16) readw(port->io_base);
298298
raw_counts[1] = (__u16) readw((char *)port->io_base + 0x08 );
299299
raw_counts[2] = (__u16) readw((char *)port->io_base + 0x10 );

src/hal/drivers/hal_gm.c

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include <rtapi_pci.h>
22
#include <rtapi_io.h>
33

4-
#include <rtapi.h> // RTAPI realtime OS API
5-
#include <rtapi_app.h> // RTAPI realtime module decls
6-
#include <hal.h> // HAL public API decls
4+
#include "rtapi.h" // RTAPI realtime OS API
5+
#include "rtapi_app.h" // RTAPI realtime module decls
6+
#include "rtapi_stdint.h" // portable ints
7+
#include "hal.h" // HAL public API decls
78
#include "gm.h" // Hardware dependent defines
89
#include <rtapi_math.h>
910

@@ -16,9 +17,11 @@ typedef struct { //encoder_t
1617
// Pins
1718
hal_bit_t *reset;
1819
hal_s32_t *counts;
20+
hal_s64_t *counts64;
1921
hal_float_t *position;
2022
hal_float_t *velocity;
2123
hal_s32_t *rawcounts;
24+
hal_s64_t *rawcounts64;
2225
hal_bit_t *index_enable;
2326

2427
// Parameters
@@ -30,11 +33,11 @@ typedef struct { //encoder_t
3033
hal_float_t min_speed_estimate;
3134

3235
// Private data
33-
hal_s32_t raw_offset;
34-
hal_s32_t index_offset;
35-
hal_s32_t last_index_latch;
36+
rtapi_s64 index_offset;
37+
rtapi_s64 last_index_latch;
3638
hal_bit_t first_index;
3739
hal_bit_t module_exist;
40+
rtapi_s32 old_counts;
3841
} encoder_t;
3942

4043
typedef struct { //switches_t
@@ -239,27 +242,27 @@ typedef struct { //stepgen_t
239242
hal_float_t *position_cmd;
240243
hal_float_t *velocity_cmd;
241244
hal_float_t *position_fb;
242-
hal_s32_t *count_fb;
245+
hal_s64_t *count_fb;
243246
hal_bit_t *enable;
244247

245248
// Parameters
246249
hal_u32_t step_type; //0: StepDir, 1: UpDown, 2: Quadrature
247-
hal_bit_t control_type; //0: position, 1: velocity
250+
hal_bit_t control_type; //0: position, 1: velocity
248251
hal_u32_t steplen;
249252
hal_u32_t stepspace;
250253
hal_u32_t dirdelay;
251254
hal_float_t maxaccel;
252255
hal_float_t maxvel;
253256
hal_bit_t polarity_A;
254-
hal_bit_t polarity_B;
257+
hal_bit_t polarity_B;
255258
hal_float_t position_scale;
256259

257260
//Saved Parameters
258261
hal_u32_t curr_steplen;
259262
hal_u32_t curr_stepspace;
260263
hal_u32_t curr_dirdelay;
261264
hal_float_t curr_maxaccel;
262-
hal_float_t curr_maxvel;
265+
hal_float_t curr_maxvel;
263266
hal_float_t curr_position_scale;
264267

265268
// Private data
@@ -623,9 +626,11 @@ ExportEncoder(void *arg, int comp_id, int version)
623626
//Export Pins
624627
if(error == 0) error = hal_pin_bit_newf(HAL_IN, &(device->encoder[i].reset), comp_id, "gm.%1d.encoder.%1d.reset", boardId, i);
625628
if(error == 0) error = hal_pin_s32_newf(HAL_OUT, &(device->encoder[i].counts), comp_id, "gm.%1d.encoder.%1d.counts", boardId, i);
629+
if(error == 0) error = hal_pin_s64_newf(HAL_OUT, &(device->encoder[i].counts64), comp_id, "gm.%1d.encoder.%1d.counts64", boardId, i);
626630
if(error == 0) error = hal_pin_float_newf(HAL_OUT, &(device->encoder[i].position), comp_id, "gm.%1d.encoder.%1d.position", boardId, i);
627631
if(error == 0) error = hal_pin_float_newf(HAL_OUT, &(device->encoder[i].velocity), comp_id, "gm.%1d.encoder.%1d.velocity", boardId, i);
628632
if(error == 0) error = hal_pin_s32_newf(HAL_OUT, &(device->encoder[i].rawcounts), comp_id, "gm.%1d.encoder.%1d.rawcounts", boardId, i);
633+
if(error == 0) error = hal_pin_s64_newf(HAL_OUT, &(device->encoder[i].rawcounts64), comp_id, "gm.%1d.encoder.%1d.rawcounts", boardId, i);
629634
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);
630635

631636
//Export Parameters
@@ -637,7 +642,7 @@ ExportEncoder(void *arg, int comp_id, int version)
637642
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);
638643

639644
//Init parameters
640-
device->encoder[i].raw_offset = pCard->ENC_counter[i];
645+
device->encoder[i].old_counts = pCard->ENC_counter[i];
641646
device->encoder[i].index_offset = 0;
642647
device->encoder[i].last_index_latch = pCard->ENC_index_latch[i];
643648
device->encoder[i].first_index = 1;
@@ -679,7 +684,7 @@ ExportStepgen(void *arg, int comp_id, int version)
679684
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);
680685
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);
681686
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);
682-
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);
687+
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);
683688
if(error == 0) error = hal_pin_bit_newf(HAL_IN, &(device->stepgen[i].enable), comp_id, "gm.%1d.stepgen.%1d.enable", boardId, i);
684689

685690
//Export Parameters.
@@ -1472,7 +1477,7 @@ encoder(void *arg, long period)
14721477
card *pCard = device->pCard;
14731478

14741479
int i;
1475-
hal_s32_t temp1 = 0, temp2;
1480+
rtapi_s32 temp1 = 0, temp2, delta_counts;
14761481
hal_float_t vel;
14771482

14781483
//Update parameters
@@ -1529,12 +1534,15 @@ encoder(void *arg, long period)
15291534
}
15301535
}
15311536
device->encoder[i].last_index_latch = temp2;
1532-
1533-
*(device->encoder[i].rawcounts) = temp1 - device->encoder[i].raw_offset;
1537+
delta_counts = temp1 - device->encoder[i].old_counts;
1538+
device->encoder[i].old_counts = temp1;
1539+
*(device->encoder[i].rawcounts) += delta_counts;
1540+
*(device->encoder[i].rawcounts64) += delta_counts;
15341541
*(device->encoder[i].counts) = *(device->encoder[i].rawcounts) - device->encoder[i].index_offset;
1542+
*(device->encoder[i].counts64) = *(device->encoder[i].rawcounts64) - device->encoder[i].index_offset;
15351543

15361544
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
1537-
*(device->encoder[i].position) = (hal_float_t) *(device->encoder[i].counts) / device->encoder[i].position_scale;
1545+
*(device->encoder[i].position) = (hal_float_t) *(device->encoder[i].counts64) / device->encoder[i].position_scale;
15381546

15391547
vel = (hal_float_t) pCard->ENC_period[i];
15401548
if(vel == 0) vel = 1;

src/hal/drivers/hal_motenc.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*
2626
* Pins:
2727
* s32 motenc.<boardId>.enc-<channel>-count
28+
* s64 motenc.<boardId>.enc-<channel>-count64
2829
* float motenc.<boardId>.enc-<channel>-position
2930
* bit motenc.<boardId>.enc-<channel>-index
3031
* bit motenc.<boardId>.enc-<channel>-index-enable
@@ -124,9 +125,10 @@
124125

125126
#include <linux/pci.h>
126127

127-
#include <rtapi.h> // RTAPI realtime OS API.
128-
#include <rtapi_app.h> // RTAPI realtime module decls.
129-
#include <hal.h> // HAL public API decls.
128+
#include "rtapi.h" // RTAPI realtime OS API.
129+
#include "rtapi_app.h" // RTAPI realtime module decls.
130+
#include "rtapi_stdint.h" // rtapi_s32 etc
131+
#include "hal.h" // HAL public API decls.
130132
#include "motenc.h" // Hardware dependent defines.
131133

132134
// Module information.
@@ -146,6 +148,7 @@ MODULE_LICENSE("GPL");
146148

147149
typedef struct {
148150
// Pins.
151+
hal_s64_t *pCount64;
149152
hal_s32_t *pCount; // Captured binary count value.
150153
hal_float_t *pPosition; // Scaled position (floating point).
151154
hal_bit_t *pIndex; // Current state of index.
@@ -161,6 +164,7 @@ typedef struct {
161164
// Private data.
162165
double oldScale; // Stored scale value.
163166
double scaleRecip; // Reciprocal value used for scaling.
167+
rtapi_s32 pCount_old; // Previous counts, for bit width extending
164168
} EncoderPinsParams;
165169

166170
typedef struct {
@@ -494,6 +498,10 @@ Device_ExportEncoderPinsParametersFunctions(Device *this, int componentId, int b
494498
componentId, "motenc.%d.enc-%02d-count", boardId, channel)) != 0)
495499
break;
496500

501+
if((halError = hal_pin_s64_newf(HAL_OUT, &(this->encoder[channel].pCount64),
502+
componentId, "motenc.%d.enc-%02d-count64", boardId, channel)) != 0)
503+
break;
504+
497505
if((halError = hal_pin_float_newf(HAL_OUT, &(this->encoder[channel].pPosition),
498506
componentId, "motenc.%d.enc-%02d-position", boardId, channel)) != 0)
499507
break;
@@ -517,6 +525,8 @@ Device_ExportEncoderPinsParametersFunctions(Device *this, int componentId, int b
517525

518526
// Init encoder.
519527
*(this->encoder[channel].pCount) = 0;
528+
*(this->encoder[channel].pCount64) = 0;
529+
this->encoder[channel].pCount_old = 0;
520530
*(this->encoder[channel].pPosition) = 0.0;
521531
*(this->encoder[channel].pIndex) = 0;
522532
*(this->encoder[channel].pIndexEnable) = 0;
@@ -794,6 +804,9 @@ Device_EncoderRead(void *arg, long period)
794804

795805
// Read encoder counts.
796806
*(pEncoder->pCount) = pCard->fpga[i].encoderCount[j];
807+
// Extend counts to 64 bits
808+
*(pEncoder->pCount64) += *(pEncoder->pCount) - pEncoder->pCount_old;
809+
pEncoder->pCount_old = *(pEncoder->pCount);
797810

798811
// Check for change in scale value.
799812
if ( pEncoder->scale != pEncoder->oldScale ) {
@@ -811,7 +824,7 @@ Device_EncoderRead(void *arg, long period)
811824
}
812825

813826
// Scale count to make floating point position.
814-
*(pEncoder->pPosition) = *(pEncoder->pCount) * pEncoder->scaleRecip;
827+
*(pEncoder->pPosition) = *(pEncoder->pCount64) * pEncoder->scaleRecip;
815828
}
816829
}
817830
}

src/hal/drivers/hal_ppmc.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@
7373

7474
#include <rtapi_slab.h> /* kmalloc() */
7575
#include <rtapi_io.h> /* kmalloc() */
76-
#include <rtapi.h> /* RTAPI realtime OS API */
77-
#include <rtapi_app.h> /* RTAPI realtime module decls */
78-
#include <hal.h> /* HAL public API decls */
79-
#include <rtapi_parport.h>
76+
#include "rtapi.h" /* RTAPI realtime OS API */
77+
#include "rtapi_app.h" /* RTAPI realtime module decls */
78+
#include "rtapi_stdint.h" // portable ints
79+
#include "hal.h" /* HAL public API decls */
80+
#include "hal_parport.h"
8081

8182
#define MAX_BUS 3 /* max number of parports (EPP busses) */
8283

@@ -293,12 +294,13 @@ typedef union {
293294
typedef struct {
294295
hal_float_t *position; /* output: scaled position pointer */
295296
hal_s32_t *count; /* output: unscaled encoder counts */
297+
hal_s64_t *count64; /* 64 bit version to avoid wrapping */
296298
hal_s32_t *delta; /* output: delta counts since last read */
297299
hal_s32_t prevdir; /* local: previous direction */
298300
hal_float_t scale; /* parameter: scale factor */
299301
hal_bit_t *index; /* output: index flag */
300302
hal_bit_t *index_enable; /* enable index pulse to reset encoder count */
301-
hal_s32_t oldreading; /* used to detect overflow / underflow of the counter JE001 */
303+
rtapi_s64 oldreading; /* used to detect overflow / underflow of the counter JE001 AP001*/
302304
unsigned int indres; /* copy of reset-on-index register bits (only valid on 1st encoder of board)*/
303305
unsigned int indrescnt; /* counts servo cycles since index reset was turned on */
304306
hal_float_t *vel; /* output: scaled velocity */
@@ -1076,7 +1078,7 @@ static void read_encoders(slot_data_t *slot)
10761078
int i, byteindex, byteindx2;
10771079
double vel; // local temporary velocity
10781080
union pos_tag {
1079-
hal_s32_t l; // JE001
1081+
rtapi_s64 l; // JE001 AP001
10801082
struct byte_tag {
10811083
signed char b0;
10821084
signed char b1;
@@ -1106,16 +1108,16 @@ static void read_encoders(slot_data_t *slot)
11061108
for (i = 0; i < 4; i++) {
11071109
slot->encoder[i].indrescnt++; /* increment counter each servo cycle */
11081110
oldpos.l = slot->encoder[i].oldreading;
1111+
pos.l = *(slot->encoder[i].count); // init the higher-order bytes
11091112
pos.byte.b0 = (signed char)slot->rd_buf[byteindex++];
11101113
pos.byte.b1 = (signed char)slot->rd_buf[byteindex++];
11111114
pos.byte.b2 = (signed char)slot->rd_buf[byteindex++];
1112-
pos.byte.b3 = oldpos.byte.b3;
11131115
/* check for - to + transition */
11141116
if ((oldpos.byte.b2 & 0xc0) == 0xc0 && (pos.byte.b2 == 0))
1115-
pos.byte.b3++;
1117+
pos.l += 0x01000000;
11161118
else
11171119
if ((oldpos.byte.b2 == 0) && (pos.byte.b2 & 0xc0) == 0xc0)
1118-
pos.byte.b3--;
1120+
pos.l -= 0x01000000;
11191121
*(slot->encoder[i].delta) = pos.l - slot->encoder[i].oldreading;
11201122
vel = (pos.l - slot->encoder[i].oldreading) /
11211123
(read_period * 1e-9 * slot->encoder[i].scale);
@@ -1135,9 +1137,9 @@ static void read_encoders(slot_data_t *slot)
11351137
/* need to properly set the 24->32 bit extension byte */
11361138
if ( pos.byte.b2 < 0 ) {
11371139
/* going backwards */
1138-
pos.byte.b3 = 0xFF;
1140+
pos.l |= 0xFFFFFFFFFF000000;
11391141
} else {
1140-
pos.byte.b3 = 0;
1142+
pos.l &= 0x0000000000FFFFFF;
11411143
}
11421144
oldpos.byte.b3 = pos.byte.b3;
11431145
}

0 commit comments

Comments
 (0)