Logo Search packages:      
Sourcecode: odyssey version File versions

void Pic18Device::write_data_memory ( DataBuffer buf,
unsigned long  addr,
bool  verify 
) [protected, virtual]

Writes data to the data eeprom.

Parameters:
buf The DataBuffer from which to retrieve the data to write.
addr The byte offset into the DataBuffer from which to start retrieving data.
verify A boolean value indicating if the written data should be read back and verified.
Postcondition:
The progress_count is incremented by the number of bytes written to the data memory. If verify is true then progress_count will have been incremented by two times the number of bytes written; once for the write and once for the verify.
Exceptions:
runtime_error Contains a description of the error along with the data memory location at which the error occured.

Definition at line 216 of file Pic18Device.cxx.

References COMMAND_CORE_INSTRUCTION, COMMAND_SHIFT_OUT_TABLAT, PicDevice::eesize, Device::progress(), Device::progress_count, THROW_ERROR, write_command(), and write_command_read_data().

Referenced by program().

               {
      uint32_t ins;
      unsigned int offset;

      offset = 0;
      try {
            /* Step 1: Direct access to data EEPROM */
            /* BCF EECON1, EEPGD */
            write_command(COMMAND_CORE_INSTRUCTION, 0x9ea6);
            /* BCF EECON1, CFGS */
            write_command(COMMAND_CORE_INSTRUCTION, 0x9ca6);

            for(offset=0; offset < this->eesize; offset++) {
                  progress(addr+(2*offset));

                  /* Step 2: Set the data EEPROM address pointer */
                  ins = 0x0e00 | (offset & 0xff);                 /* movlw <addr lo> */
                  write_command(COMMAND_CORE_INSTRUCTION, ins);
                  write_command(COMMAND_CORE_INSTRUCTION, 0x6ea9);/* movwf EEADR */
                  ins = 0x0e00 | ((offset >> 8) & 0xff);          /* movlw <addr hi> */
                  write_command(COMMAND_CORE_INSTRUCTION, ins);
                  write_command(COMMAND_CORE_INSTRUCTION, 0x6eaa);/* movwf EEADRH */

                  /* Step 3: Load the data to be written */
                  ins = 0x0e00 | (buf[(addr/2)+offset] & 0xff);   /* movlw <data> */
                  write_command(COMMAND_CORE_INSTRUCTION, ins);
                  write_command(COMMAND_CORE_INSTRUCTION, 0x6ea8);/* movwf EEDATA */

                  /* Step 4: Enable memory writes */
                  /* BSF EECON1, WREN */
                  write_command(COMMAND_CORE_INSTRUCTION, 0x84a6);

                  /* Step 5: Perform required sequence */
                  /* MOVLW 0x55 */
                  write_command(COMMAND_CORE_INSTRUCTION, 0x0e55);
                  /* MOVWF EECON2 */
                  write_command(COMMAND_CORE_INSTRUCTION, 0x6ea7);
                  /* MOVLW 0xAA */
                  write_command(COMMAND_CORE_INSTRUCTION, 0x0eaa);
                  /* MOVWF EECON2 */
                  write_command(COMMAND_CORE_INSTRUCTION, 0x6ea7);

                  /* Step 6: Initiate write */
                  /* BSF EECON1, WR */
                  write_command(COMMAND_CORE_INSTRUCTION, 0x82a6);

                  /* Step 7: Poll WR bit, repeat until the bit is clear */
                  do {
                        /* MOVF EECON1, W, 0 */
                        write_command(COMMAND_CORE_INSTRUCTION, 0x50a6);
                        /* MOVWF TABLAT */
                        write_command(COMMAND_CORE_INSTRUCTION, 0x6ef5);

                        ins = write_command_read_data(COMMAND_SHIFT_OUT_TABLAT);
                  } while(ins & 0x02);

                  /* Step 8: Disable writes */
                  /* BCF EECON1, WREN */
                  write_command(COMMAND_CORE_INSTRUCTION, 0x94a6);

                  this->progress_count++;

                  if(verify) {
                        /* Initiate a memory read */
                        /* BSF EECON1, RD */
                        write_command(COMMAND_CORE_INSTRUCTION, 0x80a6);

                        /* Load data into the serial data holding register */
                        /* MOVF EEDATA, W, 0 */
                        write_command(COMMAND_CORE_INSTRUCTION, 0x50a8);
                        /* MOVWF TABLAT */
                        write_command(COMMAND_CORE_INSTRUCTION, 0x6ef5);

                        /* Shift out data and check */
                        ins = write_command_read_data(COMMAND_SHIFT_OUT_TABLAT);
                        if(ins != (buf[(addr/2)+offset] & 0xff))
                              throw runtime_error("");
                        this->progress_count++;
                  }
            }
      } catch(std::exception& e) {
            THROW_ERROR(runtime_error,
              "Couldn't write data memory at location %d", offset);
      }
}


Generated by  Doxygen 1.6.0   Back to index