Created
May 27, 2014 00:10
-
-
Save hadleyrich/24c3482bb1c602c9c256 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Example of how to use the avr-libc EEPROM functions; | |
* http://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html | |
* | |
*/ | |
#define F_CPU 32000000 | |
#include <avr/io.h> | |
#include <avr/interrupt.h> | |
#include <avr/eeprom.h> | |
#include <util/delay.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include "usart.h" | |
#include "clock.h" | |
#define EXPECTED_MAGIC 27 | |
#define STRING_LEN 13 | |
uint8_t eeprom_magic_byte EEMEM = 0; | |
uint16_t eeprom_big EEMEM = 0; | |
uint32_t eeprom_bigger EEMEM = 0; | |
char eeprom_string[STRING_LEN] EEMEM = ""; | |
uint8_t magic_byte = 0; | |
uint16_t big = 0; | |
uint32_t bigger = 0; | |
char string[STRING_LEN] = ""; | |
int main(void) { | |
sysclk_init(); | |
sei(); | |
// configure the usart1 (attached to USB) for 9600,8,N,1 to | |
// show debugging info, see usart examples for explanation | |
usart_init(1, 128, 128); | |
usart_conf(1, 9600, 8, none, 1, 0, NULL); | |
usart_map_stdio(1); | |
usart_run(1); | |
// wait for EEPROM to be ready | |
eeprom_busy_wait(); | |
// Delay to give time for you to open a terminal and see the output | |
_delay_ms(2000); | |
// read magic byte from EEPROM to see if we've ever run before | |
magic_byte = eeprom_read_byte(&eeprom_magic_byte); // single byte | |
if (magic_byte != EXPECTED_MAGIC) { | |
puts("No magic found, setting values to defaults.\r"); | |
puts("Reset your Kakapo to run again.\r"); | |
magic_byte = EXPECTED_MAGIC; | |
big = 12345; | |
bigger = 1234567890UL; | |
strncpy(string, "Hello World!\0", STRING_LEN); | |
// update values to EEPROM | |
// the _update_ functions will test whether the existing EEPROM value is the same | |
// before writing to save flash wear, there are also _write_ functions which do not | |
eeprom_update_byte(&eeprom_magic_byte, magic_byte); | |
eeprom_update_word(&eeprom_big, big); | |
eeprom_update_dword(&eeprom_bigger, bigger); | |
eeprom_update_block((void*)&string, (void*)&eeprom_string, STRING_LEN); | |
} else { | |
puts("Magic found, reading values from EEPROM;\r"); | |
big = eeprom_read_word(&eeprom_big); // word | |
bigger = eeprom_read_dword(&eeprom_bigger); // long integer | |
eeprom_read_block((void*)&string, (void*)&eeprom_string, STRING_LEN); // char array | |
_delay_ms(100); | |
printf("magic_byte = %u\r\n", magic_byte); | |
printf("big = %u\r\n", big); | |
printf("bigger = %lu\r\n", bigger); | |
printf("string = %s\r\n", string); | |
} | |
puts("========================\r\n"); | |
_delay_ms(100); // let the characters go out the usart | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Where is the toolchain unpacked? | |
TOOLBASE ?= /usr/local/share/avr8-gnu-toolchain-linux_x86_64 | |
# What MCU do we have on this board? | |
MCU ?= atxmega64d4 | |
# Port that Kakapo popped up on | |
PORT ?= /dev/ttyUSB1 | |
# Application name | |
APP = eeprom | |
# If using libkakapo, uncomment | |
LIBKAKAPO = -lkakapo | |
#Tools we'll need | |
CC = $(TOOLBASE)/bin/avr-gcc | |
AVRDUDE = /usr/bin/avrdude | |
OBJCOPY = $(TOOLBASE)/bin/avr-objcopy | |
CFLAGS = -Os --std=c99 -funroll-loops -funsigned-char -funsigned-bitfields -fpack-struct | |
CFLAGS += -fshort-enums -Wstrict-prototypes -Wall -mcall-prologues -I. -I../../ | |
CFLAGS += -mmcu=$(MCU) | |
INCLUDE = -L../../ | |
OBJ = $(patsubst %.c,%.o,$(wildcard *.c)) | |
build: $(APP).hex | |
eeprom: $(APP).eep | |
$(AVRDUDE) -p $(MCU) -c avr109 -P $(PORT) -b 115200 -U eeprom:w:$(APP).eep | |
program: $(APP).hex | |
$(AVRDUDE) -p $(MCU) -c avr109 -P $(PORT) -b 115200 -U flash:w:$(APP).hex -e | |
$(APP).hex: $(APP).elf | |
$(OBJCOPY) -O ihex -R .eeprom $< $@ | |
$(APP).eep : $(APP).elf | |
$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \ | |
--change-section-lma .eeprom=0 -O ihex $< $@ | |
$(APP).elf: $(OBJ) | |
$(CC) $(CFLAGS) $(INCLUDE) $^ -o $@ $(LIBKAKAPO) | |
%.o: %.c %.h Makefile | |
$(CC) -c $(CFLAGS) $< -o $@ | |
clean: | |
rm -f $(OBJ) $(APP).hex $(APP).elf $(APP).eep |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment