Browse Source

Coding standards

Scott Lahteine 5 years ago
parent
commit
3a1b6fe8c1
43 changed files with 2149 additions and 2460 deletions
  1. 1
    1
      Marlin/src/HAL/HAL_DUE/DebugMonitor_Due.cpp
  2. 1
    1
      Marlin/src/HAL/HAL_DUE/u8g_com_HAL_DUE_shared_hw_spi.cpp
  3. 1
    1
      Marlin/src/HAL/HAL_LPC1768/DebugMonitor_LPC1768.cpp
  4. 16
    19
      Marlin/src/HAL/HAL_LPC1768/u8g/HAL_LCD_pin_routines.c
  5. 3
    4
      Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_ssd_hw_i2c.cpp
  6. 1
    1
      Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_st7920_hw_spi.cpp
  7. 2
    4
      Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_st7920_sw_spi.cpp
  8. 1
    1
      Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_sw_spi.cpp
  9. 3
    3
      Marlin/src/HAL/HAL_STM32F1/u8g_com_stm32duino_fsmc.cpp
  10. 2
    2
      Marlin/src/HAL/HAL_TEENSY31_32/HAL_timers_Teensy.cpp
  11. 2
    2
      Marlin/src/HAL/HAL_TEENSY35_36/HAL_timers_Teensy.cpp
  12. 1
    1
      Marlin/src/HAL/HAL_TEENSY35_36/pinsDebug.h
  13. 1
    1
      Marlin/src/HAL/shared/backtrace/unwarm.h
  14. 69
    129
      Marlin/src/HAL/shared/backtrace/unwarm_arm.cpp
  15. 116
    176
      Marlin/src/HAL/shared/backtrace/unwarm_thumb.cpp
  16. 24
    36
      Marlin/src/HAL/shared/backtrace/unwarmmem.cpp
  17. 3
    10
      Marlin/src/HAL/shared/backtrace/unwinder.cpp
  18. 1
    1
      Marlin/src/feature/emergency_parser.h
  19. 1
    1
      Marlin/src/feature/prusa_MMU2/mmu2.cpp
  20. 1
    1
      Marlin/src/gcode/calibrate/G425.cpp
  21. 3
    3
      Marlin/src/lcd/dogm/u8g_dev_ssd1306_sh1106_128x64_I2C.cpp
  22. 2
    2
      Marlin/src/lcd/dogm/u8g_dev_st7565_64128n_HAL.cpp
  23. 2
    2
      Marlin/src/lcd/dogm/u8g_dev_st7920_128x64_HAL.cpp
  24. 1
    1
      Marlin/src/lcd/dogm/u8g_dev_tft_320x240_upscale_from_128x64.cpp
  25. 2
    2
      Marlin/src/lcd/dogm/u8g_dev_uc1701_mini12864_HAL.cpp
  26. 1
    1
      Marlin/src/sd/Sd2Card.cpp
  27. 586
    621
      Marlin/src/sd/usb_flashdrive/lib/Usb.cpp
  28. 3
    2
      Marlin/src/sd/usb_flashdrive/lib/Usb.h
  29. 2
    2
      Marlin/src/sd/usb_flashdrive/lib/UsbCore.h
  30. 186
    206
      Marlin/src/sd/usb_flashdrive/lib/address.h
  31. 133
    150
      Marlin/src/sd/usb_flashdrive/lib/confdescparser.h
  32. 27
    29
      Marlin/src/sd/usb_flashdrive/lib/hexdump.h
  33. 2
    3
      Marlin/src/sd/usb_flashdrive/lib/macros.h
  34. 683
    729
      Marlin/src/sd/usb_flashdrive/lib/masstorage.cpp
  35. 8
    23
      Marlin/src/sd/usb_flashdrive/lib/masstorage.h
  36. 4
    7
      Marlin/src/sd/usb_flashdrive/lib/max3421e.h
  37. 62
    65
      Marlin/src/sd/usb_flashdrive/lib/message.cpp
  38. 42
    43
      Marlin/src/sd/usb_flashdrive/lib/message.h
  39. 32
    37
      Marlin/src/sd/usb_flashdrive/lib/parsetools.cpp
  40. 84
    87
      Marlin/src/sd/usb_flashdrive/lib/parsetools.h
  41. 30
    42
      Marlin/src/sd/usb_flashdrive/lib/printhex.h
  42. 1
    1
      Marlin/src/sd/usb_flashdrive/lib/settings.h
  43. 3
    7
      Marlin/src/sd/usb_flashdrive/lib/usb_ch9.h

+ 1
- 1
Marlin/src/HAL/HAL_DUE/DebugMonitor_Due.cpp View File

@@ -231,7 +231,7 @@ void HardFault_HandlerC(unsigned long *sp, unsigned long lr, unsigned long cause
231 231
 
232 232
   // Reset controller
233 233
   NVIC_SystemReset();
234
-  while(1) { WDT_Restart(WDT); }
234
+  for (;;) WDT_Restart(WDT);
235 235
 }
236 236
 
