Skip to content

Commit dbc3840

Browse files
committed
2 parents bdc805a + 46f4196 commit dbc3840

33 files changed

Lines changed: 1176 additions & 263 deletions

NetX/inc/u_nx_debug.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/* This file contains NetX-specific debug helpers. */
99

1010
/* API */
11-
const char *nx_status_toString(UINT status); /* Converts a NetX status macro to a printable string. Meant to be used with DEBUG_PRINTLN() (defined in another file). */
11+
const char *nx_status_toString(UINT status); /* Converts a NetX status macro to a printable string. Meant to be used with PRINTLN_...() macros (defined in another file). */
1212

1313
// clang-format on
1414
#endif /* u_nx_debug.h */

NetX/inc/u_nx_ethernet.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
#include <stdint.h>
11+
#include <stdbool.h>
1112
#include "nx_api.h"
1213

1314
/* CONFIG */
@@ -27,6 +28,26 @@ typedef enum {
2728
NODE8 = (1 << 7), // 0b10000000
2829
} ethernet_node_t;
2930
#define ETH_IP(node) IP_ADDRESS(239, 0, 0, node)
31+
32+
/* These node ids are ONLY relavent to PLCA configuration.
33+
They are meant to be used when configuring a PHY. The IDs must be sequential, and the "0" id always indicates the network's coordinator node.
34+
They have no impact on application-level IP addresses or message processing.
35+
36+
For example, if you're using the LAN8670 PHY driver, you'd probably use these enum values like this:
37+
LAN8670_PLCA_Set_Node_Count(&lan8670, PLCA_NUM_NODES);
38+
LAN8670_PLCA_Set_Node_Id(&lan8670, PLCA_VCU) // replace 'PLCA_VCU' with whatever board it is
39+
*/
40+
typedef enum {
41+
PLCA_VCU, // 0. This is the PLCA coordinator node.
42+
PLCA_COMPUTE,
43+
PLCA_TPU,
44+
PLCA_MSB1,
45+
PLCA_MSB2,
46+
PLCA_MSB3,
47+
PLCA_MSB4,
48+
PLCA_NODE8,
49+
PLCA_NUM_NODES
50+
} plca_node_id_t;
3051
/* END CONFIG */
3152

