Copying OSCCAL value from flash to register

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

Hi all,

 

I followed the AVR053 app note (https://www.microchip.com/wwwApp...) and ran the RC calibration code on my ATMega328P.  Here's the output from the batch file:

*********************************************************
 Batch file for calibration of Atmel AVR ATmega328p
 oscillator through the ISP interface with JTAGICE3/ATMELICE
 - The internal RC is calibrated to value and accuracy
   given in RC_Calibration.asm (fuses set for 8 MHz)
 - Programming FLASH and Fuses is performed initially.
 - atprogram help for more options
ECHO is off.
 $Name$
 $Revision: 1.0$
 $RCSfile$
 $Date: 2015-01-21 11:44:26 +0200 (on, 21 jan 2015) $
*********************************************************
---------------------------------------------------------

  ** S T A R T   P R O G R A M M I N G **

---------------------------------------------------------
Firmware check OK
Chiperase completed successfully
Verification of write OK
Write completed successfully.
Programming completed successfully.

 ** Start Calibration
---------------------------------------------------------
Firmware check OK
Oscillator calibration sequence succeeded.
---------------------------------------------------------

Verify that OSCCAL value is different from 0xFF.
Note, this test is intended to fail. If it does
not fail, OSCCAL equals 0xFF which is considered
an error.
Firmware check OK
[ERROR] Verification at address 0x00 failed. Expected: 0xFF. Actual: 0x5A.
          ^ Ignore Error above ^

---------------------------------------------------------
Read out new OSCCAL value from EEPROM and erase the device.
Program in customers code to FLASH, in this case test.hex.
Verify programming of customers code.
Program in new OSCCAL value in flash at byte addr 01FF.
Verify programming of new OSCCAL value in Flash.
Firmware check OK
Output written to osccal.bin
Chiperase completed successfully
Programming completed successfully.
Firmware check OK
Programming completed successfully.
Programming completed successfully.

*********************************************************
                P R O G R A M M I N G   O K
*********************************************************
Press any key to continue . . .

Based on that, it sounds like the program automatically wrote the new calibration value to flash address 0x1FF.

 

I read that byte back using the programmer and it's set to 0x90.  I'm not trying to read that flash byte and store it into the OSCAL register on boot.  I'm trying something like this in my init function:

 

uint8_t osccal_val = pgm_read_byte(0x1FF);
OSCCAL = osccal_val;

 

However, it doesn't seem to work.  I did a test to set an IO if osccal_val = 0x90 and it fails.

 

Do I need to read and set the OSCCAL value some other way?

Best,

Matt

 

 

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

When you load a new program into the chip, won't it overwrite

whatever value was written at location 0x01FF?  I'm pretty sure

you need to store a custom OSCCAL value in EEPROM and have

your program read this value and write it to the OSCCAL register.

 

--Mike

 

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

You have two options as hinted by Mike:

OSCAL = 123;
​​​​OCSAL = eeprom_read_byte(some_locn);
​​​​​​

Put either at the start of main (). Use the fixed value determined during calibration if this is a one off CPU. For multiple AVR to run the same code use EEPROM and put the per CPU value in some location in EEPROM.