//! \brief Erases the currently selected device
//!
//! @param hDlg : handle for dialogue
//!
USBDM_ErrorCode ColdfireUnlockerPanel::eraseDscDevice() {
   unsigned int deviceNumber = 0;
   uint8_t      unlockCommand[1];
   uint8_t      clockDividerValue[1];

   unlockCommand[0]     = unlockInstructionTextControl->GetHexValue();
   clockDividerValue[0] = clockDividerTextControl->GetHexValue();

   print("Erasing JTAG device #%d, Unlock command=%2.2X, Clock Divider=%2.2X\n",
         deviceNumber, unlockCommand[0], clockDividerValue[0]);

   USBDM_ErrorCode rc = BDM_RC_OK;
   do {
      rc = USBDM_SetExtendedOptions(&bdmOptions);
      if (rc != BDM_RC_OK) {
         return rc;
      }
      rc = USBDM_SetTargetTypeWithRetry(T_MC56F80xx);
      if (rc != BDM_RC_OK) {
         return rc;
      }
      USBDM_TargetReset((TargetMode_t)(RESET_SPECIAL|RESET_DEFAULT));
      if (rc != BDM_RC_OK) {
         return rc;
      }
   } while (0);

   deviceNumber = JTAG_Chain::currentDeviceNum;
   JTAG_Chain::reset();
   JTAG_Chain::writeIR(unlockCommand, JTAG_EXIT_SHIFT_DR);
   JTAG_Chain::write(clkdivDrLength, clockDividerValue, JTAG_EXIT_IDLE);
   wxMilliSleep(2000); // Let erase complete
   JTAG_Chain::reset();
   return rc;
}
/*
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_INIT_CHAIN_BUTTON
 */
void ColdfireUnlockerPanel::OnInitChainButtonClick( wxCommandEvent& event )
{
   USBDM_SetExtendedOptions(&bdmOptions);
#if TARGET == CFVx
   if (USBDM_SetTargetTypeWithRetry(T_CFVx) != BDM_RC_OK) {
      return;
   }
#elif TARGET == MC56F80xx
   if (USBDM_SetTargetTypeWithRetry(T_MC56F80xx) != BDM_RC_OK) {
      return;
   }
#endif
   loadJTAGDeviceList();
   USBDM_SetTargetType(T_OFF);
}
USBDM_ErrorCode usbdmInit(TargetType_t targetType = T_CFV1) {
   unsigned int deviceCount;
   unsigned int deviceNum;
   USBDM_ErrorCode rc = USBDM_Init();
   if (rc != BDM_RC_OK) {
      return rc;
   }
   rc = USBDM_FindDevices(&deviceCount);
   print( "usbdmInit(): Usb initialised, found %d device(s)\n", deviceCount);
   print("After USBDM_FindDevices, Time = %f\n", progressTimer->elapsedTime());
   if (rc != BDM_RC_OK) {
      return rc;
   }
   deviceNum  = 0;
   rc = USBDM_Open(deviceNum);
   if (rc != BDM_RC_OK) {
      print( "usbdmInit(): Failed to open %s, device #%d\n", getTargetTypeName(targetType), deviceNum);
      return rc;
   }
   print( "usbdmInit(): Opened %s, device #%d\n", getTargetTypeName(targetType), deviceNum);
   print("After USBDM_Open, Time = %f\n", progressTimer->elapsedTime());
   // Set up sensible default since we can't change this (at the moment)
   USBDM_ExtendedOptions_t bdmOptions = {sizeof(USBDM_ExtendedOptions_t), TARGET_TYPE};
   USBDM_GetDefaultExtendedOptions(&bdmOptions);
   bdmOptions.targetVdd                  = BDM_TARGET_VDD_3V3; // BDM_TARGET_VDD_NONE;
   bdmOptions.autoReconnect              = AUTOCONNECT_ALWAYS; // Aggressively auto-connect
   bdmOptions.guessSpeed                 = FALSE;
   bdmOptions.cycleVddOnConnect          = FALSE;
   bdmOptions.cycleVddOnReset            = FALSE;
   bdmOptions.leaveTargetPowered         = FALSE;
   bdmOptions.bdmClockSource             = CS_DEFAULT;
   bdmOptions.useResetSignal             = FALSE;
   bdmOptions.usePSTSignals              = FALSE;
   bdmOptions.interfaceFrequency         = 1000; // 1MHz
   bdmOptions.powerOnRecoveryInterval    = 100;
   bdmOptions.resetDuration              = 100;
   bdmOptions.resetReleaseInterval       = 100;
   bdmOptions.resetRecoveryInterval      = 100;
   rc = USBDM_SetExtendedOptions(&bdmOptions);
   if (rc != BDM_RC_OK) {
      print( "usbdmInit(): USBDM_SetExtendedOptions() failed\n");
      return rc;
   }
   print("After USBDM_SetExtendedOptions, Time = %f\n", progressTimer->elapsedTime());
   rc = USBDM_SetTargetType(targetType);
   if (rc != BDM_RC_OK) {
      print( "usbdmInit(): USBDM_SetTargetType() failed\n");
      return rc;
   }
   print("After USBDM_SetTargetType, Time = %f\n", progressTimer->elapsedTime());
   USBDM_TargetReset((TargetMode_t)(RESET_DEFAULT|RESET_SPECIAL));
   print("After USBDM_TargetReset, Time = %f\n", progressTimer->elapsedTime());
   if (USBDM_Connect() != BDM_RC_OK) {
      print( "usbdmInit(): Connecting failed - retry\n");
      USBDM_TargetReset((TargetMode_t)(RESET_DEFAULT|RESET_SPECIAL));
      rc = USBDM_Connect();
      if (rc != BDM_RC_OK) {
         print( "targetConnect(): USBDM_SetTargetType() failed\n");
         return rc;
      }
   }
   print("After targetConnect, Time = %f\n", progressTimer->elapsedTime());
   return BDM_RC_OK;
}