3253
typedef struct {

NetX/src/u_nx_debug.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// clang-format off
44

55
/* Converts a NetX status macro to a printable string. */
6-
/* This function is intended to be used with DEBUG_PRINTLN(), and shouldn't really ever be used outside of debugging purposes. */
6+
/* This function is intended to be used with PRINTLN_...() macros, and shouldn't really ever be used outside of debugging purposes. */
77
/* (these macros are defined in nx_api.h) */
88
const char* nx_status_toString(UINT status) {
99
switch(status) {

NetX/src/u_nx_ethernet.c

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@ static void _receive_message(NX_UDP_SOCKET *socket) {
4646
/* Recieve the packet */
4747
status = nx_udp_socket_receive(socket, &packet, NX_NO_WAIT);
4848
if(status == NX_SUCCESS) {
49+
PRINTLN_INFO("Recieved UDP socket!");
50+
51+
/* Get packet information (for debugging). */
52+
ULONG ip_address;
53+
UINT protocol;
54+
UINT port;
55+
UINT interface_index;
56+
status = nx_udp_packet_info_extract(packet, &ip_address, &protocol, &port, &interface_index);
57+
if(status != NX_SUCCESS) {
58+
PRINTLN_WARNING("Failed to extract UDP packet information (Status: %d/%s).", status, nx_status_toString(status));
59+
}
60+
else {
61+
uint8_t ip_address_byte1 = (ip_address >> 24) & 0xFF;
62+
uint8_t ip_address_byte2 = (ip_address >> 16) & 0xFF;
63+
uint8_t ip_address_byte3 = (ip_address >> 8) & 0xFF;
64+
uint8_t ip_address_byte4 = ip_address & 0xFF;
65+
PRINTLN_INFO("UDP Packet - IP: %d.%d.%d.%d, Port: %d, Protocol: %d, Interface: %d", ip_address_byte1, ip_address_byte2, ip_address_byte3, ip_address_byte4, port, protocol, interface_index);
66+
}
67+
4968
/* Extract message from packet */
5069
status = nx_packet_data_extract_offset(
5170
packet, // Packet to extract from
@@ -55,12 +74,12 @@ static void _receive_message(NX_UDP_SOCKET *socket) {
5574
&bytes_copied // Stores how many bytes were actually copied to &message
5675
);
5776
if(bytes_copied < sizeof(ethernet_message_t)) {
58-
DEBUG_PRINTLN("WARNING: Received ethernet message was smaller than expected (only received %lu of %u expected bytes).", bytes_copied, sizeof(ethernet_message_t));
77+
PRINTLN_WARNING("Received ethernet message was smaller than expected (only received %lu of %u expected bytes).", bytes_copied, sizeof(ethernet_message_t));
5978
}
6079

6180
/* Process received message */
6281
if(status == NX_SUCCESS) {
63-
DEBUG_PRINTLN("Received ethernet message! (Sender ID: %d, Message ID: %d).", message.sender_id, message.message_id);
82+
PRINTLN_INFO("Received ethernet message! (Sender ID: %d, Message ID: %d).", message.sender_id, message.message_id);
6483
device.on_recieve(message);
6584
}
6685
}
@@ -77,7 +96,7 @@ uint8_t ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve
7796

7897
/* Make sure device isn't already initialized */
7998
if(device.is_initialized) {
80-
DEBUG_PRINTLN("ERROR: Ethernet is already initialized.");
99+
PRINTLN_ERROR("Ethernet is already initialized.");
81100
return U_ERROR;
82101
}
83102

@@ -95,7 +114,7 @@ uint8_t ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve
95114
_PACKET_POOL_SIZE // Size of the pool's memory area
96115
);
97116
if(status != NX_SUCCESS) {
98-
DEBUG_PRINTLN("ERROR: Failed to create packet pool (Status: %d/%s).", status, nx_status_toString(status));
117+
PRINTLN_ERROR("Failed to create packet pool (Status: %d/%s).", status, nx_status_toString(status));
99118
return status;
100119
}
101120

@@ -112,7 +131,7 @@ uint8_t ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve
112131
_IP_THREAD_PRIORITY // Priority of the IP thread
113132
);
114133
if(status != NX_SUCCESS) {
115-
DEBUG_PRINTLN("ERROR: Failed to create IP instance (Status: %d/%s).", status, nx_status_toString(status));
134+
PRINTLN_ERROR("Failed to create IP instance (Status: %d/%s).", status, nx_status_toString(status));
116135
return status;
117136
}
118137

@@ -123,22 +142,22 @@ uint8_t ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve
123142
_ARP_CACHE_SIZE // Size of ARP cache
124143
);
125144
if(status != NX_SUCCESS) {
126-
DEBUG_PRINTLN("ERROR: Failed to enable ARP (Status: %d/%s).", status, nx_status_toString(status));
145+
PRINTLN_ERROR("Failed to enable ARP (Status: %d/%s).", status, nx_status_toString(status));
127146
return status;
128147
}
129148

130149

131150
/* Enable UDP */
132151
status = nx_udp_enable(&device.ip);
133152
if (status != NX_SUCCESS) {
134-
DEBUG_PRINTLN("ERROR: Failed to enable UDP (Status: %d/%s).", status, nx_status_toString(status));
153+
PRINTLN_ERROR("Failed to enable UDP (Status: %d/%s).", status, nx_status_toString(status));
135154
return status;
136155
}
137156

138157
/* Enable igmp */
139158
status = nx_igmp_enable(&device.ip);
140159
if (status != NX_SUCCESS) {
141-
DEBUG_PRINTLN("ERROR: Failed to enable igmp (Status: %d/%s).", status, nx_status_toString(status));
160+
PRINTLN_ERROR("Failed to enable igmp (Status: %d/%s).", status, nx_status_toString(status));
142161
return status;
143162
}
144163

@@ -156,7 +175,7 @@ uint8_t ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve
156175
ULONG address = ETH_IP(i);
157176
status = nx_igmp_multicast_join(&device.ip, address);
158177
if(status != NX_SUCCESS) {
159-
DEBUG_PRINTLN("ERROR: Failed to join multicast group (Status: %d/%s, Address: %lu).", status, nx_status_toString(status), address);
178+
PRINTLN_ERROR("Failed to join multicast group (Status: %d/%s, Address: %lu).", status, nx_status_toString(status), address);
160179
}
161180
}
162181
}
@@ -172,7 +191,7 @@ uint8_t ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve
172191
_UDP_QUEUE_MAXIMUM // UDP queue maximum
173192
);
174193
if(status != NX_SUCCESS) {
175-
DEBUG_PRINTLN("ERROR: Failed to create UDP socket (Status: %d/%s).", status, nx_status_toString(status));
194+
PRINTLN_ERROR("Failed to create UDP socket (Status: %d/%s).", status, nx_status_toString(status));
176195
return status;
177196
}
178197

