jtag_tap: Rewrite jtag_tap_shift to support non-multiple-of-8 data_bits
authorSylvain Munaut <tnt@246tNt.com>
Tue, 8 Jun 2021 16:24:51 +0000 (18:24 +0200)
committerGregory Davill <greg.davill@gmail.com>
Wed, 9 Jun 2021 00:37:17 +0000 (10:07 +0930)
Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
ecpprog/jtag_tap.c

index 737bc010cfe1b97f86072a55f84652d5387d5943..ac636ab554ea0b4735356fccd176553a3872fea4 100644 (file)
@@ -224,31 +224,27 @@ void jtag_tap_shift(
 {
        /* if 'must_end' the send last byte seperately 
         * This way we toggle TMS on the last clock cycle */
-       if(must_end)
-               data_bits -= 8;
-       
-       uint32_t data_bits_sent = 0;
-       if(data_bits){
-               while(data_bits_sent != data_bits){
-
-                       uint32_t _data_bits = MIN(4096 + 2048, data_bits - data_bits_sent);
-
-                       jtag_shift_bytes(
-                               input_data + data_bits_sent/8,
-                               output_data + data_bits_sent/8,
-                               _data_bits,
-                               false
-                       );
-                       data_bits_sent += _data_bits;
-               }
+
+       while (data_bits >= (8 + must_end)) {
+               uint32_t _data_bits = MIN(4096 + 2048, data_bits - must_end) & ~7U;
+
+               jtag_shift_bytes(
+                       input_data,
+                       output_data,
+                       _data_bits,
+                       false
+               );
+
+               data_bits   -= _data_bits;
+               input_data  += _data_bits / 8;
+               output_data += _data_bits / 8;
        }
 
-       /* Send our last byte */
-       if(must_end){
+       if (data_bits > 0) {
                _jtag_tap_shift(
-                       input_data + data_bits_sent/8,
-                       output_data + data_bits_sent/8,
-                       8,
+                       input_data,
+                       output_data,
+                       data_bits,
                        must_end
                );
        }