Logo Search packages:      
Sourcecode: odyssey version File versions

void Pic16Device::erase ( void   )  [virtual, inherited]

Erase the memory contents of the device. This is only applicable to EEPROM or flash devices that can be electronically erased.

Precondition:
set_iodevice() must have been called with a valid IO object.
Exceptions:
runtime_error Contains a textual description of the error.

Implements Device.

Definition at line 59 of file Pic16Device.cxx.

References Pic16Device::bulk_erase(), PicDevice::codesize, PicDevice::COMMAND_INC_ADDRESS, PicDevice::COMMAND_LOAD_CONFIG, Pic16Device::cp_mask, Pic16Device::cpd_mask, Pic16Device::disable_codeprotect(), PicDevice::flags, Device::io, PicDevice::memtype, Pic16Device::persistent_config_mask, PIC_HAS_OSCAL, PicDevice::pic_off(), Pic16Device::program_one_location(), Pic16Device::read_config_word(), PicDevice::read_prog_data(), PicDevice::set_program_mode(), IO::shift_bits_out(), THROW_ERROR, IO::usleep(), PicDevice::wordmask, PicDevice::write_command(), and Pic16Device::write_config_word().

                            {
      if(this->memtype != MEMTYPE_FLASH)
            throw runtime_error("Operation not supported by device");

      /* Read the config word, and OSCAL if this PIC has it */
      unsigned long cword, oscal = 0;
      try {
            this->set_program_mode();

            /* If we need to save the oscillator calibration value, increment
             * to the OSCAL address and read it. */
            if(this->flags & PIC_HAS_OSCAL) {
                  for(unsigned int i=0; i<this->codesize-1; i++)
                        this->write_command(COMMAND_INC_ADDRESS);
                  oscal = this->read_prog_data();
            }

            /* Read the current configuration word to determine if code
             * protection needs to be disabled. */
            this->write_command(COMMAND_LOAD_CONFIG);
            this->io->shift_bits_out(0x7ffe, 16, 1);  /* Dummy write of all 1's */
            this->io->usleep(1);

            /* Skip to the configuration word */
            for(int i=0; i < 7; i++)
                  this->write_command(COMMAND_INC_ADDRESS);

            cword = read_config_word();
            this->pic_off();
      } catch(std::exception& e) {
            this->pic_off();
            throw;
      }

      /* Wait a bit after exiting program mode */
      this->io->usleep(10000);

      /* If code protection or "data code protection" is enabled, we need to do
       * a disable_codeprotect() */
      if(((cword & this->cp_mask) != this->cp_none) ||
        ((cword & this->cpd_mask) != this->cpd_off)) {
            this->disable_codeprotect();
      } else {
            this->bulk_erase();
      }

      /* Check if we need to restore some state. */
      if((this->persistent_config_mask != 0) || (this->flags & PIC_HAS_OSCAL)) {
            /* Wait a bit after exiting program mode */
            this->io->usleep(10000);

            try {
                  this->set_program_mode();

                  /* Restore the OSCAL value */
                  if(this->flags & PIC_HAS_OSCAL) {
                        /* Increment to the OSCAL address */
                        for(unsigned int i=0; i<this->codesize-1; i++)
                              this->write_command(COMMAND_INC_ADDRESS);
                        if(! this->program_one_location(oscal)) {
                              THROW_ERROR(runtime_error,
                                "Couldn't restore OSCAL value of 0x%04lx", oscal);
                        }
                  }

                  if(this->persistent_config_mask != 0) {
                        try {
                              /* Restore the persistent configuration word bits */
                              this->write_command(COMMAND_LOAD_CONFIG);
                              this->io->shift_bits_out(0x7ffe, 16, 1);  /* Dummy write of all 1's */
                              this->io->usleep(1);

                              /* Skip to the configuration word */
                              for(int i=0; i < 7; i++)
                                    this->write_command(COMMAND_INC_ADDRESS);

                              write_config_word((cword & this->persistent_config_mask) |
                                (this->wordmask & ~this->persistent_config_mask));
                        } catch(std::exception& e) {
                              THROW_ERROR(runtime_error,
                                "Couldn't restore configuration bits. Values 0x%04lx",
                                cword & this->persistent_config_mask);
                        }
                  }
                  this->pic_off();
            } catch(std::exception& e) {
                  this->pic_off();
                  throw;
            }
      }
}


Generated by  Doxygen 1.6.0   Back to index