Problem trying to set the prescaler on a Tiny85

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

Hi guys,

 

I have encountered an unexpected problem finalizing the last details of a tiny85 project. I have I2C running as slave, peripherals configured, in short, everything working OK. What I wanted to do as the last step is to set the prescaler of the system clock to 1 by code. This is for the system to work at 8 MHz, but with the fuses set by default.

 

For this, I added these 2 lines in the setup block:

CLKPR = (1 << CLKPCE);
CLKPR = 0x00;

 

The problem is that, apparently, these lines brick the tiny85 after the first run. After that, I'm not able to flash it with USBasp anymore.

I flashed two tiny85s with the same result (the first time I couldn't believe what was happening). Then, for the third, I commented these two code lines, recompiled, flashed, and the problem vanished.

 

Trying to recover the chips, I built a 12-volt programmer with an Arduino Mega 2560, I forced the fuses to the default values, just in case (L:0x62, H:0xDF, E:0xFF). I can verify that the fuses are correctly set, but still, they don't respond to the USBasp.

 

I must be making some basic mistake, but I can not solve it, can anyone give me any clue? Are the lines to modify the prescaler correct? How can I recover the chips?

 

Any help is very welcome!

Thanks in advance ...

Gus

 

This topic has a solution.
Last Edited: Sun. Oct 22, 2017 - 01:20 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

casanovg wrote:
The problem is that, apparently, these lines brick the tiny85 after the first run. After that, I'm not able to flash it with USBasp anymore.

On a side note, for that two-line sequence to be effective it muse be carried out within four cycles.  So in general tell toolchain and version and optimization settings.

 

Your "solution"

casanovg wrote:
Trying to recover the chips, I built a 12-volt programmer with an Arduino Mega 2560,

... sounds a bit extreme.

 

Generally AVR8 models with CLKPR keep the value over reset.  Don't ask me why.  Don't ask me whether that also applies over a POR;  I don't remember.

 

So you just need to slow down your ISP bit rate.

 

 

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

Thank you very much for your response theusch.

 

The tool chain I am using is WinAVR 20100110. My makefile produces this output:

 

"avr-gcc -Os -g -std = gnu99 -Wall -funsigned -char -funsigned-bitfields -fpack-struct -fshort-enums -ffunction- sections -fdata-sections -DF_CPU = 8000000UL -DBAUD = 9600UL -I. -I./nb-lib -mmcu = attiny85 -c -o main.o main.c
avr-gcc -Os -g -std = gnu99 -Wall -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -ffunction-sections -fdata-sections -DF_CPU = 8000000UL -I. -I./nb-lib -mmcu = attiny85 -c -o nb-lib / usiTwiSlave.o nb-lib / usiTwiSlave.c
avr-gcc -Wl, -Map, attiny85_usitwi_s.map -Wl, -gc-sections -mmcu = attiny85 main.o nb-lib / usiTwiSlave.o -o attiny85_usitwi_s.elf
avr-objcopy -j .text -j .data -O ihex attiny85_usitwi_s.elf attiny85_usitwi_s.hex ".

 

Then I use this to flash the chips: "avrdude -c usbasp -p attiny85 -U flash: w: attiny85_usitwi_s.hex"

 

Regarding the "extreme solution", you may be right, but this problem was so unexpected that really bothered me a lot, I wanted to fix it quickly :)

 

I did not know that the value of CLKPR is maintained after reset and POR. I do not remember seeing it in the data sheet, but thanks for the data, this is beginning to make sense in relation to my problem.

 

On slowing down (I suppose you mean the -B parameter in avrdude), I will try it, but why slow it down? Am I not supposed to have raised the CPU speed from 1 to 8 Mhz with those two lines?

 

Thank you!

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

casanovg wrote:
I wanted to fix it quickly :)

Then slow down your ISP bit rate.  Takes what -- say, 10 seconds?  Rigging for HVSP:  Much longer.

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

use avr-libc function:

 

#include <power.h>

clock_prescale_set(1);

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

Hi, I'm afraid my Chinese USBasp (Baite / black) is not allowing me to slow down the bit rate with -B.

