Refactor project structure

This commit is contained in:
Rihards Skuja 2017-11-24 13:29:36 +02:00
parent 3ad4a834b9
commit 99b1d29b4c
No known key found for this signature in database
GPG Key ID: 53FA13A3F7F8571B
7 changed files with 116 additions and 155 deletions

57
.gitignore vendored
View File

@ -1,55 +1,2 @@
### C ### build/*
# Prerequisites obj/*
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
*.eep

View File

@ -2,12 +2,17 @@
# is public domain), believed to be neutral to any flavor of "make" # is public domain), believed to be neutral to any flavor of "make"
# (GNU make, BSD make, SysV make) # (GNU make, BSD make, SysV make)
SRC_DIR = src
BUILD_DIR = build
INC_DIR = include
OBJ_DIR = obj
MCU = atmega328p MCU = atmega328p
FORMAT = ihex FORMAT = ihex
TARGET = main TARGET = main
SRC = $(TARGET).c SRC = $(wildcard $(SRC_DIR)/*.c)
ASRC = OBJ = $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
LST = $(SRC:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.lst)
OPT = s OPT = s
# Name of this Makefile (used for "make depend"). # Name of this Makefile (used for "make depend").
@ -29,8 +34,7 @@ CSTANDARD = -std=gnu99
CDEFS = -DF_CPU=1000000L -D__AVR__ -D__AVR_ATmega328P__ CDEFS = -DF_CPU=1000000L -D__AVR__ -D__AVR_ATmega328P__
# Place -I options here # Place -I options here
CINCS = CINCS = -I $(INC_DIR)
CDEBUG = -g$(DEBUG) CDEBUG = -g$(DEBUG)
CWARN = -Wall -Wextra -Wstrict-prototypes CWARN = -Wall -Wextra -Wstrict-prototypes
@ -77,13 +81,12 @@ EXTMEMOPTS =
#LDMAP = $(LDFLAGS) -Wl,-Map=$(TARGET).map,--cref #LDMAP = $(LDFLAGS) -Wl,-Map=$(TARGET).map,--cref
LDFLAGS = $(EXTMEMOPTS) $(LDMAP) $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB) LDFLAGS = $(EXTMEMOPTS) $(LDMAP) $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
# Programming support using avrdude. Settings and variables. # Programming support using avrdude. Settings and variables.
AVRDUDE_PROGRAMMER = usbasp AVRDUDE_PROGRAMMER = usbasp
AVRDUDE_PORT = usb AVRDUDE_PORT = usb
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex AVRDUDE_WRITE_FLASH = -U flash:w:$(BUILD_DIR)/$(TARGET).hex
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
@ -102,7 +105,8 @@ AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
#AVRDUDE_VERBOSE = -v -v #AVRDUDE_VERBOSE = -v -v
AVRDUDE_BASIC = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) AVRDUDE_BASIC = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
AVRDUDE_FLAGS = $(AVRDUDE_BASIC) $(AVRDUDE_NO_VERIFY) $(AVRDUDE_VERBOSE) $(AVRDUDE_ERASE_COUNTER) AVRDUDE_FLAGS = $(AVRDUDE_BASIC) $(AVRDUDE_NO_VERIFY) $(AVRDUDE_VERBOSE) \
$(AVRDUDE_ERASE_COUNTER)
CC = avr-gcc CC = avr-gcc
@ -111,35 +115,28 @@ OBJDUMP = avr-objdump
SIZE = avr-size SIZE = avr-size
NM = avr-nm NM = avr-nm
AVRDUDE = avrdude AVRDUDE = avrdude
REMOVE = rm -f REMOVE = rm -rf
MV = mv -f MV = mv -f
# Define all object files.
OBJ = $(SRC:.c=.o) $(ASRC:.S=.o)
# Define all listing files.
LST = $(ASRC:.S=.lst) $(SRC:.c=.lst)
# Combine all necessary flags and optional flags. # Combine all necessary flags and optional flags.
# Add target processor to flags. # Add target processor to flags.
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) ALL_CFLAGS = -mmcu=$(MCU) $(CFLAGS)
ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
# Default target. # Default target.
all: build all: build
build: elf hex eep build: elf hex eep lss sym
elf: $(TARGET).elf elf: $(BUILD_DIR)/$(TARGET).elf
hex: $(TARGET).hex hex: $(BUILD_DIR)/$(TARGET).hex
eep: $(TARGET).eep eep: $(BUILD_DIR)/$(TARGET).eep
lss: $(TARGET).lss lss: $(BUILD_DIR)/$(TARGET).lss
sym: $(TARGET).sym sym: $(BUILD_DIR)/$(TARGET).sym
# Program the device. # Program the device.
program: $(TARGET).hex $(TARGET).eep program: $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).eep
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM) $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
@ -153,59 +150,49 @@ COFFCONVERT=$(OBJCOPY) --debugging \
--change-section-address .eeprom-0x810000 --change-section-address .eeprom-0x810000
coff: $(TARGET).elf coff: $(BUILD_DIR)/$(TARGET).elf
$(COFFCONVERT) -O coff-avr $(TARGET).elf $(TARGET).cof $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
extcoff: $(TARGET).elf extcoff: $(BUILD_DIR)/$(TARGET).elf
$(COFFCONVERT) -O coff-ext-avr $(TARGET).elf $(TARGET).cof $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
.SUFFIXES: .elf .hex .eep .lss .sym .SUFFIXES: .elf .hex .eep .lss .sym
.elf.hex: %.hex: %.elf
$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@ $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
.elf.eep: %.eep: %.elf
-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \ -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 -O $(FORMAT) $< $@ --change-section-lma .eeprom=0 -O $(FORMAT) $< $@
# Create extended listing file from ELF output file. # Create extended listing file from ELF output file.
.elf.lss: %.lss: %.elf
$(OBJDUMP) -h -S $< > $@ $(OBJDUMP) -h -S $< > $@
# Create a symbol table from ELF output file. # Create a symbol table from ELF output file.
.elf.sym: %.sym: %.elf
$(NM) -n $< > $@ $(NM) -n $< > $@
# Link: create ELF output file from object files. # Link: create ELF output file from object files.
$(TARGET).elf: $(OBJ) .SECONDARY: $(OBJ)
$(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS) $(BUILD_DIR)/%.elf: $(OBJ)
@mkdir -p $(@D)
$(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
# Compile: create object files from C source files. # Compile: create object files from C source files.
.c.o: $(OBJ_DIR)/%.o : $(SRC_DIR)/%.c
@mkdir -p $(@D)
$(CC) -c $(ALL_CFLAGS) $< -o $@ $(CC) -c $(ALL_CFLAGS) $< -o $@
# Compile: create assembler files from C source files.
.c.s:
$(CC) -S $(ALL_CFLAGS) $< -o $@
# Assemble: create object files from assembler source files.
.S.o:
$(CC) -c $(ALL_ASFLAGS) $< -o $@
# Target: clean project. # Target: clean project.
clean: clean:
$(REMOVE) $(TARGET).hex $(TARGET).eep $(TARGET).cof $(TARGET).elf \ $(REMOVE) $(BUILD_DIR)/* $(OBJ_DIR)/*
$(TARGET).map $(TARGET).sym $(TARGET).lss \
$(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d)
depend: depend:
if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \ if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \
@ -216,6 +203,6 @@ depend:
fi fi
echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \ echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \
>> $(MAKEFILE); \ >> $(MAKEFILE); \
$(CC) -M -mmcu=$(MCU) $(CDEFS) $(CINCS) $(SRC) $(ASRC) >> $(MAKEFILE) $(CC) -M -mmcu=$(MCU) $(CDEFS) $(CINCS) $(SRC) >> $(MAKEFILE)
.PHONY: all build elf hex eep lss sym program coff extcoff clean depend .PHONY: all build elf hex eep lss sym program coff extcoff clean depend

View File

@ -1,10 +1,16 @@
#ifndef PORTS_H #ifndef PORTS_H
#define PORTS_H #define PORTS_H
#define LED_1 PD3 /* DS18B20+ is configured in DS18B20 library */
#define SERVO_L PD5
/* Signal LED */
#define LED_1 PD2
/* Servo motors */
#define SERVO_L PB1
#define SERVO_R PD6 #define SERVO_R PD6
#define TEMP PC0
/* Photo sensors */
#define PH_1 PC1 #define PH_1 PC1
#define PH_2 PC2 #define PH_2 PC2
#define PH_3 PC3 #define PH_3 PC3

12
include/main.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef MAIN_H
#define MAIN_H
typedef unsigned char byte;
void initIO(void);
void blinkLed(void);
void readTemp(void);
void readPhoto(void);
void runServos(void);
#endif

21
main.c
View File

@ -1,32 +1,17 @@
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h> #include <util/delay.h>
#include "main.h" #include "main.h"
#include "io.h" #include "io.h"
#define LED_DELAY 100 void initIO()
void blinkLed(void)
{ {
DDRD |= (1 << LED_1); DDRD |= (1 << LED_1);
while (1) {
PORTD |= (1 << LED_1);
_delay_ms(LED_DELAY);
PORTD &= ~(1 << LED_1);
_delay_ms(LED_DELAY);
}
}
void runServos(void)
{
DDRD |= (1 << SERVO_L) | (1 << SERVO_R);
while (1) {
PORTD |= (1 << SERVO_L) | (1 << SERVO_R);
}
} }
int main(void) int main(void)
{ {
blinkLed(); initIO();
return 0; return 0;
} }

25
main.h
View File

@ -1,25 +0,0 @@
#ifndef MAIN_H
#define MAIN_H
#include <stdint.h>
void blinkLed(void);
void readTemp(void);
void readPhoto(void);
void runServos(void);
/// Set pin value to high
void setPinHigh(volatile uint8_t *port, int pin);
/// Set pin value to low
void setPinLow(volatile uint8_t *port, int pin);
void setPinHigh(volatile uint8_t *port, int pin)
{
*port |= (1 << pin);
}
void setPinLow(volatile uint8_t *port, int pin)
{
*port &= ~(1 << pin);
}
#endif

49
src/main.c Normal file
View File

@ -0,0 +1,49 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
/* https://davidegironi.blogspot.com/2014/09/a-ds18b20-1-wire-digital-thermometer.html */
#include "ds18b20.h"
/* https://github.com/kehribar/nrf24L01_plus */
#include "main.h"
#include "io.h"
void initIO()
{
DDRB |= (1 << SERVO_L);
TCCR1A |= (1 << COM1A1) | (1 << WGM10);
TCCR1B |= (1 << CS10) | (1 << WGM12);
DDRD |= (1 << SERVO_R);
TCCR0A |= (1 << COM0A1) | (1 << WGM00);
TCCR0B |= (1 << CS00) | (1 << WGM02);
DDRD |= (1 << LED_1);
}
void readTemp()
{
double d;
sei();
for (;;) {
d = ds18b20_gettemp();
if (d >= 21)
PORTD |= (1 << LED_1);
else
PORTD &= ~(1 << LED_1);
_delay_ms(500);
}
}
int main(void)
{
initIO();
readTemp();
/* Full speed */
OCR1A = 0x00;
OCR0A = 0x00;
return 0;
}