Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jsphuebner/stm32-sine
Browse files Browse the repository at this point in the history
jsphuebner committed Aug 27, 2024
2 parents a0045bd + 5847e01 commit 0945b42
Showing 8 changed files with 1,028 additions and 41 deletions.
16 changes: 7 additions & 9 deletions .github/workflows/CI-build.yml
Original file line number Diff line number Diff line change
@@ -51,13 +51,11 @@ jobs:
with:
name: FOC firmware hex
path: stm32_foc.hex

# Unit tests are currently broken so don't build and run them for now
#- name: Build unit tests on host
# run: |
# make -C test
#
# - name: Run unit tests on host
# run: |
# test/test_sine

- name: Build unit tests on host
run: |
make -C test
- name: Run unit tests on host
run: |
test/test_sine
11 changes: 9 additions & 2 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -3,13 +3,20 @@ CPP = g++
LD = g++
CP = cp
CFLAGS = -std=c99 -ggdb -DSTM32F1 -I../include -I../libopeninv/include -I../libopencm3/include
CPPFLAGS = -ggdb -DSTM32F1 -DCONTROL_FOC=1 -DCONTROL_SINE=0 -DCONTROL=DCONTROL_FOC -I../include -I../libopeninv/include -I../libopencm3/include
CPPFLAGS = -ggdb -DSTM32F1 -DCTRL_FOC=1 -DCTRL_SINE=0 -DCTRL=CTRL_FOC -I../include -I../libopeninv/include -I../libopencm3/include
LDFLAGS = -g
BINARY = test_sine
OBJS = test_main.o fu.o test_fu.o test_fp.o test_vcu.o my_fp.o my_string.o params.o vehiclecontrol.o \
test_throttle.o throttle.o sine_core.o temp_meas.o
test_throttle.o throttle.o sine_core.o temp_meas.o stub_canhardware.o test_canmap.o canmap.o \
stub_libopencm3.o
VPATH = ../src ../libopeninv/src

# Check if the variable GITHUB_RUN_NUMBER exists. When running on the github actions running, this
# variable is automatically available.
# Create a compiler define with the content of the variable. Or, if it does not exist, use replacement value 99999.
CPPFLAGS += $(shell \
if [ -z "$$GITHUB_RUN_NUMBER" ]; then echo "-DGITHUB_RUN_NUMBER=0"; else echo "-DGITHUB_RUN_NUMBER=$$GITHUB_RUN_NUMBER"; fi )

all: $(BINARY)

$(BINARY): $(OBJS)
45 changes: 45 additions & 0 deletions test/stub_canhardware.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* This file is part of the stm32-sine project.
*
* Copyright (C) 2021 Johannes Huebner <dev@johanneshuebner.com>
* Copyright (C) 2024 David J. Fiddes <D.J@fiddes.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stub_canhardware.h"

CanCallback* vcuCan = nullptr;
uint32_t vcuCanId;

CanHardware::CanHardware()
{}

bool CanHardware::AddCallback(CanCallback* cb)
{
vcuCan = cb;
return true;
}

bool CanHardware::RegisterUserMessage(uint32_t canId, uint32_t mask)
{
vcuCanId = canId;
return true;
}

void CanHardware::ClearUserMessages() {}

void CanHardware::HandleRx(uint32_t canId, uint32_t data[2], uint8_t dlc)
{
vcuCan->HandleRx(canId, data, dlc);
}
48 changes: 48 additions & 0 deletions test/stub_canhardware.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This file is part of the stm32-sine project.
*
* Copyright (C) 2021 Johannes Huebner <dev@johanneshuebner.com>
* Copyright (C) 2024 David J. Fiddes <D.J@fiddes.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TEST_CANHARDWARE_H
#define TEST_CANHARDWARE_H

#include "canhardware.h"
#include <stdint.h>
#include <string.h>
#include <array>

class CanStub: public CanHardware
{
void SetBaudrate(enum baudrates baudrate) {}
void Send(uint32_t canId, uint32_t data[2], uint8_t len)
{
m_canId = canId;
memcpy(&m_data[0], &data[0], sizeof(m_data));
m_len = len;
}
virtual void ConfigureFilters() {}

public:
std::array<uint8_t, 8> m_data;
uint8_t m_len;
uint32_t m_canId;
};

extern CanCallback* vcuCan;
extern uint32_t vcuCanId;

#endif // TEST_CANHARDWARE_H
49 changes: 49 additions & 0 deletions test/stub_libopencm3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* This file is part of the stm32-sine project.
*
* Copyright (C) 2024 David J. Fiddes <D.J@fiddes.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdint.h"

void flash_unlock(void)
{
}

void flash_lock(void)
{
}

void flash_set_ws(uint32_t ws)
{
}

void flash_program_word(uint32_t address, uint32_t data)
{
}

void flash_erase_page(uint32_t page_address)
{
}

