Skip to content

Commit acc0d2d

Browse files
Allow loading an OSCCAL value from EEPROM
When an EEPROM address is given through OSCCAL_EEPROM_ADDR at compiletime, a byte is read from that address on startup and written to OSCCAL (unless it is 0xff, which should never be a meaningful calibration value). This allows improving the accuracy of the UART baudrate when running off the internal oscillator. Enabling this option adds 30 bytes on the atmega328p build. This can probably be reduced by inlining eeprom_read_byte.
1 parent 814e2a8 commit acc0d2d

File tree

4 files changed

+290
-532
lines changed

4 files changed

+290
-532
lines changed

optiboot/bootloaders/optiboot/Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,14 @@ ifdef SINGLESPEED
190190
SS_CMD = -DSINGLESPEED=1
191191
endif
192192

193+
ifdef OSCCAL_EEPROM_ADDR
194+
OSCCAL_CMD = -DOSCCAL_EEPROM_ADDR=$(OSCCAL_EEPROM_ADDR)
195+
dummy = FORCE
196+
endif
197+
193198
COMMON_OPTIONS = $(BAUD_RATE_CMD) $(LED_START_FLASHES_CMD) $(BIGBOOT_CMD)
194199
COMMON_OPTIONS += $(SOFT_UART_CMD) $(LED_DATA_FLASH_CMD) $(LED_CMD) $(SS_CMD)
200+
COMMON_OPTIONS += $(OSCCAL_CMD)
195201

196202
#UART is handled separately and only passed for devices with more than one.
197203
ifdef UART

optiboot/bootloaders/optiboot/optiboot.c

+12
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@
128128
/* UART number (0..n) for devices with more than */
129129
/* one hardware uart (644P, 1284P, etc) */
130130
/* */
131+
/* OSCCAL_EEPROM_ADDR */
132+
/* On startup, load an oscillator calibration value from */
133+
/* this address in EEPROM and write it to OSCCAL (unless */
134+
/* it is 0xff). */
135+
/* */
131136
/**********************************************************/
132137

133138
/**********************************************************/
@@ -468,6 +473,13 @@ int main(void) {
468473
SP=RAMEND; // This is done by hardware reset
469474
#endif
470475

476+
#if defined(OSCCAL_EEPROM_ADDR)
477+
// Load OSCCAL before app start, so the app does not have to
478+
ch = eeprom_read_byte((uint8_t*)OSCCAL_EEPROM_ADDR);
479+
if (ch != 0xff)
480+
OSCCAL = ch;
481+
#endif
482+
471483
/*
472484
* modified Adaboot no-wait mod.
473485
* Pass the reset reason to app. Also, it appears that an Uno poweron
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1-
:107E0000112494B714BE892F8D7011F0892FDED004
2-
:107E100085E08093810082E08093C00088E18093B8
3-
:107E2000C10086E08093C20080E18093C4008EE0B0
4-
:107E3000B7D0259A86E020E33CEF91E030938500AF
5-
:107E40002093840096BBB09BFECF1D9AA8958150CD
6-
:107E5000A9F7EE24FF24B3E0AB2EBB24B394A5E036
7-
:107E6000DA2EF1E1CF2E90D0813471F48DD0082F2D
8-
:107E70009DD0023811F482E005C0013811F486E08B
9-
:107E800001C083E079D075C0823411F484E103C06D
10-
:107E9000853419F485E092D06CC0853579F474D0BE
11-
:107EA000E82EFF2471D0082F10E0102F00270E2994
12-
:107EB0001F29000F111F7AD078015BC0863521F48D
13-
:107EC00084E07CD080E0DECF843609F035C05CD021
14-
:107ED0005BD0182F59D0082FC0E0D1E055D089933E
15-
:107EE0001C17E1F763D0053409F4FFCFF701A7BEF3
16-
:107EF000E89507B600FCFDCFA701A0E0B1E02C910A
17-
:107F000030E011968C91119790E0982F8827822B62
18-
:107F1000932B1296FA010C01B7BEE89511244E5F1F
19-
:107F20005F4F1A1761F7F701D7BEE89507B600FC57
20-
:107F3000FDCFC7BEE8951DC0843769F425D024D095
21-
:107F4000082F22D033D0E701FE018591EF0114D034
22-
:107F50000150D1F70EC0853739F428D08EE10CD00E
23-
:107F600085E90AD08FE08ECF813511F488E018D0F2
24-
:107F70001DD080E101D077CF982F8091C00085FF80
25-
:107F8000FCCF9093C60008958091C00087FFFCCF7E
26-
:107F90008091C00084FD01C0A8958091C60008951D
27-
:107FA000E0E6F0E098E1908380830895EDDF803291
28-
:107FB00019F088E0F5DFFFCF84E1DECF1F93182FA3
29-
:107FC000E3DF1150E9F7F2DF1F910895282E80E0DA
30-
:087FD000E7DFEE27FF2709940B
1+
:107E00001F92CDB7DEB7112484B714BE982F9D7092
2+
:107E100009F0D7D085E08093810082E08093C00094
3+
:107E200088E18093C10086E08093C20080E1809366
4+
:107E3000C4008EE0B1D0279A26E088E19EEF31E0C1
5+
:107E4000909385008093840036BBB09BFECF1F9A31
6+
:107E5000A8952150A9F7812C912C93E0F92EEE24BE
7+
:107E6000E39425E0D22E31E1C32E8AD0813479F417
8+
:107E700087D0898397D08981823811F482E005C048
9+
:107E8000813811F486E001C083E073D06FC0823482
10+
:107E900011F484E103C0853419F485E08BD066C009
11+
:107EA000853541F46DD0882E6BD0912C982A880CA2
12+
:107EB000991C5BC0863521F484E07CD080E0E5CF5E
13+
:107EC000843609F036C05CD05BD0B82E59D0A82ECD
14+
:107ED00000E011E055D0F80181938F01BE12FACF76
15+
:107EE00061D0F5E4AF1201C0FFCFF401F7BEE89511
16+
:107EF00007B600FCFDCFB401A0E0B1E02C911296D2
17+
:107F0000AD0141505109FA01808130E0382BFB016D
18+
:107F10000901E7BEE89511246E5F7F4FBA12EECFDC
19+
:107F2000F401D7BEE89507B600FCFDCFC7BEE895C3
20+
:107F30001DC0843769F424D023D0B82E21D032D08C
21+
:107F40008401F80185918F0114D0BA94D1F70EC045
22+
:107F5000853739F427D08EE10CD085E90AD08FE03F
23+
:107F600094CF813511F488E017D01CD080E101D086
24+
:107F70007CCF9091C00095FFFCCF8093C600089500
25+
:107F80008091C00087FFFCCF8091C00084FD01C0BC
26+
:107F9000A8958091C6000895E0E6F0E098E190830E
27+
:107FA00080830895EDDF803219F088E0F5DFFFCFA0
28+
:107FB00084E1DFCFCF93C82FE3DFC150E9F7CF9142
29+
:0E7FC000F1CF282E80E0E8DFE0E0FF270994F3
3130
:027FFE00020679
3231
:0400000300007E007B
3332
:00000001FF

0 commit comments

Comments
 (0)