Attiny85 flash memory self destroy In CodeVision

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dear friends,

I need to find the simple solution that helps me to destroy(erase) flash memory by Attiny85 itself. It means that I want to write some function (for example kill();) that will complete earase flash memory page by page. In the Attiny25/45/85 I found the next article:

19.1 Performing Page Erase by SPM
To execute Page Erase, set up the address in the Z-pointer, write "00000011" to SPMCSR and execute SPM
within four clock cycles after writing SPMCSR. The data in R1 and R0 is ignored. The page address must be written
to PCPAGE in the Z-register. Other bits in the Z-pointer will be ignored during this operation.
Note: The CPU is halted during the Page Erase operation.

but I can't understand how to Implement It In C language In the CV IDE, so any help would be nice. Thank You.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And, where will this function execute from?

Why not just write 0x00 or 0xff or such like a bootloader but without loading an external file?

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You might want to look at code for "tiny bootloader".

BTW--Are you planning to erase that last page containing your erase code out from under yourself? Will the AVR even allow you to do that?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
You might want to look at code for "tiny bootloader".

BTW--Are you planning to erase that last page containing your erase code out from under yourself? Will the AVR even allow you to do that?

Yes, I just need write the "C" function In the CodeVision IDE that will be erase flash only other code but not the fuction containing erase code....

I read abut bootloaders but I don't have much free space and I need the simple function In my code that will erase most part of the code flash memory.
In other hand I need to have an ability to destroy MCU by Itself, for example when user will enter the wrong PIN code program will execute some kill(); function like

void kill()
{
//
//
//
}; 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
And, where will this function execute from?

Why not just write 0x00 or 0xff or such like a bootloader but without loading an external file?

Jim

that Is what I need, so can you advise me an example?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can adapt almost any bootloader. It will be very small since you do not have to read an external file and you do not have to do any error checking.

What you want can ONLY be done in the bootloader section. The AVR architecture prevents any change to flash from anywhere else but the bootloader section.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, but if you erase everything except your function, how will you call it again?

"In other hand I need to have an ability to destroy MCU by Itself,"

Have you tought of a robot powering a hammer (it might hit the poor innocent CPU, or, best, the fingers of someone entering a PIN with a typo)...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
You can adapt almost any bootloader. It will be very small since you do not have to read an external file and you do not have to do any error checking.

What you want can ONLY be done in the bootloader section. The AVR architecture prevents any change to flash from anywhere else but the bootloader section.

Jim

Thank You Jim, I will see In the boottloader code....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

What you want can ONLY be done in the bootloader section. The AVR architecture prevents any change to flash from anywhere else but the bootloader section.


Generally, small AVR8 models do not have a bootloader section.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Well, but if you erase everything except your function, how will you call it again?
Why would he want to?
Quote:
Generally, small AVR8 models do not have a bootloader section.
And indeed this is the case with the tiny85. In order for this to work, the function must know which page(s) that it resides so that it knows what page(s) to not erase, and all the code that you wish to erase must obviously not be within those pages. With avr-gcc you would set the function up to be within a section (which you would place at a specific address). I'm not sure how to do this in CodeVision.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

void118 wrote:
but I can't understand how to Implement It In C language In the CV IDE
This is how I would do it in AVR GCC:
#include 
#include 

// Erase all flash (SELFPRGEN must be enabled)
void __attribute__ ((__section__ (".shred"))) shred(void) {
  cli();
  while (EECR & (1<<EEPE));
  uint16_t flash = 0;
  while (1) {
    SPMCSR = (1<<PGERS)|(1<<SPMEN);
    __asm__ __volatile__ ("spm\n" : : "z" (flash));
    flash += SPM_PAGESIZE;
  } while (1);
}

Compiling (but not linking):

avr-gcc -g -c -Os -mmcu=attiny85 shred.c -o shred.o

We get:

Disassembly of section .shred:

00000000 :

  cli();
   0:	f8 94       	cli

  while (EECR & (1<<EEPE));
   2:	e1 99       	sbic	0x1c, 1	; 28
   4:	00 c0       	rjmp	.+0      	; 0x6 
   6:	e0 e0       	ldi	r30, 0x00	; 0
   8:	f0 e0       	ldi	r31, 0x00	; 0

  uint16_t flash = 0;
  while (1) {
    SPMCSR = (1<<PGERS)|(1<<SPMEN);
   a:	83 e0       	ldi	r24, 0x03	; 3
   c:	87 bf       	out	0x37, r24	; 55
    __asm__ __volatile__ ("spm\n" : : "z" (flash));
   e:	e8 95       	spm
    flash += SPM_PAGESIZE;
  10:	e0 5c       	subi	r30, 0xC0	; 192
  12:	ff 4f       	sbci	r31, 0xFF	; 255
  14:	00 c0       	rjmp	.+0      	; 0x16 <__zero_reg__+0x15>

... totaling 22 bytes.

Adding a skeletal main():

int main(void) {
  shred();
}

... we can now place this at the very end of flash in the ATtiny85:

avr-gcc -g -Os -mmcu=attiny85 shred.c -o shred.elf -Wl,--section-start=.shred=0x1FEA

... giving:

Disassembly of section .shred:

00001fea :

  cli();
    1fea:	f8 94       	cli

  while (EECR & (1<<EEPE));
    1fec:	e1 99       	sbic	0x1c, 1	; 28
    1fee:	fe cf       	rjmp	.-4      	; 0x1fec 
    1ff0:	e0 e0       	ldi	r30, 0x00	; 0
    1ff2:	f0 e0       	ldi	r31, 0x00	; 0

  uint16_t flash = 0;
  while (1) {
    SPMCSR = (1<<PGERS)|(1<<SPMEN);
    1ff4:	83 e0       	ldi	r24, 0x03	; 3
    1ff6:	87 bf       	out	0x37, r24	; 55
    __asm__ __volatile__ ("spm\n" : : "z" (flash));
    1ff8:	e8 95       	spm
    flash += SPM_PAGESIZE;
    1ffa:	e0 5c       	subi	r30, 0xC0	; 192
    1ffc:	ff 4f       	sbci	r31, 0xFF	; 255
    1ffe:	fb cf       	rjmp	.-10     	; 0x1ff6 

Disassembly of section .text:
.
.
.
00000030 
: int main(void) { shred(); 30: dc df rcall .-72 ; 0xffffffea <__eeprom_end+0xff7effea>

I am not familiar with CV, so I can't tell you how to adapt this. The two things that will be important are replacing the inline assembler with something appropriate to CV, and placing the code in the last page of flash.

I've tested this on an ATtiny85 and it works just fine, and it does in fact erase all pages including the page containing the code that does the erasing. Necessarily this is the last page erased.

Untested on other devices, but should work with devices that have page sizes 16 bytes or greater. Some might require monitoring SPMCSR between pages.

By the way, why on Earth do you need such a thing? Sort of a 'This message will self-destruct in five seconds' kind of thing? ;)

JJ

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

'This message will self-destruct in five seconds' kind of thing?

But without the smoke that's going to be very disappointing!