Skip to content

Commit c91a0ee

Browse files
authored
Merge pull request #2478 from LinuxCNC/2.9
Merge recent changes to 2.9
2 parents 404aa40 + 3537b5c commit c91a0ee

3 files changed

Lines changed: 57 additions & 43 deletions

File tree

src/hal/components/enum.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1616
//
1717

18-
/* A configurable component to use Mesa PktUART for modbus control */
19-
18+
// Convert bit pins to enumerated ints and vice-versa
2019

2120
#include "rtapi.h"
2221
#include "rtapi_slab.h"
@@ -179,26 +178,28 @@ int rtapi_app_main(void){
179178
return 0;
180179

181180
fail0:
182-
free(e.insts);
181+
rtapi_kfree(e.insts);
183182
hal_exit(comp_id);
184183
return -1;
185184

186185
}
187186

188-
static void decode(void *v_inst, long period){;
187+
static void decode(void *v_inst, long period){
188+
int i;
189189
enum_inst_t *inst = v_inst;
190-
for (int i = 1; i <= inst->num_pins; i++){
190+
for (i = 1; i <= inst->num_pins; i++){
191191
if (*(inst->hal[0].en) == *(inst->hal[i].en)){
192192
*(inst->hal[i].bit) = 1;
193193
} else {
194194
*(inst->hal[i].bit) = 0;
195195
}
196196
}
197197
}
198-
static void encode(void *v_inst, long period){;
198+
static void encode(void *v_inst, long period){
199+
int i;
199200
enum_inst_t *inst = v_inst;
200201
*(inst->hal[0].en) = 0;
201-
for (int i = 1; i <= inst->num_pins; i++){
202+
for (i = 1; i <= inst->num_pins; i++){
202203
if (*(inst->hal[i].bit)){
203204
*(inst->hal[0].en) = *(inst->hal[i].en);
204205
}

src/hal/drivers/mesa-hostmot2/modbus/mesa_modbus.c

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,15 @@ char *ports[MAX_CHAN];
156156
RTAPI_MP_ARRAY_STRING(ports, MAX_CHAN, "PktUART names");
157157

158158
int rtapi_app_main(void){
159-
160-
rtapi_set_msg_level(DEBUG);
161-
162159
int retval;
160+
int i; // instance loops
161+
int c; // channel loop
162+
int p; // pin loops
163+
int j; // generic loops
163164
char hal_name[HAL_NAME_LEN];
164-
unsigned int rxmode;
165-
unsigned int txmode;
166-
unsigned int filter;
167-
165+
166+
rtapi_set_msg_level(DEBUG);
167+
168168
if (!ports[0]) {
169169
rtapi_print_msg(RTAPI_MSG_ERR, "The "COMP_NAME" component requires at least"
170170
" one valid pktuart port, eg ports=\"hm2_5i25.0.pktuart.7\"\n");
@@ -190,12 +190,12 @@ int rtapi_app_main(void){
190190
for (m->num_insts = 0; ports[m->num_insts];m->num_insts++) {}
191191
m->insts = (hm2_modbus_inst_t*)rtapi_kmalloc(m->num_insts * sizeof(hm2_modbus_inst_t), RTAPI_GFP_KERNEL);
192192
// Parse the config string
193-
for (int i = 0; i < m->num_insts; i++) {
193+
for (i = 0; i < m->num_insts; i++) {
194194
hm2_modbus_inst_t *inst = &m->insts[i];
195195
inst->num_chans = sizeof(channels)/sizeof(channels[0]);
196196
// there may be more pins than channels
197197
inst->num_pins = 0;
198-
for (int c = 0; c < inst->num_chans; c++){
198+
for (c = 0; c < inst->num_chans; c++){
199199
inst->num_pins += channels[c].count;
200200
}
201201
// Malloc structs and pins, some in main or kernel memory, some in HAL
@@ -244,8 +244,8 @@ int rtapi_app_main(void){
244244

245245
do_setup(inst);
246246

247-
int p = 0; // HAL pin index, not aligned to channel index
248-
for (int c = 0; c < inst->num_chans; c++){
247+
p = 0; // HAL pin index, not aligned to channel index
248+
for (c = 0; c < inst->num_chans; c++){
249249
hm2_modbus_channel_t *ch = &(inst->chans[c]);
250250
hal_pin_dir_t dir;
251251
rtapi_print_msg(RTAPI_MSG_INFO, "ch %i is at %p\n", c, ch);
@@ -277,7 +277,7 @@ int rtapi_app_main(void){
277277
case 1: // read coils
278278
case 2: // read inputs
279279
if (ch->count > 1){
280-
for (int j = 0; j < ch->count; j++){
280+
for (j = 0; j < ch->count; j++){
281281
retval = hal_pin_bit_newf(dir,
282282
(hal_bit_t**)&(inst->hal->pins[p++]),
283283
comp_id, COMP_NAME".%02i.%s-%02i",
@@ -310,7 +310,7 @@ int rtapi_app_main(void){
310310
// deliberate fall-through
311311
case 3: // read holding registers
312312
case 4: // read input registers
313-
for (int j = 0; j < ch->count; j++){
313+
for (j = 0; j < ch->count; j++){
314314
switch (ch->type){
315315
case HAL_U32:
316316
if (ch->count > 1) {
@@ -391,7 +391,7 @@ int rtapi_app_main(void){
391391
break;
392392
default:
393393
rtapi_print_msg(RTAPI_MSG_ERR,
394-
"Unsupported HAL pin type in mesa_modbus definition file\n",
394+
"Unsupported HAL pin type (%i) in mesa_modbus definition file\n",
395395
ch->type);
396396
goto fail0;
397397
} // type switch
@@ -472,14 +472,14 @@ int send_modbus_pkt(hm2_modbus_inst_t *inst){
472472
rtapi_u16 checksum;
473473
rtapi_u16 fsizes[1];
474474
rtapi_u8 frames;
475-
int p = 0; // data string pointer
475+
int i;
476476

477477
checksum = RTU_CRC(ch->data, ch->ptr + 1);
478478
ch->data[++(ch->ptr)] = checksum & 0xFF;
479479
ch->data[++(ch->ptr)] = (checksum >> 8) & 0xFF;
480480

481481
rtapi_print_msg(RTAPI_MSG_INFO, "Sending to %s %i bytes ", inst->port, ch->ptr + 1);
482-
for (int i = 0; i <= ch->ptr; i++) rtapi_print_msg(RTAPI_MSG_INFO, " %02X ", ch->data[i]);
482+
for (i = 0; i <= ch->ptr; i++) rtapi_print_msg(RTAPI_MSG_INFO, " %02X ", ch->data[i]);
483483
rtapi_print_msg(RTAPI_MSG_INFO, "\n");
484484

485485
frames = 1;
@@ -543,7 +543,7 @@ void process(void *arg, long period) {
543543
break;
544544
case WAIT_FOR_SEND_BEGIN:
545545
// single cycle delay to allow for queued data
546-
rtapi_print_msg(RTAPI_MSG_INFO, "WAIT_FOR_SEND_BEGIN\n", txstatus, rxstatus);
546+
rtapi_print_msg(RTAPI_MSG_INFO, "WAIT_FOR_SEND_BEGIN RX %X TX %X\n", txstatus, rxstatus);
547547
do_timeout(inst); // just to reset the counter
548548
inst->state = WAIT_FOR_SEND_COMPLETE;
549549
break;
@@ -645,9 +645,9 @@ int build_data_frame(hm2_modbus_inst_t *inst){
645645
hm2_modbus_channel_t *ch = &(inst->chans[inst->index]);
646646
hm2_modbus_hal_t hal = *inst->hal;
647647
rtapi_u8 acc = 0;
648-
int byte_count;
649648
int r;
650649
int p = ch->start_pin;
650+
int i;
651651

652652
rtapi_print_msg(RTAPI_MSG_INFO, "building packet %X %X start pin %i\n", ch->func, ch->addr, p);
653653

@@ -686,13 +686,15 @@ int build_data_frame(hm2_modbus_inst_t *inst){
686686
}
687687
rtapi_print_msg(RTAPI_MSG_INFO, "678\n");
688688
break;
689+
default:
690+
break;
689691
}
690692
break;
691693
case 15: // Write multiple coils
692694
r += ch_append16(ch, ch->addr);
693695
r += ch_append16(ch, ch->count);
694696
r += ch_append8(ch, ( ch->count + 8 - 1) / 8);
695-
for (int i = 0; i < ch->count; i++){
697+
for (i = 0; i < ch->count; i++){
696698
if (hal.pins[p++]->b) acc += 1 << (i % 8);
697699
if (i % 8 == 7 || i == (ch->count -1)) { // time for the next byte
698700
r += ch_append8(ch, acc);
@@ -704,7 +706,7 @@ int build_data_frame(hm2_modbus_inst_t *inst){
704706
r += ch_append16(ch, ch->addr);
705707
r += ch_append16(ch, ch->count);
706708
r += ch_append8(ch, ch->count * 2);
707-
for (int i = 0; i < ch->count; i++){
709+
for (i = 0; i < ch->count; i++){
708710
switch (ch->type){
709711
case HAL_U32:
710712
r += ch_append16(ch, (rtapi_u16)hal.pins[p]->u);
@@ -717,6 +719,8 @@ int build_data_frame(hm2_modbus_inst_t *inst){
717719
r+= ch_append16(ch, (rtapi_u16)((hal.pins[p]->f - hal.pin2[p]->f) / *hal.scale[p])) ;
718720
}
719721
break;
722+
default:
723+
break;
720724
}
721725
p++ ; // increment pin pointer
722726
}
@@ -777,13 +781,14 @@ int parse_data_frame(hm2_modbus_inst_t *inst){
777781
for (i = 0; i < bytes[2] / 2; i++){
778782
switch (ch->type){
779783
case HAL_U32:
780-
hal.pins[p]->u = 256 * bytes[w++] + bytes[w++];
784+
hal.pins[p]->u = 256 * bytes[w] + bytes[w + 1];
785+
w += 2;
781786
p++;
782787
break;
783788
case HAL_S32: // wrap the result into the (s32) offset too
784789
tmp = hal.pins[p]->s;
785-
//hal.pins[p]->s = bytes[w++] * 256 + bytes[w++];
786-
hal.pins[p]->s = (rtapi_s16)( hal.pins[p]->s - 0x4000); // Testing wrap
790+
hal.pins[p]->s = bytes[w] * 256 + bytes[w + 1];
791+
w += 2;
787792
tmp = hal.pins[p]->s - tmp;
788793
if (tmp > 32768) tmp -= 65536;
789794
if (tmp < -32768) tmp += 65536;
@@ -792,10 +797,13 @@ int parse_data_frame(hm2_modbus_inst_t *inst){
792797
p++;
793798
break;
794799
case HAL_FLOAT:
795-
hal.pins[p]->f = (256 * bytes[w++] + bytes[w++])
800+
hal.pins[p]->f = (256 * bytes[w] + bytes[w + 1])
796801
* *(hal.scale[p]) + hal.pin2[p]->f;
802+
w += 2;
797803
p++;
798804
break;
805+
default:
806+
break;
799807
}
800808
}
801809
break;
@@ -821,12 +829,11 @@ int parse_data_frame(hm2_modbus_inst_t *inst){
821829
default:
822830
rtapi_print_msg(RTAPI_MSG_ERR, "Unknown or unsupported Modbus function code\n");
823831
}
824-
832+
return 0;
825833
}
826834

827835
void rtapi_app_exit(void){
828836
int i;
829-
int j;
830837
for (i = 0; i < m->num_insts;i++){
831838
if (m->insts[i].chans != NULL) rtapi_kfree(m->insts[i].chans);
832839
// rtapi_kfree(m->insts[i].hal); Automatically freed as was hal_malloc-ed
@@ -836,14 +843,15 @@ if (m != NULL) rtapi_kfree(m);
836843
hal_exit(comp_id);
837844
}
838845

839-
uint16_t RTU_CRC(rtapi_u8* buf, int len)
840-
{
846+
uint16_t RTU_CRC(rtapi_u8* buf, int len){
841847
uint16_t crc = 0xFFFF;
848+
int pos;
849+
int i;
842850

843-
for (int pos = 0; pos < len; pos++) {
851+
for (pos = 0; pos < len; pos++) {
844852
crc ^= (uint16_t)buf[pos]; // XOR byte into least sig. byte of crc
845853

846-
for (int i = 8; i != 0; i--) { // Loop over each bit
854+
for (i = 8; i != 0; i--) { // Loop over each bit
847855
if ((crc & 0x0001) != 0) { // If the LSB is set
848856
crc >>= 1; // Shift right and XOR 0xA001
849857
crc ^= 0xA001;

src/hal/drivers/mesa-hostmot2/pktuart.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,6 @@ int hm2_pktuart_read(char *name, unsigned char data[], rtapi_u8 *num_frames, rta
559559
* fsizes should be u32 x 16
560560
* FIXME: decide how to work out that the data has all been transferred
561561
*/
562-
EXPORT_SYMBOL_GPL(hm2_pktuart_queue_get_frame_sizes);
563562
int hm2_pktuart_queue_get_frame_sizes(char *name, rtapi_u32 fsizes[]){
564563
// queue as many reads of the FIFO as there are frames
565564
hostmot2_t *hm2;
@@ -589,6 +588,7 @@ int hm2_pktuart_queue_get_frame_sizes(char *name, rtapi_u32 fsizes[]){
589588
}
590589
return j - 1;
591590
}
591+
EXPORT_SYMBOL_GPL(hm2_pktuart_queue_get_frame_sizes);
592592

593593
/* This function queues sufficient reads to extract the available data.
594594
* It does no error checking and does not explicitly check if there is
@@ -599,7 +599,7 @@ int hm2_pktuart_queue_get_frame_sizes(char *name, rtapi_u32 fsizes[]){
599599
* frames, which should have been previously read by
600600
* hm2_pktuart_queue_get_frame_sizes returns the number of frame reads queued.
601601
*/
602-
EXPORT_SYMBOL_GPL(hm2_pktuart_queue_read_data);
602+
603603
int hm2_pktuart_queue_read_data(char *name, rtapi_u32 data[], int bytes) {
604604
hostmot2_t *hm2;
605605
int r;
@@ -628,35 +628,40 @@ int hm2_pktuart_queue_read_data(char *name, rtapi_u32 data[], int bytes) {
628628
}
629629
return i - 1;
630630
}
631+
EXPORT_SYMBOL_GPL(hm2_pktuart_queue_read_data);
632+
631633

632-
EXPORT_SYMBOL_GPL(hm2_pktuart_get_rx_status);
633634
rtapi_u32 hm2_pktuart_get_rx_status(char *name){
634635
hostmot2_t *hm2;
635636
int i = hm2_get_pktuart(&hm2, name);
636637
return hm2->pktuart.rx_status_reg[i];
637638
}
639+
EXPORT_SYMBOL_GPL(hm2_pktuart_get_rx_status);
640+
638641

639-
EXPORT_SYMBOL_GPL(hm2_pktuart_get_tx_status);
640642
rtapi_u32 hm2_pktuart_get_tx_status(char *name){
641643
hostmot2_t *hm2;
642644
int i = hm2_get_pktuart(&hm2, name);
643645
return hm2->pktuart.tx_status_reg[i];
644646
}
647+
EXPORT_SYMBOL_GPL(hm2_pktuart_get_tx_status);
648+
645649

646-
EXPORT_SYMBOL_GPL(hm2_pktuart_get_clock);
647650
int hm2_pktuart_get_clock(char* name){
648651
hostmot2_t *hm2;
649652
int i = hm2_get_pktuart(&hm2, name);
650653
hm2_pktuart_instance_t inst = hm2->pktuart.instance[i];
651654
return inst.clock_freq;
652655
}
656+
EXPORT_SYMBOL_GPL(hm2_pktuart_get_clock);
657+
653658

654-
EXPORT_SYMBOL_GPL(hm2_pktuart_get_version);
655659
int hm2_pktuart_get_version(char* name){
656660
hostmot2_t *hm2;
657661
hm2_get_pktuart(&hm2, name);
658662
return hm2->pktuart.tx_version + 16 * hm2->pktuart.rx_version ;
659663
}
664+
EXPORT_SYMBOL_GPL(hm2_pktuart_get_version);
660665

661666
void hm2_pktuart_print_module(hostmot2_t *hm2){
662667
int i;

0 commit comments

Comments
 (0)