Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
*/

/*
putchar is the only external dependency for this file,
if you have a working putchar, leave it commented out.
libopeninv_putchar is the only external dependency for this file,
if you have a working libopeninv_putchar, leave it commented out.
If not, uncomment the define below and
replace outbyte(c) by your own function call.

#define putchar(c) outbyte(c)
#define libopeninv_putchar(c) outbyte(c)
*/

#include <stdarg.h>
Expand All @@ -35,14 +35,14 @@
#define PAD_RIGHT 1
#define PAD_ZERO 2

extern "C" void putchar(char c);
extern "C" void libopeninv_putchar(int c);

class ExternPutChar: public IPutChar
{
public:
void PutChar(char c)
{
putchar(c);
libopeninv_putchar(c);
}
};

Expand Down
5 changes: 3 additions & 2 deletions src/terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ void Terminal::SendCurrentBuffer(uint32_t len)
}

//Backward compatibility for printf
extern "C" void putchar(int c)
extern "C" void libopeninv_putchar(int c)
{
Terminal::defaultTerminal->PutChar(c);
if (Terminal::defaultTerminal != nullptr)
Terminal::defaultTerminal->PutChar(c);
}
5 changes: 3 additions & 2 deletions test/stub_canhardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
class CanStub: public CanHardware
{
void SetBaudrate(enum baudrates baudrate) {}
void Send(uint32_t canId, uint32_t data[2], uint8_t len)
void Send(uint32_t canId, uint32_t data[2], uint8_t len, bool forceExt) override
{
(void)forceExt;
m_canId = canId;
memcpy(&m_data[0], &data[0], sizeof(m_data));
m_len = len;
Expand All @@ -45,4 +46,4 @@ class CanStub: public CanHardware
extern CanCallback* vcuCan;
extern uint32_t vcuCanId;

#endif // TEST_CANHARDWARE_H
#endif // TEST_CANHARDWARE_H
4 changes: 4 additions & 0 deletions test/stub_libopencm3.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,7 @@ void dma_enable_channel(uint32_t dma, uint8_t channel)
void dma_disable_channel(uint32_t dma, uint8_t channel)
{
}

void libopeninv_putchar(int c)
{
}
66 changes: 48 additions & 18 deletions test/test_cansdo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,60 @@ class IPutChar { public: virtual void PutChar(char c) = 0; };
#include "my_fp.h"
#include "stub_canhardware.h"
#include "test.h"
#include "sdocommands.h"

#include <memory>
#include <cstdint>


class CanSdoTest : public UnitTest
{
public:
explicit CanSdoTest(const std::list<VoidFunction>* cases) : UnitTest(cases) {}
virtual void TestCaseSetup();
};

class UserSpaceCanSdo : public CanSdo
{
public:
explicit UserSpaceCanSdo(CanHardware* hw, CanMap* cm = nullptr) : CanSdo(hw, cm) {}

bool shouldHandleUserSpaceSdo = false;
bool wasUserSpaceCalled = false;
SdoFrame lastUserSpaceRequest{};

bool ProcessUserSpaceSdo(SdoFrame* sdo) override
{
wasUserSpaceCalled = true;
lastUserSpaceRequest = *sdo;

if (shouldHandleUserSpaceSdo)
{
sdo->cmd = SDO_WRITE_REPLY;
sdo->data = 0;
return true;
}

return false;
}
};

static std::unique_ptr<CanStub> canStub;
static std::unique_ptr<CanMap> canMap;
static std::unique_ptr<CanSdo> canSdo;
static std::unique_ptr<UserSpaceCanSdo> canSdo;
CanMap* SdoCommands::canMap;

void SdoCommands::ProcessStandardCommands(CanSdo::SdoFrame* sdoFrame)
{
sdoFrame->cmd = SDO_ABORT;
sdoFrame->data = SDO_ERR_INVIDX;
}

void CanSdoTest::TestCaseSetup()
{
canStub = std::make_unique<CanStub>();
canMap = std::make_unique<CanMap>(canStub.get(), false);
canSdo = std::make_unique<CanSdo>(canStub.get(), canMap.get());
canSdo = std::make_unique<UserSpaceCanSdo>(canStub.get(), canMap.get());
Param::LoadDefaults();
}

Expand Down Expand Up @@ -383,30 +417,26 @@ static void sdo_write_error_time_aborts()

static void sdo_unknown_index_goes_to_user_space()
{
// Index 0x4000 is not handled by CanSdo itself
// Index 0x4000 is not handled internally and is offered to user-space first.
canSdo->shouldHandleUserSpaceSdo = false;
SendSdoRequest(SDO_WRITE, 0x4000, 0, 0xDEADBEEF);

// No CAN reply should be sent; the pending user-space SDO should be set
CanSdo::SdoFrame* pending = canSdo->GetPendingUserspaceSdo();
ASSERT(pending != nullptr);
ASSERT(pending->index == 0x4000);
ASSERT(pending->data == 0xDEADBEEF);
ASSERT(canSdo->wasUserSpaceCalled);
ASSERT(canSdo->lastUserSpaceRequest.index == 0x4000);
ASSERT(canSdo->lastUserSpaceRequest.data == 0xDEADBEEF);
ASSERT(canStub->m_canId == SdoRepId);
ASSERT(GetReply()->cmd == SDO_ABORT);
ASSERT(GetReply()->data == SDO_ERR_INVIDX);
}

static void sdo_reply_sent_via_send_sdo_reply()
{
canSdo->shouldHandleUserSpaceSdo = true;
SendSdoRequest(SDO_WRITE, 0x4000, 0, 0x1234);

CanSdo::SdoFrame* pending = canSdo->GetPendingUserspaceSdo();
ASSERT(pending != nullptr);

// User space fills in the reply and calls SendSdoReply
pending->cmd = SDO_WRITE_REPLY;
pending->data = 0;
canSdo->SendSdoReply(pending);

// After sending the reply, the pending flag must be cleared
ASSERT(canSdo->GetPendingUserspaceSdo() == nullptr);
ASSERT(canSdo->wasUserSpaceCalled);
ASSERT(canSdo->lastUserSpaceRequest.index == 0x4000);
ASSERT(canSdo->lastUserSpaceRequest.data == 0x1234);
ASSERT(canStub->m_canId == SdoRepId);
ASSERT(GetReply()->cmd == SDO_WRITE_REPLY);
}
Expand Down
Loading