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 ###
# Prerequisites
*.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
build/*
obj/*

View File

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

View File

@ -1,10 +1,16 @@
#ifndef PORTS_H
#define PORTS_H
#define LED_1 PD3
#define SERVO_L PD5
/* DS18B20+ is configured in DS18B20 library */
/* Signal LED */
#define LED_1 PD2
/* Servo motors */
#define SERVO_L PB1
#define SERVO_R PD6
#define TEMP PC0
/* Photo sensors */
#define PH_1 PC1
#define PH_2 PC2
#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/interrupt.h>
#include <util/delay.h>
#include "main.h"
#include "io.h"
#define LED_DELAY 100
void blinkLed(void)
void initIO()
{
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)
{
blinkLed();
initIO();
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;
}