Skip to content

Commit

Permalink
Use mcu core on basic board
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterdevinck committed Nov 2, 2018
1 parent d16cbb2 commit 90340aa
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 48 deletions.
8 changes: 3 additions & 5 deletions make/lamp.mk
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ clean:
rm -rf $(BUILD_DIR)
rm -rf $(ARTIFACT_DIR)

# TODO move flash-mcu and monitor-mcu to specific makefile, like done for flash-mcu-basic

.PHONY: flash-mcu
flash-mcu:
$(IDF_PATH)/components/esptool_py/esptool/esptool.py \
Expand All @@ -64,11 +66,7 @@ monitor-mcu:

.PHONY: flash-mcu-basic
flash-mcu-basic:
avrdude -p m328 -c usbasp -B 5 -U flash:w:$(BUILD_DIR)/mcu-basic/lamp.hex

.PHONY: fuses-mcu-basic
fuses-mcu-basic:
avrdude -p m328 -c usbasp -B 5 -U lfuse:w:0xdf:m
$(MAKE) -f $(SCRIPT_DIR)/mcu-basic.mk flash

.PHONY: menuconfig
menuconfig:
Expand Down
67 changes: 54 additions & 13 deletions make/mcu-basic.mk
Original file line number Diff line number Diff line change
@@ -1,23 +1,64 @@
PROJ = lamp
DEVICE = atmega328
PROJ = lamp
OSC = 16000000UL
LFUSE = 0xdf
DEVICE = atmega328
PROGDEV = m328
PROGVAR = usbasp

SRCDIR = src/mcu-basic/
OUTDIR = build/mcu-basic/
G++ = avr-g++
SIZE = avr-size
OBJCOPY = avr-objcopy
PROG = avrdude

SRCDIR = src/mcu-basic/
COREDIR = src/mcu/core/
STLDIR = src/mcu-basic/stl/
OUTDIR = build/mcu-basic/

C++FLAGS = -Os
C++FLAGS += -Wall
C++FLAGS += -std=c++11
C++FLAGS += -DF_CPU=$(OSC)
C++FLAGS += -DBASIC
C++FLAGS += -mmcu=$(DEVICE)
C++FLAGS += -I$(STLDIR)
C++FLAGS += -I$(COREDIR)
C++FLAGS += -fno-exceptions

OBJECTS = \
$(patsubst $(SRCDIR)%.cpp,$(OUTDIR)%.o,$(wildcard $(SRCDIR)*.cpp)) \
$(patsubst $(COREDIR)%.cpp,$(OUTDIR)%.o,$(wildcard $(COREDIR)*.cpp))

.PHONY: all
all: $(OUTDIR)$(PROJ).hex

$(OUTDIR)%.o: $(SRCDIR)%.c
mkdir -p $(OUTDIR)
avr-gcc -g -Os -mmcu=$(DEVICE) -o $@ -c $<
$(OUTDIR)%.hex: $(OUTDIR)%.elf
$(OBJCOPY) -j .text -j .data -O ihex $< $@

$(OUTDIR)%.elf: $(OUTDIR)%.o
avr-gcc -g -mmcu=$(DEVICE) -o $@ $<
avr-size --format=avr --mcu=$(DEVICE) $@
$(OUTDIR)%.elf: $(OBJECTS)
$(G++) $(C++FLAGS) $(OBJECTS) --output $@
$(SIZE) --format=avr --mcu=$(DEVICE) $@

$(OUTDIR)%.hex: $(OUTDIR)%.elf
avr-objcopy -j .text -j .data -O ihex $< $@
$(OUTDIR)%.o: $(SRCDIR)%.cpp
mkdir -p $(OUTDIR)
$(G++) $< $(C++FLAGS) -c -o $@

$(OUTDIR)%.o: $(COREDIR)%.cpp
mkdir -p $(OUTDIR)
$(G++) $< $(C++FLAGS) -c -o $@

.PHONY: clean
clean:
rm -rf $(OUTDIR)
rm -rf $(OUTDIR)

.PHONY: list
list:
echo $(OBJECTS)

.PHONY: flash
flash:
$(PROG) -p $(PROGDEV) -c $(PROGVAR) -B 5 -U flash:w:$(OUTDIR)$(PROJ).hex

.PHONY: fuses
fuses:
$(PROG) -p $(PROGDEV) -c $(PROGVAR) -B 5 -U lfuse:w:$(LFUSE):m
37 changes: 37 additions & 0 deletions src/mcu-basic/RgbLed.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "RgbLed.h"
#include <avr/io.h>

using namespace lamp;

// PD3 - OC2B - red
// PD5 - OC0B - green
// PD6 - OC0A - blue

RgbLed::RgbLed() {
DDRD = (1 << PD3) | (1 << PD5) | (1 << PD6);
TCCR0A = (1 << WGM00) | (1 << WGM01);
TCCR0B = (1 << CS00);
TCCR2A = (1 << WGM20) | (1 << WGM21);
TCCR2B = (1 << CS20);
}