I will try with the Arduino Mega as ISP. If it does not work, unfortunately, these two chips will be declared dead until I have some spare time to try to recover them.

I will go ahead with the project, the cost of the chips does not justify the lost time.

Thank you very much for your help!

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

Hi ki0bk, thanks for your suggestion.

I'm going to "risk" a tiny85 more to test your solution.

If I have problems again, for the moment I'll forget to try to change the CPU prescaler by code and I'll configure the fuses for 8MHz.

My intention was to take the chips "out of the box" and just flash code to deliver them without worrying about fuses, but it's just a "nice to have".

Regards

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

casanovg wrote:
Hi ki0bk, thanks for your suggestion. I'm going to "risk" a tiny85 more to test your solution.

If my speculation is correct, then it wouldn't be a solution.  It is a way to carry out CLKPR setting in a bulletproof manner with the GCC toolchain, regardless of optimization level.

 

If indeed the end result is that the AVR is running at 1MHz, then to do further ISP operations the bit rate must be less than 1/4 of 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.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hmmm--but I misread the OP --  clearing CLKPR, if it takes, would leave the AVR speed at 8MHz.

 

Now, if it doesn't "take" and instead sets a high prescaler, then the "persistent" CLKPR applies...

 

http://www.avrfreaks.net/forum/c...

http://www.avrfreaks.net/forum/m...

http://www.avrfreaks.net/forum/a... leading to http://lists.nongnu.org/archive/...

http://www.avrfreaks.net/forum/c... and links therein

http://www.avrfreaks.net/forum/a...

 

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.

Last Edited: Mon. Oct 16, 2017 - 03:35 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I can confirm that what ki0bk suggests:

 

#include <power.h>
clock_prescale_set (1);

 

Set the prescaler correctly without "bricking" the chip (that is, I can continue programming it with USBasp after a POR).

I tested it with a third tiny85. My initial goal would be solved in this way.

 

However, I still have to recover the first two that do not respond to the programmer. Using a mega Arduino instead of USBasp I can add the -B command without problems, however, I can't find the correct setting for them to respond.

 

@theusch: Yes, that's why I asked you yesterday "why lowering the speed rate?", the chips should be running at 8MHz after CLKPR = 0x00. Although obviously, they did not take it correctly. Now I have no idea how fast they are running after POR. I will check the links you posted to see if I find any clues.

 

Thank you

 

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

Hi, just in case someone has a similar problem, I finally found some spare time and I was able to recover the attiny85s.

 

As the only thing the chips responded to (with the tools I have at hand) was to modify the fuses with the 12V programmer, I enabled CKOUT to measure the CPU clock on CLKO (L=0xA2, H=0xDF, E=0xFF). To my surprise, the tester showed approx. 31.2 KHz, which made me realize that among several tests I've been doing to find the minimum CPU speed for reliable I2C with UsiTwiSlave.h, I been trying with high prescaler values, only that, as I was initially using a Digispark, the bootloader hid my mistake and I could continue programming.

When I went to the bare chips, one of those code tests with 8Mhz clock and 256 prescaler, sneaked in, and that's what I flashed on the "bricked" chips instead of CLKPR = (1 << CLKPCE); CLKPR = 0x00; (Yeah, I know, I'm an idiot ...).

 

Trying to fix it, I changed the fuses to work with PLL at 16MHz (L=0xB1, H=0xDF, E=0xFF), which left me with a clock of 62.5 KHz after the damn prescaler setup. Even so, the Chinese USBasp, at its lowest speed, could not put the chips in programming mode.

 

Then I found this cheap serial port programmer (http://www.circuitvalley.com/201...), along with comments that "its problem" is that it can't reach bit rates higher than 20KHz. This "problem" could be my solution. So I build the circuit, installed PonyProg and tried to erase the chips and, voilà, problem solved ...

 

I know this solution is not rocket science, but it could be useful to someone who is working with limited resources, without Atmel official tools.

 

Thank you very much ki0bk, and especially theusch for your support!

 

Last Edited: Fri. Oct 20, 2017 - 07:46 PM