PIC18F Protection bits

General discussion on mikroPascal.
Post Reply
Author
Message
Isaac
Posts: 121
Joined: 13 Jul 2006 23:03
Location: Sinaloa, Mexico

PIC18F Protection bits

#1 Post by Isaac » 13 Jun 2008 00:51

Hello guys,

Just some questions:

1. If i need only to have a read protection in my PIC, i only have to activate the CPn bit protection?

If so,

2. ¿What are the Table read protection bits for?

3. ¿ If i wanted to protect my PIC from others to write to it, then i activate the table write protection bit?

if so,

4. ¿My PIC can never been reprogramed again?


Thanks and sorry if i asked novice quesiton

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: PIC18F Protection bits

#2 Post by janni » 13 Jun 2008 01:55

Isaac wrote:1. If i need only to have a read protection in my PIC, i only have to activate the CPn bit protection?

The code protection bits serve this purpose.
If so,

2. ¿What are the Table read protection bits for?
If somebody erased part of your code (like bootloader) and introduced his own, he would be able to read the rest of your code with table reads.
3. ¿ If i wanted to protect my PIC from others to write to it, then i activate the table write protection bit?
Or if you wanted to protect your code against accidental overwriting (like by runaway code).
if so,

4. ¿My PIC can never been reprogramed again?
No, it could be erased and reprogrammed.

Isaac
Posts: 121
Joined: 13 Jul 2006 23:03
Location: Sinaloa, Mexico

#3 Post by Isaac » 13 Jun 2008 02:28

:) Thanks janni

So the best way to protect our PIC is to activate all of the protection bits, so that no one could read it and write it before erasing all of the code? And then the only way to program to the PIC is to erase it first and then program it?

I just tried to activate all of them and i got some weird behavior, some images wasnt displayed correctly on my GLCD.. odd

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

#4 Post by janni » 13 Jun 2008 13:45

Isaac wrote:I just tried to activate all of them and i got some weird behavior, some images wasnt displayed correctly on my GLCD.. odd
Not at all. You'll have to take care of constants placement, if you want to have all protection bits on. If table reads are blocked, one may only access the ROM data within a block of flash memory. In other words, both the constants and the code reading them have to fit into the same memory block.

I wouldn't be so obsessed with the code protection (unless you have reasons to suspect a serious piracy attack) and leave last block of memory with table read protection off. Just make sure that all the constants are placed there.

dctoac
Posts: 7
Joined: 25 Jan 2021 10:14

Re:

#5 Post by dctoac » 10 Feb 2021 16:17

Hello Mr Janni,

I need your help.
leave last block of memory with table read protection off. Just make sure that all the constants are placed there.
Please,Could you let me know the way for storing the constants in last block of memory? I'm using PIC18F67K22 ( Mikro C )

I would be grateful for your reply. Thank you

Kind regards,
Toni

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: PIC18F Protection bits

#6 Post by janni » 10 Feb 2021 17:21

Hi Toni,

Please don't address me Mr. It makes me feel even older than I am :wink: .

You can place constants wherever you like with the 'absolute' directive. Just take into account the note from Help:
Note :
If you want to place simple type constant into Flash memory, instead of following declaration:

const short SimpeConstant = 0xAA absolute 0x2000;

use an array consisting of single element :

const short SimpleConstant[] = {0xAA} absolute 0x2000;

In first case, compiler will recognize your attempt, but in order to save Flash space, and boost performance, it will automatically replace all instances of this constant in code with it's literal value.
In the second case your constant will be placed in Flash in the exact location specified.
To avoid overwriting one constant by another, you may use the sizeof operator, like

Code: Select all

const cteOffs=0x2000;  // base address of constants

const int a1[5]={1,2,3,4,5}   absolute cteOffs;
const int a2[3]={1,2,3}       absolute cteOffs+sizeof(a1);
const int a3[6]={1,2,3,4,5,6} absolute cteOffs+sizeof(a1)+sizeof(a2);
Note that linker removes constants and variables not used in code (though it counts sizeof operator as code). In other words, if you would not explicitly use any of the constants in my example, a1 and a2 arrays would stay in program memory but a3 would be removed.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: PIC18F Protection bits

#7 Post by janni » 10 Feb 2021 17:23

deleted
Last edited by janni on 10 Feb 2021 17:27, edited 1 time in total.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: PIC18F Protection bits

#8 Post by janni » 10 Feb 2021 17:25

Don't know what I pressed but it multiplied my post :( .

dctoac
Posts: 7
Joined: 25 Jan 2021 10:14

Re: PIC18F Protection bits

#9 Post by dctoac » 12 Feb 2021 09:23

Hi Janni, :D

Thank you for providing the requested information. I am learning so much now.
Please don't address me Mr. It makes me feel even older than I am :wink:
Yes you are right :) Feeling younger than we are may help us in many ways. It will Improve our wellbeing and mental health.

I will do what you suggested in your last message. If I need any assistance, I will ask here.

Thanks for your time. Take care

Kind regards,
Toni

kudretuzuner
Posts: 3
Joined: 08 Feb 2021 21:08

Re: PIC18F Protection bits

#10 Post by kudretuzuner » 13 Feb 2021 14:28

Hello, while programming the PIC, it writes the CONFIG settings clearly on the data sheet. Whatever you want to do it says bit by bit. This issue is actually very important. It is like the foundation of construction. With the right requests and the right bit routing, you can run the PIC properly. For example I (writing clearly) I designed the CONFIG settings as a standard as follows.
__CONFIG _CONFIG1L, 0x00;B'00000000'
__CONFIG _CONFIG1H, 0x00;B'00000000'
__CONFIG _CONFIG2L, 0x00;B'00000000'
__CONFIG _CONFIG2H, 0x00;B'00000000'
__CONFIG _CONFIG3H, 0x80;B'10000000'------------Used as R3 input or MCLR
__CONFIG _CONFIG4L, 0x80; B'10000000'----------RB6 AND RB7 NORMAL INPUT OUTPUT
__CONFIG _CONFIG5L, 0x00;B'00000000'
__CONFIG _CONFIG5H, 0x00;B'00000000'
__CONFIG _CONFIG6L, 0x00;B'00000000'
__CONFIG _CONFIG6H, 0x00;B'00000000'
__CONFIG _CONFIG7L, 0x00;B'00000000'
__CONFIG _CONFIG7H, 0x00;B'00000000'
By looking at the data sheet, you can see which bit changes to 0 or 1.
Not
What I have written is taken from the 18F4550 data sheet.

Post Reply

Return to “mikroPascal General”