Skip to content

Commit 8f453f7

Browse files
committed
Merge branch 'osccal' of https://github.com/matthijskooijman/optiboot into tinytuner
This was submitted as a pull request to optiboot's main repository * Optiboot#183 See here for why I'm merging it * SpenceKonde/ATTinyCore#141 (comment) untested yet. Conflicts: optiboot/bootloaders/optiboot/Makefile
2 parents fc15b60 + 8d68385 commit 8f453f7

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

optiboot/bootloaders/optiboot/Makefile

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export
6060
# defaults
6161
# Set this explicitly in your make rules!
6262
# MCU_TARGET = atmega168
63-
LDSECTIONS = -Wl,--section-start=.text=0x3e00 -Wl,--section-start=.version=0x3ffe
63+
LDSECTIONS = -Wl,--section-start=.text=0x3e00 -Wl,--section-start=.osccal=0x3ffd -Wl,--section-start=.version=0x3ffe
6464

6565
# Build environments
6666
# Start of some ugly makefile-isms to allow optiboot to be built
@@ -198,8 +198,19 @@ ifdef SINGLESPEED
198198
SS_CMD = -DSINGLESPEED=1
199199
endif
200200

201+
ifdef OSCCAL_EEPROM_ADDR
202+
OSCCAL_CMD = -DOSCCAL_EEPROM_ADDR=$(OSCCAL_EEPROM_ADDR)
203+
dummy = FORCE
204+
endif
205+
206+
ifdef OSCCAL_PROGMEM
207+
OSCCAL_CMD = -DOSCCAL_PROGMEM
208+
dummy = FORCE
209+
endif
210+
201211
COMMON_OPTIONS = $(BAUD_RATE_CMD) $(LED_START_FLASHES_CMD) $(BIGBOOT_CMD)
202212
COMMON_OPTIONS += $(SOFT_UART_CMD) $(LED_DATA_FLASH_CMD) $(LED_CMD) $(SS_CMD)
213+
COMMON_OPTIONS += $(OSCCAL_CMD)
203214

204215
#UART is handled separately and only passed for devices with more than one.
205216
ifdef UART
@@ -248,7 +259,7 @@ virboot8: TARGET = atmega8
248259
virboot8: MCU_TARGET = atmega8
249260
virboot8: CFLAGS += $(COMMON_OPTIONS) '-DVIRTUAL_BOOT_PARTITION' '-Dsave_vect_num=EE_RDY_vect_num'
250261
virboot8: AVR_FREQ ?= 16000000L
251-
virboot8: LDSECTIONS = -Wl,--section-start=.text=0x1c00 -Wl,--section-start=.version=0x1ffe
262+
virboot8: LDSECTIONS = -Wl,--section-start=.text=0x1c00 -Wl,--section-start=.osccal=0x1ffd -Wl,--section-start=.version=0x1ffe
252263
virboot8: $(PROGRAM)_virboot8.hex
253264
virboot8: $(PROGRAM)_virboot8.lst
254265

@@ -257,7 +268,7 @@ virboot328: TARGET = atmega328
257268
virboot328: MCU_TARGET = atmega328p
258269
virboot328: CFLAGS += $(COMMON_OPTIONS) '-DVIRTUAL_BOOT_PARTITION'
259270
virboot328: AVR_FREQ ?= 16000000L
260-
virboot328: LDSECTIONS = -Wl,--section-start=.text=0x7d80 -Wl,--section-start=.version=0x7ffe
271+
virboot328: LDSECTIONS = -Wl,--section-start=.text=0x7d80 -Wl,--section-start=.osccal=0x7ffd -Wl,--section-start=.version=0x7ffe
261272
virboot328: $(PROGRAM)_virboot328.hex
262273
virboot328: $(PROGRAM)_virboot328.lst
263274

@@ -272,7 +283,6 @@ virboot328_isp: LFUSE ?= FF
272283
virboot328_isp: EFUSE ?= 05
273284
virboot328_isp: isp
274285

275-
276286
#---------------------------------------------------------------------------
277287
# "Board-level Platform" targets.
278288
# A "Board-level Platform" implies a manufactured platform with a particular
@@ -474,10 +484,10 @@ clean:
474484
$(OBJDUMP) -h -S $< > $@
475485

476486
%.hex: %.elf
477-
$(OBJCOPY) -j .text -j .data -j .version --set-section-flags .version=alloc,load -O ihex $< $@
487+
$(OBJCOPY) -j .text -j .data -j .version -j .osccal --set-section-flags .version=alloc,load --set-section-flags .osccal=alloc,load -O ihex $< $@
478488

479489
%.srec: %.elf
480-
$(OBJCOPY) -j .text -j .data -j .version --set-section-flags .version=alloc,load -O srec $< $@
490+
$(OBJCOPY) -j .text -j .data -j .version -j .osccal -set-section-flags .version=alloc,load --set-section-flags .osccal=alloc,load -O srec $< $@
481491

482492
%.bin: %.elf
483-
$(OBJCOPY) -j .text -j .data -j .version --set-section-flags .version=alloc,load -O binary $< $@
493+
$(OBJCOPY) -j .text -j .data -j .version -j .osccal --set-section-flags .version=alloc,load --set-section-flags .osccal=alloc,load -O binary $< $@

optiboot/bootloaders/optiboot/optiboot.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@
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+
/* */
136+
/* OSCCAL_PROGMEM */
137+
/* On startup, load an oscillator calibration value from */
138+
/* the top of the flash memory (unless it is 0xff, which */
139+
/* is the default). This byte is put into its own .osccal */
140+
/* section, so its address should be set through the */
141+
/* linker. */
142+
/* */
131143
/**********************************************************/
132144

133145
/**********************************************************/
@@ -239,6 +251,11 @@
239251
unsigned const int __attribute__((section(".version")))
240252
optiboot_version = 256*(OPTIBOOT_MAJVER + OPTIBOOT_CUSTOMVER) + OPTIBOOT_MINVER;
241253

254+
#if defined(OSCCAL_PROGMEM)
255+
unsigned const char __attribute__((section(".osccal")))
256+
optiboot_osccal = 0xff;
257+
#endif
258+
242259

243260
#include <inttypes.h>
244261
#include <avr/io.h>
@@ -519,6 +536,19 @@ int main(void) {
519536
SP=RAMEND; // This is done by hardware reset
520537
#endif
521538

539+
#if defined(OSCCAL_EEPROM_ADDR)
540+
// Load OSCCAL before app start, so the app does not have to
541+
ch = eeprom_read_byte((uint8_t*)OSCCAL_EEPROM_ADDR);
542+
if (ch != 0xff)
543+
OSCCAL = ch;
544+
#endif
545+
546+
#if defined(OSCCAL_PROGMEM)
547+
// Load OSCCAL before app start, so the app does not have to
548+
ch = pgm_read_byte(&optiboot_osccal);
549+
if (ch != 0xff)
550+
OSCCAL = ch;
551+
#endif
522552
/*
523553
* modified Adaboot no-wait mod.
524554
* Pass the reset reason to app. Also, it appears that an Uno poweron

0 commit comments

Comments
 (0)