void RgbLed::setLedColor(RgbLedColor color) {
if(color.r == 0) {
TCCR2A &= ~(1 << COM2B1);
} else {
TCCR2A |= (1 << COM2B1);
OCR2B = color.r;
}
if(color.g == 0) {
TCCR0A &= ~(1 << COM0B1);
} else {
TCCR0A |= (1 << COM0B1);
OCR0B = color.g;
}
if(color.b == 0) {
TCCR0A &= ~(1 << COM0A1);
} else {
TCCR0A |= (1 << COM0A1);
OCR0A = color.b;
}
}
16 changes: 16 additions & 0 deletions src/mcu-basic/RgbLed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "IRgbLed.h"

namespace lamp {

class RgbLed : public IRgbLed {

public:
explicit RgbLed();

void setLedColor(RgbLedColor color) override;

};

}
20 changes: 0 additions & 20 deletions src/mcu-basic/lamp.c

This file was deleted.

35 changes: 35 additions & 0 deletions src/mcu-basic/lamp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "new.h"
//#include <util/delay.h>

#include "RgbLed.h"
#include "Lamp.h"

using namespace lamp;

extern "C" {
int main(void);
}

int main(void) {
auto led = new RgbLed();
auto lamp = new Lamp(led);
lamp->start(0);
while(1){
/*for(uint8_t i = 0; i < 255; i++) {
led->setLedColor({ i, 0, 0 });
_delay_ms(10);
}
for(uint8_t i = 0; i < 255; i++) {
led->setLedColor({ 0, i, 0 });
_delay_ms(10);
};
for(uint8_t i = 0; i < 255; i++) {
led->setLedColor({ 0, 0, i });
_delay_ms(10);
}
for(uint8_t i = 0; i < 255; i++) {
led->setLedColor({ i, i, i });
_delay_ms(10);
}*/
}
}
1 change: 1 addition & 0 deletions src/mcu-basic/stl/cstdint
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <stdint.h>
17 changes: 17 additions & 0 deletions src/mcu-basic/stl/new.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdlib.h>

void *operator new(size_t size) {
return malloc(size);
}

void *operator new[](size_t size) {
return malloc(size);
}

void operator delete(void * ptr) {
free(ptr);
}

void operator delete[](void * ptr) {
free(ptr);
}
6 changes: 5 additions & 1 deletion src/mcu/core/HttpHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#ifndef BASIC

#include "HttpHandler.h"

using namespace lamp;
Expand Down Expand Up @@ -48,4 +50,6 @@ string HttpHandler::handleHttpRequest(string method, string path) {
" </footer>\n"
"</body>\n"
"</html>\n";
}
}

#endif
14 changes: 12 additions & 2 deletions src/mcu/core/Lamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using namespace lamp;

#ifndef BASIC
Lamp::Lamp(
IUpdater* updater,
ILogger* logger,
Expand All @@ -19,11 +20,20 @@ Lamp::Lamp(
_upgrade = new UpgradeManager(updater, logger, httpclient);
_handler = new HttpHandler(led, _upgrade);
}
#else
Lamp::Lamp(
IRgbLed* led
) {
_led = led;
}
#endif

void Lamp::start(const int port) const {
#ifndef BASIC
_logger->logInfo("Lamp", "Lamp is starting");
const RgbLedColor color = { 0, 0, 255 };
_led->setLedColor(color);
_httpserver->start(port, _handler);
_upgrade->boot();
#endif
const RgbLedColor color = { 0, 0, 255 };
_led->setLedColor(color);
}
22 changes: 16 additions & 6 deletions src/mcu/core/Lamp.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
#pragma once

#include <cstdint>

#include "IRgbLed.h"

#ifndef BASIC
#include "IUpdater.h"
#include "ILogger.h"
#include "IHttpServer.h"
#include "IHttpClient.h"
#include "IRgbLed.h"
#include "ILedBoardChain.h"
#include "HttpHandler.h"
#include "UpgradeManager.h"

#include <cstdint>

// #include <memory>
// TODO use shared_ptr
#endif

namespace lamp {

class Lamp {

public:
#ifndef BASIC
explicit Lamp(
IUpdater* updater,
ILogger* logger,
Expand All @@ -27,19 +30,26 @@ namespace lamp {
IRgbLed* led,
ILedBoardChain* leds
);
#else
explicit Lamp(
IRgbLed* led
);
#endif

void start(int port) const;

private:
IRgbLed* _led;
#ifndef BASIC
IUpdater* _updater;
ILogger* _logger;
IHttpServer* _httpserver;
IHttpClient* _httpclient;
IRgbLed* _led;
ILedBoardChain* _leds;
IHttpHandler* _handler;
UpgradeManager* _upgrade;
#endif

};

}
}
6 changes: 5 additions & 1 deletion src/mcu/core/UpgradeManager.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#ifndef BASIC

#include "UpgradeManager.h"

#include <sstream>
Expand Down Expand Up @@ -50,4 +52,6 @@ bool UpgradeManager::upgrade(string url) const {

string UpgradeManager::getVersion() const {
return _updater->getRunningVersion();
}
}

#endif

0 comments on commit 90340aa

Please sign in to comment.