Skip to content

Commit

Permalink
tests: test the bootloading process
Browse files Browse the repository at this point in the history
  • Loading branch information
paradajz committed Jul 26, 2021
1 parent c64770d commit 38466f8
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@
"ringbuffer",
"system",
"leds",
"hw"
"hw",
"bootloader"
]
},
{
Expand Down
1 change: 0 additions & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ endif
@$(BUILD_DIR_BASE)/sysexgen/$(TARGET)/$(BUILD_TYPE)/$(TARGET).elf $(BUILD_DIR)/$(TARGET)$(SYSEX_BINARY_SUFFIX).bin $(BUILD_DIR)/$(TARGET).sysex.syx
#ascii sysex file for use with web configurator
@hexdump -v -e '/1 "%02x "' $(BUILD_DIR)/$(TARGET).sysex.syx | sed 's/f7/f7\n/g' | sed 's/^ *//' | tr a-z A-Z > $(BUILD_DIR)/$(TARGET).sysex
@rm $(BUILD_DIR)/$(TARGET)$(SYSEX_BINARY_SUFFIX).bin
@echo SysEx file created: $(BUILD_DIR)/$(TARGET).sysex

pre-build: $(PINS_GEN_SOURCE) $(TSCREEN_GEN_SOURCE)
Expand Down
12 changes: 9 additions & 3 deletions tests/Sources.mk
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@ modules/u8g2/csrc/u8x8_gpio.c \
modules/u8g2/csrc/u8x8_d_ssd1306_128x64_noname.c \
modules/u8g2/csrc/u8x8_d_ssd1306_128x32.c

ifeq ($(ARCH),stm32)
ifeq ($(ARCH), avr)
SOURCES_COMMON += board/$(ARCH)/common/FlashPages.cpp
else
SOURCES_COMMON += $(MCU_DIR)/FlashPages.cpp
endif

#common include dirs
INCLUDE_DIRS_COMMON := \
-I"./" \
-I"./unity" \
-I"../src/application/" \
-I"../modules/" \
-I"../src/" \
-I"../modules/"
-I"../src/bootloader/" \
-I"../src/application/" \
-I"../src/board/$(ARCH)/variants/$(MCU_FAMILY)" \
-I"../src/$(MCU_DIR)" \
-I"../src/$(BOARD_TARGET_DIR)/"
7 changes: 7 additions & 0 deletions tests/src/bootloader/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
vpath application/%.cpp ../src
vpath bootloader/%.cpp ../src
vpath common/%.cpp ../src

SOURCES_$(shell basename $(dir $(lastword $(MAKEFILE_LIST)))) := \
bootloader/SysExParser/SysExParser.cpp \
bootloader/updater/Updater.cpp
159 changes: 159 additions & 0 deletions tests/src/bootloader/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#include "unity/Framework.h"
#include "SysExParser/SysExParser.h"
#include "updater/Updater.h"
#include "board/Board.h"
#include "board/Internal.h"

#include <iostream>
#include <fstream>
#include <vector>
#include <array>
#include <iterator>
#include <string>
#include <cstddef>

namespace
{
class BTLDRWriter : public Updater::BTLDRWriter
{
public:
uint32_t pageSize(size_t index) override
{
return Board::detail::map::flashPageDescriptor(index).size;
}

void erasePage(size_t index) override
{
}

void fillPage(size_t index, uint32_t address, uint16_t data) override
{
writtenBytes.push_back(data & 0xFF);
writtenBytes.push_back(data >> 8);
}

void writePage(size_t index) override
{
}

void apply() override
{
updated = true;
}

std::vector<uint8_t> writtenBytes = {};
bool updated = false;
};

const std::string fw_build_dir = "../src/build/merged/";
const std::string fw_build_type_subdir = "release/";

SysExParser _sysExParser;
BTLDRWriter _btldrWriter;
Updater _updater = Updater(_btldrWriter, COMMAND_FW_UPDATE_START, COMMAND_FW_UPDATE_END, FW_UID);

void sysExToUSBMidi(std::vector<uint8_t> sysex, std::vector<MIDI::USBMIDIpacket_t>& packets)
{
class MIDIHWA : public MIDI::HWA
{
public:
MIDIHWA(std::vector<MIDI::USBMIDIpacket_t>& packets)
: _packets(packets)
{}

bool init() override
{
return true;
}

bool dinRead(uint8_t& data) override
{
return false;
}

bool dinWrite(uint8_t data) override
{
return false;
}

bool usbRead(MIDI::USBMIDIpacket_t& USBMIDIpacket) override
{
return false;
}

bool usbWrite(MIDI::USBMIDIpacket_t& USBMIDIpacket) override
{
_packets.push_back(USBMIDIpacket);
return true;
}

private:
std::vector<MIDI::USBMIDIpacket_t>& _packets;
};

MIDIHWA midiHWA(packets);
MIDI midi(midiHWA);

midi.init();
midi.enableUSBMIDI();

midi.sendSysEx(sysex.size(), &sysex[0], true);
}

} // namespace

TEST_CASE(Bootloader)
{
std::string syxPath = fw_build_dir + BOARD_STRING + "/" + fw_build_type_subdir + BOARD_STRING + ".sysex.syx";
std::string binaryPath = fw_build_dir + BOARD_STRING + "/" + fw_build_type_subdir + BOARD_STRING + "_sysex.bin";

std::ifstream sysExStream(syxPath, std::ios::in | std::ios::binary);
std::vector<uint8_t> sysExVector((std::istreambuf_iterator<char>(sysExStream)), std::istreambuf_iterator<char>());
std::ifstream binaryStream(binaryPath, std::ios::in | std::ios::binary);
std::vector<uint8_t> binaryVector((std::istreambuf_iterator<char>(binaryStream)), std::istreambuf_iterator<char>());

std::vector<uint8_t> singleSysExMsg = {};
std::vector<MIDI::USBMIDIpacket_t> packets = {};

//go over the entire .sysex file
//upon reaching the end of single sysex message, convert it
//into series of USB MIDI packets
for (size_t i = 0; i < sysExVector.size(); i++)
{
singleSysExMsg.push_back(sysExVector.at(i));

if (sysExVector.at(i) == 0xF7)
{
sysExToUSBMidi(singleSysExMsg, packets);
singleSysExMsg.clear();
}
}

//now we have the entire file in form of USB MIDI packets
//parse each message and once parsing passes, feed the parsed data into fw updater
for (size_t packet = 0; packet < packets.size(); packet++)
{
if (_sysExParser.isValidMessage(packets.at(packet)))
{
size_t dataSize = _sysExParser.dataBytes();
uint8_t data = 0;

if (dataSize)
{
for (size_t i = 0; i < dataSize; i++)
{
if (_sysExParser.value(i, data))
{
_updater.feed(data);
}
}
}
}
}

//once all data has been fed into updater, firmware update procedure should be complete
TEST_ASSERT(_btldrWriter.updated == true);

//written content should also match the original binary file from which .syx file has been created
TEST_ASSERT(_btldrWriter.writtenBytes == binaryVector);
}

0 comments on commit 38466f8

Please sign in to comment.