@@ -183,7 +202,7 @@ uint8_t ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve
183202
TX_WAIT_FOREVER // Wait forever
184203
);
185204
if(status != NX_SUCCESS) {
186-
DEBUG_PRINTLN("ERROR: Failed to bind UDP socket (Status: %d/%s).", status, nx_status_toString(status));
205+
PRINTLN_ERROR("Failed to bind UDP socket (Status: %d/%s).", status, nx_status_toString(status));
187206
nx_udp_socket_delete(&device.socket);
188207
return status;
189208
}
@@ -194,7 +213,7 @@ uint8_t ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve
194213
&_receive_message // Callback function
195214
);
196215
if(status != NX_SUCCESS) {
197-
DEBUG_PRINTLN("ERROR: Failed to set recieve callback (Status: %d/%s).", status, nx_status_toString(status));
216+
PRINTLN_ERROR("Failed to set recieve callback (Status: %d/%s).", status, nx_status_toString(status));
198217
nx_udp_socket_unbind(&device.socket);
199218
nx_udp_socket_delete(&device.socket);
200219
return status;
@@ -203,7 +222,7 @@ uint8_t ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve
203222
/* Mark device as initialized. */
204223
device.is_initialized = true;
205224

206-
DEBUG_PRINTLN("Ran ethernet_init().");
225+
PRINTLN_INFO("Ran ethernet_init().");
207226
return NX_SUCCESS;
208227
}
209228

