Skip to content

Commit

Permalink
test: CAN pingpong test v2, with loop
Browse files Browse the repository at this point in the history
  • Loading branch information
LinjingZhang committed Oct 22, 2024
1 parent 893d92a commit ea52721
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 37 deletions.
48 changes: 26 additions & 22 deletions libraries/CAN/test/unit/src/features/Test_CAN_connected_node1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,48 @@ TEST_GROUP(CAN_connected_node1Internal);
// Setup method called by Unity before every individual test defined for this test group.
static TEST_SETUP(CAN_connected_node1Internal)
{
memset(node1Data, 0, sizeof(node1Data));
memset(receivedData, 0, sizeof(receivedData));
}

// Tear down method called by Unity after every individual test defined for this test group.
static TEST_TEAR_DOWN(CAN_connected_node1Internal)
{
}

void testSendReceive()
void testSendReceive(uint8_t dataLength, uint8_t iterations)
{

CAN.beginPacket(CAN_ID_1);
for (uint8_t i = 0; i < globalQuantity; ++i) {
CAN.write(node1Data[i]);
}
CAN.endPacket();
#ifdef TRACE_OUTPUT
printArray("Sent Data", node1Data, globalQuantity);
#endif
while (!newDataReceivedNode1) {
}

if (newDataReceivedNode1) {
for (uint8_t i = 0; i < globalQuantity; ++i) {
UNITY_TEST_ASSERT_EQUAL_UINT8(node1Data[i] + node1Increment, receivedData[i], __LINE__, "Data mismatch");
canDataLength = dataLength;

for(uint8_t loop = 0; loop < iterations; ++loop) {
TEST_ASSERT_TRUE(CAN.beginPacket(CAN_ID_1));
TEST_ASSERT_EQUAL_UINT8(canDataLength, CAN.write(node1Data, canDataLength));
TEST_ASSERT_EQUAL(1, CAN.endPacket());

while (!newDataReceivedNode1) {
}
newDataReceivedNode1 = false;

#ifdef TRACE_OUTPUT
printArray("Sent Data", node1Data, globalQuantity);
printArray("Received Data", receivedData, globalQuantity);
#endif
if (newDataReceivedNode1) {
for (uint8_t i = 0; i < canDataLength; ++i) {
TEST_ASSERT_EQUAL_UINT8(node1Data[i] + node2Increment, receivedData[i]);
}
newDataReceivedNode1 = false;

#ifdef TRACE_OUTPUT
printArray("\nSent Data", node1Data, canDataLength);
printArray("Received Data", receivedData, canDataLength);
#endif
for(uint8_t i = 0; i < canDataLength; ++i) {
node1Data[i] = receivedData[i] + node1Increment;
}
}
}
}


TEST_IFX(CAN_connected_node1Internal, checkPingPong)
{
testSendReceive();
testSendReceive(3,5);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ void processReceivedMessagesNode2()
{
if (newDataReceivedNode2) {
// Process the received data
for (uint8_t i = 0; i < globalQuantity; ++i) {
canDataLength = CAN.packetDlc();
for (uint8_t i = 0; i < canDataLength; ++i) {
node2Data[i] = receivedData[i] + node2Increment;
}

// Send processed data back to Node1
CAN.beginPacket(CAN_ID_2);
for (uint8_t i = 0; i < globalQuantity; ++i) {
for (uint8_t i = 0; i < canDataLength; ++i) {
CAN.write(node2Data[i]);
}
CAN.endPacket();
Expand All @@ -39,7 +40,8 @@ void processReceivedMessagesNode2()
newDataReceivedNode2 = false;

#ifdef TRACE_OUTPUT
printArray("Processed Data", node2Data, globalQuantity);
printArray("\nReceived Data", receivedData, canDataLength);
printArray("Sent Data", node2Data, canDataLength);
#endif
}
}
Expand All @@ -57,10 +59,6 @@ static TEST_TEAR_DOWN(CAN_connected_node2Internal)
TEST_IFX(CAN_connected_node2Internal, checkPingPong)
{
processReceivedMessagesNode2();

#ifdef TRACE_OUTPUT
printArray("\nNode2 data", node2Data, globalQuantity);
#endif
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@
#define CAN_ID_2 0x321

// variables
static uint8_t node1Data[8] = {0, 1, 2, 3, 4, 5, 6, 7};
static uint8_t node1Increment = 1;
static uint8_t globalQuantity = 8;


const static uint8_t node1Increment = 10;
const static uint8_t node2Increment = 1;
const static uint8_t canDataLengthMax = 8;

static uint8_t node1Data[canDataLengthMax];
static uint8_t receivedData[canDataLengthMax];
static uint8_t canDataLength = canDataLengthMax;

volatile bool newDataReceivedNode1 = false;
static uint8_t receivedData[8];


// test feature includes requiring the above defined variables
#include "Test_CAN_connected_node1.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
#define CAN_ID_2 0x321

// variables
static uint8_t node2Data[8];
static uint8_t node2Increment = 1;
static uint8_t globalQuantity = 8;

const static uint8_t node1Increment = 10;
const static uint8_t node2Increment = 1;
const static uint8_t canDataLengthMax = 8;

static uint8_t canDataLength = canDataLengthMax;
static uint8_t node2Data[canDataLengthMax] = {0};
static uint8_t receivedData[canDataLengthMax] = {0};

volatile bool newDataReceivedNode2 = false;
static uint8_t receivedData[8];


// test feature includes requiring the above defined variables
#include "Test_CAN_connected_node2.hpp"
Expand Down

0 comments on commit ea52721

Please sign in to comment.