diff --git a/Makefile.example b/Makefile.example new file mode 100644 index 0000000..43912aa --- /dev/null +++ b/Makefile.example @@ -0,0 +1,181 @@ +################################### +# Makefile for MightyPork/avr-lib # +# Revision 3 # +################################### + +## ===== CPU settings ===== + +# CPU type +MCU = atmega328p + +# CPU frequency +F_CPU = 16000000 + +# Fuses +LFUSE = 0xFF +HFUSE = 0xDE +EFUSE = 0x05 + +OPTIMIZE = s + + +## ===== Source files ===== + +# Main C file +MAIN = main.c + +# Library directory (with C and H files) +LIB_DIR = lib/ + +# C files in the library directory +LIB_C_FILES = uart.c iopins.c stream.c adc.c dht11.c sonar.c onewire.c spi.c sd.c fat16.c + +# C files that need aconfig file - uncomment when needed, but also add the configs! + +#LIB_C_FILES += lcd.c +#LIB_C_FILES += sipo_pwm.c +#LIB_C_FILES += color.c wsrgb.c +#LIB_C_FILES += debouce.c + + +LIB_H_FILES = adc.h calc.h dht11.h fat16.h fat16_internal.h iopins.h nsdelay.h onewire.h sd.h sonar.h spi.h stream.h uart.h +LIB_H_FILES += lcd.h color.h wsrgb.h debounce.h + + + + +## ===== Programmer ===== + +PROGRAMMER_TYPE = arduino +PROGRAMMER_ARGS = -b 57600 -P /dev/ttyUSB0 + + +## ===== C flags ===== + +CFLAGS = -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -I. -I$(LIB_DIR) +CFLAGS += -funsigned-char +CFLAGS += -funsigned-bitfields +CFLAGS += -fpack-struct +CFLAGS += -fshort-enums +CFLAGS += -finline-functions +CFLAGS += -ffunction-sections +CFLAGS += -fdata-sections +CFLAGS += -Wall +CFLAGS += -Wextra +CFLAGS += -Wno-main +CFLAGS += -Wno-comment +CFLAGS += -Wno-unused-but-set-variable +CFLAGS += -Wfatal-errors +CFLAGS += -Wl,--gc-sections +CFLAGS += -Wl,--relax +CFLAGS += -Wl,--relax + +#CFLAGS += -lm ## Math +#CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## Floating-point printf +#CFLAGS += -Wl,-u,vfprintf -lprintf_min ## Smaller printf + +CFLAGS_BUILD = $(CFLAGS) -O$(OPTIMIZE) + + +# --------------------------------------------------------------------------- + + +## Defined programs / locations +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +AVRSIZE = avr-size +AVRDUDE = avrdude +UART_TERM = gtkterm -p /dev/ttyUSB0 + +## === File lists === +TARGET = $(strip $(basename $(MAIN))) +SRC1 = $(TARGET).c +SRC = $(SRC1) +EXTRA_SOURCE = $(addprefix $(LIB_DIR), $(LIB_C_FILES)) +LIB_H_FILES_FILES = $(addprefix $(LIB_DIR), $(LIB_H_FILES)) +SRC += $(EXTRA_SOURCE) + +HEADERS = $(SRC:.c=.h) +OBJ = $(SRC:.c=.o) + + +## === File generation === +all: $(TARGET).hex size +pre: $(TARGET).pre + +%.hex: %.elf + $(OBJCOPY) -R .eeprom -O ihex $< $@ + +%.elf: $(SRC) $(LIB_H_FILES_FILES) Makefile + $(CC) $(CFLAGS_BUILD) $(SRC) --output $@ + +%.pre: $(SRC1) + $(CC) $(CFLAGS) -E $(SRC1) --output $@ + +%.eeprom: %.elf + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ + +%.lst: %.elf + $(OBJDUMP) -S $< > $@ + +# Show debug info +debug: + @echo + @echo "Source files:" $(SRC) + @echo "MCU, F_CPU, BAUD:" $(MCU), $(F_CPU), $(BAUD) + @echo + + +# Disassemble the ELF +disassemble: $(TARGET).lst +dis: disassemble +lst: disassemble + +# Make eeprom file +eeprom: $(TARGET).eeprom + +# Show how big the resulting program is +size: $(TARGET).elf + $(AVRSIZE) -C --mcu=$(MCU) $(TARGET).elf + +# Clean all produced trash +clean: + rm -f $(TARGET).elf $(TARGET).hex $(TARGET).obj \ + $(TARGET).o $(TARGET).d $(TARGET).eep $(TARGET).lst \ + $(TARGET).lss $(TARGET).sym $(TARGET).map $(TARGET)~ \ + $(TARGET).eeprom + +# Clean all trash +purge: + rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~ + + +## === avrdude === + +flash: $(TARGET).hex + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U flash:w:$< + +flashe: $(TARGET).eeprom + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U eeprom:w:$< + +shell: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nt + +fser: all flash ser + +ser: + $(UART_TERM) + +# === fuses === + +FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m + +fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) \ + $(PROGRAMMER_ARGS) $(FUSE_STRING) +show_fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nv + +set_default_fuses: FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m +set_default_fuses: fuses diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/examples/README.md b/examples/README.md index bc85502..d1f276b 100644 --- a/examples/README.md +++ b/examples/README.md @@ -3,6 +3,3 @@ Library usage examples Those examples should work, but they are mostly just a reference on how to use different parts of the library. - -To try to build and flash an example, first change the `MAIN` field in the Makefile -to it's name. diff --git a/Makefile b/examples/blinky/Makefile similarity index 97% rename from Makefile rename to examples/blinky/Makefile index 8f6ea67..43912aa 100644 --- a/Makefile +++ b/examples/blinky/Makefile @@ -30,12 +30,14 @@ LIB_DIR = lib/ # C files in the library directory LIB_C_FILES = uart.c iopins.c stream.c adc.c dht11.c sonar.c onewire.c spi.c sd.c fat16.c -# Extra Files that need config file: +# C files that need aconfig file - uncomment when needed, but also add the configs! #LIB_C_FILES += lcd.c +#LIB_C_FILES += sipo_pwm.c #LIB_C_FILES += color.c wsrgb.c #LIB_C_FILES += debouce.c + LIB_H_FILES = adc.h calc.h dht11.h fat16.h fat16_internal.h iopins.h nsdelay.h onewire.h sd.h sonar.h spi.h stream.h uart.h LIB_H_FILES += lcd.h color.h wsrgb.h debounce.h diff --git a/examples/blinky/lib b/examples/blinky/lib new file mode 120000 index 0000000..58677dd --- /dev/null +++ b/examples/blinky/lib @@ -0,0 +1 @@ +../../lib \ No newline at end of file diff --git a/examples/blinky/main.c b/examples/blinky/main.c new file mode 100644 index 0000000..1a9ffab --- /dev/null +++ b/examples/blinky/main.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include + +#include "lib/iopins.h" +#include "lib/uart.h" + +// Demo for ATMega328p on Arduino Pro Mini / Arduino Nano + +// Open serial port monitor to see debug messages. + +void main() +{ + // also a basic UART example, good for debugging + uart_init(9600); + uart_puts("Starting a blinking DEMO\r\n"); + + as_output(D13); // configure the pin D13 + + while(1) + { + uart_puts_P(PSTR("FOO\r\n")); // _P variant - string saved in program memory. Saves RAM space. + + toggle_pin(D13); // blink the LED + + _delay_ms(500); + } +} diff --git a/examples/Makefile b/examples/lcd_test/Makefile similarity index 57% rename from examples/Makefile rename to examples/lcd_test/Makefile index abf7935..3d014d8 100644 --- a/examples/Makefile +++ b/examples/lcd_test/Makefile @@ -1,60 +1,100 @@ -## === CPU settings === +################################### +# Makefile for MightyPork/avr-lib # +# Revision 3 # +################################### + +## ===== CPU settings ===== + # CPU type MCU = atmega328p + # CPU frequency F_CPU = 16000000 + # Fuses LFUSE = 0xFF HFUSE = 0xDE EFUSE = 0x05 +OPTIMIZE = s + + +## ===== Source files ===== -## === Source files === # Main C file -MAIN = sonar_to_lcd.c -# Extra C files in this folder -LOCAL_SOURCE = +MAIN = main.c + +# Library directory (with C and H files) +LIB_DIR = lib/ -# Library directory (with C files) -EXTRA_SOURCE_DIR = lib/ # C files in the library directory -EXTRA_SOURCE_FILES = uart.c lcd.c sonar.c stream.c +LIB_C_FILES = uart.c iopins.c stream.c adc.c dht11.c sonar.c onewire.c spi.c sd.c fat16.c + +# C files that need aconfig file - uncomment when needed, but also add the configs! + +LIB_C_FILES += lcd.c +#LIB_C_FILES += sipo_pwm.c +#LIB_C_FILES += color.c wsrgb.c +#LIB_C_FILES += debouce.c + + +LIB_H_FILES = adc.h calc.h dht11.h fat16.h fat16_internal.h iopins.h nsdelay.h onewire.h sd.h sonar.h spi.h stream.h uart.h +LIB_H_FILES += lcd.h color.h wsrgb.h debounce.h -## === Programmer === + + +## ===== Programmer ===== + PROGRAMMER_TYPE = arduino PROGRAMMER_ARGS = -b 57600 -P /dev/ttyUSB0 -## === C flags === +## ===== C flags ===== + +CFLAGS = -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -I. -I$(LIB_DIR) +CFLAGS += -funsigned-char +CFLAGS += -funsigned-bitfields +CFLAGS += -fpack-struct +CFLAGS += -fshort-enums +CFLAGS += -finline-functions +CFLAGS += -ffunction-sections +CFLAGS += -fdata-sections +CFLAGS += -Wall +CFLAGS += -Wextra +CFLAGS += -Wno-main +CFLAGS += -Wno-comment +CFLAGS += -Wno-unused-but-set-variable +CFLAGS += -Wfatal-errors +CFLAGS += -Wl,--gc-sections +CFLAGS += -Wl,--relax +CFLAGS += -Wl,--relax + +#CFLAGS += -lm ## Math +#CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## Floating-point printf +#CFLAGS += -Wl,-u,vfprintf -lprintf_min ## Smaller printf -CFLAGS = -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -I. -I$(EXTRA_SOURCE_DIR) -CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -CFLAGS += -Wall -Wno-main -Wno-strict-prototypes -Wno-comment -CFLAGS += -g2 -Wextra -Wfatal-errors -Wno-unused-but-set-variable -CFLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,--relax -# CFLAGS += -lm ## Math -# CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## for floating-point printf -# CFLAGS += -Wl,-u,vfprintf -lprintf_min ## for smaller printf -CFLAGS_BUILD = $(CFLAGS) -Os +CFLAGS_BUILD = $(CFLAGS) -O$(OPTIMIZE) # --------------------------------------------------------------------------- + ## Defined programs / locations CC = avr-gcc OBJCOPY = avr-objcopy OBJDUMP = avr-objdump AVRSIZE = avr-size AVRDUDE = avrdude +UART_TERM = gtkterm -p /dev/ttyUSB0 ## === File lists === TARGET = $(strip $(basename $(MAIN))) SRC1 = $(TARGET).c SRC = $(SRC1) -EXTRA_SOURCE = $(addprefix $(EXTRA_SOURCE_DIR), $(EXTRA_SOURCE_FILES)) +EXTRA_SOURCE = $(addprefix $(LIB_DIR), $(LIB_C_FILES)) +LIB_H_FILES_FILES = $(addprefix $(LIB_DIR), $(LIB_H_FILES)) SRC += $(EXTRA_SOURCE) -SRC += $(LOCAL_SOURCE) HEADERS = $(SRC:.c=.h) OBJ = $(SRC:.c=.o) @@ -67,7 +107,7 @@ pre: $(TARGET).pre %.hex: %.elf $(OBJCOPY) -R .eeprom -O ihex $< $@ -%.elf: $(SRC) +%.elf: $(SRC) $(LIB_H_FILES_FILES) Makefile $(CC) $(CFLAGS_BUILD) $(SRC) --output $@ %.pre: $(SRC1) @@ -87,12 +127,6 @@ debug: @echo -flser: all flash serial - -serial: - gtkterm -p /dev/ttyUSB0 - - # Disassemble the ELF disassemble: $(TARGET).lst dis: disassemble @@ -114,7 +148,7 @@ clean: # Clean all trash purge: - rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~ *.pre + rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~ ## === avrdude === @@ -128,6 +162,10 @@ flashe: $(TARGET).eeprom shell: $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nt +fser: all flash ser + +ser: + $(UART_TERM) # === fuses === diff --git a/examples/lcd_config.h b/examples/lcd_test/lcd_config.h similarity index 100% rename from examples/lcd_config.h rename to examples/lcd_test/lcd_config.h diff --git a/examples/lcd_test/lib b/examples/lcd_test/lib new file mode 120000 index 0000000..58677dd --- /dev/null +++ b/examples/lcd_test/lib @@ -0,0 +1 @@ +../../lib \ No newline at end of file diff --git a/examples/lcd_test.c b/examples/lcd_test/main.c similarity index 100% rename from examples/lcd_test.c rename to examples/lcd_test/main.c diff --git a/examples/lib b/examples/lib deleted file mode 120000 index 386215c..0000000 --- a/examples/lib +++ /dev/null @@ -1 +0,0 @@ -/home/ondra/elektro/avr-lib/lib \ No newline at end of file diff --git a/examples/sonar_lcd/Makefile b/examples/sonar_lcd/Makefile new file mode 100644 index 0000000..3d014d8 --- /dev/null +++ b/examples/sonar_lcd/Makefile @@ -0,0 +1,181 @@ +################################### +# Makefile for MightyPork/avr-lib # +# Revision 3 # +################################### + +## ===== CPU settings ===== + +# CPU type +MCU = atmega328p + +# CPU frequency +F_CPU = 16000000 + +# Fuses +LFUSE = 0xFF +HFUSE = 0xDE +EFUSE = 0x05 + +OPTIMIZE = s + + +## ===== Source files ===== + +# Main C file +MAIN = main.c + +# Library directory (with C and H files) +LIB_DIR = lib/ + +# C files in the library directory +LIB_C_FILES = uart.c iopins.c stream.c adc.c dht11.c sonar.c onewire.c spi.c sd.c fat16.c + +# C files that need aconfig file - uncomment when needed, but also add the configs! + +LIB_C_FILES += lcd.c +#LIB_C_FILES += sipo_pwm.c +#LIB_C_FILES += color.c wsrgb.c +#LIB_C_FILES += debouce.c + + +LIB_H_FILES = adc.h calc.h dht11.h fat16.h fat16_internal.h iopins.h nsdelay.h onewire.h sd.h sonar.h spi.h stream.h uart.h +LIB_H_FILES += lcd.h color.h wsrgb.h debounce.h + + + + +## ===== Programmer ===== + +PROGRAMMER_TYPE = arduino +PROGRAMMER_ARGS = -b 57600 -P /dev/ttyUSB0 + + +## ===== C flags ===== + +CFLAGS = -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -I. -I$(LIB_DIR) +CFLAGS += -funsigned-char +CFLAGS += -funsigned-bitfields +CFLAGS += -fpack-struct +CFLAGS += -fshort-enums +CFLAGS += -finline-functions +CFLAGS += -ffunction-sections +CFLAGS += -fdata-sections +CFLAGS += -Wall +CFLAGS += -Wextra +CFLAGS += -Wno-main +CFLAGS += -Wno-comment +CFLAGS += -Wno-unused-but-set-variable +CFLAGS += -Wfatal-errors +CFLAGS += -Wl,--gc-sections +CFLAGS += -Wl,--relax +CFLAGS += -Wl,--relax + +#CFLAGS += -lm ## Math +#CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## Floating-point printf +#CFLAGS += -Wl,-u,vfprintf -lprintf_min ## Smaller printf + +CFLAGS_BUILD = $(CFLAGS) -O$(OPTIMIZE) + + +# --------------------------------------------------------------------------- + + +## Defined programs / locations +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +AVRSIZE = avr-size +AVRDUDE = avrdude +UART_TERM = gtkterm -p /dev/ttyUSB0 + +## === File lists === +TARGET = $(strip $(basename $(MAIN))) +SRC1 = $(TARGET).c +SRC = $(SRC1) +EXTRA_SOURCE = $(addprefix $(LIB_DIR), $(LIB_C_FILES)) +LIB_H_FILES_FILES = $(addprefix $(LIB_DIR), $(LIB_H_FILES)) +SRC += $(EXTRA_SOURCE) + +HEADERS = $(SRC:.c=.h) +OBJ = $(SRC:.c=.o) + + +## === File generation === +all: $(TARGET).hex size +pre: $(TARGET).pre + +%.hex: %.elf + $(OBJCOPY) -R .eeprom -O ihex $< $@ + +%.elf: $(SRC) $(LIB_H_FILES_FILES) Makefile + $(CC) $(CFLAGS_BUILD) $(SRC) --output $@ + +%.pre: $(SRC1) + $(CC) $(CFLAGS) -E $(SRC1) --output $@ + +%.eeprom: %.elf + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ + +%.lst: %.elf + $(OBJDUMP) -S $< > $@ + +# Show debug info +debug: + @echo + @echo "Source files:" $(SRC) + @echo "MCU, F_CPU, BAUD:" $(MCU), $(F_CPU), $(BAUD) + @echo + + +# Disassemble the ELF +disassemble: $(TARGET).lst +dis: disassemble +lst: disassemble + +# Make eeprom file +eeprom: $(TARGET).eeprom + +# Show how big the resulting program is +size: $(TARGET).elf + $(AVRSIZE) -C --mcu=$(MCU) $(TARGET).elf + +# Clean all produced trash +clean: + rm -f $(TARGET).elf $(TARGET).hex $(TARGET).obj \ + $(TARGET).o $(TARGET).d $(TARGET).eep $(TARGET).lst \ + $(TARGET).lss $(TARGET).sym $(TARGET).map $(TARGET)~ \ + $(TARGET).eeprom + +# Clean all trash +purge: + rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~ + + +## === avrdude === + +flash: $(TARGET).hex + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U flash:w:$< + +flashe: $(TARGET).eeprom + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U eeprom:w:$< + +shell: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nt + +fser: all flash ser + +ser: + $(UART_TERM) + +# === fuses === + +FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m + +fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) \ + $(PROGRAMMER_ARGS) $(FUSE_STRING) +show_fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nv + +set_default_fuses: FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m +set_default_fuses: fuses diff --git a/examples/sonar_lcd/lcd_config.h b/examples/sonar_lcd/lcd_config.h new file mode 100644 index 0000000..68473b6 --- /dev/null +++ b/examples/sonar_lcd/lcd_config.h @@ -0,0 +1,11 @@ +#pragma once + +// Pin config file for LCD. + +#define LCD_RS 2 +#define LCD_RW 3 +#define LCD_E 4 +#define LCD_D4 5 +#define LCD_D5 6 +#define LCD_D6 7 +#define LCD_D7 8 diff --git a/examples/sonar_lcd/lib b/examples/sonar_lcd/lib new file mode 120000 index 0000000..58677dd --- /dev/null +++ b/examples/sonar_lcd/lib @@ -0,0 +1 @@ +../../lib \ No newline at end of file diff --git a/examples/sonar_to_lcd.c b/examples/sonar_lcd/main.c similarity index 100% rename from examples/sonar_to_lcd.c rename to examples/sonar_lcd/main.c diff --git a/examples/sonar_simple/Makefile b/examples/sonar_simple/Makefile new file mode 100644 index 0000000..43912aa --- /dev/null +++ b/examples/sonar_simple/Makefile @@ -0,0 +1,181 @@ +################################### +# Makefile for MightyPork/avr-lib # +# Revision 3 # +################################### + +## ===== CPU settings ===== + +# CPU type +MCU = atmega328p + +# CPU frequency +F_CPU = 16000000 + +# Fuses +LFUSE = 0xFF +HFUSE = 0xDE +EFUSE = 0x05 + +OPTIMIZE = s + + +## ===== Source files ===== + +# Main C file +MAIN = main.c + +# Library directory (with C and H files) +LIB_DIR = lib/ + +# C files in the library directory +LIB_C_FILES = uart.c iopins.c stream.c adc.c dht11.c sonar.c onewire.c spi.c sd.c fat16.c + +# C files that need aconfig file - uncomment when needed, but also add the configs! + +#LIB_C_FILES += lcd.c +#LIB_C_FILES += sipo_pwm.c +#LIB_C_FILES += color.c wsrgb.c +#LIB_C_FILES += debouce.c + + +LIB_H_FILES = adc.h calc.h dht11.h fat16.h fat16_internal.h iopins.h nsdelay.h onewire.h sd.h sonar.h spi.h stream.h uart.h +LIB_H_FILES += lcd.h color.h wsrgb.h debounce.h + + + + +## ===== Programmer ===== + +PROGRAMMER_TYPE = arduino +PROGRAMMER_ARGS = -b 57600 -P /dev/ttyUSB0 + + +## ===== C flags ===== + +CFLAGS = -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -I. -I$(LIB_DIR) +CFLAGS += -funsigned-char +CFLAGS += -funsigned-bitfields +CFLAGS += -fpack-struct +CFLAGS += -fshort-enums +CFLAGS += -finline-functions +CFLAGS += -ffunction-sections +CFLAGS += -fdata-sections +CFLAGS += -Wall +CFLAGS += -Wextra +CFLAGS += -Wno-main +CFLAGS += -Wno-comment +CFLAGS += -Wno-unused-but-set-variable +CFLAGS += -Wfatal-errors +CFLAGS += -Wl,--gc-sections +CFLAGS += -Wl,--relax +CFLAGS += -Wl,--relax + +#CFLAGS += -lm ## Math +#CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## Floating-point printf +#CFLAGS += -Wl,-u,vfprintf -lprintf_min ## Smaller printf + +CFLAGS_BUILD = $(CFLAGS) -O$(OPTIMIZE) + + +# --------------------------------------------------------------------------- + + +## Defined programs / locations +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +AVRSIZE = avr-size +AVRDUDE = avrdude +UART_TERM = gtkterm -p /dev/ttyUSB0 + +## === File lists === +TARGET = $(strip $(basename $(MAIN))) +SRC1 = $(TARGET).c +SRC = $(SRC1) +EXTRA_SOURCE = $(addprefix $(LIB_DIR), $(LIB_C_FILES)) +LIB_H_FILES_FILES = $(addprefix $(LIB_DIR), $(LIB_H_FILES)) +SRC += $(EXTRA_SOURCE) + +HEADERS = $(SRC:.c=.h) +OBJ = $(SRC:.c=.o) + + +## === File generation === +all: $(TARGET).hex size +pre: $(TARGET).pre + +%.hex: %.elf + $(OBJCOPY) -R .eeprom -O ihex $< $@ + +%.elf: $(SRC) $(LIB_H_FILES_FILES) Makefile + $(CC) $(CFLAGS_BUILD) $(SRC) --output $@ + +%.pre: $(SRC1) + $(CC) $(CFLAGS) -E $(SRC1) --output $@ + +%.eeprom: %.elf + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ + +%.lst: %.elf + $(OBJDUMP) -S $< > $@ + +# Show debug info +debug: + @echo + @echo "Source files:" $(SRC) + @echo "MCU, F_CPU, BAUD:" $(MCU), $(F_CPU), $(BAUD) + @echo + + +# Disassemble the ELF +disassemble: $(TARGET).lst +dis: disassemble +lst: disassemble + +# Make eeprom file +eeprom: $(TARGET).eeprom + +# Show how big the resulting program is +size: $(TARGET).elf + $(AVRSIZE) -C --mcu=$(MCU) $(TARGET).elf + +# Clean all produced trash +clean: + rm -f $(TARGET).elf $(TARGET).hex $(TARGET).obj \ + $(TARGET).o $(TARGET).d $(TARGET).eep $(TARGET).lst \ + $(TARGET).lss $(TARGET).sym $(TARGET).map $(TARGET)~ \ + $(TARGET).eeprom + +# Clean all trash +purge: + rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~ + + +## === avrdude === + +flash: $(TARGET).hex + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U flash:w:$< + +flashe: $(TARGET).eeprom + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U eeprom:w:$< + +shell: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nt + +fser: all flash ser + +ser: + $(UART_TERM) + +# === fuses === + +FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m + +fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) \ + $(PROGRAMMER_ARGS) $(FUSE_STRING) +show_fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nv + +set_default_fuses: FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m +set_default_fuses: fuses diff --git a/examples/sonar_simple/lib b/examples/sonar_simple/lib new file mode 120000 index 0000000..58677dd --- /dev/null +++ b/examples/sonar_simple/lib @@ -0,0 +1 @@ +../../lib \ No newline at end of file diff --git a/examples/sonar_simple.c b/examples/sonar_simple/main.c similarity index 97% rename from examples/sonar_simple.c rename to examples/sonar_simple/main.c index cb9db0e..46e94db 100644 --- a/examples/sonar_simple.c +++ b/examples/sonar_simple/main.c @@ -9,7 +9,6 @@ #include #include "lib/uart.h" -#include "lib/arduino_pins.h" #include "lib/sonar.h" #include "lib/stream.h" @@ -73,7 +72,6 @@ void main() int16_t res = sonar_result; - // Print if (res < 0) { put_str(uart, "NO OBSTACLES\r\n"); diff --git a/examples/uart_echo_rot13/Makefile b/examples/uart_echo_rot13/Makefile new file mode 100644 index 0000000..43912aa --- /dev/null +++ b/examples/uart_echo_rot13/Makefile @@ -0,0 +1,181 @@ +################################### +# Makefile for MightyPork/avr-lib # +# Revision 3 # +################################### + +## ===== CPU settings ===== + +# CPU type +MCU = atmega328p + +# CPU frequency +F_CPU = 16000000 + +# Fuses +LFUSE = 0xFF +HFUSE = 0xDE +EFUSE = 0x05 + +OPTIMIZE = s + + +## ===== Source files ===== + +# Main C file +MAIN = main.c + +# Library directory (with C and H files) +LIB_DIR = lib/ + +# C files in the library directory +LIB_C_FILES = uart.c iopins.c stream.c adc.c dht11.c sonar.c onewire.c spi.c sd.c fat16.c + +# C files that need aconfig file - uncomment when needed, but also add the configs! + +#LIB_C_FILES += lcd.c +#LIB_C_FILES += sipo_pwm.c +#LIB_C_FILES += color.c wsrgb.c +#LIB_C_FILES += debouce.c + + +LIB_H_FILES = adc.h calc.h dht11.h fat16.h fat16_internal.h iopins.h nsdelay.h onewire.h sd.h sonar.h spi.h stream.h uart.h +LIB_H_FILES += lcd.h color.h wsrgb.h debounce.h + + + + +## ===== Programmer ===== + +PROGRAMMER_TYPE = arduino +PROGRAMMER_ARGS = -b 57600 -P /dev/ttyUSB0 + + +## ===== C flags ===== + +CFLAGS = -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -I. -I$(LIB_DIR) +CFLAGS += -funsigned-char +CFLAGS += -funsigned-bitfields +CFLAGS += -fpack-struct +CFLAGS += -fshort-enums +CFLAGS += -finline-functions +CFLAGS += -ffunction-sections +CFLAGS += -fdata-sections +CFLAGS += -Wall +CFLAGS += -Wextra +CFLAGS += -Wno-main +CFLAGS += -Wno-comment +CFLAGS += -Wno-unused-but-set-variable +CFLAGS += -Wfatal-errors +CFLAGS += -Wl,--gc-sections +CFLAGS += -Wl,--relax +CFLAGS += -Wl,--relax + +#CFLAGS += -lm ## Math +#CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## Floating-point printf +#CFLAGS += -Wl,-u,vfprintf -lprintf_min ## Smaller printf + +CFLAGS_BUILD = $(CFLAGS) -O$(OPTIMIZE) + + +# --------------------------------------------------------------------------- + + +## Defined programs / locations +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +AVRSIZE = avr-size +AVRDUDE = avrdude +UART_TERM = gtkterm -p /dev/ttyUSB0 + +## === File lists === +TARGET = $(strip $(basename $(MAIN))) +SRC1 = $(TARGET).c +SRC = $(SRC1) +EXTRA_SOURCE = $(addprefix $(LIB_DIR), $(LIB_C_FILES)) +LIB_H_FILES_FILES = $(addprefix $(LIB_DIR), $(LIB_H_FILES)) +SRC += $(EXTRA_SOURCE) + +HEADERS = $(SRC:.c=.h) +OBJ = $(SRC:.c=.o) + + +## === File generation === +all: $(TARGET).hex size +pre: $(TARGET).pre + +%.hex: %.elf + $(OBJCOPY) -R .eeprom -O ihex $< $@ + +%.elf: $(SRC) $(LIB_H_FILES_FILES) Makefile + $(CC) $(CFLAGS_BUILD) $(SRC) --output $@ + +%.pre: $(SRC1) + $(CC) $(CFLAGS) -E $(SRC1) --output $@ + +%.eeprom: %.elf + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ + +%.lst: %.elf + $(OBJDUMP) -S $< > $@ + +# Show debug info +debug: + @echo + @echo "Source files:" $(SRC) + @echo "MCU, F_CPU, BAUD:" $(MCU), $(F_CPU), $(BAUD) + @echo + + +# Disassemble the ELF +disassemble: $(TARGET).lst +dis: disassemble +lst: disassemble + +# Make eeprom file +eeprom: $(TARGET).eeprom + +# Show how big the resulting program is +size: $(TARGET).elf + $(AVRSIZE) -C --mcu=$(MCU) $(TARGET).elf + +# Clean all produced trash +clean: + rm -f $(TARGET).elf $(TARGET).hex $(TARGET).obj \ + $(TARGET).o $(TARGET).d $(TARGET).eep $(TARGET).lst \ + $(TARGET).lss $(TARGET).sym $(TARGET).map $(TARGET)~ \ + $(TARGET).eeprom + +# Clean all trash +purge: + rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~ + + +## === avrdude === + +flash: $(TARGET).hex + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U flash:w:$< + +flashe: $(TARGET).eeprom + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U eeprom:w:$< + +shell: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nt + +fser: all flash ser + +ser: + $(UART_TERM) + +# === fuses === + +FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m + +fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) \ + $(PROGRAMMER_ARGS) $(FUSE_STRING) +show_fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nv + +set_default_fuses: FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m +set_default_fuses: fuses diff --git a/examples/uart_echo_rot13/lib b/examples/uart_echo_rot13/lib new file mode 120000 index 0000000..58677dd --- /dev/null +++ b/examples/uart_echo_rot13/lib @@ -0,0 +1 @@ +../../lib \ No newline at end of file diff --git a/examples/uart_echo_rot13.c b/examples/uart_echo_rot13/main.c similarity index 97% rename from examples/uart_echo_rot13.c rename to examples/uart_echo_rot13/main.c index f86f46c..c18e46a 100644 --- a/examples/uart_echo_rot13.c +++ b/examples/uart_echo_rot13/main.c @@ -2,7 +2,6 @@ #include #include -#include "lib/meta.h" #include "lib/uart.h" // diff --git a/examples/uart_isr/Makefile b/examples/uart_isr/Makefile new file mode 100644 index 0000000..43912aa --- /dev/null +++ b/examples/uart_isr/Makefile @@ -0,0 +1,181 @@ +################################### +# Makefile for MightyPork/avr-lib # +# Revision 3 # +################################### + +## ===== CPU settings ===== + +# CPU type +MCU = atmega328p + +# CPU frequency +F_CPU = 16000000 + +# Fuses +LFUSE = 0xFF +HFUSE = 0xDE +EFUSE = 0x05 + +OPTIMIZE = s + + +## ===== Source files ===== + +# Main C file +MAIN = main.c + +# Library directory (with C and H files) +LIB_DIR = lib/ + +# C files in the library directory +LIB_C_FILES = uart.c iopins.c stream.c adc.c dht11.c sonar.c onewire.c spi.c sd.c fat16.c + +# C files that need aconfig file - uncomment when needed, but also add the configs! + +#LIB_C_FILES += lcd.c +#LIB_C_FILES += sipo_pwm.c +#LIB_C_FILES += color.c wsrgb.c +#LIB_C_FILES += debouce.c + + +LIB_H_FILES = adc.h calc.h dht11.h fat16.h fat16_internal.h iopins.h nsdelay.h onewire.h sd.h sonar.h spi.h stream.h uart.h +LIB_H_FILES += lcd.h color.h wsrgb.h debounce.h + + + + +## ===== Programmer ===== + +PROGRAMMER_TYPE = arduino +PROGRAMMER_ARGS = -b 57600 -P /dev/ttyUSB0 + + +## ===== C flags ===== + +CFLAGS = -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -I. -I$(LIB_DIR) +CFLAGS += -funsigned-char +CFLAGS += -funsigned-bitfields +CFLAGS += -fpack-struct +CFLAGS += -fshort-enums +CFLAGS += -finline-functions +CFLAGS += -ffunction-sections +CFLAGS += -fdata-sections +CFLAGS += -Wall +CFLAGS += -Wextra +CFLAGS += -Wno-main +CFLAGS += -Wno-comment +CFLAGS += -Wno-unused-but-set-variable +CFLAGS += -Wfatal-errors +CFLAGS += -Wl,--gc-sections +CFLAGS += -Wl,--relax +CFLAGS += -Wl,--relax + +#CFLAGS += -lm ## Math +#CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## Floating-point printf +#CFLAGS += -Wl,-u,vfprintf -lprintf_min ## Smaller printf + +CFLAGS_BUILD = $(CFLAGS) -O$(OPTIMIZE) + + +# --------------------------------------------------------------------------- + + +## Defined programs / locations +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +AVRSIZE = avr-size +AVRDUDE = avrdude +UART_TERM = gtkterm -p /dev/ttyUSB0 + +## === File lists === +TARGET = $(strip $(basename $(MAIN))) +SRC1 = $(TARGET).c +SRC = $(SRC1) +EXTRA_SOURCE = $(addprefix $(LIB_DIR), $(LIB_C_FILES)) +LIB_H_FILES_FILES = $(addprefix $(LIB_DIR), $(LIB_H_FILES)) +SRC += $(EXTRA_SOURCE) + +HEADERS = $(SRC:.c=.h) +OBJ = $(SRC:.c=.o) + + +## === File generation === +all: $(TARGET).hex size +pre: $(TARGET).pre + +%.hex: %.elf + $(OBJCOPY) -R .eeprom -O ihex $< $@ + +%.elf: $(SRC) $(LIB_H_FILES_FILES) Makefile + $(CC) $(CFLAGS_BUILD) $(SRC) --output $@ + +%.pre: $(SRC1) + $(CC) $(CFLAGS) -E $(SRC1) --output $@ + +%.eeprom: %.elf + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ + +%.lst: %.elf + $(OBJDUMP) -S $< > $@ + +# Show debug info +debug: + @echo + @echo "Source files:" $(SRC) + @echo "MCU, F_CPU, BAUD:" $(MCU), $(F_CPU), $(BAUD) + @echo + + +# Disassemble the ELF +disassemble: $(TARGET).lst +dis: disassemble +lst: disassemble + +# Make eeprom file +eeprom: $(TARGET).eeprom + +# Show how big the resulting program is +size: $(TARGET).elf + $(AVRSIZE) -C --mcu=$(MCU) $(TARGET).elf + +# Clean all produced trash +clean: + rm -f $(TARGET).elf $(TARGET).hex $(TARGET).obj \ + $(TARGET).o $(TARGET).d $(TARGET).eep $(TARGET).lst \ + $(TARGET).lss $(TARGET).sym $(TARGET).map $(TARGET)~ \ + $(TARGET).eeprom + +# Clean all trash +purge: + rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~ + + +## === avrdude === + +flash: $(TARGET).hex + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U flash:w:$< + +flashe: $(TARGET).eeprom + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U eeprom:w:$< + +shell: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nt + +fser: all flash ser + +ser: + $(UART_TERM) + +# === fuses === + +FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m + +fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) \ + $(PROGRAMMER_ARGS) $(FUSE_STRING) +show_fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nv + +set_default_fuses: FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m +set_default_fuses: fuses diff --git a/examples/uart_isr/lib b/examples/uart_isr/lib new file mode 120000 index 0000000..58677dd --- /dev/null +++ b/examples/uart_isr/lib @@ -0,0 +1 @@ +../../lib \ No newline at end of file diff --git a/examples/uart_isr.c b/examples/uart_isr/main.c similarity index 100% rename from examples/uart_isr.c rename to examples/uart_isr/main.c diff --git a/examples/uart_keyhandler/Makefile b/examples/uart_keyhandler/Makefile new file mode 100644 index 0000000..43912aa --- /dev/null +++ b/examples/uart_keyhandler/Makefile @@ -0,0 +1,181 @@ +################################### +# Makefile for MightyPork/avr-lib # +# Revision 3 # +################################### + +## ===== CPU settings ===== + +# CPU type +MCU = atmega328p + +# CPU frequency +F_CPU = 16000000 + +# Fuses +LFUSE = 0xFF +HFUSE = 0xDE +EFUSE = 0x05 + +OPTIMIZE = s + + +## ===== Source files ===== + +# Main C file +MAIN = main.c + +# Library directory (with C and H files) +LIB_DIR = lib/ + +# C files in the library directory +LIB_C_FILES = uart.c iopins.c stream.c adc.c dht11.c sonar.c onewire.c spi.c sd.c fat16.c + +# C files that need aconfig file - uncomment when needed, but also add the configs! + +#LIB_C_FILES += lcd.c +#LIB_C_FILES += sipo_pwm.c +#LIB_C_FILES += color.c wsrgb.c +#LIB_C_FILES += debouce.c + + +LIB_H_FILES = adc.h calc.h dht11.h fat16.h fat16_internal.h iopins.h nsdelay.h onewire.h sd.h sonar.h spi.h stream.h uart.h +LIB_H_FILES += lcd.h color.h wsrgb.h debounce.h + + + + +## ===== Programmer ===== + +PROGRAMMER_TYPE = arduino +PROGRAMMER_ARGS = -b 57600 -P /dev/ttyUSB0 + + +## ===== C flags ===== + +CFLAGS = -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -I. -I$(LIB_DIR) +CFLAGS += -funsigned-char +CFLAGS += -funsigned-bitfields +CFLAGS += -fpack-struct +CFLAGS += -fshort-enums +CFLAGS += -finline-functions +CFLAGS += -ffunction-sections +CFLAGS += -fdata-sections +CFLAGS += -Wall +CFLAGS += -Wextra +CFLAGS += -Wno-main +CFLAGS += -Wno-comment +CFLAGS += -Wno-unused-but-set-variable +CFLAGS += -Wfatal-errors +CFLAGS += -Wl,--gc-sections +CFLAGS += -Wl,--relax +CFLAGS += -Wl,--relax + +#CFLAGS += -lm ## Math +#CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## Floating-point printf +#CFLAGS += -Wl,-u,vfprintf -lprintf_min ## Smaller printf + +CFLAGS_BUILD = $(CFLAGS) -O$(OPTIMIZE) + + +# --------------------------------------------------------------------------- + + +## Defined programs / locations +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +AVRSIZE = avr-size +AVRDUDE = avrdude +UART_TERM = gtkterm -p /dev/ttyUSB0 + +## === File lists === +TARGET = $(strip $(basename $(MAIN))) +SRC1 = $(TARGET).c +SRC = $(SRC1) +EXTRA_SOURCE = $(addprefix $(LIB_DIR), $(LIB_C_FILES)) +LIB_H_FILES_FILES = $(addprefix $(LIB_DIR), $(LIB_H_FILES)) +SRC += $(EXTRA_SOURCE) + +HEADERS = $(SRC:.c=.h) +OBJ = $(SRC:.c=.o) + + +## === File generation === +all: $(TARGET).hex size +pre: $(TARGET).pre + +%.hex: %.elf + $(OBJCOPY) -R .eeprom -O ihex $< $@ + +%.elf: $(SRC) $(LIB_H_FILES_FILES) Makefile + $(CC) $(CFLAGS_BUILD) $(SRC) --output $@ + +%.pre: $(SRC1) + $(CC) $(CFLAGS) -E $(SRC1) --output $@ + +%.eeprom: %.elf + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ + +%.lst: %.elf + $(OBJDUMP) -S $< > $@ + +# Show debug info +debug: + @echo + @echo "Source files:" $(SRC) + @echo "MCU, F_CPU, BAUD:" $(MCU), $(F_CPU), $(BAUD) + @echo + + +# Disassemble the ELF +disassemble: $(TARGET).lst +dis: disassemble +lst: disassemble + +# Make eeprom file +eeprom: $(TARGET).eeprom + +# Show how big the resulting program is +size: $(TARGET).elf + $(AVRSIZE) -C --mcu=$(MCU) $(TARGET).elf + +# Clean all produced trash +clean: + rm -f $(TARGET).elf $(TARGET).hex $(TARGET).obj \ + $(TARGET).o $(TARGET).d $(TARGET).eep $(TARGET).lst \ + $(TARGET).lss $(TARGET).sym $(TARGET).map $(TARGET)~ \ + $(TARGET).eeprom + +# Clean all trash +purge: + rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~ + + +## === avrdude === + +flash: $(TARGET).hex + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U flash:w:$< + +flashe: $(TARGET).eeprom + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U eeprom:w:$< + +shell: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nt + +fser: all flash ser + +ser: + $(UART_TERM) + +# === fuses === + +FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m + +fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) \ + $(PROGRAMMER_ARGS) $(FUSE_STRING) +show_fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nv + +set_default_fuses: FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m +set_default_fuses: fuses diff --git a/examples/uart_keyhandler/lib b/examples/uart_keyhandler/lib new file mode 120000 index 0000000..58677dd --- /dev/null +++ b/examples/uart_keyhandler/lib @@ -0,0 +1 @@ +../../lib \ No newline at end of file diff --git a/examples/uart_keyhandler.c b/examples/uart_keyhandler/main.c similarity index 84% rename from examples/uart_keyhandler.c rename to examples/uart_keyhandler/main.c index 337b3b1..19d3a55 100644 --- a/examples/uart_keyhandler.c +++ b/examples/uart_keyhandler/main.c @@ -10,10 +10,7 @@ // Example of asynchronous UART key handling // // It recognizes special keys like arrows and some F keys, -// check the header file for full list. -// -// You need uart_ansi for this. -// +// check uart.h file for full list. ISR(USART_RX_vect) { @@ -27,8 +24,8 @@ ISR(USART_RX_vect) void key_handler(uint8_t code, bool special) { put_str_P(uart, special ? PSTR("Special: ") : PSTR("Char: ")); - put_char(uart, code); // the actual character - put_char(uart, ' '); // space + put_c(uart, code); // the actual character + put_c(uart, ' '); // space put_u8(uart, code); // as number put_nl(uart); // crlf } diff --git a/examples/uart_simple/Makefile b/examples/uart_simple/Makefile new file mode 100644 index 0000000..43912aa --- /dev/null +++ b/examples/uart_simple/Makefile @@ -0,0 +1,181 @@ +################################### +# Makefile for MightyPork/avr-lib # +# Revision 3 # +################################### + +## ===== CPU settings ===== + +# CPU type +MCU = atmega328p + +# CPU frequency +F_CPU = 16000000 + +# Fuses +LFUSE = 0xFF +HFUSE = 0xDE +EFUSE = 0x05 + +OPTIMIZE = s + + +## ===== Source files ===== + +# Main C file +MAIN = main.c + +# Library directory (with C and H files) +LIB_DIR = lib/ + +# C files in the library directory +LIB_C_FILES = uart.c iopins.c stream.c adc.c dht11.c sonar.c onewire.c spi.c sd.c fat16.c + +# C files that need aconfig file - uncomment when needed, but also add the configs! + +#LIB_C_FILES += lcd.c +#LIB_C_FILES += sipo_pwm.c +#LIB_C_FILES += color.c wsrgb.c +#LIB_C_FILES += debouce.c + + +LIB_H_FILES = adc.h calc.h dht11.h fat16.h fat16_internal.h iopins.h nsdelay.h onewire.h sd.h sonar.h spi.h stream.h uart.h +LIB_H_FILES += lcd.h color.h wsrgb.h debounce.h + + + + +## ===== Programmer ===== + +PROGRAMMER_TYPE = arduino +PROGRAMMER_ARGS = -b 57600 -P /dev/ttyUSB0 + + +## ===== C flags ===== + +CFLAGS = -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -I. -I$(LIB_DIR) +CFLAGS += -funsigned-char +CFLAGS += -funsigned-bitfields +CFLAGS += -fpack-struct +CFLAGS += -fshort-enums +CFLAGS += -finline-functions +CFLAGS += -ffunction-sections +CFLAGS += -fdata-sections +CFLAGS += -Wall +CFLAGS += -Wextra +CFLAGS += -Wno-main +CFLAGS += -Wno-comment +CFLAGS += -Wno-unused-but-set-variable +CFLAGS += -Wfatal-errors +CFLAGS += -Wl,--gc-sections +CFLAGS += -Wl,--relax +CFLAGS += -Wl,--relax + +#CFLAGS += -lm ## Math +#CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## Floating-point printf +#CFLAGS += -Wl,-u,vfprintf -lprintf_min ## Smaller printf + +CFLAGS_BUILD = $(CFLAGS) -O$(OPTIMIZE) + + +# --------------------------------------------------------------------------- + + +## Defined programs / locations +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +AVRSIZE = avr-size +AVRDUDE = avrdude +UART_TERM = gtkterm -p /dev/ttyUSB0 + +## === File lists === +TARGET = $(strip $(basename $(MAIN))) +SRC1 = $(TARGET).c +SRC = $(SRC1) +EXTRA_SOURCE = $(addprefix $(LIB_DIR), $(LIB_C_FILES)) +LIB_H_FILES_FILES = $(addprefix $(LIB_DIR), $(LIB_H_FILES)) +SRC += $(EXTRA_SOURCE) + +HEADERS = $(SRC:.c=.h) +OBJ = $(SRC:.c=.o) + + +## === File generation === +all: $(TARGET).hex size +pre: $(TARGET).pre + +%.hex: %.elf + $(OBJCOPY) -R .eeprom -O ihex $< $@ + +%.elf: $(SRC) $(LIB_H_FILES_FILES) Makefile + $(CC) $(CFLAGS_BUILD) $(SRC) --output $@ + +%.pre: $(SRC1) + $(CC) $(CFLAGS) -E $(SRC1) --output $@ + +%.eeprom: %.elf + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ + +%.lst: %.elf + $(OBJDUMP) -S $< > $@ + +# Show debug info +debug: + @echo + @echo "Source files:" $(SRC) + @echo "MCU, F_CPU, BAUD:" $(MCU), $(F_CPU), $(BAUD) + @echo + + +# Disassemble the ELF +disassemble: $(TARGET).lst +dis: disassemble +lst: disassemble + +# Make eeprom file +eeprom: $(TARGET).eeprom + +# Show how big the resulting program is +size: $(TARGET).elf + $(AVRSIZE) -C --mcu=$(MCU) $(TARGET).elf + +# Clean all produced trash +clean: + rm -f $(TARGET).elf $(TARGET).hex $(TARGET).obj \ + $(TARGET).o $(TARGET).d $(TARGET).eep $(TARGET).lst \ + $(TARGET).lss $(TARGET).sym $(TARGET).map $(TARGET)~ \ + $(TARGET).eeprom + +# Clean all trash +purge: + rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~ + + +## === avrdude === + +flash: $(TARGET).hex + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U flash:w:$< + +flashe: $(TARGET).eeprom + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U eeprom:w:$< + +shell: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nt + +fser: all flash ser + +ser: + $(UART_TERM) + +# === fuses === + +FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m + +fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) \ + $(PROGRAMMER_ARGS) $(FUSE_STRING) +show_fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nv + +set_default_fuses: FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m +set_default_fuses: fuses diff --git a/examples/uart_simple/lib b/examples/uart_simple/lib new file mode 120000 index 0000000..58677dd --- /dev/null +++ b/examples/uart_simple/lib @@ -0,0 +1 @@ +../../lib \ No newline at end of file diff --git a/examples/uart_simple.c b/examples/uart_simple/main.c similarity index 92% rename from examples/uart_simple.c rename to examples/uart_simple/main.c index 0c86e76..ab80fee 100644 --- a/examples/uart_simple.c +++ b/examples/uart_simple/main.c @@ -3,11 +3,8 @@ // #include -#include #include -#include #include -#include #include "lib/uart.h" diff --git a/examples/uart_stream/Makefile b/examples/uart_stream/Makefile new file mode 100644 index 0000000..43912aa --- /dev/null +++ b/examples/uart_stream/Makefile @@ -0,0 +1,181 @@ +################################### +# Makefile for MightyPork/avr-lib # +# Revision 3 # +################################### + +## ===== CPU settings ===== + +# CPU type +MCU = atmega328p + +# CPU frequency +F_CPU = 16000000 + +# Fuses +LFUSE = 0xFF +HFUSE = 0xDE +EFUSE = 0x05 + +OPTIMIZE = s + + +## ===== Source files ===== + +# Main C file +MAIN = main.c + +# Library directory (with C and H files) +LIB_DIR = lib/ + +# C files in the library directory +LIB_C_FILES = uart.c iopins.c stream.c adc.c dht11.c sonar.c onewire.c spi.c sd.c fat16.c + +# C files that need aconfig file - uncomment when needed, but also add the configs! + +#LIB_C_FILES += lcd.c +#LIB_C_FILES += sipo_pwm.c +#LIB_C_FILES += color.c wsrgb.c +#LIB_C_FILES += debouce.c + + +LIB_H_FILES = adc.h calc.h dht11.h fat16.h fat16_internal.h iopins.h nsdelay.h onewire.h sd.h sonar.h spi.h stream.h uart.h +LIB_H_FILES += lcd.h color.h wsrgb.h debounce.h + + + + +## ===== Programmer ===== + +PROGRAMMER_TYPE = arduino +PROGRAMMER_ARGS = -b 57600 -P /dev/ttyUSB0 + + +## ===== C flags ===== + +CFLAGS = -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -I. -I$(LIB_DIR) +CFLAGS += -funsigned-char +CFLAGS += -funsigned-bitfields +CFLAGS += -fpack-struct +CFLAGS += -fshort-enums +CFLAGS += -finline-functions +CFLAGS += -ffunction-sections +CFLAGS += -fdata-sections +CFLAGS += -Wall +CFLAGS += -Wextra +CFLAGS += -Wno-main +CFLAGS += -Wno-comment +CFLAGS += -Wno-unused-but-set-variable +CFLAGS += -Wfatal-errors +CFLAGS += -Wl,--gc-sections +CFLAGS += -Wl,--relax +CFLAGS += -Wl,--relax + +#CFLAGS += -lm ## Math +#CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## Floating-point printf +#CFLAGS += -Wl,-u,vfprintf -lprintf_min ## Smaller printf + +CFLAGS_BUILD = $(CFLAGS) -O$(OPTIMIZE) + + +# --------------------------------------------------------------------------- + + +## Defined programs / locations +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +AVRSIZE = avr-size +AVRDUDE = avrdude +UART_TERM = gtkterm -p /dev/ttyUSB0 + +## === File lists === +TARGET = $(strip $(basename $(MAIN))) +SRC1 = $(TARGET).c +SRC = $(SRC1) +EXTRA_SOURCE = $(addprefix $(LIB_DIR), $(LIB_C_FILES)) +LIB_H_FILES_FILES = $(addprefix $(LIB_DIR), $(LIB_H_FILES)) +SRC += $(EXTRA_SOURCE) + +HEADERS = $(SRC:.c=.h) +OBJ = $(SRC:.c=.o) + + +## === File generation === +all: $(TARGET).hex size +pre: $(TARGET).pre + +%.hex: %.elf + $(OBJCOPY) -R .eeprom -O ihex $< $@ + +%.elf: $(SRC) $(LIB_H_FILES_FILES) Makefile + $(CC) $(CFLAGS_BUILD) $(SRC) --output $@ + +%.pre: $(SRC1) + $(CC) $(CFLAGS) -E $(SRC1) --output $@ + +%.eeprom: %.elf + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ + +%.lst: %.elf + $(OBJDUMP) -S $< > $@ + +# Show debug info +debug: + @echo + @echo "Source files:" $(SRC) + @echo "MCU, F_CPU, BAUD:" $(MCU), $(F_CPU), $(BAUD) + @echo + + +# Disassemble the ELF +disassemble: $(TARGET).lst +dis: disassemble +lst: disassemble + +# Make eeprom file +eeprom: $(TARGET).eeprom + +# Show how big the resulting program is +size: $(TARGET).elf + $(AVRSIZE) -C --mcu=$(MCU) $(TARGET).elf + +# Clean all produced trash +clean: + rm -f $(TARGET).elf $(TARGET).hex $(TARGET).obj \ + $(TARGET).o $(TARGET).d $(TARGET).eep $(TARGET).lst \ + $(TARGET).lss $(TARGET).sym $(TARGET).map $(TARGET)~ \ + $(TARGET).eeprom + +# Clean all trash +purge: + rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~ + + +## === avrdude === + +flash: $(TARGET).hex + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U flash:w:$< + +flashe: $(TARGET).eeprom + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U eeprom:w:$< + +shell: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nt + +fser: all flash ser + +ser: + $(UART_TERM) + +# === fuses === + +FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m + +fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) \ + $(PROGRAMMER_ARGS) $(FUSE_STRING) +show_fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nv + +set_default_fuses: FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m +set_default_fuses: fuses diff --git a/examples/uart_stream/lib b/examples/uart_stream/lib new file mode 120000 index 0000000..58677dd --- /dev/null +++ b/examples/uart_stream/lib @@ -0,0 +1 @@ +../../lib \ No newline at end of file diff --git a/examples/uart_stream.c b/examples/uart_stream/main.c similarity index 89% rename from examples/uart_stream.c rename to examples/uart_stream/main.c index 63e1491..52c29f6 100644 --- a/examples/uart_stream.c +++ b/examples/uart_stream/main.c @@ -3,11 +3,8 @@ // #include -#include #include -#include #include -#include #include "lib/uart.h" #include "lib/stream.h"