@@ -156,15 +156,15 @@ char *ports[MAX_CHAN];
156156RTAPI_MP_ARRAY_STRING (ports , MAX_CHAN , "PktUART names" );
157157
158158int 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
827835void rtapi_app_exit (void ){
828836int i ;
829- int j ;
830837for (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);
836843hal_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 ;
0 commit comments