CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

18F452: Strange code protection problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
VanHauser



Joined: 03 Oct 2005
Posts: 88
Location: Ploiesti, Romania

View user's profile Send private message

18F452: Strange code protection problem
PostPosted: Sun Jan 08, 2006 5:09 am     Reply with quote

I enabled code protection for a 18F452 like this:
Code:
#fuses HS,  PROTECT,NOOSCSEN,BROWNOUT,BORV45,WDT,WDT128,PUT,NOSTVREN,NODEBUG,NOLVP,WRT,NOCPB,WRTB,WRTC,NOCPD,NOWRTD,NOEBTR,NOEBTRB

You cand see that the boot block is unprotected (NOCPB), so I can read it back from the PIC, but nothing more. The problem appears when I enable CPB, the firmware cannot write to the internal EEPROM anymore, though it's not protected at all! Is the PIC confusing EEPROM addresses with program memory addresses or am I doing something wrong?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jan 08, 2006 3:28 pm     Reply with quote

Quote:
the firmware cannot write to the internal EEPROM anymore,

I made a test program using your #fuses statement. The only thing I
changed was the oscillator setting: HS to XT. I compiled it with PCH
vs. 3.241, and it works OK. It can write and read from internal eeprom.
Here is output of the program given below:
Quote:

Wrote 00, Read 00
Wrote 01, Read 01
Wrote 02, Read 02
Wrote 03, Read 03
Wrote 04, Read 04
Wrote 05, Read 05
Wrote 06, Read 06
Wrote 07, Read 07
Wrote 08, Read 08
Wrote 09, Read 09


Code:
#include <18F452.h> 
#fuses XT, PROTECT,NOOSCSEN,BROWNOUT,BORV45,WDT,WDT128,PUT,NOSTVREN,NODEBUG,NOLVP,WRT,NOCPB,WRTB,WRTC,NOCPD,NOWRTD,NOEBTR,NOEBTRB
#use delay(clock=4000000, restart_wdt)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS, restart_wdt)

//=============================
void main()
{
int8 i;
int8 c;

for(i = 0; i < 10; i++)
   {
    write_eeprom(0, i);
    c = read_eeprom(0);
    printf("Wrote %X, Read %X\n\r", i , c);
    restart_wdt();
   }

while(1)
  {
   restart_wdt();
  }
}


Here are the fuse settings shown at the end of the .LST file.
Do they match the ones shown in your .LST file ?
Code:
Configuration Fuses:
   Word  1: 2100   XT NOOSCSEN
   Word  2: 0F02   BROWNOUT WDT128 WDT BORV45 PUT
   Word  3: 0100   CCP2C1
   Word  4: 0080   NOSTVREN NODEBUG NOLVP
   Word  5: C000   PROTECT NOCPD NOCPB
   Word  6: 8000   WRT NOWRTD WRTB WRTC
   Word  7: 400F   NOEBTR NOEBTRB
VanHauser



Joined: 03 Oct 2005
Posts: 88
Location: Ploiesti, Romania

View user's profile Send private message

PostPosted: Fri Jan 13, 2006 8:20 am     Reply with quote

It works for me too with the fuses you used. Try it with CPB instead of NOCPB, mine fails if I do so Shocked
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jan 13, 2006 4:50 pm     Reply with quote

I don't have my 18F452 board with me at the moment, so I tested it
with a 18F458 board. This is with PCH vs. 3.241. It still worked.
I can test it later with a 18F452, but the 18F458 is similar.

Have you tried the test program shown below ?

Results:
Quote:

Wrote 00, Read 00
Wrote 01, Read 01
Wrote 02, Read 02
Wrote 03, Read 03
Wrote 04, Read 04
Wrote 05, Read 05
Wrote 06, Read 06
Wrote 07, Read 07
Wrote 08, Read 08
Wrote 09, Read 09


I changed the fuse to CPB as you said:
Quote:
#include <18F458.h>
#fuses XT, PROTECT,NOOSCSEN,BROWNOUT,BORV45,WDT,WDT128,PUT,NOSTVREN,NODEBUG,NOLVP,WRT,CPB,WRTB,WRTC,NOCPD,NOWRTD,NOEBTR,NOEBTRB
#use delay(clock=4000000, restart_wdt)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS, restart_wdt)

//=============================
void main()
{
int8 i;
int8 c;

for(i = 0; i < 10; i++)
{
write_eeprom(0, i);
c = read_eeprom(0);
printf("Wrote %X, Read %X\n\r", i , c);
restart_wdt();
}

while(1)
{
restart_wdt();
}
}
VanHauser



Joined: 03 Oct 2005
Posts: 88
Location: Ploiesti, Romania

View user's profile Send private message

PostPosted: Sat Jan 14, 2006 7:17 am     Reply with quote

My routine that writes to the internal EEPROM also reads back for verification. It makes 3 write/read attempts. write_eeprom() and read_eeprom() work both, BUT what is read back is not the same with what is was written! This happens just by enabling CPB.
Here is the routine:

Code:
#separate
void save_config() {
  int8 addr;
  signed int8 i;
  signed int8 attempt;
  int8 *ptr;
  int1 failed;

  config.markAA = 0xAA;
  config.mark55 = 0x55;
  // Make the CRC, excluding the last two bytes assigned to it
  config.crc = generate_crc((int8 *)&config, sizeof(config)-2);
  attempt=0;
  do {
    // Write
    addr = 0;
    ptr = (int8 *) &config;
    for (i=0; i<sizeof(config); i++) {
      write_eeprom(addr, *ptr);
      addr++;
      ptr++;
    }
    // Verify
    addr = 0;
    ptr = (int8 *) &config;
    failed = FALSE;
    for (i=0; i<sizeof(config); i++) {
      if ((*ptr) != read_eeprom(addr)) {
        failed = TRUE;
        break;
      }
      addr++;
      ptr++;
    }
    if (!failed) return;
    attempt++;
  } while (failed && (attempt<WRITE_ATTEMPTS));

  // HALT, every write/verify attempt failed
  write_fault_code = 'C';
  goto_address(ADDR_HALT);
}


PCM Programmer, I am sorry but I can't test your code for the moment, I don't have any boards. The project the routine makes part of is in stable mode, but I want to enable full protection. I forgot to mention that the crystal it uses is 8 MHz.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group