@@ -213,7 +232,7 @@ ethernet_message_t ethernet_create_message(uint8_t message_id, ethernet_node_t r
213232

214233
/* Check data length */
215234
if (data_length > ETH_MESSAGE_SIZE) {
216-
DEBUG_PRINTLN("ERROR: Data length exceeds maximum (message_id: %d).", message_id);
235+
PRINTLN_ERROR("Data length exceeds maximum (message_id: %d).", message_id);
217236
return message; // Return empty message.
218237
}
219238

@@ -234,13 +253,13 @@ uint8_t ethernet_send_message(ethernet_message_t *message) {
234253

235254
/* Check if ethernet is initialized */
236255
if(!device.is_initialized) {
237-
DEBUG_PRINTLN("ERROR: Ethernet device is not initialized, so ethernet_send_message() will not work.");
256+
PRINTLN_ERROR("Ethernet device is not initialized, so ethernet_send_message() will not work.");
238257
return U_ERROR;
239258
}
240259

241260
/* Check data length */
242261
if (message->data_length > ETH_MESSAGE_SIZE) {
243-
DEBUG_PRINTLN("ERROR: Data length exceeds maximum (Message ID: %d).", message->message_id);
262+
PRINTLN_ERROR("Data length exceeds maximum (Message ID: %d).", message->message_id);
244263
return U_ERROR;
245264
}
246265

@@ -252,7 +271,7 @@ uint8_t ethernet_send_message(ethernet_message_t *message) {
252271
TX_WAIT_FOREVER // Wait indefinitely until a packet is available
253272
);
254273
if(status != NX_SUCCESS) {
255-
DEBUG_PRINTLN("ERROR: Failed to allocate packet (Status: %d/%s, Message ID: %d).", status, nx_status_toString(status), message->message_id);
274+
PRINTLN_ERROR("Failed to allocate packet (Status: %d/%s, Message ID: %d).", status, nx_status_toString(status), message->message_id);
256275
return U_ERROR;
257276
}
258277

@@ -265,7 +284,7 @@ uint8_t ethernet_send_message(ethernet_message_t *message) {
265284
TX_WAIT_FOREVER // Wait indefinitely
266285
);
267286
if(status != NX_SUCCESS) {
268-
DEBUG_PRINTLN("ERROR: Failed to append data to packet (Status: %d/%s, Message ID: %d).", status, nx_status_toString(status), message->message_id);
287+
PRINTLN_ERROR("Failed to append data to packet (Status: %d/%s, Message ID: %d).", status, nx_status_toString(status), message->message_id);
269288
nx_packet_release(packet);
270289
return U_ERROR;
271290
}
@@ -278,12 +297,12 @@ uint8_t ethernet_send_message(ethernet_message_t *message) {
278297
ETH_UDP_PORT
279298
);
280299
if(status != NX_SUCCESS) {
281-
DEBUG_PRINTLN("ERROR: Failed to send packet (Status: %d/%s, Message ID: %d).", status, nx_status_toString(status), message->message_id);
300+
PRINTLN_ERROR("Failed to send packet (Status: %d/%s, Message ID: %d).", status, nx_status_toString(status), message->message_id);
282301
nx_packet_release(packet);
283302
return U_ERROR;
284303
}
285304

286-
DEBUG_PRINTLN("Sent ethernet message (Recipient ID: %d, Message ID: %d).", message->recipient_id, message->message_id);
305+
PRINTLN_INFO("Sent ethernet message (Recipient ID: %d, Message ID: %d).", message->recipient_id, message->message_id);
287306
return U_SUCCESS;
288307
}
289308
// clang-format on

Testing/Makefile

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
3+
# Compiler
4+
CC := gcc
5+
6+
# Project directories
7+
PROJECT_DIR := /home/app
8+
TEST_DIR := $(PROJECT_DIR)/Tests
9+
MOCKS_DIR := $(TEST_DIR)/Mocks
10+
TEST_BUILD := $(TEST_DIR)/build
11+
TEST_BUILD_BIN := $(TEST_BUILD)/bin
12+
UNITY_SRC := /cmock_portable/vendor/unity/src
13+
CMOCK_SRC := /cmock_portable/src
14+
EMEBEDDED_TESTING_DIR := Drivers/Embedded-Base/Testing
15+
16+
# Command line variables (must be provided)
17+
TEST_NAME ?=
18+
TEST_FILE ?=
19+
TEST_PACKAGE ?=
20+
C_SOURCES ?=
21+
INCLUDE_DIRS ?=
22+
23+
TEST_BUILD_OBJS := $(TEST_BUILD)/objs/$(TEST_PACKAGE)
24+
25+
# Sanity check
26+
ifeq ($(TEST_NAME),)
27+
$(error You must define TEST_NAME)
28+
endif
29+
ifeq ($(TEST_FILE),)
30+
$(error You must define TEST_FILE)
31+
endif
32+
ifeq ($(TEST_PACKAGE),)
33+
$(error You must define TEST_PACKAGE)
34+
endif
35+
ifeq ($(C_SOURCES),)
36+
$(error You must define C_SOURCES)
37+
endif
38+
39+
# Output executable
40+
TARGET := $(TEST_BUILD_BIN)/$(TEST_NAME)
41+
42+
# Include dirs
43+
C_INCLUDES := \
44+
-I$(EMEBEDDED_TESTING_DIR)/manual_mocks \
45+
-I$(UNITY_SRC) \
46+
-I$(CMOCK_SRC) \
47+
$(addprefix -I,$(INCLUDE_DIRS)) \
48+
-I$(MOCKS_DIR)/$(TEST_PACKAGE) \
49+
-I$(TEST_DIR)/Inc
50+
51+
CFLAGS = -w -DTESTING $(C_INCLUDES)
52+
53+
TEST_SOURCES = $(CMOCK_SRC)/cmock.c $(UNITY_SRC)/unity.c
54+
55+
# All source files
56+
SOURCES := $(TEST_FILE) $(C_SOURCES)
57+
TEST_SRCS := $(TEST)/cmock.c $(UNITY_SRC)/unity.c
58+
59+
# Object files
60+
OBJS := $(patsubst %.c,$(TEST_BUILD_OBJS)/%.o,$(SOURCES))
61+
TEST_OBJS := $(TEST_BUILD_OBJS)/cmock.o $(TEST_BUILD_OBJS)/unity.o
62+
63+
ALL_OBJS := $(OBJS) $(TEST_OBJS)
64+
65+
# Default target
66+
all: $(TARGET)
67+
68+
# Link executable
69+
$(TARGET): $(ALL_OBJS)
70+
@mkdir -p $(dir $@)
71+
$(CC) $(CFLAGS) $^ -o $@ -lm
72+
73+
# Compile object files
74+
$(TEST_BUILD_OBJS)/%.o: %.c
75+
@mkdir -p $(dir $@)
76+
$(CC) $(CFLAGS) -c $< -o $@ -lm
77+
78+
$(TEST_BUILD_OBJS)/cmock.o: $(CMOCK_SRC)/cmock.c
79+
@mkdir -p $(dir $@)
80+
$(CC) $(CFLAGS) -c $< -o $@
81+
82+
$(TEST_BUILD_OBJS)/unity.o: $(UNITY_SRC)/unity.c
83+
@mkdir -p $(dir $@)
84+
@echo "Compiling $< -> $@"
85+
$(CC) $(CFLAGS) -c $< -o $@
86+
87+
88+
# Clean
89+
clean:
90+
rm -rf $(TEST_BUILD)

Testing/cmock-config.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
:cmock:
3+
:mock_prefix: mock_
4+
:includes:
5+
- stubs.h
6+
:plugins:
7+
- :ignore
8+
- :ignore_arg
9+
- :callback
10+
- :expect
11+
- :expect_any_args
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
#ifndef _TEST_EXAMPLE_H
3+
#define _TEST_EXAMPLE_H
4+
5+
#include "unity.h"
6+
7+
#endif
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
#include "test_example.h"
3+
#include <stdbool.h>
4+
#include <stdlib.h>
5+
6+
// clang-format off
7+
8+
// gets run before every test function
9+
void setUp(void) {
10+
11+
}
12+
13+
// gets run after every test function
14+
void tearDown(void) {
15+
16+
}
17+
18+
// test function
19+
void test_exmaple(void) {
20+
TEST_ASSERT_EQUAL_INT(9 + 10, 19);
21+
}
22+
23+
// run tests in main function
24+
int main(void) {
25+
UNITY_BEGIN();
26+
RUN_TEST(test_exmaple);
27+
return UNITY_END();
28+
}
29+
30+
// clang-format on

0 commit comments

Comments
 (0)