// Too hard - details depend on target too much
//
USBDM_ErrorCode HCS12Unsecure::unsecureHCS12Target(void) {
   unsigned long speed;
   USBDMStatus_t usbdmStatus;

   print("HCS12Unsecure::unsecureHCS12Target()\n");

   USBDM_GetBDMStatus(&usbdmStatus);
   USBDM_GetSpeed(&speed);

   // Confirm speed
   HCS12Unsecure dialogue(false, speed);
   dialogue.Create(NULL);
   int getCancelOK = dialogue.ShowModal();

   if (getCancelOK != wxID_OK)
      return BDM_RC_FAIL;
   USBDM_SetSpeed(speed);
   dialogue.bulkEraseMemory();
   unsigned long bdmscr;
   USBDM_ReadStatusReg(&bdmscr); // for debug = UNSEC should be set
   dialogue.programSecurityLocation();
//   closeLogFile();

   return BDM_RC_OK;
}
//! Sets the communication speed.
//!
//! @param crystal_frequency crystal frequency in MHz.
//! This is used to determine the BDM communication speed (assumed
//! to be half the crystal frequency given).
//!
//! @return 0 => Success,\n !=0 => Fail
//!
TBDML_API unsigned char _tbdml_set_speed(float crystal_frequency) {
int speed;

   print("_tbdml_set_target_type()");

   speed = floor((1000/2)*crystal_frequency); // Convert to BDM comm freq in kHz
   return USBDM_SetSpeed(speed);              // Set BDM coms speed in kHz
}
//! Sets the communication speed.
//!
//! @param crystal_frequency crystal frequency in MHz.
//! This is used to determine the BDM communication speed (assumed
//! to be half the crystal frequency given).
//!
//! @return 0 => Success,\n !=0 => Fail
//!
OSBDM_API unsigned char _opensourcebdm_set_speed(float crystal_frequency) {
int speed;

   print("_opensourcebdm_set_speed()");

   speed = floor((1000/2)*crystal_frequency); // Convert to BDM comm freq in kHz
   return USBDM_SetSpeed(speed);              // Set BDM coms speed in kHz
}
//! \brief Sets the FCLKDIV/ECLKDIV register value based on
//! the BDM communication speed.
//!
//! Prepares the target for Flash and eeprom operations. \n
//! - Unprotects flash & eeprom [FPROT/EPROT registers]
//! - Set Flash clock divider [FCLKDIV/ECLKDIV registers] based busFrequency
//! - Clear flash & eeprom errors [FSTAT/ESTAT registers]
//! - Unprotects flash (FPROT)
//! - Set Flash clock divider (FCDIV)
//! - Disable COP
//!
//! @return error code, see \ref FlashError_t
//!
//! @note Assumes target has been reset & connected
//!
USBDM_ErrorCode HCS12Unsecure::initialiseTargetFlash() {
   uint8_t  fcdivCheckValue;
   static const unsigned char allOnes   = 0xFF;
   static const unsigned char allZeroes = 0x00;
   USBDM_ErrorCode rc;

   print("HCS12Unsecure::initialiseTargetFlash()\n");

   // Determine the FCLKDIV/ECLKDIV value
   //=========================================

   // Set user supplied speed
   rc = USBDM_SetSpeed(busFrequency);
   if ((rc != BDM_RC_OK) && (rc != BDM_RC_BDM_EN_FAILED))
      return rc;

   // Calculate the required FCDIV value
   fcdivValue = findFlashDividerValue((long)round(busFrequency), 150, 200);

   print("HCS12Unsecure::initialiseTargetFlash(): fBus = %ld kHz, FCDIV=0x%2.2X (%d)\n",
                   (long)round(busFrequency/1000.0), fcdivValue, fcdivValue);

   if (fcdivValue == 0xFF)
      return BDM_RC_UNKNOWN_SPEED;

   // Disable COP
   if (USBDM_WriteMemory(1, 1, HCS12DeviceData::getCOPCTL(), &COPCTL_value) != BDM_RC_OK)
      return BDM_RC_FAIL;

   //   fcdivValue  |= 0x80;

   // Unprotect the Flash
   if (USBDM_WriteMemory(1,1,HCS12DeviceData::getFPROTAddress(),&allOnes) != BDM_RC_OK)
      return BDM_RC_FAIL;

   // Clear any Flash errors (set FTSTMOD_WALL so all banks are affected)
   if (USBDM_WriteMemory(1, 1, HCS12DeviceData::getFTSTMODAddress(), &HCS12_FTSTMOD_WRALL) != BDM_RC_OK)
      return BDM_RC_FAIL;
   if (USBDM_WriteMemory(1,1,HCS12DeviceData::getFSTATAddress(),&HCS12_clearFlashErrors) != BDM_RC_OK)
      return BDM_RC_FAIL;
   if (USBDM_WriteMemory(1, 1, HCS12DeviceData::getFTSTMODAddress(), &allZeroes) != BDM_RC_OK)
      return BDM_RC_FAIL;

   // Set FCLKDIV value & verify
   if (USBDM_WriteMemory(1,1,HCS12DeviceData::getFCLKDIVAddress(),&fcdivValue) != BDM_RC_OK)
      return BDM_RC_FAIL;
   if (USBDM_ReadMemory(1,1,HCS12DeviceData::getFCLKDIVAddress(),&fcdivCheckValue) != BDM_RC_OK)
      return BDM_RC_FAIL;
   if (fcdivCheckValue != (fcdivValue|FCLKDIV_FDIVLD))
      return BDM_RC_FAIL;


   if (hasEEPROM) {
      // Set up the eeprom
      //====================================

      // Assume errors here mean there isn't actually any EEPROM
      // Unprotect the eeprom
      if (USBDM_WriteMemory(1,1,HCS12DeviceData::getEPROTAddress(),&allOnes) != BDM_RC_OK)
         hasEEPROM = false;

      // Clear any eeprom errors
      if (USBDM_WriteMemory(1,1,HCS12DeviceData::getESTATAddress(),&HCS12_clearFlashErrors) != BDM_RC_OK)
         hasEEPROM = false;

      // Set ECLKDIV value & verify
      if (USBDM_WriteMemory(1,1,HCS12DeviceData::getECLKDIVAddress(),&fcdivValue) != BDM_RC_OK)
         hasEEPROM = false;
      if (USBDM_ReadMemory(1,1,HCS12DeviceData::getECLKDIVAddress(),&fcdivCheckValue) != BDM_RC_OK)
         hasEEPROM = false;
      if (fcdivCheckValue != (fcdivValue|ECLKDIV_EDIVLD))
         hasEEPROM = false;
   }
   return BDM_RC_OK;
}