Store setup settings in eeprom

To have the values of `value_contrast` and `value_backlight` persist
over turn offs they are loaded and stored from and in the eeprom.

For preventing the flash target from earasing the eeprom the `EESAVE`
high fuse byte is set to 0 (programmed).

The values are now loaded when booted and stored when a new value is
set in the setup menu.
This commit is contained in:
finga 2021-09-11 15:44:11 +02:00
parent acf4bc5754
commit 05c9888a75
2 changed files with 27 additions and 3 deletions

View file

@ -5,9 +5,12 @@ PROGRAMMER := usbasp
SPEED := 8000000UL SPEED := 8000000UL
TARGET := main.hex TARGET := main.hex
EEP := main.eep
BIN := main.elf BIN := main.elf
OBJ := main.o OBJ := main.o
HIGH_FUSE := 0xD6
SHELL := sh SHELL := sh
CC := avr-gcc CC := avr-gcc
OBJCOPY := avr-objcopy OBJCOPY := avr-objcopy
@ -16,24 +19,33 @@ AVRDUDE := avrdude
CFLAGS := -mmcu=$(MCU) -D F_CPU=$(SPEED) -Os -Wall -Werror -Wextra CFLAGS := -mmcu=$(MCU) -D F_CPU=$(SPEED) -Os -Wall -Werror -Wextra
all: $(TARGET) all: $(TARGET) $(EEP)
$(TARGET): $(BIN) $(TARGET): $(BIN)
${OBJCOPY} -O ihex -j .text -j .data $< $@ ${OBJCOPY} -O ihex -j .text -j .data $< $@
$(EEP): $(BIN)
${OBJCOPY} --change-section-lma .eeprom=0 -O ihex -j .eeprom $< $@
$(BIN): $(OBJ) $(BIN): $(OBJ)
$(CC) $(CFLAGS) $< -o $@ $(CC) $(CFLAGS) $< -o $@
%.o: %.c %.o: %.c
$(CC) $(CFLAGS) -c $< -o $@ $(CC) $(CFLAGS) -c $< -o $@
.PHONY: flash clean check size .PHONY: flash eeprom fuses clean check size
flash: $(TARGET) size flash: $(TARGET) size
$(AVRDUDE) -p $(MCU) -c $(PROGRAMMER) -U flash:w:$<:a $(AVRDUDE) -p $(MCU) -c $(PROGRAMMER) -U flash:w:$<:a
eeprom: $(EEP) size
$(AVRDUDE) -p $(MCU) -c $(PROGRAMMER) -U eeprom:w:$<:a
fuses:
$(AVRDUDE) -p $(MCU) -c $(PROGRAMMER) -U hfuse:w:$(HIGH_FUSE):m
clean: clean:
$(RM) $(TARGET) $(BIN) $(OBJ) $(RM) $(TARGET) $(EEP) $(BIN) $(OBJ)
check: check:
cppcheck main.c cppcheck main.c

View file

@ -1,3 +1,4 @@
#include <avr/eeprom.h>
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <util/delay.h> #include <util/delay.h>
@ -15,6 +16,9 @@
#define ENC_A (PINB & (1 << PB6)) #define ENC_A (PINB & (1 << PB6))
#define ENC_B (PINB & (1 << PB7)) #define ENC_B (PINB & (1 << PB7))
uint8_t EEMEM eeprom_contrast = 0;
uint8_t EEMEM eeprom_backlight = 0;
static const uint8_t sacred_chao[200] = { 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF0, 0xF8, 0xFC, 0xFC, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, 0xFE, 0xFC, 0xFC, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, static const uint8_t sacred_chao[200] = { 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF0, 0xF8, 0xFC, 0xFC, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, 0xFE, 0xFC, 0xFC, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00,
0x80, 0xF0, 0xFC, 0xFE, 0xFF, 0xFF, 0x7F, 0x3F, 0x1F, 0x3F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x1F, 0x0F, 0x0F, 0x07, 0x07, 0x07, 0x03, 0xC3, 0xE3, 0x73, 0x37, 0x17, 0x07, 0x0F, 0x1E, 0x3C, 0xF0, 0x80, 0x80, 0xF0, 0xFC, 0xFE, 0xFF, 0xFF, 0x7F, 0x3F, 0x1F, 0x3F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x1F, 0x0F, 0x0F, 0x07, 0x07, 0x07, 0x03, 0xC3, 0xE3, 0x73, 0x37, 0x17, 0x07, 0x0F, 0x1E, 0x3C, 0xF0, 0x80,
0xFF, 0xFF, 0xFF, 0xE7, 0xC3, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0xFC, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFC, 0x30, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xC3, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0xFC, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFC, 0x30, 0x00, 0x00, 0xFF,
@ -553,10 +557,12 @@ static void update_setup(enum input event) {
change_state(home); change_state(home);
break; break;
case change_contrast: case change_contrast:
eeprom_update_byte(&eeprom_contrast, value_contrast);
setup_state = contrast; setup_state = contrast;
lcd_setup(); lcd_setup();
break; break;
case change_backlight: case change_backlight:
eeprom_update_byte(&eeprom_backlight, value_backlight);
setup_state = backlight; setup_state = backlight;
lcd_setup(); lcd_setup();
break; break;
@ -571,10 +577,12 @@ static void update_setup(enum input event) {
break; break;
case change_contrast: case change_contrast:
setup_state = contrast; setup_state = contrast;
value_contrast = eeprom_read_byte(&eeprom_contrast);
lcd_setup(); lcd_setup();
break; break;
case change_backlight: case change_backlight:
setup_state = backlight; setup_state = backlight;
value_backlight = eeprom_read_byte(&eeprom_backlight);
lcd_setup(); lcd_setup();
break; break;
} }
@ -676,6 +684,10 @@ ISR(TIMER1_COMPA_vect) {
} }
int main(void) { int main(void) {
// Load contrast and backlight values from EEPROM
value_contrast = eeprom_read_byte(&eeprom_contrast);
value_backlight = eeprom_read_byte(&eeprom_backlight);
// FastPWM: 1.25kHz // FastPWM: 1.25kHz
// TODO: Try to get the backlit even more dim // TODO: Try to get the backlit even more dim
TCCR0A = (1 << WGM01) | (1 << WGM00) | (1 << COM0B1); TCCR0A = (1 << WGM01) | (1 << WGM00) | (1 << COM0B1);