     int byte_count = BYTE_COUNT(data_length_bits);
 
     for (int i = 0; i < byte_count; ++i) {
-      uint8_t byte_out = data[i];
+      uint8_t byte_out = data[byte_count - 1 - i];
       uint8_t tdo_byte = 0;
       for (int j = 0; j < 8 && data_length_bits-- > 0; ++j) {
         if (data_length_bits == 0 && is_end) {


  void set_state(int state) {
    if (current_state_ != state) {
      DEBUG("Switching to state %s.", state_name(state));
    }
    current_state_ = state;
  }

    Serial.println();                               \
  }

#if 0
#define DEBUG_STATE(s) { Serial.print("D State -> "); \
    Serial.print(state_name(s)); }
#else
#define DEBUG_STATE(s) {}
#endif

#if 0
#define DEBUG(fmt, ...)  { Serial.print("D ");  \
    p(fmt, ## __VA_ARGS__);                     \
    Serial.println();                           \