237 237
 __attribute__((naked)) void NMI_Handler(void) {

+ 1
- 1
Marlin/src/HAL/HAL_DUE/u8g_com_HAL_DUE_shared_hw_spi.cpp View File

@@ -95,7 +95,7 @@ void u8g_SetPILevel_DUE_hw_spi(u8g_t *u8g, uint8_t pin_index, uint8_t level) {
95 95
 }
96 96
 
97 97
 uint8_t u8g_com_HAL_DUE_shared_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
98
-  switch(msg) {
98
+  switch (msg) {
99 99
     case U8G_COM_MSG_STOP:
100 100
       break;
101 101
 

+ 1
- 1
Marlin/src/HAL/HAL_LPC1768/DebugMonitor_LPC1768.cpp View File

@@ -206,7 +206,7 @@ void HardFault_HandlerC(unsigned long *sp, unsigned long lr, unsigned long cause
206 206
   // Reset controller
207 207
   NVIC_SystemReset();
208 208
 
209
-  while(1) { watchdog_init(); }
209
+  for (;;) watchdog_init();
210 210
 }
211 211
 
212 212
 extern "C" {

+ 16
- 19
Marlin/src/HAL/HAL_LPC1768/u8g/HAL_LCD_pin_routines.c View File

@@ -45,12 +45,11 @@
45 45
 #define OUTPUT 1
46 46
 #define INPUT_PULLUP 2
47 47
 
48
-
49 48
 uint8_t LPC1768_PIN_PORT(const uint8_t pin);
50 49
 uint8_t LPC1768_PIN_PIN(const uint8_t pin);
51 50
 
52 51
 #ifdef __cplusplus
53
-    extern "C" {
52
+  extern "C" {
54 53
 #endif
55 54
 
56 55
 // I/O functions
@@ -63,22 +62,21 @@ void pinMode_LCD(uint8_t pin, uint8_t mode) {
63 62
                              PINSEL_FUNC_0,
64 63
                              PINSEL_PINMODE_TRISTATE,
65 64
                              PINSEL_PINMODE_NORMAL };
66
-  switch(mode) {
67
-  case INPUT:
68
-    LPC_GPIO(LPC1768_PIN_PORT(pin))->FIODIR &= ~LPC_PIN(LPC1768_PIN_PIN(pin));
69
-    PINSEL_ConfigPin(&config);
70
-    break;
71
-  case OUTPUT:
72
-    LPC_GPIO(LPC1768_PIN_PORT(pin))->FIODIR |=  LPC_PIN(LPC1768_PIN_PIN(pin));
73
-    PINSEL_ConfigPin(&config);
74
-    break;
75
-  case INPUT_PULLUP:
76
-    LPC_GPIO(LPC1768_PIN_PORT(pin))->FIODIR &= ~LPC_PIN(LPC1768_PIN_PIN(pin));
77
-    config.Pinmode = PINSEL_PINMODE_PULLUP;
78
-    PINSEL_ConfigPin(&config);
79
-    break;
80
-  default:
81
-    break;
65
+  switch (mode) {
66
+    case INPUT:
67
+      LPC_GPIO(LPC1768_PIN_PORT(pin))->FIODIR &= ~LPC_PIN(LPC1768_PIN_PIN(pin));
68
+      PINSEL_ConfigPin(&config);
69
+      break;
70
+    case OUTPUT:
71
+      LPC_GPIO(LPC1768_PIN_PORT(pin))->FIODIR |=  LPC_PIN(LPC1768_PIN_PIN(pin));
72
+      PINSEL_ConfigPin(&config);
73
+      break;
74
+    case INPUT_PULLUP:
75
+      LPC_GPIO(LPC1768_PIN_PORT(pin))->FIODIR &= ~LPC_PIN(LPC1768_PIN_PIN(pin));
76
+      config.Pinmode = PINSEL_PINMODE_PULLUP;
77
+      PINSEL_ConfigPin(&config);
78
+      break;
79
+    default: break;
82 80
   }
83 81
 }
84 82
 
@@ -105,7 +103,6 @@ uint8_t u8g_GetPinLevel(uint8_t pin) {
105 103
   return (uint32_t)LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOPIN & LPC_PIN(LPC1768_PIN_PIN(pin)) ? 1 : 0;
106 104
 }
107 105
 
108
-
109 106
 #ifdef __cplusplus
110 107
   }
111 108
 #endif

+ 3
- 4
Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_ssd_hw_i2c.cpp View File

@@ -95,9 +95,8 @@ uint8_t u8g_com_ssd_I2C_start_sequence(u8g_t *u8g) {
95 95
   if (u8g->pin_list[U8G_PI_SET_A0] == 0) return 1;
96 96
 
97 97
   /* setup bus, might be a repeated start */
98
-  if (u8g_i2c_start(I2C_SLA) == 0)
99
-    return 0;
100
-  if (u8g->pin_list[U8G_PI_A0_STATE] == 0 ) {
98
+  if (u8g_i2c_start(I2C_SLA) == 0) return 0;
99
+  if (u8g->pin_list[U8G_PI_A0_STATE] == 0) {
101 100
     if (u8g_i2c_send_byte(I2C_CMD_MODE) == 0) return 0;
102 101
   }
103 102
   else if (u8g_i2c_send_byte(I2C_DATA_MODE) == 0)
@@ -108,7 +107,7 @@ uint8_t u8g_com_ssd_I2C_start_sequence(u8g_t *u8g) {
108 107
 }
109 108
 
110 109
 uint8_t u8g_com_HAL_LPC1768_ssd_hw_i2c_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
111
-  switch(msg) {
110
+  switch (msg) {
112 111
     case U8G_COM_MSG_INIT:
113 112
       //u8g_com_arduino_digital_write(u8g, U8G_PI_SCL, HIGH);
114 113
       //u8g_com_arduino_digital_write(u8g, U8G_PI_SDA, HIGH);

+ 1
- 1
Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_st7920_hw_spi.cpp View File

@@ -91,7 +91,7 @@ static void u8g_com_LPC1768_st7920_write_byte_hw_spi(uint8_t rs, uint8_t val) {
91 91
 }
92 92
 
93 93
 uint8_t u8g_com_HAL_LPC1768_ST7920_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
94
-  switch(msg) {
94
+  switch (msg) {
95 95
     case U8G_COM_MSG_INIT:
96 96
       u8g_SetPILevel(u8g, U8G_PI_CS, 0);
97 97
       u8g_SetPIOutput(u8g, U8G_PI_CS);

+ 2
- 4
Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_st7920_sw_spi.cpp View File

@@ -89,10 +89,8 @@ static void u8g_com_LPC1768_st7920_write_byte_sw_spi(uint8_t rs, uint8_t val) {
89 89
   swSpiTransfer(val << 4, SPI_speed, SCK_pin_ST7920_HAL, -1, MOSI_pin_ST7920_HAL_HAL);
90 90
 }
91 91
 
92
-uint8_t u8g_com_HAL_LPC1768_ST7920_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
93
-{
94
-  switch(msg)
95
-  {
92
+uint8_t u8g_com_HAL_LPC1768_ST7920_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
93
+  switch (msg) {
96 94
     case U8G_COM_MSG_INIT:
97 95
       SCK_pin_ST7920_HAL = u8g->pin_list[U8G_PI_SCK];
98 96
       MOSI_pin_ST7920_HAL_HAL = u8g->pin_list[U8G_PI_MOSI];

+ 1
- 1
Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_sw_spi.cpp View File

@@ -72,7 +72,7 @@ static void u8g_sw_spi_HAL_LPC1768_shift_out(uint8_t dataPin, uint8_t clockPin,
72 72
 }
73 73
 
74 74
 uint8_t u8g_com_HAL_LPC1768_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
75
-  switch(msg) {
75
+  switch (msg) {
76 76
     case U8G_COM_MSG_INIT:
77 77
       u8g_SetPIOutput(u8g, U8G_PI_SCK);
78 78
       u8g_SetPIOutput(u8g, U8G_PI_MOSI);

+ 3
- 3
Marlin/src/HAL/HAL_STM32F1/u8g_com_stm32duino_fsmc.cpp View File

@@ -58,7 +58,7 @@ uint8_t u8g_com_stm32duino_fsmc_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, voi
58 58
 
59 59
   static uint8_t isCommand;
60 60
 
61
-  switch(msg) {
61
+  switch (msg) {
62 62
     case U8G_COM_MSG_STOP:
63 63
       break;
64 64
     case U8G_COM_MSG_INIT:
@@ -154,7 +154,7 @@ void LCD_IO_Init(uint8_t cs, uint8_t rs) {
154 154
   if (fsmcInit) return;
155 155
   fsmcInit = 1;
156 156
 
157
-  switch(cs) {
157
+  switch (cs) {
158 158
     case FSMC_CS_NE1: controllerAddress = (uint32_t)FSMC_NOR_PSRAM_REGION1; break;
159 159
     case FSMC_CS_NE2: controllerAddress = (uint32_t)FSMC_NOR_PSRAM_REGION2; break;
160 160
     case FSMC_CS_NE3: controllerAddress = (uint32_t)FSMC_NOR_PSRAM_REGION3; break;
@@ -164,7 +164,7 @@ void LCD_IO_Init(uint8_t cs, uint8_t rs) {
164 164
 
165 165
   #define _ORADDR(N) controllerAddress |= (_BV32(N) - 2)
166 166
 
167
-  switch(rs) {
167
+  switch (rs) {
168 168
     case FSMC_RS_A0:  _ORADDR( 1); break;
169 169
     case FSMC_RS_A1:  _ORADDR( 2); break;
170 170
     case FSMC_RS_A2:  _ORADDR( 3); break;

+ 2
- 2
Marlin/src/HAL/HAL_TEENSY31_32/HAL_timers_Teensy.cpp View File

@@ -71,7 +71,7 @@ void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
71 71
 }
72 72
 
73 73
 void HAL_timer_enable_interrupt(const uint8_t timer_num) {
74
-  switch(timer_num) {
74
+  switch (timer_num) {
75 75
     case 0: NVIC_ENABLE_IRQ(IRQ_FTM0); break;
76 76
     case 1: NVIC_ENABLE_IRQ(IRQ_FTM1); break;
77 77
   }
@@ -98,7 +98,7 @@ bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
98 98
 }
99 99
 
100 100
 void HAL_timer_isr_prologue(const uint8_t timer_num) {
101
-  switch(timer_num) {
101
+  switch (timer_num) {
102 102
     case 0:
103 103
       FTM0_CNT = 0x0000;
104 104
       FTM0_SC &= ~FTM_SC_TOF; // Clear FTM Overflow flag

+ 2
- 2
Marlin/src/HAL/HAL_TEENSY35_36/HAL_timers_Teensy.cpp View File

@@ -72,7 +72,7 @@ void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
72 72
 }
73 73
 
74 74
 void HAL_timer_enable_interrupt(const uint8_t timer_num) {
75
-  switch(timer_num) {
75
+  switch (timer_num) {
76 76
     case 0: NVIC_ENABLE_IRQ(IRQ_FTM0); break;
77 77
     case 1: NVIC_ENABLE_IRQ(IRQ_FTM1); break;
78 78
   }
@@ -99,7 +99,7 @@ bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
99 99
 }
100 100
 
101 101
 void HAL_timer_isr_prologue(const uint8_t timer_num) {
102
-  switch(timer_num) {
102
+  switch (timer_num) {
103 103
     case 0:
104 104
       FTM0_CNT = 0x0000;
105 105
       FTM0_SC &= ~FTM_SC_TOF; // Clear FTM Overflow flag

+ 1
- 1
Marlin/src/HAL/HAL_TEENSY35_36/pinsDebug.h View File

@@ -76,7 +76,7 @@ void HAL_analog_pin_state(char buffer[], int8_t pin) {
76 76
  */
77 77
 bool HAL_pwm_status(int8_t pin) {
78 78
   char buffer[20];   // for the sprintf statements
79
-  switch(pin) {
79
+  switch (pin) {
80 80
     FTM_CASE(0,0);
81 81
     FTM_CASE(0,1);
82 82
     FTM_CASE(0,2);

+ 1
- 1
Marlin/src/HAL/shared/backtrace/unwarm.h View File

@@ -103,7 +103,7 @@ typedef struct {
103 103
  *  Macros
104 104
  **************************************************************************/
105 105
 
106
-#define M_IsOriginValid(v) (((v) & 0x7F) ? true : false)
106
+#define M_IsOriginValid(v) !!((v) & 0x7F)
107 107
 #define M_Origin2Str(v)    ((v) ? "VALID" : "INVALID")
108 108
 
109 109
 #ifdef UNW_DEBUG

+ 69
- 129
Marlin/src/HAL/shared/backtrace/unwarm_arm.cpp View File

@@ -32,23 +32,17 @@
32 32
  * \retval false This is not a data-processing instruction,
33 33
  */
34 34
 static bool isDataProc(uint32_t instr) {
35
-
36 35
   uint8_t opcode = (instr & 0x01E00000) >> 21;
37
-  bool    S      = (instr & 0x00100000) ? true : false;
38
-
39
-  if ((instr & 0xFC000000) != 0xE0000000) {
40
-    return false;
41
-  }
42
-  else if (!S && opcode >= 8 && opcode <= 11) {
43
-    /* TST, TEQ, CMP and CMN all require S to be set */
44
-    return false;
45
-  }
46
-  else
47
-    return true;
36
+  if ((instr & 0xFC000000) != 0xE0000000) return false;
37
+
38
+  /* TST, TEQ, CMP and CMN all require S to be set */
39
+  bool S = !!(instr & 0x00100000);
40
+  if (!S && opcode >= 8 && opcode <= 11) return false;
41
+
42
+  return true;
48 43
 }
49 44
 
50 45
 UnwResult UnwStartArm(UnwState * const state) {
51
-
52 46
   bool found = false;
53 47
   uint16_t   t = UNW_MAX_INSTR_COUNT;
54 48
 
@@ -56,9 +50,8 @@ UnwResult UnwStartArm(UnwState * const state) {
56 50
     uint32_t instr;
57 51
 
58 52
     /* Attempt to read the instruction */
59
-    if (!state->cb->readW(state->regData[15].v, &instr)) {
53
+    if (!state->cb->readW(state->regData[15].v, &instr))
60 54
       return UNWIND_IREAD_W_FAIL;
61
-    }
62 55
 
63 56
     UnwPrintd4("A %x %x %08x:", state->regData[13].v, state->regData[15].v, instr);
64 57
 
@@ -103,31 +96,20 @@ UnwResult UnwStartArm(UnwState * const state) {
103 96
       }
104 97
 
105 98
       /* Determine the return mode */
106
-      if (state->regData[rn].v & 0x1) {
107
-
108
-        /* Branching to THUMB */
99
+      if (state->regData[rn].v & 0x1) /* Branching to THUMB */
109 100
         return UnwStartThumb(state);
110
-      }
111
-      else {
112
-
113
-        /* Branch to ARM */
114 101
 
115
-        /* Account for the auto-increment which isn't needed */
116
-        state->regData[15].v -= 4;
117
-      }
102
+      /* Branch to ARM */
103
+      /* Account for the auto-increment which isn't needed */
104
+      state->regData[15].v -= 4;
118 105
     }
119 106
     /* Branch */
120 107
     else if ((instr & 0xFF000000) == 0xEA000000) {
121 108
 
122
-      int32_t offset = (instr & 0x00FFFFFF);
123
-
124
-      /* Shift value */
125
-      offset = offset << 2;
109
+      int32_t offset = (instr & 0x00FFFFFF) << 2;
126 110
 
127 111
       /* Sign extend if needed */
128
-      if (offset & 0x02000000) {
129
-        offset |= 0xFC000000;
130
-      }
112
+      if (offset & 0x02000000) offset |= 0xFC000000;
131 113
 
132 114
       UnwPrintd2("B %d\n", offset);
133 115
 
@@ -142,11 +124,12 @@ UnwResult UnwStartArm(UnwState * const state) {
142 124
 
143 125
     /* MRS */
144 126
     else if ((instr & 0xFFBF0FFF) == 0xE10F0000) {
145
-#ifdef UNW_DEBUG
146
-      bool R     = (instr & 0x00400000) ? true : false;
147
-#endif
127
+      #ifdef UNW_DEBUG
128
+        const bool R = !!(instr & 0x00400000);
129
+      #else
130
+        constexpr bool R = false;
131
+      #endif
148 132
       uint8_t rd = (instr & 0x0000F000) >> 12;
149
-
150 133
       UnwPrintd4("MRS r%d,%s\t; r%d invalidated", rd, R ? "SPSR" : "CPSR", rd);
151 134
 
152 135
       /* Status registers untracked */
@@ -154,11 +137,10 @@ UnwResult UnwStartArm(UnwState * const state) {
154 137
     }
155 138
     /* MSR */
156 139
     else if ((instr & 0xFFB0F000) == 0xE120F000) {
157
-#ifdef UNW_DEBUG
158
-      bool R = (instr & 0x00400000) ? true : false;
140
+      #ifdef UNW_DEBUG
141
+        UnwPrintd2("MSR %s_?, ???", (instr & 0x00400000) ? "SPSR" : "CPSR");
142
+      #endif
159 143
 
160
-      UnwPrintd2("MSR %s_?, ???", R ? "SPSR" : "CPSR");
161
-#endif
162 144
       /* Status registers untracked.
163 145
        *  Potentially this could change processor mode and switch
164 146
        *  banked registers r8-r14.  Most likely is that r13 (sp) will
@@ -170,18 +152,18 @@ UnwResult UnwStartArm(UnwState * const state) {
170 152
     }
171 153
     /* Data processing */
172 154
     else if (isDataProc(instr)) {
173
-      bool            I = (instr & 0x02000000) ? true : false;
155
+      bool            I = !!(instr & 0x02000000);
174 156
       uint8_t    opcode = (instr & 0x01E00000) >> 21;
175
-#ifdef UNW_DEBUG
176
-      bool            S = (instr & 0x00100000) ? true : false;
177
-#endif
157
+      #ifdef UNW_DEBUG
158
+        bool          S = !!(instr & 0x00100000);
159
+      #endif
178 160
       uint8_t        rn = (instr & 0x000F0000) >> 16;
179 161
       uint8_t        rd = (instr & 0x0000F000) >> 12;
180 162
       uint16_t operand2 = (instr & 0x00000FFF);
181 163
       uint32_t        op2val;
182 164
       int             op2origin;
183 165
 
184
-      switch(opcode) {
166
+      switch (opcode) {
185 167
         case  0: UnwPrintd4("AND%s r%d,r%d,", S ? "S" : "", rd, rn); break;
186 168
         case  1: UnwPrintd4("EOR%s r%d,r%d,", S ? "S" : "", rd, rn); break;
187 169
         case  2: UnwPrintd4("SUB%s r%d,r%d,", S ? "S" : "", rd, rn); break;
@@ -217,26 +199,23 @@ UnwResult UnwStartArm(UnwState * const state) {
217 199
 
218 200
         /* Register and shift */
219 201
         uint8_t  rm        = (operand2 & 0x000F);
220
-        uint8_t  regShift  = (operand2 & 0x0010) ? true : false;
202
+        uint8_t  regShift  = !!(operand2 & 0x0010);
221 203
         uint8_t  shiftType = (operand2 & 0x0060) >> 5;
222 204
         uint32_t shiftDist;
223
-#ifdef UNW_DEBUG
224
-        const char * const shiftMnu[4] = { "LSL", "LSR", "ASR", "ROR" };
225
-#endif
205
+        #ifdef UNW_DEBUG
206
+          const char * const shiftMnu[4] = { "LSL", "LSR", "ASR", "ROR" };
207
+        #endif
226 208
         UnwPrintd2("r%d ", rm);
227 209
 
228 210
         /* Get the shift distance */
229 211
         if (regShift) {
230
-
231 212
           uint8_t rs = (operand2 & 0x0F00) >> 8;
232 213
 
233 214
           if (operand2 & 0x00800) {
234
-
235 215
             UnwPrintd1("\nError: Bit should be zero\n");
236 216
             return UNWIND_ILLEGAL_INSTR;
237 217
           }
238 218
           else if (rs == 15) {
239
-
240 219
             UnwPrintd1("\nError: Cannot use R15 with register shift\n");
241 220
             return UNWIND_ILLEGAL_INSTR;
242 221
           }
@@ -250,46 +229,33 @@ UnwResult UnwStartArm(UnwState * const state) {
250 229
         else {
251 230
           shiftDist = (operand2 & 0x0F80) >> 7;
252 231
           op2origin = REG_VAL_FROM_CONST;
253
-
254
-          if (shiftDist) {
255
-            UnwPrintd3("%s #%d", shiftMnu[shiftType], shiftDist);
256
-          }
232
+          if (shiftDist) UnwPrintd3("%s #%d", shiftMnu[shiftType], shiftDist);
257 233
           UnwPrintd3("\t; r%d %s", rm, M_Origin2Str(state->regData[rm].o));
258 234
         }
259 235
 
260 236
         /* Apply the shift type to the source register */
261
-        switch(shiftType) {
237
+        switch (shiftType) {
262 238
           case 0: /* logical left */
263 239
             op2val = state->regData[rm].v << shiftDist;
264 240
             break;
265 241
 
266 242
           case 1: /* logical right */
267
-            if (!regShift && shiftDist == 0) {
268
-              shiftDist = 32;
269
-            }
270
-
243
+            if (!regShift && shiftDist == 0) shiftDist = 32;
271 244
             op2val = state->regData[rm].v >> shiftDist;
272 245
             break;
273 246
 
274 247
           case 2: /* arithmetic right */
275
-            if (!regShift && shiftDist == 0) {
276
-              shiftDist = 32;
277
-            }
248
+            if (!regShift && shiftDist == 0) shiftDist = 32;
278 249
 
279 250
             if (state->regData[rm].v & 0x80000000) {
280
-
281 251
               /* Register shifts maybe greater than 32 */
282
-              if (shiftDist >= 32) {
252
+              if (shiftDist >= 32)
283 253
                 op2val = 0xFFFFFFFF;
284
-              }
285
-              else {
286
-                op2val = state->regData[rm].v >> shiftDist;
287
-                op2val |= 0xFFFFFFFF << (32 - shiftDist);
288
-              }
254
+              else
255
+                op2val = (state->regData[rm].v >> shiftDist) | (0xFFFFFFFF << (32 - shiftDist));
289 256
             }
290
-            else {
257
+            else
291 258
               op2val = state->regData[rm].v >> shiftDist;
292
-            }
293 259
             break;
294 260
 
295 261
           case 3: /* rotate right */
@@ -317,19 +283,14 @@ UnwResult UnwStartArm(UnwState * const state) {
317 283
         }
318 284
 
319 285
         /* Decide the data origin */
320
-        if (M_IsOriginValid(op2origin) &&
321
-           M_IsOriginValid(state->regData[rm].o)) {
322
-
323
-          op2origin = state->regData[rm].o;
324
-          op2origin |= REG_VAL_ARITHMETIC;
325
-        }
326
-        else {
286
+        if (M_IsOriginValid(op2origin) && M_IsOriginValid(state->regData[rm].o))
287
+          op2origin = REG_VAL_ARITHMETIC | state->regData[rm].o;
288
+        else
327 289
           op2origin = REG_VAL_INVALID;
328
-        }
329 290
       }
330 291
 
331 292
       /* Propagate register validity */
332
-      switch(opcode) {
293
+      switch (opcode) {
333 294
         case  0: /* AND: Rd := Op1 AND Op2 */
334 295
         case  1: /* EOR: Rd := Op1 EOR Op2 */
335 296
         case  2: /* SUB: Rd:= Op1 - Op2 */
@@ -374,14 +335,11 @@ UnwResult UnwStartArm(UnwState * const state) {
374 335
          *  to specify the shift amount the PC will be 12 bytes
375 336
          *  ahead.
376 337
          */
377
-        if (!I && (operand2 & 0x0010))
378
-          state->regData[rn].v += 12;
379
-        else
380
-          state->regData[rn].v += 8;
338
+        state->regData[rn].v += ((!I && (operand2 & 0x0010)) ? 12 : 8);
381 339
       }
382 340
 
383 341
       /* Compute values */
384
-      switch(opcode) {
342
+      switch (opcode) {
385 343
         case  0: /* AND: Rd := Op1 AND Op2 */
386 344
           state->regData[rd].v = state->regData[rn].v & op2val;
387 345
           break;
@@ -429,12 +387,8 @@ UnwResult UnwStartArm(UnwState * const state) {
429 387
       }
430 388
 
431 389
       /* Remove the prefetch offset from the PC */
432
-      if (rd != 15 && rn == 15) {
433
-        if (!I && (operand2 & 0x0010))
434
-          state->regData[rn].v -= 12;
435
-        else
436
-          state->regData[rn].v -= 8;
437
-      }
390
+      if (rd != 15 && rn == 15)
391
+        state->regData[rn].v -= ((!I && (operand2 & 0x0010)) ? 12 : 8);
438 392
     }
439 393
 
440 394
     /* Block Data Transfer
@@ -442,26 +396,25 @@ UnwResult UnwStartArm(UnwState * const state) {
442 396
      */
443 397
     else if ((instr & 0xFE000000) == 0xE8000000) {
444 398
 
445
-      bool     P         = (instr & 0x01000000) ? true : false;
446
-      bool     U         = (instr & 0x00800000) ? true : false;
447
-      bool     S         = (instr & 0x00400000) ? true : false;
448
-      bool     W         = (instr & 0x00200000) ? true : false;
449
-      bool     L         = (instr & 0x00100000) ? true : false;
399
+      bool     P         = !!(instr & 0x01000000),
400
+               U         = !!(instr & 0x00800000),
401
+               S         = !!(instr & 0x00400000),
402
+               W         = !!(instr & 0x00200000),
403
+               L         = !!(instr & 0x00100000);
450 404
       uint16_t baseReg   = (instr & 0x000F0000) >> 16;
451 405
       uint16_t regList   = (instr & 0x0000FFFF);
452 406
       uint32_t addr      = state->regData[baseReg].v;
453 407
       bool     addrValid = M_IsOriginValid(state->regData[baseReg].o);
454 408
       int8_t r;
455 409
 
456
-#ifdef UNW_DEBUG
457
-      /* Display the instruction */
458
-      if (L) {
459
-        UnwPrintd6("LDM%c%c r%d%s, {reglist}%s\n", P ? 'E' : 'F', U ? 'D' : 'A', baseReg, W ? "!" : "", S ? "^" : "");
460
-      }
461
-      else {
462
-        UnwPrintd6("STM%c%c r%d%s, {reglist}%s\n", !P ? 'E' : 'F', !U ? 'D' : 'A', baseReg, W ? "!" : "", S ? "^" : "");
463
-      }
464
-#endif
410
+      #ifdef UNW_DEBUG
411
+        /* Display the instruction */
412
+        if (L)
413
+          UnwPrintd6("LDM%c%c r%d%s, {reglist}%s\n", P ? 'E' : 'F', U ? 'D' : 'A', baseReg, W ? "!" : "", S ? "^" : "");
414
+        else
415
+          UnwPrintd6("STM%c%c r%d%s, {reglist}%s\n", !P ? 'E' : 'F', !U ? 'D' : 'A', baseReg, W ? "!" : "", S ? "^" : "");
416
+      #endif
417
+
465 418
       /* S indicates that banked registers (untracked) are used, unless
466 419
        *  this is a load including the PC when the S-bit indicates that
467 420
        *  that CPSR is loaded from SPSR (also untracked, but ignored).
@@ -489,44 +442,35 @@ UnwResult UnwStartArm(UnwState * const state) {
489 442
         /* Check if the register is to be transferred */
490 443
         if (regList & (0x01 << r)) {
491 444
 
492
-          if (P)
493
-            addr += U ? 4 : -4;
445
+          if (P) addr += U ? 4 : -4;
494 446
 
495 447
           if (L) {
496 448
 
497 449
             if (addrValid) {
498 450
 
499
-              if (!UnwMemReadRegister(state, addr, &state->regData[r])) {
451
+              if (!UnwMemReadRegister(state, addr, &state->regData[r]))
500 452
                 return UNWIND_DREAD_W_FAIL;
501
-              }
502 453
 
503 454
               /* Update the origin if read via the stack pointer */
504
-              if (M_IsOriginValid(state->regData[r].o) && baseReg == 13) {
455
+              if (M_IsOriginValid(state->regData[r].o) && baseReg == 13)
505 456
                 state->regData[r].o = REG_VAL_FROM_STACK;
506
-              }
507 457
 
508 458
               UnwPrintd5(" R%d = 0x%08x\t; r%d %s\n",r,state->regData[r].v,r, M_Origin2Str(state->regData[r].o));
509 459
             }
510 460
             else {
511
-
512 461
               /* Invalidate the register as the base reg was invalid */
513 462
               state->regData[r].o = REG_VAL_INVALID;
514
-
515 463
               UnwPrintd2(" R%d = ???\n", r);
516 464
             }
517 465
           }
518 466
           else {
519
-            if (addrValid) {
520
-              if (!UnwMemWriteRegister(state, state->regData[13].v, &state->regData[r])) {
521
-                return UNWIND_DWRITE_W_FAIL;
522
-              }
523
-            }
467
+            if (addrValid && !UnwMemWriteRegister(state, state->regData[13].v, &state->regData[r]))
468
+              return UNWIND_DWRITE_W_FAIL;
524 469
 
525 470
             UnwPrintd2(" R%d = 0x%08x\n", r);
526 471
           }
527 472
 
528
-          if (!P)
529
-            addr += U ? 4 : -4;
473
+          if (!P) addr += U ? 4 : -4;
530 474
         }
531 475
 
532 476
         /* Check the next register */
@@ -535,8 +479,7 @@ UnwResult UnwStartArm(UnwState * const state) {
535 479
       } while (r >= 0 && r <= 15);
536 480
 
537 481
       /* Check the writeback bit */
538
-      if (W)
539
-        state->regData[baseReg].v = addr;
482
+      if (W) state->regData[baseReg].v = addr;
540 483
 
541 484
       /* Check if the PC was loaded */
542 485
       if (L && (regList & (0x01 << 15))) {
@@ -547,9 +490,8 @@ UnwResult UnwStartArm(UnwState * const state) {
547 490
         }
548 491
         else {
549 492
           /* Store the return address */
550
-          if (!UnwReportRetAddr(state, state->regData[15].v)) {
493
+          if (!UnwReportRetAddr(state, state->regData[15].v))
551 494
             return UNWIND_TRUNCATED;
552
-          }
553 495
 
554 496
           UnwPrintd2("  Return PC=0x%x", state->regData[15].v);
555 497
 
@@ -585,9 +527,7 @@ UnwResult UnwStartArm(UnwState * const state) {
585 527
     /* Garbage collect the memory hash (used only for the stack) */
586 528
     UnwMemHashGC(state);
587 529
 
588
-    t--;
589
-    if (t == 0)
590
-      return UNWIND_EXHAUSTED;
530
+    if (--t == 0) return UNWIND_EXHAUSTED;
591 531
 
592 532
   } while (!found);
593 533
 

+ 116
- 176
Marlin/src/HAL/shared/backtrace/unwarm_thumb.cpp View File

@@ -25,17 +25,11 @@
25 25
  * \param value   The value to sign extend.
26 26
  * \return The signed-11 bit value stored in a 16bit data type.
27 27
  */
28
-static int32_t signExtend11(uint16_t value) {
29
-
30
-  if(value & 0x400) {
31
-    value |= 0xFFFFF800;
32
-  }
33
-
34
-  return value;
28
+static int32_t signExtend11(const uint16_t value) {
29
+  return (value & 0x400) ? value | 0xFFFFF800 : value;
35 30
 }
36 31
 
37 32
 UnwResult UnwStartThumb(UnwState * const state) {
38
-
39 33
   bool found = false;
40 34
   uint16_t t = UNW_MAX_INSTR_COUNT;
41 35
   uint32_t lastJumpAddr = 0;  // Last JUMP address, to try to detect infinite loops
@@ -45,20 +39,19 @@ UnwResult UnwStartThumb(UnwState * const state) {
45 39
     uint16_t instr;
46 40
 
47 41
     /* Attempt to read the instruction */
48
-    if(!state->cb->readH(state->regData[15].v & (~0x1), &instr)) {
42
+    if (!state->cb->readH(state->regData[15].v & (~0x1), &instr))
49 43
       return UNWIND_IREAD_H_FAIL;
50
-    }
51 44
 
52 45
     UnwPrintd4("T %x %x %04x:", state->regData[13].v, state->regData[15].v, instr);
53 46
 
54 47
     /* Check that the PC is still on Thumb alignment */
55
-    if(!(state->regData[15].v & 0x1)) {
48
+    if (!(state->regData[15].v & 0x1)) {
56 49
       UnwPrintd1("\nError: PC misalignment\n");
57 50
       return UNWIND_INCONSISTENT;
58 51
     }
59 52
 
60 53
     /* Check that the SP and PC have not been invalidated */
61
-    if(!M_IsOriginValid(state->regData[13].o) || !M_IsOriginValid(state->regData[15].o)) {
54
+    if (!M_IsOriginValid(state->regData[13].o) || !M_IsOriginValid(state->regData[15].o)) {
62 55
       UnwPrintd1("\nError: PC or SP invalidated\n");
63 56
       return UNWIND_INCONSISTENT;
64 57
     }
@@ -73,9 +66,8 @@ UnwResult UnwStartThumb(UnwState * const state) {
73 66
       state->regData[15].v += 2;
74 67
 
75 68
       /* Attempt to read the 2nd part of the instruction */
76
-      if(!state->cb->readH(state->regData[15].v & (~0x1), &instr2)) {
69
+      if (!state->cb->readH(state->regData[15].v & (~0x1), &instr2))
77 70
         return UNWIND_IREAD_H_FAIL;
78
-      }
79 71
 
80 72
       UnwPrintd3(" %x %04x:", state->regData[15].v, instr2);
81 73
 
@@ -84,26 +76,25 @@ UnwResult UnwStartThumb(UnwState * const state) {
84 76
        *  PUSH and POP
85 77
        */
86 78
       if ((instr & 0xFE6F) == 0xE82D) {
87
-        bool     L     = (instr &  0x10) ? true : false;
79
+        bool     L     = !!(instr &  0x10);
88 80
         uint16_t rList = instr2;
89 81
 
90
-        if(L) {
82
+        if (L) {
91 83
           uint8_t r;
92 84
 
93 85
           /* Load from memory: POP */
94 86
           UnwPrintd1("POP {Rlist}\n");
95 87
 
96 88
           /* Load registers from stack */
97
-          for(r = 0; r < 16; r++) {
98
-            if(rList & (0x1 << r)) {
89
+          for (r = 0; r < 16; r++) {
90
+            if (rList & (0x1 << r)) {
99 91
 
100 92
               /* Read the word */
101
-              if(!UnwMemReadRegister(state, state->regData[13].v, &state->regData[r])) {
93
+              if (!UnwMemReadRegister(state, state->regData[13].v, &state->regData[r]))
102 94
                 return UNWIND_DREAD_W_FAIL;
103
-              }
104 95
 
105 96
               /* Alter the origin to be from the stack if it was valid */
106
-              if(M_IsOriginValid(state->regData[r].o)) {
97
+              if (M_IsOriginValid(state->regData[r].o)) {
107 98
 
108 99
                 state->regData[r].o = REG_VAL_FROM_STACK;
109 100
 
@@ -114,7 +105,7 @@ UnwResult UnwStartThumb(UnwState * const state) {
114 105
                    *  the caller was from Thumb.  This would allow return
115 106
                    *  by BX for interworking APCS.
116 107
                    */
117
-                  if((state->regData[15].v & 0x1) == 0) {
108
+                  if ((state->regData[15].v & 0x1) == 0) {
118 109
                     UnwPrintd2("Warning: Return address not to Thumb: 0x%08x\n", state->regData[15].v);
119 110
 
120 111
                     /* Pop into the PC will not switch mode */
@@ -122,9 +113,8 @@ UnwResult UnwStartThumb(UnwState * const state) {
122 113
                   }
123 114
 
124 115
                   /* Store the return address */
125
-                  if(!UnwReportRetAddr(state, state->regData[15].v)) {
116
+                  if (!UnwReportRetAddr(state, state->regData[15].v))
126 117
                     return UNWIND_TRUNCATED;
127
-                  }
128 118
 
129 119
                   /* Now have the return address */
130 120
                   UnwPrintd2(" Return PC=%x\n", state->regData[15].v);
@@ -155,15 +145,14 @@ UnwResult UnwStartThumb(UnwState * const state) {
155 145
           /* Store to memory: PUSH */
156 146
           UnwPrintd1("PUSH {Rlist}");
157 147
 
158
-          for(r = 15; r >= 0; r--) {
159
-            if(rList & (0x1 << r)) {
148
+          for (r = 15; r >= 0; r--) {
149
+            if (rList & (0x1 << r)) {
160 150
               UnwPrintd4("\n  r%d = 0x%08x\t; %s", r, state->regData[r].v, M_Origin2Str(state->regData[r].o));
161 151
 
162 152
               state->regData[13].v -= 4;
163 153
 
164
-              if(!UnwMemWriteRegister(state, state->regData[13].v, &state->regData[r])) {
154
+              if (!UnwMemWriteRegister(state, state->regData[13].v, &state->regData[r]))
165 155
                 return UNWIND_DWRITE_W_FAIL;
166
-              }
167 156
             }
168 157
           }
169 158
         }
@@ -180,9 +169,8 @@ UnwResult UnwStartThumb(UnwState * const state) {
180 169
 
181 170
         state->regData[13].v -= 4;
182 171
 
183
-        if(!UnwMemWriteRegister(state, state->regData[13].v, &state->regData[r])) {
172
+        if (!UnwMemWriteRegister(state, state->regData[13].v, &state->regData[r]))
184 173
           return UNWIND_DWRITE_W_FAIL;
185
-        }
186 174
       }
187 175
       /*
188 176
        * POP register
@@ -194,12 +182,11 @@ UnwResult UnwStartThumb(UnwState * const state) {
194 182
         UnwPrintd2("POP {R%d}\n", r);
195 183
 
196 184
         /* Read the word */
197
-        if(!UnwMemReadRegister(state, state->regData[13].v, &state->regData[r])) {
185
+        if (!UnwMemReadRegister(state, state->regData[13].v, &state->regData[r]))
198 186
           return UNWIND_DREAD_W_FAIL;
199
-        }
200 187
 
201 188
         /* Alter the origin to be from the stack if it was valid */
202
-        if(M_IsOriginValid(state->regData[r].o)) {
189
+        if (M_IsOriginValid(state->regData[r].o)) {
203 190
 
204 191
           state->regData[r].o = REG_VAL_FROM_STACK;
205 192
 
@@ -210,7 +197,7 @@ UnwResult UnwStartThumb(UnwState * const state) {
210 197
              *  the caller was from Thumb.  This would allow return
211 198
              *  by BX for interworking APCS.
212 199
              */
213
-            if((state->regData[15].v & 0x1) == 0) {
200
+            if ((state->regData[15].v & 0x1) == 0) {
214 201
               UnwPrintd2("Warning: Return address not to Thumb: 0x%08x\n", state->regData[15].v);
215 202
 
216 203
               /* Pop into the PC will not switch mode */
@@ -218,9 +205,8 @@ UnwResult UnwStartThumb(UnwState * const state) {
218 205
             }
219 206
 
220 207
             /* Store the return address */
221
-            if(!UnwReportRetAddr(state, state->regData[15].v)) {
208
+            if (!UnwReportRetAddr(state, state->regData[15].v))
222 209
               return UNWIND_TRUNCATED;
223
-            }
224 210
 
225 211
             /* Now have the return address */
226 212
             UnwPrintd2(" Return PC=%x\n", state->regData[15].v);
@@ -255,7 +241,7 @@ UnwResult UnwStartThumb(UnwState * const state) {
255 241
          * the switch clauses
256 242
          */
257 243
         uint8_t rn = instr & 0xF;
258
-        bool H = (instr2 & 0x10) ? true : false;
244
+        bool H = !!(instr2 & 0x10);
259 245
 
260 246
         UnwPrintd5("TB%c [r%d,r%d%s]\n", H ? 'H' : 'B', rn, (instr2 & 0xF), H ? ",LSL #1" : "");
261 247
 
@@ -263,15 +249,14 @@ UnwResult UnwStartThumb(UnwState * const state) {
263 249
         if (rn == 15) {
264 250
           if (H) {
265 251
             uint16_t rv;
266
-            if(!state->cb->readH((state->regData[15].v & (~1)) + 2, &rv)) {
252
+            if (!state->cb->readH((state->regData[15].v & (~1)) + 2, &rv))
267 253
               return UNWIND_DREAD_H_FAIL;
268
-            }
269 254
             state->regData[15].v += rv * 2;
270
-          } else {
255
+          }
256
+          else {
271 257
             uint8_t rv;
272
-            if(!state->cb->readB((state->regData[15].v & (~1)) + 2, &rv)) {
258
+            if (!state->cb->readB((state->regData[15].v & (~1)) + 2, &rv))
273 259
               return UNWIND_DREAD_B_FAIL;
274
-            }
275 260
             state->regData[15].v += rv * 2;
276 261
           }
277 262
         }
@@ -355,12 +340,11 @@ UnwResult UnwStartThumb(UnwState * const state) {
355 340
           UnwPrintd2(" Return PC=%x", state->regData[15].v);
356 341
 
357 342
           /* Report the return address, including mode bit */
358
-          if(!UnwReportRetAddr(state, state->regData[15].v)) {
343
+          if (!UnwReportRetAddr(state, state->regData[15].v))
359 344
             return UNWIND_TRUNCATED;
360
-          }
361 345
 
362 346
           /* Determine the new mode */
363
-          if(state->regData[15].v & 0x1) {
347
+          if (state->regData[15].v & 0x1) {
364 348
             /* Branching to THUMB */
365 349
 
366 350
             /* Account for the auto-increment which isn't needed */
@@ -411,10 +395,10 @@ UnwResult UnwStartThumb(UnwState * const state) {
411 395
        * PC-relative load
412 396
        *  LDR Rd,[PC, #+/-imm]
413 397
        */
414
-      else if((instr & 0xFF7F) == 0xF85F) {
398
+      else if ((instr & 0xFF7F) == 0xF85F) {
415 399
         uint8_t  rt    = (instr2 & 0xF000) >> 12;
416 400
         uint8_t  imm12 = (instr2 & 0x0FFF);
417
-        bool     A     = (instr  & 0x80) ? true : false;
401
+        bool     A     = !!(instr  & 0x80);
418 402
         uint32_t address;
419 403
 
420 404
         /* Compute load address, adding a word to account for prefetch */
@@ -424,9 +408,8 @@ UnwResult UnwStartThumb(UnwState * const state) {
424 408
 
425 409
         UnwPrintd4("LDR r%d,[PC #%c0x%08x]", rt, A?'+':'-', address);
426 410
 
427
-        if(!UnwMemReadRegister(state, address, &state->regData[rt])) {
411
+        if (!UnwMemReadRegister(state, address, &state->regData[rt]))
428 412
           return UNWIND_DREAD_W_FAIL;
429
-        }
430 413
       }
431 414
       /*
432 415
        * LDR immediate.
@@ -441,11 +424,11 @@ UnwResult UnwStartThumb(UnwState * const state) {
441 424
         /* If destination is PC and we don't know the source value, then fail */
442 425
         if (!M_IsOriginValid(state->regData[rn].o)) {
443 426
           state->regData[rt].o = state->regData[rn].o;
444
-        } else {
427
+        }
428
+        else {
445 429
           uint32_t address = state->regData[rn].v + imm12;
446
-          if(!UnwMemReadRegister(state, address, &state->regData[rt])) {
430
+          if (!UnwMemReadRegister(state, address, &state->regData[rt]))
447 431
             return UNWIND_DREAD_W_FAIL;
448
-          }
449 432
         }
450 433
       }
451 434
       /*
@@ -459,31 +442,20 @@ UnwResult UnwStartThumb(UnwState * const state) {
459 442
         uint8_t     rn = (instr  & 0xF);
460 443
         uint8_t     rt = (instr2 & 0xF000) >> 12;
461 444
         uint16_t  imm8 = (instr2 & 0xFF);
462
-        bool         P = (instr2 & 0x400) ? true : false;
463
-        bool         U = (instr2 & 0x200) ? true : false;
464
-        bool         W = (instr2 & 0x100) ? true : false;
445
+        bool         P = !!(instr2 & 0x400);
446
+        bool         U = !!(instr2 & 0x200);
447
+        bool         W = !!(instr2 & 0x100);
465 448
 
466
-        if (!M_IsOriginValid(state->regData[rn].o)) {
449
+        if (!M_IsOriginValid(state->regData[rn].o))
467 450
           state->regData[rt].o = state->regData[rn].o;
468
-        } else {
469
-          uint32_t offaddress = state->regData[rn].v + imm8;
470
-          if (U) offaddress += imm8;
471
-          else offaddress -= imm8;
472
-
473
-          uint32_t address;
474
-          if (P) {
475
-            address = offaddress;
476
-          } else {
477
-            address = state->regData[rn].v;
478
-          }
451
+        else {
452
+          uint32_t offaddress = state->regData[rn].v + (U ? imm8 + imm8 : 0),
453
+                   address = P ? offaddress : state->regData[rn].v;
479 454
 
480
-          if(!UnwMemReadRegister(state, address, &state->regData[rt])) {
455
+          if (!UnwMemReadRegister(state, address, &state->regData[rt]))
481 456
             return UNWIND_DREAD_W_FAIL;
482
-          }
483 457
 
484
-          if (W) {
485
-            state->regData[rn].v = offaddress;
486
-          }
458
+          if (W) state->regData[rn].v = offaddress;
487 459
         }
488 460
       }
489 461
       /*
@@ -493,30 +465,28 @@ UnwResult UnwStartThumb(UnwState * const state) {
493 465
        *  Where Rt is PC, Rn value is known, Rm is not known or unknown
494 466
        */
495 467
       else if ((instr & 0xFFF0) == 0xF850 && (instr2 & 0x0FC0) == 0x0000) {
496
-        uint8_t   rn = (instr  & 0xF);
497
-        uint8_t   rt = (instr2 & 0xF000) >> 12;
498
-        uint8_t   rm = (instr2 & 0xF);
499
-        uint8_t imm2 = (instr2 & 0x30) >> 4;
468
+        const uint8_t rn = (instr  & 0xF),
469
+                      rt = (instr2 & 0xF000) >> 12,
470
+                      rm = (instr2 & 0xF),
471
+                    imm2 = (instr2 & 0x30) >> 4;
500 472
 
501
-        if (!M_IsOriginValid(state->regData[rn].o) ||
502
-            !M_IsOriginValid(state->regData[rm].o)) {
473
+        if (!M_IsOriginValid(state->regData[rn].o) || !M_IsOriginValid(state->regData[rm].o)) {
503 474
 
504 475
           /* If Rt is PC, and Rn is known, then do an exception and assume
505 476
              Rm equals 0 => This takes the first case in a switch() */
506 477
           if (rt == 15 && M_IsOriginValid(state->regData[rn].o)) {
507 478
             uint32_t address = state->regData[rn].v;
508
-            if(!UnwMemReadRegister(state, address, &state->regData[rt])) {
479
+            if (!UnwMemReadRegister(state, address, &state->regData[rt]))
509 480
               return UNWIND_DREAD_W_FAIL;
510
-            }
511
-          } else {
512
-            /* Propagate unknown value */
513
-            state->regData[rt].o = state->regData[rn].o;
514 481
           }
515
-        } else {
482
+          else /* Propagate unknown value */
483
+            state->regData[rt].o = state->regData[rn].o;
484
+
485
+        }
486
+        else {
516 487
           uint32_t address = state->regData[rn].v + (state->regData[rm].v << imm2);
517
-          if(!UnwMemReadRegister(state, address, &state->regData[rt])) {
488
+          if (!UnwMemReadRegister(state, address, &state->regData[rt]))
518 489
             return UNWIND_DREAD_W_FAIL;
519
-          }
520 490
         }
521 491
       }
522 492
       else {
@@ -533,14 +503,14 @@ UnwResult UnwStartThumb(UnwState * const state) {
533 503
      *  LSR Rd, Rs, #Offset5
534 504
      *  ASR Rd, Rs, #Offset5
535 505
      */
536
-    else if((instr & 0xE000) == 0x0000 && (instr & 0x1800) != 0x1800) {
506
+    else if ((instr & 0xE000) == 0x0000 && (instr & 0x1800) != 0x1800) {
537 507
       bool signExtend;
538
-      uint8_t op      = (instr & 0x1800) >> 11;
539
-      uint8_t offset5 = (instr & 0x07C0) >>  6;
540
-      uint8_t rs      = (instr & 0x0038) >>  3;
541
-      uint8_t rd      = (instr & 0x0007);
508
+      const uint8_t op      = (instr & 0x1800) >> 11,
509
+                    offset5 = (instr & 0x07C0) >>  6,
510
+                    rs      = (instr & 0x0038) >>  3,
511
+                    rd      = (instr & 0x0007);
542 512
 
543
-      switch(op) {
513
+      switch (op) {
544 514
         case 0: /* LSL */
545 515
           UnwPrintd6("LSL r%d, r%d, #%d\t; r%d %s", rd, rs, offset5, rs, M_Origin2Str(state->regData[rs].o));
546 516
           state->regData[rd].v = state->regData[rs].v << offset5;
@@ -558,11 +528,9 @@ UnwResult UnwStartThumb(UnwState * const state) {
558 528
         case 2: /* ASR */
559 529
           UnwPrintd6("ASL r%d, r%d, #%d\t; r%d %s", rd, rs, offset5, rs, M_Origin2Str(state->regData[rs].o));
560 530
 
561
-          signExtend = (state->regData[rs].v & 0x8000) ? true : false;
531
+          signExtend = !!(state->regData[rs].v & 0x8000);
562 532
           state->regData[rd].v = state->regData[rs].v >> offset5;
563
-          if(signExtend) {
564
-            state->regData[rd].v |= 0xFFFFFFFF << (32 - offset5);
565
-          }
533
+          if (signExtend) state->regData[rd].v |= 0xFFFFFFFF << (32 - offset5);
566 534
           state->regData[rd].o = state->regData[rs].o;
567 535
           state->regData[rd].o |= REG_VAL_ARITHMETIC;
568 536
           break;
@@ -574,9 +542,9 @@ UnwResult UnwStartThumb(UnwState * const state) {
574 542
      *  SUB Rd, Rs, Rn
575 543
      *  SUB Rd, Rs, #Offset3
576 544
      */
577
-    else if((instr & 0xF800) == 0x1800) {
578
-      bool    I  = (instr & 0x0400) ? true : false;
579
-      bool    op = (instr & 0x0200) ? true : false;
545
+    else if ((instr & 0xF800) == 0x1800) {
546
+      bool    I  = !!(instr & 0x0400);
547
+      bool    op = !!(instr & 0x0200);
580 548
       uint8_t rn = (instr & 0x01C0) >> 6;
581 549
       uint8_t rs = (instr & 0x0038) >> 3;
582 550
       uint8_t rd = (instr & 0x0007);
@@ -584,36 +552,24 @@ UnwResult UnwStartThumb(UnwState * const state) {
584 552
       /* Print decoding */
585 553
       UnwPrintd6("%s r%d, r%d, %c%d\t;",op ? "SUB" : "ADD",rd, rs,I ? '#' : 'r',rn);
586 554
       UnwPrintd5("r%d %s, r%d %s",rd, M_Origin2Str(state->regData[rd].o),rs, M_Origin2Str(state->regData[rs].o));
587
-      if(!I) {
555
+      if (!I) {
588 556
 
589 557
         UnwPrintd3(", r%d %s", rn, M_Origin2Str(state->regData[rn].o));
590 558
 
591 559
         /* Perform calculation */
592
-        if(op) {
593
-          state->regData[rd].v = state->regData[rs].v - state->regData[rn].v;
594
-        }
595
-        else {
596
-          state->regData[rd].v = state->regData[rs].v + state->regData[rn].v;
597
-        }
560
+        state->regData[rd].v = state->regData[rs].v + (op ? -state->regData[rn].v : state->regData[rn].v);
598 561
 
599 562
         /* Propagate the origin */
600
-        if(M_IsOriginValid(state->regData[rs].o) &&
601
-           M_IsOriginValid(state->regData[rn].o)) {
563
+        if (M_IsOriginValid(state->regData[rs].o) && M_IsOriginValid(state->regData[rn].o)) {
602 564
           state->regData[rd].o = state->regData[rs].o;
603 565
           state->regData[rd].o |= REG_VAL_ARITHMETIC;
604 566
         }
605
-        else {
567
+        else
606 568
           state->regData[rd].o = REG_VAL_INVALID;
607
-        }
608 569
       }
609 570
       else {
610 571
         /* Perform calculation */
611
-        if(op) {
612
-          state->regData[rd].v = state->regData[rs].v - rn;
613
-        }
614
-        else {
615
-          state->regData[rd].v = state->regData[rs].v + rn;
616
-        }
572
+        state->regData[rd].v = state->regData[rs].v + (op ? -rn : rn);
617 573
 
618 574
         /* Propagate the origin */
619 575
         state->regData[rd].o = state->regData[rs].o;
@@ -626,13 +582,13 @@ UnwResult UnwStartThumb(UnwState * const state) {
626 582
      *  ADD Rd, #Offset8
627 583
      *  SUB Rd, #Offset8
628 584
      */
629
-    else if((instr & 0xE000) == 0x2000) {
585
+    else if ((instr & 0xE000) == 0x2000) {
630 586
 
631 587
       uint8_t op      = (instr & 0x1800) >> 11;
632 588
       uint8_t rd      = (instr & 0x0700) >>  8;
633 589
       uint8_t offset8 = (instr & 0x00FF);
634 590
 
635
-      switch(op) {
591
+      switch (op) {
636 592
         case 0: /* MOV */
637 593
           UnwPrintd3("MOV r%d, #0x%x", rd, offset8);
638 594
           state->regData[rd].v = offset8;
@@ -675,7 +631,7 @@ UnwResult UnwStartThumb(UnwState * const state) {
675 631
      *  BIC Rd, Rs
676 632
      *  MVN Rd, Rs
677 633
      */
678
-    else if((instr & 0xFC00) == 0x4000) {
634
+    else if ((instr & 0xFC00) == 0x4000) {
679 635
       uint8_t op = (instr & 0x03C0) >> 6;
680 636
       uint8_t rs = (instr & 0x0038) >> 3;
681 637
       uint8_t rd = (instr & 0x0007);
@@ -688,7 +644,7 @@ UnwResult UnwStartThumb(UnwState * const state) {
688 644
         "ORR", "MUL", "BIC", "MVN" };
689 645
 #endif
690 646
       /* Print the mnemonic and registers */
691
-      switch(op) {
647
+      switch (op) {
692 648
         case 0: /* AND */
693 649
         case 1: /* EOR */
694 650
         case 2: /* LSL */
@@ -720,7 +676,7 @@ UnwResult UnwStartThumb(UnwState * const state) {
720 676
       }
721 677
 
722 678
       /* Perform operation */
723
-      switch(op) {
679
+      switch (op) {
724 680
         case 0: /* AND */
725 681
           state->regData[rd].v &= state->regData[rs].v;
726 682
           break;
@@ -738,7 +694,7 @@ UnwResult UnwStartThumb(UnwState * const state) {
738 694
           break;
739 695
 
740 696
         case 4: /* ASR */
741
-          if(state->regData[rd].v & 0x80000000) {
697
+          if (state->regData[rd].v & 0x80000000) {
742 698
             state->regData[rd].v >>= state->regData[rs].v;
743 699
             state->regData[rd].v |= 0xFFFFFFFF << (32 - state->regData[rs].v);
744 700
           }
@@ -782,7 +738,7 @@ UnwResult UnwStartThumb(UnwState * const state) {
782 738
       }
783 739
 
784 740
       /* Propagate data origins */
785
-      switch(op) {
741
+      switch (op) {
786 742
         case 0: /* AND */
787 743
         case 1: /* EOR */
788 744
         case 2: /* LSL */
@@ -792,13 +748,12 @@ UnwResult UnwStartThumb(UnwState * const state) {
792 748
         case 12: /* ORR */
793 749
         case 13: /* MUL */
794 750
         case 14: /* BIC */
795
-          if(M_IsOriginValid(state->regData[rd].o) && M_IsOriginValid(state->regData[rs].o)) {
751
+          if (M_IsOriginValid(state->regData[rd].o) && M_IsOriginValid(state->regData[rs].o)) {
796 752
             state->regData[rd].o = state->regData[rs].o;
797 753
             state->regData[rd].o |= REG_VAL_ARITHMETIC;
798 754
           }
799
-          else {
755
+          else
800 756
             state->regData[rd].o = REG_VAL_INVALID;
801
-          }
802 757
           break;
803 758
 
804 759
         case 5: /* ADC */
@@ -825,7 +780,7 @@ UnwResult UnwStartThumb(UnwState * const state) {
825 780
      *  CMP Hd, Rs
826 781
      *  MOV Hd, Hs
827 782
      */
828
-    else if((instr & 0xFC00) == 0x4400) {
783
+    else if ((instr & 0xFC00) == 0x4400) {
829 784
       uint8_t op  = (instr & 0x0300) >> 8;
830 785
       bool    h1  = (instr & 0x0080) ? true: false;
831 786
       bool    h2  = (instr & 0x0040) ? true: false;
@@ -833,12 +788,10 @@ UnwResult UnwStartThumb(UnwState * const state) {
833 788
       uint8_t rhd = (instr & 0x0007);
834 789
 
835 790
       /* Adjust the register numbers */
836
-      if(h2)
837
-        rhs += 8;
838
-      if(h1)
839
-        rhd += 8;
791
+      if (h2) rhs += 8;
792
+      if (h1) rhd += 8;
840 793
 
841
-      switch(op) {
794
+      switch (op) {
842 795
         case 0: /* ADD */
843 796
           UnwPrintd5("ADD r%d, r%d\t; r%d %s", rhd, rhs, rhs, M_Origin2Str(state->regData[rhs].o));
844 797
           state->regData[rhd].v += state->regData[rhs].v;
@@ -861,28 +814,25 @@ UnwResult UnwStartThumb(UnwState * const state) {
861 814
           UnwPrintd4("BX r%d\t; r%d %s\n", rhs, rhs, M_Origin2Str(state->regData[rhs].o));
862 815
 
863 816
           /* Only follow BX if the data was from the stack or BX LR */
864
-          if(rhs == 14 || state->regData[rhs].o == REG_VAL_FROM_STACK) {
817
+          if (rhs == 14 || state->regData[rhs].o == REG_VAL_FROM_STACK) {
865 818
             UnwPrintd2(" Return PC=0x%x\n", state->regData[rhs].v & (~0x1));
866 819
 
867 820
             /* Report the return address, including mode bit */
868
-            if(!UnwReportRetAddr(state, state->regData[rhs].v)) {
821
+            if (!UnwReportRetAddr(state, state->regData[rhs].v))
869 822
               return UNWIND_TRUNCATED;
870
-            }
871 823
 
872 824
             /* Update the PC */
873 825
             state->regData[15].v = state->regData[rhs].v;
874 826
 
875 827
             /* Determine the new mode */
876
-            if(state->regData[rhs].v & 0x1) {
828
+            if (state->regData[rhs].v & 0x1) {
877 829
               /* Branching to THUMB */
878 830
 
879 831
               /* Account for the auto-increment which isn't needed */
880 832
               state->regData[15].v -= 2;
881 833
             }
882
-            else {
883
-              /* Branch to ARM */
834
+            else /* Branch to ARM */
884 835
               return UnwStartArm(state);
885
-            }
886 836
           }
887 837
           else {
888 838
             UnwPrintd4("\nError: BX to invalid register: r%d = 0x%x (%s)\n", rhs, state->regData[rhs].o, M_Origin2Str(state->regData[rhs].o));
@@ -893,7 +843,7 @@ UnwResult UnwStartThumb(UnwState * const state) {
893 843
     /* Format 9: PC-relative load
894 844
      *  LDR Rd,[PC, #imm]
895 845
      */
896
-    else if((instr & 0xF800) == 0x4800) {
846
+    else if ((instr & 0xF800) == 0x4800) {
897 847
       uint8_t  rd    = (instr & 0x0700) >> 8;
898 848
       uint8_t  word8 = (instr & 0x00FF);
899 849
       uint32_t address;
@@ -903,19 +853,18 @@ UnwResult UnwStartThumb(UnwState * const state) {
903 853
 
904 854
       UnwPrintd3("LDR r%d, 0x%08x", rd, address);
905 855
 
906
-      if(!UnwMemReadRegister(state, address, &state->regData[rd])) {
856
+      if (!UnwMemReadRegister(state, address, &state->regData[rd]))
907 857
         return UNWIND_DREAD_W_FAIL;
908
-      }
909 858
     }
910 859
     /* Format 13: add offset to Stack Pointer
911 860
      *  ADD sp,#+imm
912 861
      *  ADD sp,#-imm
913 862
      */
914
-    else if((instr & 0xFF00) == 0xB000) {
863
+    else if ((instr & 0xFF00) == 0xB000) {
915 864
       uint8_t value = (instr & 0x7F) * 4;
916 865
 
917 866
       /* Check the negative bit */
918
-      if((instr & 0x80) != 0) {
867
+      if ((instr & 0x80) != 0) {
919 868
         UnwPrintd2("SUB sp,#0x%x", value);
920 869
         state->regData[13].v -= value;
921 870
       }
@@ -930,29 +879,27 @@ UnwResult UnwStartThumb(UnwState * const state) {
930 879
      *  POP {Rlist}
931 880
      *  POP {Rlist, PC}
932 881
      */
933
-    else if((instr & 0xF600) == 0xB400) {
934
-      bool    L     = (instr & 0x0800) ? true : false;
935
-      bool    R     = (instr & 0x0100) ? true : false;
882
+    else if ((instr & 0xF600) == 0xB400) {
883
+      bool    L     = !!(instr & 0x0800);
884
+      bool    R     = !!(instr & 0x0100);
936 885
       uint8_t rList = (instr & 0x00FF);
937 886
 
938
-      if(L) {
887
+      if (L) {
939 888
         uint8_t r;
940 889
 
941 890
         /* Load from memory: POP */
942 891
         UnwPrintd2("POP {Rlist%s}\n", R ? ", PC" : "");
943 892
 
944
-        for(r = 0; r < 8; r++) {
945
-          if(rList & (0x1 << r)) {
893
+        for (r = 0; r < 8; r++) {
894
+          if (rList & (0x1 << r)) {
946 895
 
947 896
             /* Read the word */
948
-            if(!UnwMemReadRegister(state, state->regData[13].v, &state->regData[r])) {
897
+            if (!UnwMemReadRegister(state, state->regData[13].v, &state->regData[r]))
949 898
               return UNWIND_DREAD_W_FAIL;
950
-            }
951 899
 
952 900
             /* Alter the origin to be from the stack if it was valid */
953
-            if(M_IsOriginValid(state->regData[r].o)) {
901
+            if (M_IsOriginValid(state->regData[r].o))
954 902
               state->regData[r].o = REG_VAL_FROM_STACK;
955
-            }
956 903
 
957 904
             state->regData[13].v += 4;
958 905
 
@@ -961,14 +908,13 @@ UnwResult UnwStartThumb(UnwState * const state) {
961 908
         }
962 909
 
963 910
         /* Check if the PC is to be popped */
964
-        if(R) {
911
+        if (R) {
965 912
           /* Get the return address */
966
-          if(!UnwMemReadRegister(state, state->regData[13].v, &state->regData[15])) {
913
+          if (!UnwMemReadRegister(state, state->regData[13].v, &state->regData[15]))
967 914
             return UNWIND_DREAD_W_FAIL;
968
-          }
969 915
 
970 916
           /* Alter the origin to be from the stack if it was valid */
971
-          if(!M_IsOriginValid(state->regData[15].o)) {
917
+          if (!M_IsOriginValid(state->regData[15].o)) {
972 918
             /* Return address is not valid */
973 919
             UnwPrintd1("PC popped with invalid address\n");
974 920
             return UNWIND_FAILURE;
@@ -978,7 +924,7 @@ UnwResult UnwStartThumb(UnwState * const state) {
978 924
              *  the caller was from Thumb.  This would allow return
979 925
              *  by BX for interworking APCS.
980 926
              */
981
-            if((state->regData[15].v & 0x1) == 0) {
927
+            if ((state->regData[15].v & 0x1) == 0) {
982 928
               UnwPrintd2("Warning: Return address not to Thumb: 0x%08x\n", state->regData[15].v);
983 929
 
984 930
               /* Pop into the PC will not switch mode */
@@ -986,9 +932,8 @@ UnwResult UnwStartThumb(UnwState * const state) {
986 932
             }
987 933
 
988 934
             /* Store the return address */
989
-            if(!UnwReportRetAddr(state, state->regData[15].v)) {
935
+            if (!UnwReportRetAddr(state, state->regData[15].v))
990 936
               return UNWIND_TRUNCATED;
991
-            }
992 937
 
993 938
             /* Now have the return address */
994 939
             UnwPrintd2(" Return PC=%x\n", state->regData[15].v);
@@ -1008,26 +953,24 @@ UnwResult UnwStartThumb(UnwState * const state) {
1008 953
         UnwPrintd2("PUSH {Rlist%s}", R ? ", LR" : "");
1009 954
 
1010 955
         /* Check if the LR is to be pushed */
1011
-        if(R) {
956
+        if (R) {
1012 957
           UnwPrintd3("\n  lr = 0x%08x\t; %s", state->regData[14].v, M_Origin2Str(state->regData[14].o));
1013 958
 
1014 959
           state->regData[13].v -= 4;
1015 960
 
1016 961
           /* Write the register value to memory */
1017
-          if(!UnwMemWriteRegister(state, state->regData[13].v, &state->regData[14])) {
962
+          if (!UnwMemWriteRegister(state, state->regData[13].v, &state->regData[14]))
1018 963
             return UNWIND_DWRITE_W_FAIL;
1019
-          }
1020 964
         }
1021 965
 
1022
-        for(r = 7; r >= 0; r--) {
1023
-          if(rList & (0x1 << r)) {
966
+        for (r = 7; r >= 0; r--) {
967
+          if (rList & (0x1 << r)) {
1024 968
             UnwPrintd4("\n  r%d = 0x%08x\t; %s", r, state->regData[r].v, M_Origin2Str(state->regData[r].o));
1025 969
 
1026 970
             state->regData[13].v -= 4;
1027 971
 
1028
-            if(!UnwMemWriteRegister(state, state->regData[13].v, &state->regData[r])) {
972
+            if (!UnwMemWriteRegister(state, state->regData[13].v, &state->regData[r]))
1029 973
               return UNWIND_DWRITE_W_FAIL;
1030
-            }
1031 974
           }
1032 975
         }
1033 976
       }
@@ -1037,7 +980,7 @@ UnwResult UnwStartThumb(UnwState * const state) {
1037 980
      * Conditional branches
1038 981
      * Bcond
1039 982
      */
1040
-    else if((instr & 0xF000) == 0xD000) {
983
+    else if ((instr & 0xF000) == 0xD000) {
1041 984
       int32_t branchValue = (instr & 0xFF);
1042 985
       if (branchValue & 0x80) branchValue |= 0xFFFFFF00;
1043 986
 
@@ -1066,7 +1009,7 @@ UnwResult UnwStartThumb(UnwState * const state) {
1066 1009
     /* Format 18: unconditional branch
1067 1010
      *  B label
1068 1011
      */
1069
-    else if((instr & 0xF800) == 0xE000) {
1012
+    else if ((instr & 0xF800) == 0xE000) {
1070 1013
       uint32_t v;
1071 1014
       int32_t branchValue = signExtend11(instr & 0x07FF);
1072 1015
 
@@ -1106,8 +1049,7 @@ UnwResult UnwStartThumb(UnwState * const state) {
1106 1049
     UnwPrintd1("\n");
1107 1050
 
1108 1051
     /* Should never hit the reset vector */
1109
-    if(state->regData[15].v == 0)
1110
-      return UNWIND_RESET;
1052
+    if (state->regData[15].v == 0) return UNWIND_RESET;
1111 1053
 
1112 1054
     /* Check next address */
1113 1055
     state->regData[15].v += 2;
@@ -1115,11 +1057,9 @@ UnwResult UnwStartThumb(UnwState * const state) {
1115 1057
     /* Garbage collect the memory hash (used only for the stack) */
1116 1058
     UnwMemHashGC(state);
1117 1059
 
1118
-    t--;
1119
-    if(t == 0)
1120
-      return UNWIND_EXHAUSTED;
1060
+    if (--t == 0) return UNWIND_EXHAUSTED;
1121 1061
 
1122
-  } while(!found);
1062
+  } while (!found);
1123 1063
 
1124 1064
   return UNWIND_SUCCESS;
1125 1065
 }

+ 24
- 36
Marlin/src/HAL/shared/backtrace/unwarmmem.cpp View File

@@ -19,7 +19,7 @@
19 19
 #include "unwarmmem.h"
20 20
 #include "unwarm.h"
21 21
 
22
-#define M_IsIdxUsed(a, v) (((a)[v >> 3] & (1 << (v & 0x7))) ? true : false)
22
+#define M_IsIdxUsed(a, v) !!((a)[v >> 3] & (1 << (v & 0x7)))
23 23
 #define M_SetIdxUsed(a, v) ((a)[v >> 3] |= (1 << (v & 0x7)))
24 24
 #define M_ClrIdxUsed(a, v) ((a)[v >> 3] &= ~(1 << (v & 0x7)))
25 25
 
@@ -34,11 +34,9 @@ static int16_t memHashIndex(MemData * const memData, const uint32_t addr) {
34 34
 
35 35
   do {
36 36
     /* Check if the element is occupied */
37
-    if(M_IsIdxUsed(memData->used, s)) {
37
+    if (M_IsIdxUsed(memData->used, s)) {
38 38
       /* Check if it is occupied with the sought data */
39
-      if(memData->a[s] == addr) {
40
-        return s;
41
-      }
39
+      if (memData->a[s] == addr) return s;
42 40
     }
43 41
     else {
44 42
       /* Item is free, this is where the item should be stored */
@@ -47,10 +45,8 @@ static int16_t memHashIndex(MemData * const memData, const uint32_t addr) {
47 45
 
48 46
     /* Search the next entry */
49 47
     s++;
50
-    if(s > MEM_HASH_SIZE) {
51
-      s = 0;
52
-    }
53
-  } while(s != v);
48
+    if (s > MEM_HASH_SIZE) s = 0;
49
+  } while (s != v);
54 50
 
55 51
   /* Search failed, hash is full and the address not stored */
56 52
   return -1;
@@ -58,9 +54,9 @@ static int16_t memHashIndex(MemData * const memData, const uint32_t addr) {
58 54
 
59 55
 bool UnwMemHashRead(MemData * const memData, uint32_t addr,uint32_t * const data, bool * const tracked) {
60 56
 
61
-  int16_t i = memHashIndex(memData, addr);
57
+  const int16_t i = memHashIndex(memData, addr);
62 58
 
63
-  if(i >= 0 && M_IsIdxUsed(memData->used, i) && memData->a[i] == addr) {
59
+  if (i >= 0 && M_IsIdxUsed(memData->used, i) && memData->a[i] == addr) {
64 60
     *data    = memData->v[i];
65 61
     *tracked = M_IsIdxUsed(memData->tracked, i);
66 62
     return true;
@@ -72,44 +68,36 @@ bool UnwMemHashRead(MemData * const memData, uint32_t addr,uint32_t * const data
72 68
 }
73 69
 
74 70
 bool UnwMemHashWrite(MemData * const memData, uint32_t addr, uint32_t val, bool valValid) {
71
+  const int16_t i = memHashIndex(memData, addr);
72
+  if (i < 0) return false; /* Hash full */
75 73
 
76
-  int16_t i = memHashIndex(memData, addr);
74
+  /* Store the item */
75
+  memData->a[i] = addr;
76
+  M_SetIdxUsed(memData->used, i);
77 77
 
78
-  if(i < 0){
79
-    /* Hash full */
80
-    return false;
78
+  if (valValid) {
79
+    memData->v[i] = val;
80
+    M_SetIdxUsed(memData->tracked, i);
81 81
   }
82 82
   else {
83
-    /* Store the item */
84
-    memData->a[i] = addr;
85
-    M_SetIdxUsed(memData->used, i);
86
-
87
-    if(valValid)
88
-    {
89
-      memData->v[i] = val;
90
-      M_SetIdxUsed(memData->tracked, i);
91
-    }
92
-    else {
93
-      #ifdef UNW_DEBUG
94
-        memData->v[i] = 0xDEADBEEF;
95
-      #endif
96
-      M_ClrIdxUsed(memData->tracked, i);
97
-    }
98
-
99
-    return true;
83
+    #ifdef UNW_DEBUG
84
+      memData->v[i] = 0xDEADBEEF;
85
+    #endif
86
+    M_ClrIdxUsed(memData->tracked, i);
100 87
   }
88
+
89
+  return true;
101 90
 }
102 91
 
103 92
 void UnwMemHashGC(UnwState * const state) {
104 93
 
105 94
   const uint32_t minValidAddr = state->regData[13].v;
106 95
   MemData * const memData  = &state->memData;
107
-  uint16_t       t;
96
+  uint16_t t;
108 97
 
109
-  for(t = 0; t < MEM_HASH_SIZE; t++) {
110
-    if(M_IsIdxUsed(memData->used, t) && (memData->a[t] < minValidAddr)) {
98
+  for (t = 0; t < MEM_HASH_SIZE; t++) {
99
+    if (M_IsIdxUsed(memData->used, t) && (memData->a[t] < minValidAddr)) {
111 100
       UnwPrintd3("MemHashGC: Free elem %d, addr 0x%08x\n", t, memData->a[t]);
112
-
113 101
       M_ClrIdxUsed(memData->used, t);
114 102
     }
115 103
   }

+ 3
- 10
Marlin/src/HAL/shared/backtrace/unwinder.cpp View File

@@ -33,13 +33,11 @@ static int HasUnwindTableInfo(void) {
33 33
 }
34 34
 
35 35
 UnwResult UnwindStart(UnwindFrame* frame, const UnwindCallbacks *cb, void *data) {
36
-
37 36
   if (HasUnwindTableInfo()) {
38
-
39 37
     /* We have unwind information tables */
40 38
     return UnwindByTableStart(frame, cb, data);
41
-
42
-  } else {
39
+  }
40
+  else {
43 41
 
44 42
     /* We don't have unwind information tables */
45 43
     UnwState state;
@@ -48,12 +46,7 @@ UnwResult UnwindStart(UnwindFrame* frame, const UnwindCallbacks *cb, void *data)
48 46
     UnwInitState(&state, cb, data, frame->pc, frame->sp);
49 47
 
50 48
     /* Check the Thumb bit */
51
-    if(frame->pc & 0x1) {
52
-      return UnwStartThumb(&state);
53
-    }
54
-    else {
55
-      return UnwStartArm(&state);
56
-    }
49
+    return (frame->pc & 0x1) ? UnwStartThumb(&state) : UnwStartArm(&state);
57 50
   }
58 51
 }
59 52
 #endif

+ 1
- 1
Marlin/src/feature/emergency_parser.h View File

@@ -143,7 +143,7 @@ public:
143 143
         break;
144 144
 
145 145
       case EP_M876:
146
-        switch(c) {
146
+        switch (c) {
147 147
           case ' ': break;
148 148
           case 'S': state = EP_M876S; break;
149 149
           default:  state = EP_IGNORE; break;

+ 1
- 1
Marlin/src/feature/prusa_MMU2/mmu2.cpp View File

@@ -557,7 +557,7 @@ void MMU2::toolChange(const char* special) {
557 557
     set_runout_valid(false);
558 558
     KEEPALIVE_STATE(IN_HANDLER);
559 559
 
560
-    switch(*special) {
560
+    switch (*special) {
561 561
       case '?': {
562 562
         uint8_t index = mmu2_chooseFilament();
563 563
         while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);

+ 1
- 1
Marlin/src/gcode/calibrate/G425.cpp View File

@@ -248,7 +248,7 @@ inline void probe_side(measurements_t &m, const float uncertainty, const side_t
248 248
 
249 249
   park_above_object(m, uncertainty);
250 250
 
251
-  switch(side) {
251
+  switch (side) {
252 252
     case TOP: {
253 253
       const float measurement = measure(Z_AXIS, -1, true, &m.backlash[TOP], uncertainty);
254 254
       m.obj_center[Z_AXIS] = measurement - dimensions[Z_AXIS] / 2;

+ 3
- 3
Marlin/src/lcd/dogm/u8g_dev_ssd1306_sh1106_128x64_I2C.cpp View File

@@ -112,7 +112,7 @@ static const uint8_t u8g_dev_sh1106_128x64_init_seq_2_wire[] PROGMEM = {
112 112
 };
113 113
 
114 114
 uint8_t u8g_dev_sh1106_128x64_2x_2_wire_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
115
-  switch(msg) {
115
+  switch (msg) {
116 116
     case U8G_DEV_MSG_INIT:
117 117
       u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_300NS);
118 118
       u8g_WriteEscSeqP_2_wire(u8g, dev, u8g_dev_sh1106_128x64_init_seq_2_wire);
@@ -180,7 +180,7 @@ static const uint8_t u8g_dev_ssd1306_128x64_init_seq_2_wire[] PROGMEM = {
180 180
 };
181 181
 
182 182
 uint8_t u8g_dev_ssd1306_128x64_2x_2_wire_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
183
-  switch(msg) {
183
+  switch (msg) {
184 184
     case U8G_DEV_MSG_INIT:
185 185
       u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_300NS);
186 186
       u8g_WriteEscSeqP_2_wire(u8g, dev, u8g_dev_ssd1306_128x64_init_seq_2_wire);
@@ -227,7 +227,7 @@ u8g_dev_t u8g_dev_ssd1306_128x64_2x_i2c_2_wire = { u8g_dev_ssd1306_128x64_2x_2_w
227 227
 uint8_t u8g_WriteEscSeqP_2_wire(u8g_t *u8g, u8g_dev_t *dev, const uint8_t *esc_seq) {
228 228
   uint8_t is_escape = 0;
229 229
   uint8_t value;
230
-  for(;;) {
230
+  for (;;) {
231 231
     value = u8g_pgm_read(esc_seq);
232 232
     if (is_escape == 0) {
233 233
       if (value != 255) {

+ 2
- 2
Marlin/src/lcd/dogm/u8g_dev_st7565_64128n_HAL.cpp View File

@@ -149,7 +149,7 @@ static const uint8_t u8g_dev_st7565_64128n_HAL_sleep_off[] PROGMEM = {
149 149
 };
150 150
 
151 151
 uint8_t u8g_dev_st7565_64128n_HAL_fn(u8g_t *u8g, u8g_dev_t *dev, const uint8_t msg, void *arg) {
152
-  switch(msg) {
152
+  switch (msg) {
153 153
     case U8G_DEV_MSG_INIT:
154 154
       u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_400NS);
155 155
       u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_64128n_HAL_init_seq);
@@ -183,7 +183,7 @@ uint8_t u8g_dev_st7565_64128n_HAL_fn(u8g_t *u8g, u8g_dev_t *dev, const uint8_t m
183 183
 }
184 184
 
185 185
 uint8_t u8g_dev_st7565_64128n_HAL_2x_fn(u8g_t *u8g, u8g_dev_t *dev, const uint8_t msg, void *arg) {
186
-  switch(msg) {
186
+  switch (msg) {
187 187
     case U8G_DEV_MSG_INIT:
188 188
       u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_400NS);
189 189
       u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_64128n_HAL_init_seq);

+ 2
- 2
Marlin/src/lcd/dogm/u8g_dev_st7920_128x64_HAL.cpp View File

@@ -106,7 +106,7 @@ void clear_graphics_DRAM(u8g_t *u8g, u8g_dev_t *dev) {
106 106
 }
107 107
 
108 108
 uint8_t u8g_dev_st7920_128x64_HAL_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
109
-  switch(msg) {
109
+  switch (msg) {
110 110
     case U8G_DEV_MSG_INIT:
111 111
       u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_400NS);
112 112
       u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7920_128x64_HAL_init_seq);
@@ -149,7 +149,7 @@ uint8_t u8g_dev_st7920_128x64_HAL_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, vo
149 149
 }
150 150
 
151 151
 uint8_t u8g_dev_st7920_128x64_HAL_4x_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
152
-  switch(msg) {
152
+  switch (msg) {
153 153
     case U8G_DEV_MSG_INIT:
154 154
       u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_400NS);
155 155
       u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7920_128x64_HAL_init_seq);

+ 1
- 1
Marlin/src/lcd/dogm/u8g_dev_tft_320x240_upscale_from_128x64.cpp View File

@@ -125,7 +125,7 @@ uint8_t u8g_dev_tft_320x240_upscale_from_128x64_fn(u8g_t *u8g, u8g_dev_t *dev, u
125 125
   uint16_t buffer[256];
126 126
   uint32_t i, j, k;
127 127
 
128
-  switch(msg) {
128
+  switch (msg) {
129 129
     case U8G_DEV_MSG_INIT:
130 130
       dev->com_fn(u8g, U8G_COM_MSG_INIT, U8G_SPI_CLK_CYCLE_NONE, &lcd_id);
131 131
       if (lcd_id == 0x040404) return 0; // No connected display on FSMC

+ 2
- 2
Marlin/src/lcd/dogm/u8g_dev_uc1701_mini12864_HAL.cpp View File

@@ -109,7 +109,7 @@ static const uint8_t u8g_dev_uc1701_mini12864_HAL_data_start[] PROGMEM = {
109 109
 };
110 110
 
111 111
 uint8_t u8g_dev_uc1701_mini12864_HAL_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
112
-  switch(msg) {
112
+  switch (msg) {
113 113
     case U8G_DEV_MSG_INIT:
114 114
       u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_300NS);
115 115
       u8g_WriteEscSeqP(u8g, dev, u8g_dev_uc1701_mini12864_HAL_init_seq);
@@ -138,7 +138,7 @@ uint8_t u8g_dev_uc1701_mini12864_HAL_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg,
138 138
 }
139 139
 
140 140
 uint8_t u8g_dev_uc1701_mini12864_HAL_2x_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
141
-  switch(msg) {
141
+  switch (msg) {
142 142
     case U8G_DEV_MSG_INIT:
143 143
       u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_300NS);
144 144
       u8g_WriteEscSeqP(u8g, dev, u8g_dev_uc1701_mini12864_HAL_init_seq);

+ 1
- 1
Marlin/src/sd/Sd2Card.cpp View File

@@ -339,7 +339,7 @@ bool Sd2Card::readBlock(uint32_t blockNumber, uint8_t* dst) {
339 339
 
340 340
   #if ENABLED(SD_CHECK_AND_RETRY)
341 341
     uint8_t retryCnt = 3;
342
-    for(;;) {
342
+    for (;;) {
343 343
       if (cardCommand(CMD17, blockNumber))
344 344
         error(SD_CARD_ERROR_CMD17);
345 345
       else if (readData(dst, 512))

+ 586
- 621
Marlin/src/sd/usb_flashdrive/lib/Usb.cpp
File diff suppressed because it is too large
View File


+ 3
- 2
Marlin/src/sd/usb_flashdrive/lib/Usb.h View File

@@ -22,8 +22,9 @@
22 22
  * Web      :  http://www.circuitsathome.com
23 23
  * e-mail   :  support@circuitsathome.com
24 24
  */
25
+#pragma once
26
+
25 27
 /* USB functions */
26
-#ifndef _usb_h_
27 28
 #define _usb_h_
28 29
 
29 30
 #include "../../../inc/MarlinConfigPre.h"
@@ -50,4 +51,4 @@
50 51
 #include "parsetools.h"
51 52
 #include "confdescparser.h"
52 53
 
53
-#endif //_usb_h_
54
+#undef _usb_h_

+ 2
- 2
Marlin/src/sd/usb_flashdrive/lib/UsbCore.h View File

@@ -231,8 +231,8 @@ public:
231 231
   };
232 232
 
233 233
   uint8_t RegisterDeviceClass(USBDeviceConfig *pdev) {
234
-    for(uint8_t i = 0; i < USB_NUMDEVICES; i++) {
235
-      if(!devConfig[i]) {
234
+    for (uint8_t i = 0; i < USB_NUMDEVICES; i++) {
235
+      if (!devConfig[i]) {
236 236
         devConfig[i] = pdev;
237 237
         return 0;
238 238
       }

+ 186
- 206
Marlin/src/sd/usb_flashdrive/lib/address.h View File

@@ -22,13 +22,11 @@
22 22
  * Web      :  http://www.circuitsathome.com
23 23
  * e-mail   :  support@circuitsathome.com
24 24
  */
25
+#pragma once
25 26
 
26
-#if !defined(_usb_h_) || defined(__ADDRESS_H__)
27
-#error "Never include address.h directly; include Usb.h instead"
28
-#else
29
-#define __ADDRESS_H__
30
-
31
-
27
+#ifndef _usb_h_
28
+  #error "Never include address.h directly; include Usb.h instead"
29
+#endif
32 30
 
33 31
 /* NAK powers. To save space in endpoint data structure, amount of retries before giving up and returning 0x4 is stored in */
34 32
 /* bmNakPower as a power of 2. The actual nak_limit is then calculated as nak_limit = ( 2^bmNakPower - 1) */
@@ -38,18 +36,18 @@
38 36
 #define USB_NAK_NONAK                   0               //Do not count NAKs, stop retrying after USB Timeout
39 37
 
40 38
 struct EpInfo {
41
-        uint8_t epAddr; // Endpoint address
42
-        uint8_t maxPktSize; // Maximum packet size
43
-
44
-        union {
45
-                uint8_t epAttribs;
46
-
47
-                struct {
48
-                        uint8_t bmSndToggle : 1; // Send toggle, when zero bmSNDTOG0, bmSNDTOG1 otherwise
49
-                        uint8_t bmRcvToggle : 1; // Send toggle, when zero bmRCVTOG0, bmRCVTOG1 otherwise
50
-                        uint8_t bmNakPower : 6; // Binary order for NAK_LIMIT value
51
-                } __attribute__((packed));
52
-        };
39
+  uint8_t epAddr; // Endpoint address
40
+  uint8_t maxPktSize; // Maximum packet size
41
+
42
+  union {
43
+    uint8_t epAttribs;
44
+
45
+    struct {
46
+      uint8_t bmSndToggle : 1; // Send toggle, when zero bmSNDTOG0, bmSNDTOG1 otherwise
47
+      uint8_t bmRcvToggle : 1; // Send toggle, when zero bmRCVTOG0, bmRCVTOG1 otherwise
48
+      uint8_t bmNakPower : 6; // Binary order for NAK_LIMIT value
49
+    } __attribute__((packed));
50
+  };
53 51
 } __attribute__((packed));
54 52
 
55 53
 //        7   6   5   4   3   2   1   0
@@ -63,17 +61,15 @@ struct EpInfo {
63 61
 //
64 62
 
65 63
 struct UsbDeviceAddress {
66
-
67
-        union {
68
-
69
-                struct {
70
-                        uint8_t bmAddress : 3; // device address/port number
71
-                        uint8_t bmParent : 3; // parent hub address
72
-                        uint8_t bmHub : 1; // hub flag
73
-                        uint8_t bmReserved : 1; // reserved, must be zero
74
-                } __attribute__((packed));
75
-                uint8_t devAddress;
76
-        };
64
+  union {
65
+    struct {
66
+      uint8_t bmAddress : 3; // device address/port number
67
+      uint8_t bmParent : 3; // parent hub address
68
+      uint8_t bmHub : 1; // hub flag
69
+      uint8_t bmReserved : 1; // reserved, must be zero
70
+    } __attribute__((packed));
71
+    uint8_t devAddress;
72
+  };
77 73
 } __attribute__((packed));
78 74
 
79 75
 #define bmUSB_DEV_ADDR_ADDRESS          0x07
@@ -81,18 +77,18 @@ struct UsbDeviceAddress {
81 77
 #define bmUSB_DEV_ADDR_HUB              0x40
82 78
 
83 79
 struct UsbDevice {
84
-        EpInfo *epinfo; // endpoint info pointer
85
-        UsbDeviceAddress address;
86
-        uint8_t epcount; // number of endpoints
87
-        bool lowspeed; // indicates if a device is the low speed one
88
-        //      uint8_t devclass; // device class
80
+  EpInfo *epinfo; // endpoint info pointer
81
+  UsbDeviceAddress address;
82
+  uint8_t epcount; // number of endpoints
83
+  bool lowspeed; // indicates if a device is the low speed one
84
+  //      uint8_t devclass; // device class
89 85
 } __attribute__((packed));
90 86
 
91 87
 class AddressPool {
92
-public:
93
-        virtual UsbDevice* GetUsbDevicePtr(uint8_t addr) = 0;
94
-        virtual uint8_t AllocAddress(uint8_t parent, bool is_hub = false, uint8_t port = 0) = 0;
95
-        virtual void FreeAddress(uint8_t addr) = 0;
88
+  public:
89
+    virtual UsbDevice* GetUsbDevicePtr(uint8_t addr) = 0;
90
+    virtual uint8_t AllocAddress(uint8_t parent, bool is_hub = false, uint8_t port = 0) = 0;
91
+    virtual void FreeAddress(uint8_t addr) = 0;
96 92
 };
97 93
 
98 94
 typedef void (*UsbDeviceHandleFunc)(UsbDevice *pdev);
@@ -102,190 +98,174 @@ typedef void (*UsbDeviceHandleFunc)(UsbDevice *pdev);
102 98
 
103 99
 template <const uint8_t MAX_DEVICES_ALLOWED>
104 100
 class AddressPoolImpl : public AddressPool {
105
-        EpInfo dev0ep; //Endpoint data structure used during enumeration for uninitialized device
101
+  EpInfo dev0ep; //Endpoint data structure used during enumeration for uninitialized device
102
+
103
+  uint8_t hubCounter; // hub counter is kept
104
+  // in order to avoid hub address duplication
106 105
 
107
-        uint8_t hubCounter; // hub counter is kept
108
-        // in order to avoid hub address duplication
106
+  UsbDevice thePool[MAX_DEVICES_ALLOWED];
109 107
 
110
-        UsbDevice thePool[MAX_DEVICES_ALLOWED];
108
+  // Initialize address pool entry
111 109
 
112
-        // Initializes address pool entry
110
+  void InitEntry(uint8_t index) {
111
+    thePool[index].address.devAddress = 0;
112
+    thePool[index].epcount = 1;
113
+    thePool[index].lowspeed = 0;
114
+    thePool[index].epinfo = &dev0ep;
115
+  }
113 116
 
114
-        void InitEntry(uint8_t index) {
115
-                thePool[index].address.devAddress = 0;
116
-                thePool[index].epcount = 1;
117
-                thePool[index].lowspeed = 0;
118
-                thePool[index].epinfo = &dev0ep;
119
-        };
117
+  // Return thePool index for a given address
120 118
 
121
-        // Returns thePool index for a given address
119
+  uint8_t FindAddressIndex(uint8_t address = 0) {
120
+    for (uint8_t i = 1; i < MAX_DEVICES_ALLOWED; i++)
121
+      if (thePool[i].address.devAddress == address)
122
+        return i;
122 123
 
123
-        uint8_t FindAddressIndex(uint8_t address = 0) {
124
-                for(uint8_t i = 1; i < MAX_DEVICES_ALLOWED; i++) {
125
-                        if(thePool[i].address.devAddress == address)
126
-                                return i;
127
-                }
128
-                return 0;
129
-        };
124
+    return 0;
125
+  }
130 126
 
131
-        // Returns thePool child index for a given parent
127
+  // Return thePool child index for a given parent
132 128
 
133
-        uint8_t FindChildIndex(UsbDeviceAddress addr, uint8_t start = 1) {
134
-                for(uint8_t i = (start < 1 || start >= MAX_DEVICES_ALLOWED) ? 1 : start; i < MAX_DEVICES_ALLOWED; i++) {
135
-                        if(thePool[i].address.bmParent == addr.bmAddress)
136
-                                return i;
137
-                }
138
-                return 0;
139
-        };
129
+  uint8_t FindChildIndex(UsbDeviceAddress addr, uint8_t start = 1) {
130
+    for (uint8_t i = (start < 1 || start >= MAX_DEVICES_ALLOWED) ? 1 : start; i < MAX_DEVICES_ALLOWED; i++) {
131
+      if (thePool[i].address.bmParent == addr.bmAddress)
132
+        return i;
133
+    }
134
+    return 0;
135
+  }
140 136
 
141
-        // Frees address entry specified by index parameter
137
+  // Frees address entry specified by index parameter
142 138
 
143
-        void FreeAddressByIndex(uint8_t index) {
144
-                // Zero field is reserved and should not be affected
145
-                if(index == 0)
146
-                        return;
139
+  void FreeAddressByIndex(uint8_t index) {
140
+    // Zero field is reserved and should not be affected
141
+    if (index == 0) return;
147 142
 
148
-                UsbDeviceAddress uda = thePool[index].address;
149
-                // If a hub was switched off all port addresses should be freed
150
-                if(uda.bmHub == 1) {
151
-                        for(uint8_t i = 1; (i = FindChildIndex(uda, i));)
152
-                                FreeAddressByIndex(i);
143
+    UsbDeviceAddress uda = thePool[index].address;
144
+    // If a hub was switched off all port addresses should be freed
145
+    if (uda.bmHub == 1) {
146
+      for (uint8_t i = 1; (i = FindChildIndex(uda, i));)
147
+        FreeAddressByIndex(i);
153 148
 
154
-                        // If the hub had the last allocated address, hubCounter should be decremented
155
-                        if(hubCounter == uda.bmAddress)
156
-                                hubCounter--;
157
-                }
158
-                InitEntry(index);
159
-        }
149
+      // If the hub had the last allocated address, hubCounter should be decremented
150
+      if (hubCounter == uda.bmAddress) hubCounter--;
151
+    }
152
+    InitEntry(index);
153
+  }
160 154
 
161
-        // Initializes the whole address pool at once
155
+  // Initialize the whole address pool at once
162 156
 
163
-        void InitAllAddresses() {
164
-                for(uint8_t i = 1; i < MAX_DEVICES_ALLOWED; i++)
165
-                        InitEntry(i);
157
+  void InitAllAddresses() {
158
+    for (uint8_t i = 1; i < MAX_DEVICES_ALLOWED; i++)
159
+      InitEntry(i);
166 160
 
167
-                hubCounter = 0;
168
-        };
161
+    hubCounter = 0;
162
+  }
169 163
 
170 164
 public:
171 165
 
172
-        AddressPoolImpl() : hubCounter(0) {
173
-                // Zero address is reserved
174
-                InitEntry(0);
175
-
176
-                thePool[0].address.devAddress = 0;
177
-                thePool[0].epinfo = &dev0ep;
178
-                dev0ep.epAddr = 0;
179
-                dev0ep.maxPktSize = 8;
180
-                dev0ep.bmSndToggle = 0; // Set DATA0/1 toggles to 0
181
-                dev0ep.bmRcvToggle = 0;
182
-                dev0ep.bmNakPower = USB_NAK_MAX_POWER;
183
-
184
-                InitAllAddresses();
185
-        };
186
-
187
-        // Returns a pointer to a specified address entry
188
-
189
-        virtual UsbDevice* GetUsbDevicePtr(uint8_t addr) {
190
-                if(!addr)
191
-                        return thePool;
192
-
193
-                uint8_t index = FindAddressIndex(addr);
194
-
195
-                return (!index) ? NULL : thePool + index;
196
-        };
197
-
198
-        // Performs an operation specified by pfunc for each addressed device
199
-
200
-        void ForEachUsbDevice(UsbDeviceHandleFunc pfunc) {
201
-                if(!pfunc)
202
-                        return;
203
-
204
-                for(uint8_t i = 1; i < MAX_DEVICES_ALLOWED; i++)
205
-                        if(thePool[i].address.devAddress)
206
-                                pfunc(thePool + i);
207
-        };
208
-
209
-        // Allocates new address
210
-
211
-        virtual uint8_t AllocAddress(uint8_t parent, bool is_hub = false, uint8_t port = 0) {
212
-                /* if (parent != 0 && port == 0)
213
-                        USB_HOST_SERIAL.println("PRT:0"); */
214
-                UsbDeviceAddress _parent;
215
-                _parent.devAddress = parent;
216
-                if(_parent.bmReserved || port > 7)
217
-                        //if(parent > 127 || port > 7)
218
-                        return 0;
219
-
220
-                if(is_hub && hubCounter == 7)
221
-                        return 0;
222
-
223
-                // finds first empty address entry starting from one
224
-                uint8_t index = FindAddressIndex(0);
225
-
226
-                if(!index) // if empty entry is not found
227
-                        return 0;
228
-
229
-                if(_parent.devAddress == 0) {
230
-                        if(is_hub) {
231
-                                thePool[index].address.devAddress = 0x41;
232
-                                hubCounter++;
233
-                        } else
234
-                                thePool[index].address.devAddress = 1;
235
-
236
-                        return thePool[index].address.devAddress;
237
-                }
238
-
239
-                UsbDeviceAddress addr;
240
-                addr.devAddress = 0; // Ensure all bits are zero
241
-                addr.bmParent = _parent.bmAddress;
242
-                if(is_hub) {
243
-                        addr.bmHub = 1;
244
-                        addr.bmAddress = ++hubCounter;
245
-                } else {
246
-                        addr.bmHub = 0;
247
-                        addr.bmAddress = port;
248
-                }
249
-                thePool[index].address = addr;
250
-                /*
251
-                                USB_HOST_SERIAL.print("Addr:");
252
-                                USB_HOST_SERIAL.print(addr.bmHub, HEX);
253
-                                USB_HOST_SERIAL.print(".");
254
-                                USB_HOST_SERIAL.print(addr.bmParent, HEX);
255
-                                USB_HOST_SERIAL.print(".");
256
-                                USB_HOST_SERIAL.println(addr.bmAddress, HEX);
257
-                 */
258
-                return thePool[index].address.devAddress;
259
-        };
260
-
261
-        // Empties pool entry
262
-
263
-        virtual void FreeAddress(uint8_t addr) {
264
-                // if the root hub is disconnected all the addresses should be initialized
265
-                if(addr == 0x41) {
266
-                        InitAllAddresses();
267
-                        return;
268
-                }
269
-                uint8_t index = FindAddressIndex(addr);
270
-                FreeAddressByIndex(index);
271
-        };
272
-
273
-        // Returns number of hubs attached
274
-        // It can be rather helpfull to find out if there are hubs attached than getting the exact number of hubs.
275
-        //uint8_t GetNumHubs()
276
-        //{
277
-        //        return hubCounter;
278
-        //};
279
-        //uint8_t GetNumDevices()
280
-        //{
281
-        //        uint8_t counter = 0;
282
-
283
-        //        for (uint8_t i=1; i<MAX_DEVICES_ALLOWED; i++)
284
-        //                if (thePool[i].address != 0);
285
-        //                        counter ++;
286
-
287
-        //        return counter;
288
-        //};
166
+  AddressPoolImpl() : hubCounter(0) {
167
+    // Zero address is reserved
168
+    InitEntry(0);
169
+
170
+    thePool[0].address.devAddress = 0;
171
+    thePool[0].epinfo = &dev0ep;
172
+    dev0ep.epAddr = 0;
173
+    dev0ep.maxPktSize = 8;
174
+    dev0ep.bmSndToggle = 0; // Set DATA0/1 toggles to 0
175
+    dev0ep.bmRcvToggle = 0;
176
+    dev0ep.bmNakPower = USB_NAK_MAX_POWER;
177
+
178
+    InitAllAddresses();
179
+  }
180
+
181
+  // Return a pointer to a specified address entry
182
+
183
+  virtual UsbDevice* GetUsbDevicePtr(uint8_t addr) {
184
+    if (!addr) return thePool;
185
+    uint8_t index = FindAddressIndex(addr);
186
+    return index ? thePool + index : NULL;
187
+  }
188
+
189
+  // Perform an operation specified by pfunc for each addressed device
190
+
191
+  void ForEachUsbDevice(UsbDeviceHandleFunc pfunc) {
192
+    if (pfunc) {
193
+      for (uint8_t i = 1; i < MAX_DEVICES_ALLOWED; i++)
194
+        if (thePool[i].address.devAddress)
195
+          pfunc(thePool + i);
196
+    }
197
+  }
198
+
199
+  // Allocate new address
200
+
201
+  virtual uint8_t AllocAddress(uint8_t parent, bool is_hub = false, uint8_t port = 0) {
202
+    /* if (parent != 0 && port == 0)
203
+      USB_HOST_SERIAL.println("PRT:0"); */
204
+    UsbDeviceAddress _parent;
205
+    _parent.devAddress = parent;
206
+    if (_parent.bmReserved || port > 7)
207
+      //if(parent > 127 || port > 7)
208
+      return 0;
209
+
210
+    if (is_hub && hubCounter == 7) return 0;
211
+
212
+    // finds first empty address entry starting from one
213
+    uint8_t index = FindAddressIndex(0);
214
+
215
+    if (!index) return 0; // if empty entry is not found
216
+
217
+    if (_parent.devAddress == 0) {
218
+      if (is_hub) {
219
+        thePool[index].address.devAddress = 0x41;
220
+        hubCounter++;
221
+      }
222
+      else
223
+        thePool[index].address.devAddress = 1;
224
+
225
+      return thePool[index].address.devAddress;
226
+    }
227
+
228
+    UsbDeviceAddress addr;
229
+    addr.devAddress = 0; // Ensure all bits are zero
230
+    addr.bmParent = _parent.bmAddress;
231
+    if (is_hub) {
232
+      addr.bmHub = 1;
233
+      addr.bmAddress = ++hubCounter;
234
+    }
235
+    else {
236
+      addr.bmHub = 0;
237
+      addr.bmAddress = port;
238
+    }
239
+    thePool[index].address = addr;
240
+    /*
241
+      USB_HOST_SERIAL.print("Addr:");
242
+      USB_HOST_SERIAL.print(addr.bmHub, HEX);
243
+      USB_HOST_SERIAL.print(".");
244
+      USB_HOST_SERIAL.print(addr.bmParent, HEX);
245
+      USB_HOST_SERIAL.print(".");
246
+      USB_HOST_SERIAL.println(addr.bmAddress, HEX);
247
+    */
248
+    return thePool[index].address.devAddress;
249
+  }
250
+
251
+  // Empty the pool entry
252
+
253
+  virtual void FreeAddress(uint8_t addr) {
254
+    // if the root hub is disconnected all the addresses should be initialized
255
+    if (addr == 0x41) {
256
+      InitAllAddresses();
257
+      return;
258
+    }
259
+    FreeAddressByIndex(FindAddressIndex(addr));
260
+  }
261
+
262
+  // Return number of hubs attached
263
+  // It can be helpful to find out if hubs are attached when getting the exact number of hubs.
264
+  //uint8_t GetNumHubs() { return hubCounter; }
265
+  //uint8_t GetNumDevices() {
266
+  //  uint8_t counter = 0;
267
+  //  for (uint8_t i = 1; i < MAX_DEVICES_ALLOWED; i++)
268
+  //    if (thePool[i].address != 0); counter++;
269
+  //  return counter;
270
+  //}
289 271
 };
290
-
291
-#endif // __ADDRESS_H__

+ 133
- 150
Marlin/src/sd/usb_flashdrive/lib/confdescparser.h View File

@@ -22,197 +22,180 @@
22 22
  * Web      :  http://www.circuitsathome.com
23 23
  * e-mail   :  support@circuitsathome.com
24 24
  */
25
-#if !defined(_usb_h_) || defined(__CONFDESCPARSER_H__)
26
-#error "Never include confdescparser.h directly; include Usb.h instead"
27
-#else
25
+#pragma once
28 26
 
29
-#define __CONFDESCPARSER_H__
27
+#ifndef _usb_h_
28
+  #error "Never include confdescparser.h directly; include Usb.h instead"
29
+#endif
30 30
 
31 31
 class UsbConfigXtracter {
32 32
 public:
33
-        //virtual void ConfigXtract(const USB_CONFIGURATION_DESCRIPTOR *conf) = 0;
34
-        //virtual void InterfaceXtract(uint8_t conf, const USB_INTERFACE_DESCRIPTOR *iface) = 0;
33
+  //virtual void ConfigXtract(const USB_CONFIGURATION_DESCRIPTOR *conf) = 0;
34
+  //virtual void InterfaceXtract(uint8_t conf, const USB_INTERFACE_DESCRIPTOR *iface) = 0;
35 35
 
36
-        virtual void EndpointXtract(uint8_t conf __attribute__((unused)), uint8_t iface __attribute__((unused)), uint8_t alt __attribute__((unused)), uint8_t proto __attribute__((unused)), const USB_ENDPOINT_DESCRIPTOR *ep __attribute__((unused))) {
37
-        };
36
+  virtual void EndpointXtract(uint8_t conf __attribute__((unused)), uint8_t iface __attribute__((unused)), uint8_t alt __attribute__((unused)), uint8_t proto __attribute__((unused)), const USB_ENDPOINT_DESCRIPTOR *ep __attribute__((unused))) {
37
+  }
38 38
 };
39 39
 
40
-#define CP_MASK_COMPARE_CLASS                   1
41
-#define CP_MASK_COMPARE_SUBCLASS                2
42
-#define CP_MASK_COMPARE_PROTOCOL                4
43
-#define CP_MASK_COMPARE_ALL                     7
40
+#define CP_MASK_COMPARE_CLASS     1
41
+#define CP_MASK_COMPARE_SUBCLASS  2
42
+#define CP_MASK_COMPARE_PROTOCOL  4
43
+#define CP_MASK_COMPARE_ALL       7
44 44
 
45 45
 // Configuration Descriptor Parser Class Template
46 46
 
47 47
 template <const uint8_t CLASS_ID, const uint8_t SUBCLASS_ID, const uint8_t PROTOCOL_ID, const uint8_t MASK>
48 48
 class ConfigDescParser : public USBReadParser {
49
-        UsbConfigXtracter *theXtractor;
50
-        MultiValueBuffer theBuffer;
51
-        MultiByteValueParser valParser;
52
-        ByteSkipper theSkipper;
53
-        uint8_t varBuffer[16 /*sizeof(USB_CONFIGURATION_DESCRIPTOR)*/];
49
+  UsbConfigXtracter *theXtractor;
50
+  MultiValueBuffer theBuffer;
51
+  MultiByteValueParser valParser;
52
+  ByteSkipper theSkipper;
53
+  uint8_t varBuffer[16 /*sizeof(USB_CONFIGURATION_DESCRIPTOR)*/];
54 54
 
55
-        uint8_t stateParseDescr; // ParseDescriptor state
55
+  uint8_t stateParseDescr; // ParseDescriptor state
56 56
 
57
-        uint8_t dscrLen; // Descriptor length
58
-        uint8_t dscrType; // Descriptor type
57
+  uint8_t dscrLen; // Descriptor length
58
+  uint8_t dscrType; // Descriptor type
59 59
 
60
-        bool isGoodInterface; // Apropriate interface flag
61
-        uint8_t confValue; // Configuration value
62
-        uint8_t protoValue; // Protocol value
63
-        uint8_t ifaceNumber; // Interface number
64
-        uint8_t ifaceAltSet; // Interface alternate settings
60
+  bool isGoodInterface; // Apropriate interface flag
61
+  uint8_t confValue; // Configuration value
62
+  uint8_t protoValue; // Protocol value
63
+  uint8_t ifaceNumber; // Interface number
64
+  uint8_t ifaceAltSet; // Interface alternate settings
65 65
 
66
-        bool UseOr;
67
-        bool ParseDescriptor(uint8_t **pp, uint16_t *pcntdn);
68
-        void PrintHidDescriptor(const USB_HID_DESCRIPTOR *pDesc);
66
+  bool UseOr;
67
+  bool ParseDescriptor(uint8_t **pp, uint16_t *pcntdn);
68
+  void PrintHidDescriptor(const USB_HID_DESCRIPTOR *pDesc);
69 69
 
70 70
 public:
71 71
 
72
-        void SetOR(void) {
73
-                UseOr = true;
74
-        }
75
-        ConfigDescParser(UsbConfigXtracter *xtractor);
76
-        void Parse(const uint16_t len, const uint8_t *pbuf, const uint16_t &offset);
72
+  void SetOR(void) { UseOr = true; }
73
+  ConfigDescParser(UsbConfigXtracter *xtractor);
74
+  void Parse(const uint16_t len, const uint8_t *pbuf, const uint16_t &offset);
77 75
 };
78 76
 
79 77
 template <const uint8_t CLASS_ID, const uint8_t SUBCLASS_ID, const uint8_t PROTOCOL_ID, const uint8_t MASK>
80 78
 ConfigDescParser<CLASS_ID, SUBCLASS_ID, PROTOCOL_ID, MASK>::ConfigDescParser(UsbConfigXtracter *xtractor) :
81
-theXtractor(xtractor),
82
-stateParseDescr(0),
83
-dscrLen(0),
84
-dscrType(0),
85
-UseOr(false) {
86
-        theBuffer.pValue = varBuffer;
87
-        valParser.Initialize(&theBuffer);
88
-        theSkipper.Initialize(&theBuffer);
89
-};
79
+  theXtractor(xtractor),
80
+  stateParseDescr(0),
81
+  dscrLen(0),
82
+  dscrType(0),
83
+  UseOr(false) {
84
+    theBuffer.pValue = varBuffer;
85
+    valParser.Initialize(&theBuffer);
86
+    theSkipper.Initialize(&theBuffer);
87
+  };
90 88
 
91 89
 template <const uint8_t CLASS_ID, const uint8_t SUBCLASS_ID, const uint8_t PROTOCOL_ID, const uint8_t MASK>
92 90
 void ConfigDescParser<CLASS_ID, SUBCLASS_ID, PROTOCOL_ID, MASK>::Parse(const uint16_t len, const uint8_t *pbuf, const uint16_t &offset __attribute__((unused))) {
93
-        uint16_t cntdn = (uint16_t)len;
94
-        uint8_t *p = (uint8_t*)pbuf;
95
-
96
-        while(cntdn)
97
-                if(!ParseDescriptor(&p, &cntdn))
98
-                        return;
91
+  uint16_t cntdn = (uint16_t)len;
92
+  uint8_t *p = (uint8_t*)pbuf;
93
+  while (cntdn) if (!ParseDescriptor(&p, &cntdn)) return;
99 94
 }
100 95
 
101 96
 /* Parser for the configuration descriptor. Takes values for class, subclass, protocol fields in interface descriptor and
102 97
   compare masks for them. When the match is found, calls EndpointXtract passing buffer containing endpoint descriptor */
103 98
 template <const uint8_t CLASS_ID, const uint8_t SUBCLASS_ID, const uint8_t PROTOCOL_ID, const uint8_t MASK>
104 99
 bool ConfigDescParser<CLASS_ID, SUBCLASS_ID, PROTOCOL_ID, MASK>::ParseDescriptor(uint8_t **pp, uint16_t *pcntdn) {
105
-        USB_CONFIGURATION_DESCRIPTOR* ucd = reinterpret_cast<USB_CONFIGURATION_DESCRIPTOR*>(varBuffer);
106
-        USB_INTERFACE_DESCRIPTOR* uid = reinterpret_cast<USB_INTERFACE_DESCRIPTOR*>(varBuffer);
107
-        switch(stateParseDescr) {
108
-                case 0:
109
-                        theBuffer.valueSize = 2;
110
-                        valParser.Initialize(&theBuffer);
111
-                        stateParseDescr = 1;
112
-                case 1:
113
-                        if(!valParser.Parse(pp, pcntdn))
114
-                                return false;
115
-                        dscrLen = *((uint8_t*)theBuffer.pValue);
116
-                        dscrType = *((uint8_t*)theBuffer.pValue + 1);
117
-                        stateParseDescr = 2;
118
-                case 2:
119
-                        // This is a sort of hack. Assuming that two bytes are all ready in the buffer
120
-                        //      the pointer is positioned two bytes ahead in order for the rest of descriptor
121
-                        //      to be read right after the size and the type fields.
122
-                        // This should be used carefully. varBuffer should be used directly to handle data
123
-                        //      in the buffer.
124
-                        theBuffer.pValue = varBuffer + 2;
125
-                        stateParseDescr = 3;
126
-                case 3:
127
-                        switch(dscrType) {
128
-                                case USB_DESCRIPTOR_INTERFACE:
129
-                                        isGoodInterface = false;
130
-                                        break;
131
-                                case USB_DESCRIPTOR_CONFIGURATION:
132
-                                case USB_DESCRIPTOR_ENDPOINT:
133
-                                case HID_DESCRIPTOR_HID:
134
-                                        break;
135
-                        }
136
-                        theBuffer.valueSize = dscrLen - 2;
137
-                        valParser.Initialize(&theBuffer);
138
-                        stateParseDescr = 4;
139
-                case 4:
140
-                        switch(dscrType) {
141
-                                case USB_DESCRIPTOR_CONFIGURATION:
142
-                                        if(!valParser.Parse(pp, pcntdn))
143
-                                                return false;
144
-                                        confValue = ucd->bConfigurationValue;
145
-                                        break;
146
-                                case USB_DESCRIPTOR_INTERFACE:
147
-                                        if(!valParser.Parse(pp, pcntdn))
148
-                                                return false;
149
-                                        if((MASK & CP_MASK_COMPARE_CLASS) && uid->bInterfaceClass != CLASS_ID)
150
-                                                break;
151
-                                        if((MASK & CP_MASK_COMPARE_SUBCLASS) && uid->bInterfaceSubClass != SUBCLASS_ID)
152
-                                                break;
153
-                                        if(UseOr) {
154
-                                                if((!((MASK & CP_MASK_COMPARE_PROTOCOL) && uid->bInterfaceProtocol)))
155
-                                                        break;
156
-                                        } else {
157
-                                                if((MASK & CP_MASK_COMPARE_PROTOCOL) && uid->bInterfaceProtocol != PROTOCOL_ID)
158
-                                                        break;
159
-                                        }
160
-                                        isGoodInterface = true;
161
-                                        ifaceNumber = uid->bInterfaceNumber;
162
-                                        ifaceAltSet = uid->bAlternateSetting;
163
-                                        protoValue = uid->bInterfaceProtocol;
164
-                                        break;
165
-                                case USB_DESCRIPTOR_ENDPOINT:
166
-                                        if(!valParser.Parse(pp, pcntdn))
167
-                                                return false;
168
-                                        if(isGoodInterface)
169
-                                                if(theXtractor)
170
-                                                        theXtractor->EndpointXtract(confValue, ifaceNumber, ifaceAltSet, protoValue, (USB_ENDPOINT_DESCRIPTOR*)varBuffer);
171
-                                        break;
172
-                                        //case HID_DESCRIPTOR_HID:
173
-                                        //      if (!valParser.Parse(pp, pcntdn))
174
-                                        //              return false;
175
-                                        //      PrintHidDescriptor((const USB_HID_DESCRIPTOR*)varBuffer);
176
-                                        //      break;
177
-                                default:
178
-                                        if(!theSkipper.Skip(pp, pcntdn, dscrLen - 2))
179
-                                                return false;
180
-                        }
181
-                        theBuffer.pValue = varBuffer;
182
-                        stateParseDescr = 0;
183
-        }
184
-        return true;
100
+  USB_CONFIGURATION_DESCRIPTOR* ucd = reinterpret_cast<USB_CONFIGURATION_DESCRIPTOR*>(varBuffer);
101
+  USB_INTERFACE_DESCRIPTOR* uid = reinterpret_cast<USB_INTERFACE_DESCRIPTOR*>(varBuffer);
102
+  switch (stateParseDescr) {
103
+    case 0:
104
+      theBuffer.valueSize = 2;
105
+      valParser.Initialize(&theBuffer);
106
+      stateParseDescr = 1;
107
+    case 1:
108
+      if (!valParser.Parse(pp, pcntdn)) return false;
109
+      dscrLen = *((uint8_t*)theBuffer.pValue);
110
+      dscrType = *((uint8_t*)theBuffer.pValue + 1);
111
+      stateParseDescr = 2;
112
+    case 2:
113
+      // This is a sort of hack. Assuming that two bytes are all ready in the buffer
114
+      //      the pointer is positioned two bytes ahead in order for the rest of descriptor
115
+      //      to be read right after the size and the type fields.
116
+      // This should be used carefully. varBuffer should be used directly to handle data
117
+      //      in the buffer.
118
+      theBuffer.pValue = varBuffer + 2;
119
+      stateParseDescr = 3;
120
+    case 3:
121
+      switch (dscrType) {
122
+        case USB_DESCRIPTOR_INTERFACE:
123
+          isGoodInterface = false;
124
+          break;
125
+        case USB_DESCRIPTOR_CONFIGURATION:
126
+        case USB_DESCRIPTOR_ENDPOINT:
127
+        case HID_DESCRIPTOR_HID:
128
+          break;
129
+      }
130
+      theBuffer.valueSize = dscrLen - 2;
131
+      valParser.Initialize(&theBuffer);
132
+      stateParseDescr = 4;
133
+    case 4:
134
+      switch (dscrType) {
135
+        case USB_DESCRIPTOR_CONFIGURATION:
136
+          if (!valParser.Parse(pp, pcntdn)) return false;
137
+          confValue = ucd->bConfigurationValue;
138
+          break;
139
+        case USB_DESCRIPTOR_INTERFACE:
140
+          if (!valParser.Parse(pp, pcntdn)) return false;
141
+          if ((MASK & CP_MASK_COMPARE_CLASS) && uid->bInterfaceClass != CLASS_ID)
142
+            break;
143
+          if ((MASK & CP_MASK_COMPARE_SUBCLASS) && uid->bInterfaceSubClass != SUBCLASS_ID)
144
+            break;
145
+          if (UseOr) {
146
+            if ((!((MASK & CP_MASK_COMPARE_PROTOCOL) && uid->bInterfaceProtocol))) break;
147
+          }
148
+          else if ((MASK & CP_MASK_COMPARE_PROTOCOL) && uid->bInterfaceProtocol != PROTOCOL_ID)
149
+            break;
150
+          isGoodInterface = true;
151
+          ifaceNumber = uid->bInterfaceNumber;
152
+          ifaceAltSet = uid->bAlternateSetting;
153
+          protoValue = uid->bInterfaceProtocol;
154
+          break;
155
+        case USB_DESCRIPTOR_ENDPOINT:
156
+          if (!valParser.Parse(pp, pcntdn)) return false;
157
+          if (isGoodInterface && theXtractor)
158
+            theXtractor->EndpointXtract(confValue, ifaceNumber, ifaceAltSet, protoValue, (USB_ENDPOINT_DESCRIPTOR*)varBuffer);
159
+          break;
160
+          //case HID_DESCRIPTOR_HID:
161
+          //  if (!valParser.Parse(pp, pcntdn)) return false;
162
+          //  PrintHidDescriptor((const USB_HID_DESCRIPTOR*)varBuffer);
163
+          //  break;
164
+        default:
165
+          if (!theSkipper.Skip(pp, pcntdn, dscrLen - 2)) return false;
166
+      }
167
+      theBuffer.pValue = varBuffer;
168
+      stateParseDescr = 0;
169
+  }
170
+  return true;
185 171
 }
186 172
 
187 173
 template <const uint8_t CLASS_ID, const uint8_t SUBCLASS_ID, const uint8_t PROTOCOL_ID, const uint8_t MASK>
188 174
 void ConfigDescParser<CLASS_ID, SUBCLASS_ID, PROTOCOL_ID, MASK>::PrintHidDescriptor(const USB_HID_DESCRIPTOR *pDesc) {
189
-        Notify(PSTR("\r\n\r\nHID Descriptor:\r\n"), 0x80);
190
-        Notify(PSTR("bDescLength:\t\t"), 0x80);
191
-        PrintHex<uint8_t > (pDesc->bLength, 0x80);
175
+  Notify(PSTR("\r\n\r\nHID Descriptor:\r\n"), 0x80);
176
+  Notify(PSTR("bDescLength:\t\t"), 0x80);
177
+  PrintHex<uint8_t > (pDesc->bLength, 0x80);
192 178
 
193
-        Notify(PSTR("\r\nbDescriptorType:\t"), 0x80);
194
-        PrintHex<uint8_t > (pDesc->bDescriptorType, 0x80);
179
+  Notify(PSTR("\r\nbDescriptorType:\t"), 0x80);
180
+  PrintHex<uint8_t > (pDesc->bDescriptorType, 0x80);
195 181
 
196
-        Notify(PSTR("\r\nbcdHID:\t\t\t"), 0x80);
197
-        PrintHex<uint16_t > (pDesc->bcdHID, 0x80);
182
+  Notify(PSTR("\r\nbcdHID:\t\t\t"), 0x80);
183
+  PrintHex<uint16_t > (pDesc->bcdHID, 0x80);
198 184
 
199
-        Notify(PSTR("\r\nbCountryCode:\t\t"), 0x80);
200
-        PrintHex<uint8_t > (pDesc->bCountryCode, 0x80);
185
+  Notify(PSTR("\r\nbCountryCode:\t\t"), 0x80);
186
+  PrintHex<uint8_t > (pDesc->bCountryCode, 0x80);
201 187
 
202
-        Notify(PSTR("\r\nbNumDescriptors:\t"), 0x80);
203
-        PrintHex<uint8_t > (pDesc->bNumDescriptors, 0x80);
188
+  Notify(PSTR("\r\nbNumDescriptors:\t"), 0x80);
189
+  PrintHex<uint8_t > (pDesc->bNumDescriptors, 0x80);
204 190
 
205
-        for(uint8_t i = 0; i < pDesc->bNumDescriptors; i++) {
206
-                HID_CLASS_DESCRIPTOR_LEN_AND_TYPE *pLT = (HID_CLASS_DESCRIPTOR_LEN_AND_TYPE*)&(pDesc->bDescrType);
191
+  for (uint8_t i = 0; i < pDesc->bNumDescriptors; i++) {
192
+    HID_CLASS_DESCRIPTOR_LEN_AND_TYPE *pLT = (HID_CLASS_DESCRIPTOR_LEN_AND_TYPE*)&(pDesc->bDescrType);
207 193
 
208
-                Notify(PSTR("\r\nbDescrType:\t\t"), 0x80);
209
-                PrintHex<uint8_t > (pLT[i].bDescrType, 0x80);
194
+    Notify(PSTR("\r\nbDescrType:\t\t"), 0x80);
195
+    PrintHex<uint8_t > (pLT[i].bDescrType, 0x80);
210 196
 
211
-                Notify(PSTR("\r\nwDescriptorLength:\t"), 0x80);
212
-                PrintHex<uint16_t > (pLT[i].wDescriptorLength, 0x80);
213
-        }
214
-        Notify(PSTR("\r\n"), 0x80);
197
+    Notify(PSTR("\r\nwDescriptorLength:\t"), 0x80);
198
+    PrintHex<uint16_t > (pLT[i].wDescriptorLength, 0x80);
199
+  }
200
+  Notify(PSTR("\r\n"), 0x80);
215 201
 }
216
-
217
-
218
-#endif // __CONFDESCPARSER_H__

+ 27
- 29
Marlin/src/sd/usb_flashdrive/lib/hexdump.h View File

@@ -22,49 +22,47 @@
22 22
  * Web      :  http://www.circuitsathome.com
23 23
  * e-mail   :  support@circuitsathome.com
24 24
  */
25
+#pragma once
25 26
 
26
-#if !defined(_usb_h_) || defined(__HEXDUMP_H__)
27
-#error "Never include hexdump.h directly; include Usb.h instead"
28
-#else
29
-#define __HEXDUMP_H__
27
+#ifndef _usb_h_
28
+  #error "Never include hexdump.h directly; include Usb.h instead"
29
+#endif
30 30
 
31 31
 extern int UsbDEBUGlvl;
32 32
 
33 33
 template <class BASE_CLASS, class LEN_TYPE, class OFFSET_TYPE>
34 34
 class HexDumper : public BASE_CLASS {
35
-        uint8_t byteCount;
36
-        OFFSET_TYPE byteTotal;
35
+  uint8_t byteCount;
36
+  OFFSET_TYPE byteTotal;
37 37
 
38 38
 public:
39 39
 
40
-        HexDumper() : byteCount(0), byteTotal(0) {
41
-        };
40
+  HexDumper() : byteCount(0), byteTotal(0) {
41
+  };
42 42
 
43
-        void Initialize() {
44
-                byteCount = 0;
45
-                byteTotal = 0;
46
-        };
43
+  void Initialize() {
44
+    byteCount = 0;
45
+    byteTotal = 0;
46
+  };
47 47
 
48
-        void Parse(const LEN_TYPE len, const uint8_t *pbuf, const OFFSET_TYPE &offset);
48
+  void Parse(const LEN_TYPE len, const uint8_t *pbuf, const OFFSET_TYPE &offset);
49 49
 };
50 50
 
51 51
 template <class BASE_CLASS, class LEN_TYPE, class OFFSET_TYPE>
52 52
 void HexDumper<BASE_CLASS, LEN_TYPE, OFFSET_TYPE>::Parse(const LEN_TYPE len, const uint8_t *pbuf, const OFFSET_TYPE &offset __attribute__((unused))) {
53
-        if(UsbDEBUGlvl >= 0x80) { // Fully bypass this block of code if we do not debug.
54
-                for(LEN_TYPE j = 0; j < len; j++, byteCount++, byteTotal++) {
55
-                        if(!byteCount) {
56
-                                PrintHex<OFFSET_TYPE > (byteTotal, 0x80);
57
-                                E_Notify(PSTR(": "), 0x80);
58
-                        }
59
-                        PrintHex<uint8_t > (pbuf[j], 0x80);
60
-                        E_Notify(PSTR(" "), 0x80);
53
+  if (UsbDEBUGlvl >= 0x80) { // Fully bypass this block of code if we do not debug.
54
+    for (LEN_TYPE j = 0; j < len; j++, byteCount++, byteTotal++) {
55
+      if (!byteCount) {
56
+        PrintHex<OFFSET_TYPE > (byteTotal, 0x80);
57
+        E_Notify(PSTR(": "), 0x80);
58
+      }
59
+      PrintHex<uint8_t > (pbuf[j], 0x80);
60
+      E_Notify(PSTR(" "), 0x80);
61 61
 
62
-                        if(byteCount == 15) {
63
-                                E_Notify(PSTR("\r\n"), 0x80);
64
-                                byteCount = 0xFF;
65
-                        }
66
-                }
67
-        }
62
+      if (byteCount == 15) {
63
+        E_Notify(PSTR("\r\n"), 0x80);
64
+        byteCount = 0xFF;
65
+      }
66
+    }
67
+  }
68 68
 }
69
-
70
-#endif // __HEXDUMP_H__

+ 2
- 3
Marlin/src/sd/usb_flashdrive/lib/macros.h View File

@@ -22,13 +22,12 @@
22 22
  * Web      :  http://www.circuitsathome.com
23 23
  * e-mail   :  support@circuitsathome.com
24 24
  */
25
+#pragma once
25 26
 
26 27
 #ifndef _usb_h_
27 28
   #error "Never include macros.h directly; include Usb.h instead"
28 29
 #endif
29 30
 
30
-#pragma once
31
-
32 31
 ////////////////////////////////////////////////////////////////////////////////
33 32
 // HANDY MACROS
34 33
 ////////////////////////////////////////////////////////////////////////////////
@@ -36,7 +35,7 @@
36 35
 #define VALUE_BETWEEN(v,l,h) (((v)>(l)) && ((v)<(h)))
37 36
 #define VALUE_WITHIN(v,l,h) (((v)>=(l)) && ((v)<=(h)))
38 37
 #define output_pgm_message(wa,fp,mp,el) wa = &mp, fp((char *)pgm_read_pointer(wa), el)
39
-#define output_if_between(v,l,h,wa,fp,mp,el) if(VALUE_BETWEEN(v,l,h)) output_pgm_message(wa,fp,mp[v-(l+1)],el);
38
+#define output_if_between(v,l,h,wa,fp,mp,el) if (VALUE_BETWEEN(v,l,h)) output_pgm_message(wa,fp,mp[v-(l+1)],el);
40 39
 
41 40
 #define SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))
42 41
 #ifndef __BYTE_GRABBING_DEFINED__

+ 683
- 729
Marlin/src/sd/usb_flashdrive/lib/masstorage.cpp
File diff suppressed because it is too large
View File


+ 8
- 23
Marlin/src/sd/usb_flashdrive/lib/masstorage.h View File

@@ -408,7 +408,7 @@ public:
408 408
 
409 409
   CommandBlockWrapper() :
410 410
   CommandBlockWrapperBase(0, 0, 0), bmReserved1(0), bmReserved2(0) {
411
-    for(int i = 0; i < 16; i++) CBWCB[i] = 0;
411
+    for (int i = 0; i < 16; i++) CBWCB[i] = 0;
412 412
   }
413 413
 
414 414
   // Generic Wrap, CDB zeroed.
@@ -416,7 +416,7 @@ public:
416 416
   CommandBlockWrapper(uint32_t tag, uint32_t xflen, uint8_t flgs, uint8_t lu, uint8_t cmdlen, uint8_t cmd) :
417 417
   CommandBlockWrapperBase(tag, xflen, flgs),
418 418
   bmCBWLUN(lu), bmReserved1(0), bmCBWCBLength(cmdlen), bmReserved2(0) {
419
-    for(int i = 0; i < 16; i++) CBWCB[i] = 0;
419
+    for (int i = 0; i < 16; i++) CBWCB[i] = 0;
420 420
     // Type punning can cause optimization problems and bugs.
421 421
     // Using reinterpret_cast to a dreinterpretifferent object is the proper way to do this.
422 422
     //(((BASICCDB_t *) CBWCB)->LUN) = cmd;
@@ -493,27 +493,17 @@ protected:
493 493
   bool WriteOk[MASS_MAX_SUPPORTED_LUN];
494 494
   void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr);
495 495
 
496
-
497 496
   // Additional Initialization Method for Subclasses
498 497
 
499
-  virtual uint8_t OnInit() {
500
-    return 0;
501
-  };
498
+  virtual uint8_t OnInit() { return 0; }
502 499
 
503 500
 public:
504 501
   BulkOnly(USB *p);
505 502
 
506
-  uint8_t GetLastUsbError() {
507
-    return bLastUsbError;
508
-  };
503
+  uint8_t GetLastUsbError() { return bLastUsbError; };
509 504
 
510
-  uint8_t GetbMaxLUN() {
511
-    return bMaxLUN; // Max LUN
512
-  }
513
-
514
-  uint8_t GetbTheLUN() {
515
-    return bTheLUN; // Active LUN
516
-  }
505
+  uint8_t GetbMaxLUN() { return bMaxLUN; } // Max LUN
506
+  uint8_t GetbTheLUN() { return bTheLUN; } // Active LUN
517 507
 
518 508
   bool WriteProtected(uint8_t lun);
519 509
   uint8_t MediaCTL(uint8_t lun, uint8_t ctl);
@@ -533,16 +523,12 @@ public:
533 523
   uint8_t Release();
534 524
   uint8_t Poll();
535 525
 
536
-  virtual uint8_t GetAddress() {
537
-    return bAddress;
538
-  };
526
+  virtual uint8_t GetAddress() { return bAddress; }
539 527
 
540 528
   // UsbConfigXtracter implementation
541 529
   void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
542 530
 
543
-  virtual bool DEVCLASSOK(uint8_t klass) {
544
-    return (klass == USB_CLASS_MASS_STORAGE);
545
-  }
531
+  virtual bool DEVCLASSOK(uint8_t klass) { return klass == USB_CLASS_MASS_STORAGE; }
546 532
 
547 533
   uint8_t SCSITransaction6(CDB6_t *cdb, uint16_t buf_size, void *buf, uint8_t dir);
548 534
   uint8_t SCSITransaction10(CDB10_t *cdb, uint16_t buf_size, void *buf, uint8_t dir);
@@ -573,5 +559,4 @@ private:
573 559
   uint8_t Transaction(CommandBlockWrapper *cbw, uint16_t bsize, void *buf);
574 560
   uint8_t HandleUsbError(uint8_t error, uint8_t index);
575 561
   uint8_t HandleSCSIError(uint8_t status);
576
-
577 562
 };

+ 4
- 7
Marlin/src/sd/usb_flashdrive/lib/max3421e.h View File

@@ -22,11 +22,11 @@
22 22
  * Web      :  http://www.circuitsathome.com
23 23
  * e-mail   :  support@circuitsathome.com
24 24
  */
25
-#if !defined(_usb_h_) || defined(_max3421e_h_)
26
-#error "Never include max3421e.h directly; include Usb.h instead"
27
-#else
25
+#pragma once
28 26
 
29
-#define _max3421e_h_
27
+#ifndef _usb_h_
28
+  #error "Never include max3421e.h directly; include Usb.h instead"
29
+#endif
30 30
 
31 31
 /* MAX3421E register/bit names and bitmasks */
32 32
 
@@ -231,6 +231,3 @@
231 231
 
232 232
 #define MODE_FS_HOST    (bmDPPULLDN|bmDMPULLDN|bmHOST|bmSOFKAENAB)
233 233
 #define MODE_LS_HOST    (bmDPPULLDN|bmDMPULLDN|bmHOST|bmLOWSPEED|bmSOFKAENAB)
234
-
235
-
236
-#endif //_max3421e_h_

+ 62
- 65
Marlin/src/sd/usb_flashdrive/lib/message.cpp View File

@@ -35,97 +35,94 @@
35 35
 int UsbDEBUGlvl = 0x80;
36 36
 
37 37
 void E_Notifyc(char c, int lvl) {
38
-        if(UsbDEBUGlvl < lvl) return;
39
-#if defined(ARDUINO) && ARDUINO >=100
40
-        USB_HOST_SERIAL.print(c);
41
-#else
42
-        USB_HOST_SERIAL.print(c, BYTE);
43
-#endif
44
-        //USB_HOST_SERIAL.flush();
38
+  if (UsbDEBUGlvl < lvl) return;
39
+  USB_HOST_SERIAL.print(c
40
+    #if !defined(ARDUINO) || ARDUINO < 100
41
+      , BYTE
42
+    #endif
43
+  );
44
+  //USB_HOST_SERIAL.flush();
45 45
 }
46 46
 
47 47
 void E_Notify(char const * msg, int lvl) {
48
-        if(UsbDEBUGlvl < lvl) return;
49
-        if(!msg) return;
50
-        char c;
51
-
52
-        while((c = pgm_read_byte(msg++))) E_Notifyc(c, lvl);
48
+  if (UsbDEBUGlvl < lvl) return;
49
+  if (!msg) return;
50
+  while (const char c = pgm_read_byte(msg++)) E_Notifyc(c, lvl);
53 51
 }
54 52
 
55 53
 void E_NotifyStr(char const * msg, int lvl) {
56
-        if(UsbDEBUGlvl < lvl) return;
57
-        if(!msg) return;
58
-        char c;
59
-
60
-        while((c = *msg++)) E_Notifyc(c, lvl);
54
+  if (UsbDEBUGlvl < lvl) return;
55
+  if (!msg) return;
56
+  while (const char c = *msg++) E_Notifyc(c, lvl);
61 57
 }
62 58
 
63 59
 void E_Notify(uint8_t b, int lvl) {
64
-        if(UsbDEBUGlvl < lvl) return;
65
-#if defined(ARDUINO) && ARDUINO >=100
66
-        USB_HOST_SERIAL.print(b);
67
-#else
68
-        USB_HOST_SERIAL.print(b, DEC);
69
-#endif
70
-        //USB_HOST_SERIAL.flush();
60
+  if (UsbDEBUGlvl < lvl) return;
61
+  USB_HOST_SERIAL.print(b
62
+    #if !defined(ARDUINO) || ARDUINO < 100
63
+      , DEC
64
+    #endif
65
+  );
66
+  //USB_HOST_SERIAL.flush();
71 67
 }
72 68
 
73 69
 void E_Notify(double d, int lvl) {
74
-        if(UsbDEBUGlvl < lvl) return;
75
-        USB_HOST_SERIAL.print(d);
76
-        //USB_HOST_SERIAL.flush();
70
+  if (UsbDEBUGlvl < lvl) return;
71
+  USB_HOST_SERIAL.print(d);
72
+  //USB_HOST_SERIAL.flush();
77 73
 }
78 74
 
79 75
 #ifdef DEBUG_USB_HOST
80 76
 
81
-void NotifyFailGetDevDescr(void) {
82
-        Notify(PSTR("\r\ngetDevDescr "), 0x80);
83
-}
77
+  void NotifyFailGetDevDescr(void) {
78
+    Notify(PSTR("\r\ngetDevDescr "), 0x80);
79
+  }
84 80
 
85
-void NotifyFailSetDevTblEntry(void) {
86
-        Notify(PSTR("\r\nsetDevTblEn "), 0x80);
87
-}
81
+  void NotifyFailSetDevTblEntry(void) {
82
+    Notify(PSTR("\r\nsetDevTblEn "), 0x80);
83
+  }
88 84
 
89
-void NotifyFailGetConfDescr(void) {
90
-        Notify(PSTR("\r\ngetConf "), 0x80);
91
-}
85
+  void NotifyFailGetConfDescr(void) {
86
+    Notify(PSTR("\r\ngetConf "), 0x80);
87
+  }
92 88
 
93
-void NotifyFailSetConfDescr(void) {
94
-        Notify(PSTR("\r\nsetConf "), 0x80);
95
-}
89
+  void NotifyFailSetConfDescr(void) {
90
+    Notify(PSTR("\r\nsetConf "), 0x80);
91
+  }
96 92
 
97
-void NotifyFailGetDevDescr(uint8_t reason) {
98
-        NotifyFailGetDevDescr();
99
-        NotifyFail(reason);
100
-}
93
+  void NotifyFailGetDevDescr(uint8_t reason) {
94
+    NotifyFailGetDevDescr();
95
+    NotifyFail(reason);
96
+  }
101 97
 
102
-void NotifyFailSetDevTblEntry(uint8_t reason) {
103
-        NotifyFailSetDevTblEntry();
104
-        NotifyFail(reason);
98
+  void NotifyFailSetDevTblEntry(uint8_t reason) {
99
+    NotifyFailSetDevTblEntry();
100
+    NotifyFail(reason);
105 101
 
106
-}
102
+  }
107 103
 
108
-void NotifyFailGetConfDescr(uint8_t reason) {
109
-        NotifyFailGetConfDescr();
110
-        NotifyFail(reason);
111
-}
104
+  void NotifyFailGetConfDescr(uint8_t reason) {
105
+    NotifyFailGetConfDescr();
106
+    NotifyFail(reason);
107
+  }
112 108
 
113
-void NotifyFailSetConfDescr(uint8_t reason) {
114
-        NotifyFailSetConfDescr();
115
-        NotifyFail(reason);
116
-}
109
+  void NotifyFailSetConfDescr(uint8_t reason) {
110
+    NotifyFailSetConfDescr();
111
+    NotifyFail(reason);
112
+  }
117 113
 
118
-void NotifyFailUnknownDevice(uint16_t VID, uint16_t PID) {
119
-        Notify(PSTR("\r\nUnknown Device Connected - VID: "), 0x80);
120
-        D_PrintHex<uint16_t > (VID, 0x80);
121
-        Notify(PSTR(" PID: "), 0x80);
122
-        D_PrintHex<uint16_t > (PID, 0x80);
123
-}
114
+  void NotifyFailUnknownDevice(uint16_t VID, uint16_t PID) {
115
+    Notify(PSTR("\r\nUnknown Device Connected - VID: "), 0x80);
116
+    D_PrintHex<uint16_t > (VID, 0x80);
117
+    Notify(PSTR(" PID: "), 0x80);
118
+    D_PrintHex<uint16_t > (PID, 0x80);
119
+  }
120
+
121
+  void NotifyFail(uint8_t rcode) {
122
+    D_PrintHex<uint8_t > (rcode, 0x80);
123
+    Notify(PSTR("\r\n"), 0x80);
124
+  }
124 125
 
125
-void NotifyFail(uint8_t rcode) {
126
-        D_PrintHex<uint8_t > (rcode, 0x80);
127
-        Notify(PSTR("\r\n"), 0x80);
128
-}
129 126
 #endif // DEBUG_USB_HOST
130 127
 
131 128
 #endif // USB_FLASH_DRIVE_SUPPORT

+ 42
- 43
Marlin/src/sd/usb_flashdrive/lib/message.h View File

@@ -22,10 +22,11 @@
22 22
  * Web      :  http://www.circuitsathome.com
23 23
  * e-mail   :  support@circuitsathome.com
24 24
  */
25
-#if !defined(_usb_h_) || defined(__MESSAGE_H__)
26
-#error "Never include message.h directly; include Usb.h instead"
27
-#else
28
-#define __MESSAGE_H__
25
+#pragma once
26
+
27
+#ifndef _usb_h_
28
+  #error "Never include message.h directly; include Usb.h instead"
29
+#endif
29 30
 
30 31
 extern int UsbDEBUGlvl;
31 32
 
@@ -35,52 +36,50 @@ void E_NotifyStr(char const * msg, int lvl);
35 36
 void E_Notifyc(char c, int lvl);
36 37
 
37 38
 #ifdef DEBUG_USB_HOST
38
-#define Notify E_Notify
39
-#define NotifyStr E_NotifyStr
40
-#define Notifyc E_Notifyc
41
-void NotifyFailGetDevDescr(uint8_t reason);
42
-void NotifyFailSetDevTblEntry(uint8_t reason);
43
-void NotifyFailGetConfDescr(uint8_t reason);
44
-void NotifyFailSetConfDescr(uint8_t reason);
45
-void NotifyFailGetDevDescr(void);
46
-void NotifyFailSetDevTblEntry(void);
47
-void NotifyFailGetConfDescr(void);
48
-void NotifyFailSetConfDescr(void);
49
-void NotifyFailUnknownDevice(uint16_t VID, uint16_t PID);
50
-void NotifyFail(uint8_t rcode);
39
+  #define Notify E_Notify
40
+  #define NotifyStr E_NotifyStr
41
+  #define Notifyc E_Notifyc
42
+  void NotifyFailGetDevDescr(uint8_t reason);
43
+  void NotifyFailSetDevTblEntry(uint8_t reason);
44
+  void NotifyFailGetConfDescr(uint8_t reason);
45
+  void NotifyFailSetConfDescr(uint8_t reason);
46
+  void NotifyFailGetDevDescr(void);
47
+  void NotifyFailSetDevTblEntry(void);
48
+  void NotifyFailGetConfDescr(void);
49
+  void NotifyFailSetConfDescr(void);
50
+  void NotifyFailUnknownDevice(uint16_t VID, uint16_t PID);
51
+  void NotifyFail(uint8_t rcode);
51 52
 #else
52
-#define Notify(...) ((void)0)
53
-#define NotifyStr(...) ((void)0)
54
-#define Notifyc(...) ((void)0)
55
-#define NotifyFailGetDevDescr(...) ((void)0)
56
-#define NotifyFailSetDevTblEntry(...) ((void)0)
57
-#define NotifyFailGetConfDescr(...) ((void)0)
58
-#define NotifyFailGetDevDescr(...) ((void)0)
59
-#define NotifyFailSetDevTblEntry(...) ((void)0)
60
-#define NotifyFailGetConfDescr(...) ((void)0)
61
-#define NotifyFailSetConfDescr(...) ((void)0)
62
-#define NotifyFailUnknownDevice(...) ((void)0)
63
-#define NotifyFail(...) ((void)0)
53
+  #define Notify(...) ((void)0)
54
+  #define NotifyStr(...) ((void)0)
55
+  #define Notifyc(...) ((void)0)
56
+  #define NotifyFailGetDevDescr(...) ((void)0)
57
+  #define NotifyFailSetDevTblEntry(...) ((void)0)
58
+  #define NotifyFailGetConfDescr(...) ((void)0)
59
+  #define NotifyFailGetDevDescr(...) ((void)0)
60
+  #define NotifyFailSetDevTblEntry(...) ((void)0)
61
+  #define NotifyFailGetConfDescr(...) ((void)0)
62
+  #define NotifyFailSetConfDescr(...) ((void)0)
63
+  #define NotifyFailUnknownDevice(...) ((void)0)
64
+  #define NotifyFail(...) ((void)0)
64 65
 #endif
65 66
 
66 67
 template <class ERROR_TYPE>
67 68
 void ErrorMessage(uint8_t level, char const * msg, ERROR_TYPE rcode = 0) {
68
-#ifdef DEBUG_USB_HOST
69
-        Notify(msg, level);
70
-        Notify(PSTR(": "), level);
71
-        D_PrintHex<ERROR_TYPE > (rcode, level);
72
-        Notify(PSTR("\r\n"), level);
73
-#endif
69
+  #ifdef DEBUG_USB_HOST
70
+    Notify(msg, level);
71
+    Notify(PSTR(": "), level);
72
+    D_PrintHex<ERROR_TYPE > (rcode, level);
73
+    Notify(PSTR("\r\n"), level);
74
+  #endif
74 75
 }
75 76
 
76 77
 template <class ERROR_TYPE>
77 78
 void ErrorMessage(char const * msg __attribute__((unused)), ERROR_TYPE rcode __attribute__((unused)) = 0) {
78
-#ifdef DEBUG_USB_HOST
79
-        Notify(msg, 0x80);
80
-        Notify(PSTR(": "), 0x80);
81
-        D_PrintHex<ERROR_TYPE > (rcode, 0x80);
82
-        Notify(PSTR("\r\n"), 0x80);
83
-#endif
79
+  #ifdef DEBUG_USB_HOST
80
+    Notify(msg, 0x80);
81
+    Notify(PSTR(": "), 0x80);
82
+    D_PrintHex<ERROR_TYPE > (rcode, 0x80);
83
+    Notify(PSTR("\r\n"), 0x80);
84
+  #endif
84 85
 }
85
-
86
-#endif // __MESSAGE_H__

+ 32
- 37
Marlin/src/sd/usb_flashdrive/lib/parsetools.cpp View File

@@ -30,53 +30,48 @@
30 30
 #include "Usb.h"
31 31
 
32 32
 bool MultiByteValueParser::Parse(uint8_t **pp, uint16_t *pcntdn) {
33
-        if(!pBuf) {
34
-                Notify(PSTR("Buffer pointer is NULL!\r\n"), 0x80);
35
-                return false;
36
-        }
37
-        for(; countDown && (*pcntdn); countDown--, (*pcntdn)--, (*pp)++)
38
-                pBuf[valueSize - countDown] = (**pp);
33
+  if (!pBuf) {
34
+    Notify(PSTR("Buffer pointer is NULL!\r\n"), 0x80);
35
+    return false;
36
+  }
37
+  for (; countDown && (*pcntdn); countDown--, (*pcntdn)--, (*pp)++)
38
+    pBuf[valueSize - countDown] = (**pp);
39 39
 
40
-        if(countDown)
41
-                return false;
40
+  if (countDown) return false;
42 41
 
43
-        countDown = valueSize;
44
-        return true;
42
+  countDown = valueSize;
43
+  return true;
45 44
 }
46 45
 
47 46
 bool PTPListParser::Parse(uint8_t **pp, uint16_t *pcntdn, PTP_ARRAY_EL_FUNC pf, const void *me) {
48
-        switch(nStage) {
49
-                case 0:
50
-                        pBuf->valueSize = lenSize;
51
-                        theParser.Initialize(pBuf);
52
-                        nStage = 1;
47
+  switch (nStage) {
48
+    case 0:
49
+      pBuf->valueSize = lenSize;
50
+      theParser.Initialize(pBuf);
51
+      nStage = 1;
53 52
 
54
-                case 1:
55
-                        if(!theParser.Parse(pp, pcntdn))
56
-                                return false;
53
+    case 1:
54
+      if (!theParser.Parse(pp, pcntdn)) return false;
57 55
 
58
-                        arLen = 0;
59
-                        arLen = (pBuf->valueSize >= 4) ? *((uint32_t*)pBuf->pValue) : (uint32_t)(*((uint16_t*)pBuf->pValue));
60
-                        arLenCntdn = arLen;
61
-                        nStage = 2;
56
+      arLen = 0;
57
+      arLen = (pBuf->valueSize >= 4) ? *((uint32_t*)pBuf->pValue) : (uint32_t)(*((uint16_t*)pBuf->pValue));
58
+      arLenCntdn = arLen;
59
+      nStage = 2;
62 60
 
63
-                case 2:
64
-                        pBuf->valueSize = valSize;
65
-                        theParser.Initialize(pBuf);
66
-                        nStage = 3;
61
+    case 2:
62
+      pBuf->valueSize = valSize;
63
+      theParser.Initialize(pBuf);
64
+      nStage = 3;
67 65
 
68
-                case 3:
69
-                        for(; arLenCntdn; arLenCntdn--) {
70
-                                if(!theParser.Parse(pp, pcntdn))
71
-                                        return false;
66
+    case 3:
67
+      for (; arLenCntdn; arLenCntdn--) {
68
+        if (!theParser.Parse(pp, pcntdn)) return false;
69
+        if (pf) pf(pBuf, (arLen - arLenCntdn), me);
70
+      }
72 71
 
73
-                                if(pf)
74
-                                        pf(pBuf, (arLen - arLenCntdn), me);
75
-                        }
76
-
77
-                        nStage = 0;
78
-        }
79
-        return true;
72
+      nStage = 0;
73
+  }
74
+  return true;
80 75
 }
81 76
 
82 77
 #endif // USB_FLASH_DRIVE_SUPPORT

+ 84
- 87
Marlin/src/sd/usb_flashdrive/lib/parsetools.h View File

@@ -22,67 +22,65 @@
22 22
  * Web      :  http://www.circuitsathome.com
23 23
  * e-mail   :  support@circuitsathome.com
24 24
  */
25
+#pragma once
25 26
 
26
-#if !defined(_usb_h_) || defined(__PARSETOOLS_H__)
27
-#error "Never include parsetools.h directly; include Usb.h instead"
28
-#else
29
-#define __PARSETOOLS_H__
27
+#ifndef _usb_h_
28
+  #error "Never include parsetools.h directly; include Usb.h instead"
29
+#endif
30 30
 
31 31
 struct MultiValueBuffer {
32
-        uint8_t valueSize;
33
-        void *pValue;
32
+  uint8_t valueSize;
33
+  void *pValue;
34 34
 } __attribute__((packed));
35 35
 
36 36
 class MultiByteValueParser {
37
-        uint8_t * pBuf;
38
-        uint8_t countDown;
39
-        uint8_t valueSize;
37
+  uint8_t * pBuf;
38
+  uint8_t countDown;
39
+  uint8_t valueSize;
40 40
 
41 41
 public:
42 42
 
43
-        MultiByteValueParser() : pBuf(NULL), countDown(0), valueSize(0) {
44
-        };
43
+  MultiByteValueParser() : pBuf(NULL), countDown(0), valueSize(0) {
44
+  };
45 45
 
46
-        const uint8_t* GetBuffer() {
47
-                return pBuf;
48
-        };
46
+  const uint8_t* GetBuffer() { return pBuf; }
49 47
 
50
-        void Initialize(MultiValueBuffer * const pbuf) {
51
-                pBuf = (uint8_t*)pbuf->pValue;
52
-                countDown = valueSize = pbuf->valueSize;
53
-        };
48
+  void Initialize(MultiValueBuffer * const pbuf) {
49
+    pBuf = (uint8_t*)pbuf->pValue;
50
+    countDown = valueSize = pbuf->valueSize;
51
+  }
54 52
 
55
-        bool Parse(uint8_t **pp, uint16_t *pcntdn);
53
+  bool Parse(uint8_t **pp, uint16_t *pcntdn);
56 54
 };
57 55
 
58 56
 class ByteSkipper {
59
-        uint8_t *pBuf;
60
-        uint8_t nStage;
61
-        uint16_t countDown;
57
+  uint8_t *pBuf;
58
+  uint8_t nStage;
59
+  uint16_t countDown;
62 60
 
63 61
 public:
64 62
 
65
-        ByteSkipper() : pBuf(NULL), nStage(0), countDown(0) {
66
-        };
67
-
68
-        void Initialize(MultiValueBuffer *pbuf) {
69
-                pBuf = (uint8_t*)pbuf->pValue;
70
-                countDown = 0;
71
-        };
72
-
73
-        bool Skip(uint8_t **pp, uint16_t *pcntdn, uint16_t bytes_to_skip) {
74
-                switch(nStage) {
75
-                        case 0:
76
-                                countDown = bytes_to_skip;
77
-                                nStage++;
78
-                        case 1:
79
-                                for(; countDown && (*pcntdn); countDown--, (*pp)++, (*pcntdn)--);
80
-
81
-                                if(!countDown)
82
-                                        nStage = 0;
83
-                };
84
-                return (!countDown);
85
-        };
63
+  ByteSkipper() : pBuf(NULL), nStage(0), countDown(0) {
64
+  }
65
+
66
+  void Initialize(MultiValueBuffer *pbuf) {
67
+    pBuf = (uint8_t*)pbuf->pValue;
68
+    countDown = 0;
69
+  }
70
+
71
+  bool Skip(uint8_t **pp, uint16_t *pcntdn, uint16_t bytes_to_skip) {
72
+    switch (nStage) {
73
+      case 0:
74
+        countDown = bytes_to_skip;
75
+        nStage++;
76
+      case 1:
77
+        for (; countDown && (*pcntdn); countDown--, (*pp)++, (*pcntdn)--);
78
+
79
+        if (!countDown)
80
+          nStage = 0;
81
+    }
82
+    return (!countDown);
83
+  }
86 84
 };
87 85
 
88 86
 // Pointer to a callback function triggered for each element of PTP array when used with PTPArrayParser
@@ -91,58 +89,57 @@ typedef void (*PTP_ARRAY_EL_FUNC)(const MultiValueBuffer * const p, uint32_t cou
91 89
 class PTPListParser {
92 90
 public:
93 91
 
94
-        enum ParseMode {
95
-                modeArray, modeRange/*, modeEnum*/
96
-        };
92
+  enum ParseMode {
93
+    modeArray, modeRange/*, modeEnum*/
94
+  };
97 95
 
98 96
 private:
99
-        uint8_t nStage;
100
-        uint8_t enStage;
97
+  uint8_t nStage;
98
+  uint8_t enStage;
101 99
 
102
-        uint32_t arLen;
103
-        uint32_t arLenCntdn;
100
+  uint32_t arLen;
101
+  uint32_t arLenCntdn;
104 102
 
105
-        uint8_t lenSize; // size of the array length field in bytes
106
-        uint8_t valSize; // size of the array element in bytes
103
+  uint8_t lenSize; // size of the array length field in bytes
104
+  uint8_t valSize; // size of the array element in bytes
107 105
 
108
-        MultiValueBuffer *pBuf;
106
+  MultiValueBuffer *pBuf;
109 107
 
110
-        // The only parser for both size and array element parsing
111
-        MultiByteValueParser theParser;
108
+  // The only parser for both size and array element parsing
109
+  MultiByteValueParser theParser;
112 110
 
113
-        uint8_t /*ParseMode*/ prsMode;
111
+  uint8_t /*ParseMode*/ prsMode;
114 112
 
115 113
 public:
116 114
 
117
-        PTPListParser() :
118
-        nStage(0),
119
-        enStage(0),
120
-        arLen(0),
121
-        arLenCntdn(0),
122
-        lenSize(0),
123
-        valSize(0),
124
-        pBuf(NULL),
125
-        prsMode(modeArray) {
126
-        };
127
-
128
-        void Initialize(const uint8_t len_size, const uint8_t val_size, MultiValueBuffer * const p, const uint8_t mode = modeArray) {
129
-                pBuf = p;
130
-                lenSize = len_size;
131
-                valSize = val_size;
132
-                prsMode = mode;
133
-
134
-                if(prsMode == modeRange) {
135
-                        arLenCntdn = arLen = 3;
136
-                        nStage = 2;
137
-                } else {
138
-                        arLenCntdn = arLen = 0;
139
-                        nStage = 0;
140
-                }
141
-                enStage = 0;
142
-                theParser.Initialize(p);
143
-        };
144
-
145
-        bool Parse(uint8_t **pp, uint16_t *pcntdn, PTP_ARRAY_EL_FUNC pf, const void *me = NULL);
115
+  PTPListParser() :
116
+    nStage(0),
117
+    enStage(0),
118
+    arLen(0),
119
+    arLenCntdn(0),
120
+    lenSize(0),
121
+    valSize(0),
122
+    pBuf(NULL),
123
+    prsMode(modeArray) { }
124
+  ;
125
+
126
+  void Initialize(const uint8_t len_size, const uint8_t val_size, MultiValueBuffer * const p, const uint8_t mode = modeArray) {
127
+    pBuf = p;
128
+    lenSize = len_size;
129
+    valSize = val_size;
130
+    prsMode = mode;
131
+
132
+    if (prsMode == modeRange) {
133
+      arLenCntdn = arLen = 3;
134
+      nStage = 2;
135
+    }
136
+    else {
137
+      arLenCntdn = arLen = 0;
138
+      nStage = 0;
139
+    }
140
+    enStage = 0;
141
+    theParser.Initialize(p);
142
+  }
143
+
144
+  bool Parse(uint8_t **pp, uint16_t *pcntdn, PTP_ARRAY_EL_FUNC pf, const void *me = NULL);
146 145
 };
147
-
148
-#endif // __PARSETOOLS_H__

+ 30
- 42
Marlin/src/sd/usb_flashdrive/lib/printhex.h View File

@@ -22,71 +22,59 @@
22 22
  * Web      :  http://www.circuitsathome.com
23 23
  * e-mail   :  support@circuitsathome.com
24 24
  */
25
+#pragma once
25 26
 
26
-#if !defined(_usb_h_) || defined(__PRINTHEX_H__)
27
-#error "Never include printhex.h directly; include Usb.h instead"
28
-#else
29
-#define __PRINTHEX_H__
27
+#ifndef _usb_h_
28
+  #error "Never include printhex.h directly; include Usb.h instead"
29
+#endif
30 30
 
31 31
 void E_Notifyc(char c, int lvl);
32 32
 
33 33
 template <class T>
34 34
 void PrintHex(T val, int lvl) {
35
-        int num_nibbles = sizeof (T) * 2;
36
-
37
-        do {
38
-                char v = 48 + (((val >> (num_nibbles - 1) * 4)) & 0x0f);
39
-                if(v > 57) v += 7;
40
-                E_Notifyc(v, lvl);
41
-        } while(--num_nibbles);
35
+  int num_nibbles = sizeof (T) * 2;
36
+  do {
37
+    char v = 48 + (((val >> (num_nibbles - 1) * 4)) & 0x0f);
38
+    if (v > 57) v += 7;
39
+    E_Notifyc(v, lvl);
40
+  } while (--num_nibbles);
42 41
 }
43 42
 
44 43
 template <class T>
45 44
 void PrintBin(T val, int lvl) {
46
-        for(T mask = (((T)1) << ((sizeof (T) << 3) - 1)); mask; mask >>= 1)
47
-                if(val & mask)
48
-                        E_Notifyc('1', lvl);
49
-                else
50
-                        E_Notifyc('0', lvl);
45
+  for (T mask = (((T)1) << ((sizeof (T) << 3) - 1)); mask; mask >>= 1)
46
+    E_Notifyc(val & mask ? '1' : '0', lvl);
51 47
 }
52 48
 
53 49
 template <class T>
54 50
 void SerialPrintHex(T val) {
55
-        int num_nibbles = sizeof (T) * 2;
56
-
57
-        do {
58
-                char v = 48 + (((val >> (num_nibbles - 1) * 4)) & 0x0f);
59
-                if(v > 57) v += 7;
60
-                USB_HOST_SERIAL.print(v);
61
-        } while(--num_nibbles);
51
+  int num_nibbles = sizeof (T) * 2;
52
+  do {
53
+    char v = 48 + (((val >> (num_nibbles - 1) * 4)) & 0x0f);
54
+    if (v > 57) v += 7;
55
+    USB_HOST_SERIAL.print(v);
56
+  } while (--num_nibbles);
62 57
 }
63 58
 
64 59
 template <class T>
65 60
 void PrintHex2(Print *prn, T val) {
66
-        T mask = (((T)1) << (((sizeof (T) << 1) - 1) << 2));
67
-
68
-        while(mask > 1) {
69
-                if(val < mask)
70
-                        prn->print("0");
71
-
72
-                mask >>= 4;
73
-        }
74
-        prn->print((T)val, HEX);
61
+  T mask = (((T)1) << (((sizeof (T) << 1) - 1) << 2));
62
+  while (mask > 1) {
63
+    if (val < mask) prn->print("0");
64
+    mask >>= 4;
65
+  }
66
+  prn->print((T)val, HEX);
75 67
 }
76 68
 
77 69
 template <class T> void D_PrintHex(T val __attribute__((unused)), int lvl __attribute__((unused))) {
78
-#ifdef DEBUG_USB_HOST
79
-        PrintHex<T > (val, lvl);
80
-#endif
70
+  #ifdef DEBUG_USB_HOST
71
+    PrintHex<T > (val, lvl);
72
+  #endif
81 73
 }
82 74
 
83 75
 template <class T>
84 76
 void D_PrintBin(T val, int lvl) {
85
-#ifdef DEBUG_USB_HOST
86
-        PrintBin<T > (val, lvl);
87
-#endif
77
+  #ifdef DEBUG_USB_HOST
78
+    PrintBin<T > (val, lvl);
79
+  #endif
88 80
 }
89
-
90
-
91
-
92
-#endif // __PRINTHEX_H__

+ 1
- 1
Marlin/src/sd/usb_flashdrive/lib/settings.h View File

@@ -35,7 +35,7 @@
35 35
    * flash drives and simple USB hard drives.
36 36
    * Disable this by defining DELAY(x) to be delay(x).
37 37
    */
38
-  #define delay(x)  if((x) < 200) safe_delay(x)
38
+  #define delay(x)  if ((x) < 200) safe_delay(x)
39 39
   /* Almost all USB flash drives and simple USB hard drives fail the write
40 40
    * protect test and add 20 - 30 seconds to USB init.  Set SKIP_WRITE_PROTECT
41 41
    * to nonzero to skip the test and assume the drive is writable.

+ 3
- 7
Marlin/src/sd/usb_flashdrive/lib/usb_ch9.h View File

@@ -23,12 +23,11 @@
23 23
  * e-mail   :  support@circuitsathome.com
24 24
  */
25 25
 
26
-#if !defined(_usb_h_) || defined(_ch9_h_)
27
-#error "Never include usb_ch9.h directly; include Usb.h instead"
28
-#else
26
+#ifndef _usb_h_
27
+  #error "Never include usb_ch9.h directly; include Usb.h instead"
28
+#endif
29 29
 
30 30
 /* USB chapter 9 structures */
31
-#define _ch9_h_
32 31
 
33 32
 /* Misc.USB constants */
34 33
 #define DEV_DESCR_LEN   18      //device descriptor length
@@ -81,7 +80,6 @@
81 80
 #define HID_DESCRIPTOR_HID                      0x21
82 81
 
83 82
 
84
-
85 83
 /* OTG SET FEATURE Constants    */
86 84
 #define OTG_FEATURE_B_HNP_ENABLE                3       // SET FEATURE OTG - Enable B device to perform HNP
87 85
 #define OTG_FEATURE_A_HNP_SUPPORT               4       // SET FEATURE OTG - A device supports HNP
@@ -170,5 +168,3 @@ typedef struct {
170 168
         uint8_t bDescrType; // Type of class descriptor
171 169
         uint16_t wDescriptorLength; // Total size of the Report descriptor
172 170
 } __attribute__((packed)) HID_CLASS_DESCRIPTOR_LEN_AND_TYPE;
173
-
174
-#endif // _ch9_h_

Loading…
Cancel
Save