uint16_t desig_get_flash_size(void)
{
return 8;
}

uint32_t crc_calculate(uint32_t data)
{
return 0xaa55;
}
2 changes: 1 addition & 1 deletion test/test.h
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ extern int _failedAssertions;
std::cout << "Test " << __FILE__ << "::" << __func__ << " passed." << std::endl; \
else \
{ \
std::cout << "Assertion failed: " << STRING(c) << " in " __FILE__ " : " << __LINE__ << std::endl; \
std::cout << "Assertion failed: " << STRING(c) << " in " __FILE__ " : " << std::dec << __LINE__ << std::endl; \
_failedAssertions++; \
}

862 changes: 862 additions & 0 deletions test/test_canmap.cpp

Large diffs are not rendered by default.

36 changes: 7 additions & 29 deletions test/test_vcu.cpp
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
#include "hwdefs.h"
#include "pwmgeneration.h"
#include "vehiclecontrol.h"
#include "stub_canhardware.h"
#include "test.h"

static uint32_t crc32_word(uint32_t Crc, uint32_t Data);
@@ -32,8 +33,6 @@ static uint32_t crc = 0;
static uint32_t rtc = 0;
static uint32_t speed = 0;
static ERROR_MESSAGE_NUM errorMessage;
static CanCallback* vcuCan = 0;
static uint32_t vcuCanId;

class VCUTest: public UnitTest
{
@@ -42,13 +41,6 @@ class VCUTest: public UnitTest
virtual void TestCaseSetup();
};

class CanStub: public CanHardware
{
void SetBaudrate(enum baudrates baudrate) {}
void Send(uint32_t canId, uint32_t data[2], uint8_t len) {}
virtual void ConfigureFilters() {}
};

static void FillInCanData(uint32_t* data, uint32_t pot, uint32_t pot2, uint32_t canio, uint32_t cruisespeed, uint32_t regenPreset, uint32_t seq)
{
uint32_t crc = 0xFFFFFFFF;
@@ -71,7 +63,7 @@ static void CanTest2()

FillInCanData(data, 100, 200, CAN_IO_FWD, 1000, 50, 1);

vcuCan->HandleRx(vcuCanId, data);
vcuCan->HandleRx(vcuCanId, data, 8);
VehicleControl::GetDigInputs();
VehicleControl::ProcessThrottle();

@@ -89,7 +81,7 @@ static void CanTest3()
FillInCanData(data, 100, 200, CAN_IO_START, 1000, 50, 1);
Param::SetInt(Param::potmode, POTMODE_DUALCHANNEL | POTMODE_CAN);

vcuCan->HandleRx(vcuCanId, data);
vcuCan->HandleRx(vcuCanId, data, 8);
VehicleControl::GetDigInputs();
VehicleControl::ProcessThrottle();

@@ -106,10 +98,10 @@ static void TestCanSeqError1()

Param::SetInt(Param::potmode, POTMODE_DUALCHANNEL | POTMODE_CAN);
FillInCanData(data, 100, 200, CAN_IO_START, 1000, 60, 1);
vcuCan->HandleRx(vcuCanId, data);
vcuCan->HandleRx(vcuCanId, data, 8);
//call again with same sequence counter -> triggers an error message
FillInCanData(data, 100, 200, CAN_IO_START, 1000, 60, 1);
vcuCan->HandleRx(vcuCanId, data);
vcuCan->HandleRx(vcuCanId, data, 8);

ASSERT(errorMessage == ERR_CANCOUNTER);
ASSERT(Param::GetInt(Param::regenpreset) == 60); //Only after 5 errors will this be reset to 0
@@ -125,7 +117,7 @@ static void TestCanSeqError2()
{
//Call 6 times with same sequence counter -> will trigger unrecoverable error
FillInCanData(data, 100, 200, CAN_IO_START, 1000, 60, 1);
vcuCan->HandleRx(vcuCanId, data);
vcuCan->HandleRx(vcuCanId, data, 8);
}

//Now simulate 500ms or 50 rtc ticks passed
@@ -165,7 +157,7 @@ extern "C" uint32_t rtc_get_counter_val()
return rtc;
}

extern "C" void timer_set_oc_value(uint32_t, uint16_t, uint16_t)
extern "C" void timer_set_oc_value(uint32_t, enum tim_oc_id, uint32_t)
{

}
@@ -266,20 +258,6 @@ void Param::Change(Param::PARAM_NUM p)

}

CanHardware::CanHardware() {}

bool CanHardware::AddReceiveCallback(CanCallback* cb)
{
vcuCan = cb;
return true;
}

bool CanHardware::RegisterUserMessage(uint32_t canId)
{
vcuCanId = canId;
return true;
}

const char* errorListString = "";
HWREV hwRev = HW_REV3;
uint16_t AnaIn::values[NUM_SAMPLES*ANA_IN_COUNT];

0 comments on commit 0945b42

Please sign in to comment.