diff --git a/src/printf.cpp b/src/printf.cpp index 9cf2c08..ac4b92a 100644 --- a/src/printf.cpp +++ b/src/printf.cpp @@ -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 @@ -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); } }; diff --git a/src/terminal.cpp b/src/terminal.cpp index 0a2d771..2723e43 100644 --- a/src/terminal.cpp +++ b/src/terminal.cpp @@ -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); } diff --git a/test/stub_canhardware.h b/test/stub_canhardware.h index 13331cb..2fd5947 100644 --- a/test/stub_canhardware.h +++ b/test/stub_canhardware.h @@ -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; @@ -45,4 +46,4 @@ class CanStub: public CanHardware extern CanCallback* vcuCan; extern uint32_t vcuCanId; -#endif // TEST_CANHARDWARE_H \ No newline at end of file +#endif // TEST_CANHARDWARE_H diff --git a/test/stub_libopencm3.c b/test/stub_libopencm3.c index 8165bd1..b90a35b 100644 --- a/test/stub_libopencm3.c +++ b/test/stub_libopencm3.c @@ -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) +{ +} diff --git a/test/test_cansdo.cpp b/test/test_cansdo.cpp index c54d5c6..dff4b06 100644 --- a/test/test_cansdo.cpp +++ b/test/test_cansdo.cpp @@ -30,10 +30,12 @@ class IPutChar { public: virtual void PutChar(char c) = 0; }; #include "my_fp.h" #include "stub_canhardware.h" #include "test.h" +#include "sdocommands.h" #include #include + class CanSdoTest : public UnitTest { public: @@ -41,15 +43,47 @@ class CanSdoTest : public UnitTest 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; static std::unique_ptr canMap; -static std::unique_ptr canSdo; +static std::unique_ptr 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(); canMap = std::make_unique(canStub.get(), false); - canSdo = std::make_unique(canStub.get(), canMap.get()); + canSdo = std::make_unique(canStub.get(), canMap.get()); Param::LoadDefaults(); } @@ -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); }