stlink_target_voltage: Check for libusb error
authorMaxime Coquelin <mcoquelin.stm32@gmail.com>
Thu, 10 Mar 2016 15:39:26 +0000 (16:39 +0100)
committerMaxime Coquelin <mcoquelin.stm32@gmail.com>
Mon, 14 Mar 2016 14:12:44 +0000 (15:12 +0100)
_stlink_usb_target_voltage already returns an error value.
If value return is positive, this is a voltage, if negative this is an error.
Check the return on callers side to inform there is an error in reading the
voltage, instead of notifying of a too low voltage.

Signed-off-by: Maxime Coquelin <mcoquelin.stm32@gmail.com>
src/stlink-common.c

index 68395936253e85bde02fbab3a45ebd8cf97e25a5..204491cf69f12e00dc0800fc6f8d922b59dcd748 100644 (file)
@@ -1609,7 +1609,10 @@ int write_loader_to_sram(stlink_t *sl, stm32_addr_t* addr, size_t* size) {
             sl->chip_id == STM32_CHIPID_F4_LP || sl->chip_id == STM32_CHIPID_F4_HD || (sl->chip_id == STM32_CHIPID_F411RE) ||
             (sl->chip_id == STM32_CHIPID_F446) || (sl->chip_id == STM32_CHIPID_F4_DSI)){
         int voltage = stlink_target_voltage(sl);
-        if (voltage > 2700) {
+        if (voltage == -1) {
+            printf("Failed to read Target voltage\n");
+            return voltage;
+        } else if (voltage > 2700) {
             loader_code = loader_code_stm32f4;
             loader_size = sizeof(loader_code_stm32f4);
         } else {
@@ -1829,7 +1832,10 @@ int stlink_write_flash(stlink_t *sl, stm32_addr_t addr, uint8_t* base, uint32_t
         if (sl->chip_id != STM32_CHIPID_L4) {
             /* set parallelisim to 32 bit*/
             int voltage = stlink_target_voltage(sl);
-            if (voltage > 2700) {
+            if (voltage == -1) {
+                printf("Failed to read Target voltage\n");
+                return voltage;
+            } else if (voltage > 2700) {
                 printf("enabling 32-bit flash writes\n");
                 write_flash_cr_psiz(sl, 2);
             } else {
@@ -1839,7 +1845,10 @@ int stlink_write_flash(stlink_t *sl, stm32_addr_t addr, uint8_t* base, uint32_t
         } else {
             /* L4 does not have a byte-write mode */
             int voltage = stlink_target_voltage(sl);
-            if (voltage < 1710) {
+            if (voltage == -1) {
+                printf("Failed to read Target voltage\n");
+                return voltage;
+            } else if (voltage < 1710) {
                 printf("Target voltage (%d mV) too low for flash writes!\n", voltage);
                 return -1;
             }