Testing the MS/TP datalink layer on Linux.
This commit is contained in:
@@ -29,16 +29,19 @@
|
|||||||
#if PRINT_ENABLED
|
#if PRINT_ENABLED
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#endif
|
#endif
|
||||||
|
/* Linux Includes */
|
||||||
|
#include <sched.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
/* project includes */
|
||||||
#include "bacdef.h"
|
#include "bacdef.h"
|
||||||
#include "mstp.h"
|
#include "mstp.h"
|
||||||
#include "dlmstp.h"
|
#include "dlmstp.h"
|
||||||
#include "rs485.h"
|
#include "rs485.h"
|
||||||
#include "npdu.h"
|
#include "npdu.h"
|
||||||
|
|
||||||
#define WIN32_LEAN_AND_MEAN
|
|
||||||
#define STRICT 1
|
|
||||||
#include <windows.h>
|
|
||||||
|
|
||||||
/* Number of MS/TP Packets Rx/Tx */
|
/* Number of MS/TP Packets Rx/Tx */
|
||||||
uint16_t MSTP_Packets = 0;
|
uint16_t MSTP_Packets = 0;
|
||||||
|
|
||||||
@@ -72,29 +75,37 @@ void dlmstp_init(void)
|
|||||||
Receive_Buffer.pdu_len = 0;
|
Receive_Buffer.pdu_len = 0;
|
||||||
/* initialize hardware */
|
/* initialize hardware */
|
||||||
RS485_Initialize();
|
RS485_Initialize();
|
||||||
MSTP_Port.InputBuffer = &Receive_Buffer.pdu[0];
|
|
||||||
MSTP_Init(&MSTP_Port);
|
MSTP_Init(&MSTP_Port);
|
||||||
/* FIXME: implement your data storage */
|
/* FIXME: implement your data storage */
|
||||||
data = 64; /* I2C_Read_Byte(
|
data = 64;
|
||||||
EEPROM_DEVICE_ADDRESS,
|
#if 0
|
||||||
EEPROM_MSTP_MAC_ADDR); */
|
/* I2C_Read_Byte(
|
||||||
|
EEPROM_DEVICE_ADDRESS,
|
||||||
|
EEPROM_MSTP_MAC_ADDR); */
|
||||||
|
#endif
|
||||||
if (data <= 127)
|
if (data <= 127)
|
||||||
MSTP_Port.This_Station = data;
|
MSTP_Port.This_Station = data;
|
||||||
else
|
else
|
||||||
dlmstp_set_my_address(DEFAULT_MAC_ADDRESS);
|
dlmstp_set_my_address(DEFAULT_MAC_ADDRESS);
|
||||||
|
|
||||||
/* FIXME: implement your data storage */
|
/* FIXME: implement your data storage */
|
||||||
data = 127; /* I2C_Read_Byte(
|
data = 127;
|
||||||
EEPROM_DEVICE_ADDRESS,
|
#if 0
|
||||||
EEPROM_MSTP_MAX_MASTER_ADDR); */
|
/* I2C_Read_Byte(
|
||||||
|
EEPROM_DEVICE_ADDRESS,
|
||||||
|
EEPROM_MSTP_MAX_MASTER_ADDR); */
|
||||||
|
#endif
|
||||||
if ((data <= 127) && (data >= MSTP_Port.This_Station))
|
if ((data <= 127) && (data >= MSTP_Port.This_Station))
|
||||||
MSTP_Port.Nmax_master = data;
|
MSTP_Port.Nmax_master = data;
|
||||||
else
|
else
|
||||||
dlmstp_set_max_master(DEFAULT_MAX_MASTER);
|
dlmstp_set_max_master(DEFAULT_MAX_MASTER);
|
||||||
/* FIXME: implement your data storage */
|
/* FIXME: implement your data storage */
|
||||||
data = 1;
|
data = 1;
|
||||||
|
#if 0
|
||||||
/* I2C_Read_Byte(
|
/* I2C_Read_Byte(
|
||||||
EEPROM_DEVICE_ADDRESS,
|
EEPROM_DEVICE_ADDRESS,
|
||||||
EEPROM_MSTP_MAX_INFO_FRAMES_ADDR); */
|
EEPROM_MSTP_MAX_INFO_FRAMES_ADDR); */
|
||||||
|
#endif
|
||||||
if (data >= 1)
|
if (data >= 1)
|
||||||
MSTP_Port.Nmax_info_frames = data;
|
MSTP_Port.Nmax_info_frames = data;
|
||||||
else
|
else
|
||||||
@@ -146,13 +157,12 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */
|
|||||||
return bytes_sent;
|
return bytes_sent;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dlmstp_receive_fsm_task(void *pArg)
|
static void *dlmstp_receive_fsm_task(void *pArg)
|
||||||
{
|
{
|
||||||
uint8_t bytes_remaining;
|
uint8_t bytes_remaining;
|
||||||
bool received_frame;
|
bool received_frame;
|
||||||
|
|
||||||
//(void)SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
|
for (;;) {
|
||||||
while (TRUE) {
|
|
||||||
/* only do receive state machine while we don't have a frame */
|
/* only do receive state machine while we don't have a frame */
|
||||||
if ((MSTP_Port.ReceivedValidFrame == false) &&
|
if ((MSTP_Port.ReceivedValidFrame == false) &&
|
||||||
(MSTP_Port.ReceivedInvalidFrame == false)) {
|
(MSTP_Port.ReceivedInvalidFrame == false)) {
|
||||||
@@ -168,14 +178,13 @@ static void dlmstp_receive_fsm_task(void *pArg)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dlmstp_master_fsm_task(void *pArg)
|
static void *dlmstp_master_fsm_task(void *pArg)
|
||||||
{
|
{
|
||||||
//(void)SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
|
for (;;) {
|
||||||
while (TRUE) {
|
|
||||||
/* only do master state machine while rx is idle */
|
/* only do master state machine while rx is idle */
|
||||||
if (MSTP_Port.receive_state == MSTP_RECEIVE_STATE_IDLE) {
|
if (MSTP_Port.receive_state == MSTP_RECEIVE_STATE_IDLE) {
|
||||||
while (MSTP_Master_Node_FSM(&MSTP_Port)) {
|
while (MSTP_Master_Node_FSM(&MSTP_Port)) {
|
||||||
Sleep(1);
|
sched_yield();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
/* see if there is a packet available, and a place
|
/* see if there is a packet available, and a place
|
||||||
@@ -335,6 +344,7 @@ void dlmstp_get_broadcast_address(BACNET_ADDRESS * dest)
|
|||||||
|
|
||||||
#ifdef TEST_DLMSTP
|
#ifdef TEST_DLMSTP
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
void npdu_handler(
|
void npdu_handler(
|
||||||
BACNET_ADDRESS * src, /* source address */
|
BACNET_ADDRESS * src, /* source address */
|
||||||
uint8_t * pdu, /* PDU data */
|
uint8_t * pdu, /* PDU data */
|
||||||
@@ -346,86 +356,50 @@ void npdu_handler(
|
|||||||
fprintf(stderr, "NPDU: received PDU!\n");
|
fprintf(stderr, "NPDU: received PDU!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_transmit_task(void *pArg)
|
void *test_milliseconds_task(void *pArg)
|
||||||
{
|
{
|
||||||
while (TRUE) {
|
struct timespec timeOut,remains;
|
||||||
Sleep(1000);
|
|
||||||
|
timeOut.tv_sec = 0;
|
||||||
|
timeOut.tv_nsec = 1000000; /* 1 millisecond */
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
nanosleep(&timeOut, &remains);
|
||||||
|
dlmstp_millisecond_timer();
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int rc = 0;
|
||||||
|
pthread_t hThread;
|
||||||
|
struct timespec timeOut,remains;
|
||||||
|
|
||||||
|
timeOut.tv_sec = 1;
|
||||||
|
timeOut.tv_nsec = 0; /* 1 millisecond */
|
||||||
|
|
||||||
|
/* initialize our interface */
|
||||||
|
RS485_Set_Interface("/dev/ttyS0");
|
||||||
|
RS485_Set_Baud_Rate(38400);
|
||||||
|
dlmstp_init();
|
||||||
|
dlmstp_set_my_address(0x05);
|
||||||
|
/* start our MilliSec task */
|
||||||
|
rc = pthread_create(&hThread, NULL, test_milliseconds_task, NULL);
|
||||||
|
rc = pthread_create(&hThread, NULL, dlmstp_receive_fsm_task, NULL);
|
||||||
|
rc = pthread_create(&hThread, NULL, dlmstp_master_fsm_task, NULL);
|
||||||
|
/* forever task */
|
||||||
|
for (;;) {
|
||||||
|
#if 0
|
||||||
MSTP_Create_And_Send_Frame(
|
MSTP_Create_And_Send_Frame(
|
||||||
&MSTP_Port,
|
&MSTP_Port,
|
||||||
FRAME_TYPE_TEST_REQUEST,
|
FRAME_TYPE_TEST_REQUEST,
|
||||||
MSTP_Port.SourceAddress,
|
MSTP_Port.SourceAddress,
|
||||||
MSTP_Port.This_Station,
|
MSTP_Port.This_Station,
|
||||||
NULL, 0);
|
NULL, 0);
|
||||||
}
|
#endif
|
||||||
}
|
nanosleep(&timeOut, &remains);
|
||||||
|
|
||||||
/* returns a delta timestamp */
|
|
||||||
uint32_t timestamp_ms(void)
|
|
||||||
{
|
|
||||||
DWORD ticks = 0, delta_ticks = 0;
|
|
||||||
static DWORD last_ticks = 0;
|
|
||||||
|
|
||||||
ticks = GetTickCount();
|
|
||||||
delta_ticks =
|
|
||||||
(ticks >= last_ticks ?
|
|
||||||
ticks - last_ticks :
|
|
||||||
MAXDWORD - last_ticks);
|
|
||||||
last_ticks = ticks;
|
|
||||||
|
|
||||||
return delta_ticks;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_millisecond_task(void *pArg)
|
|
||||||
{
|
|
||||||
DWORD ticks = 0;
|
|
||||||
DWORD last_ticks = GetTickCount();
|
|
||||||
DWORD total_ticks = 0;
|
|
||||||
|
|
||||||
(void)SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
|
|
||||||
while (TRUE) {
|
|
||||||
ticks = GetTickCount();
|
|
||||||
if (ticks > last_ticks) {
|
|
||||||
total_ticks = ticks - last_ticks;
|
|
||||||
} else if (ticks == last_ticks) {
|
|
||||||
Sleep(1);
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
/* integer rollover */
|
|
||||||
total_ticks = (MAXDWORD - last_ticks) + ticks;
|
|
||||||
}
|
|
||||||
while (total_ticks) {
|
|
||||||
dlmstp_millisecond_timer();
|
|
||||||
total_ticks--;
|
|
||||||
}
|
|
||||||
last_ticks = ticks;
|
|
||||||
Sleep(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
unsigned long hThread = 0;
|
|
||||||
uint32_t arg_value = 0;
|
|
||||||
|
|
||||||
RS485_Set_Interface("COM4");
|
|
||||||
RS485_Set_Baud_Rate(38400);
|
|
||||||
dlmstp_set_my_address(0x05);
|
|
||||||
dlmstp_init();
|
|
||||||
hThread = _beginthread(test_millisecond_task,4096,&arg_value);
|
|
||||||
if (hThread == 0) {
|
|
||||||
fprintf(stderr, "Failed to start timer task\n");
|
|
||||||
}
|
|
||||||
hThread = _beginthread(dlmstp_receive_fsm_task,4096,&arg_value);
|
|
||||||
if (hThread == 0) {
|
|
||||||
fprintf(stderr, "Failed to start recive FSM task\n");
|
|
||||||
}
|
|
||||||
hThread = _beginthread(dlmstp_master_fsm_task,4096,&arg_value);
|
|
||||||
if (hThread == 0) {
|
|
||||||
fprintf(stderr, "Failed to start Master Node FSM task\n");
|
|
||||||
}
|
|
||||||
/* forever task */
|
|
||||||
for (;;) {
|
|
||||||
Sleep(10000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
#Makefile to build test case
|
||||||
|
CC = gcc
|
||||||
|
BASEDIR = .
|
||||||
|
# -g for debugging with gdb
|
||||||
|
DEFINES = -DBIG_ENDIAN=0 -DBACDL_MSTP=1 -DTEST_DLMSTP
|
||||||
|
INCLUDES = -I. -I../../
|
||||||
|
CFLAGS = -Wall $(INCLUDES) $(DEFINES) -g
|
||||||
|
|
||||||
|
SRCS = rs485.c \
|
||||||
|
dlmstp.c \
|
||||||
|
mstp.c \
|
||||||
|
../../crc.c
|
||||||
|
|
||||||
|
OBJS = ${SRCS:.c=.o}
|
||||||
|
|
||||||
|
TARGET = dlmstp
|
||||||
|
|
||||||
|
all: ${TARGET}
|
||||||
|
|
||||||
|
${TARGET}: ${OBJS}
|
||||||
|
${CC} -pthread -o $@ ${OBJS}
|
||||||
|
|
||||||
|
.c.o:
|
||||||
|
${CC} -c ${CFLAGS} $*.c -o $@
|
||||||
|
|
||||||
|
depend:
|
||||||
|
rm -f .depend
|
||||||
|
${CC} -MM ${CFLAGS} *.c >> .depend
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf core ${TARGET} $(OBJS) *.bak *.1 *.ini
|
||||||
|
|
||||||
|
include: .depend
|
||||||
@@ -49,6 +49,7 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <sched.h>
|
||||||
|
|
||||||
/* Local includes */
|
/* Local includes */
|
||||||
#include "mstp.h"
|
#include "mstp.h"
|
||||||
@@ -201,6 +202,7 @@ void RS485_Send_Frame(
|
|||||||
turnaround_time = 1;
|
turnaround_time = 1;
|
||||||
while (mstp_port->SilenceTimer < turnaround_time) {
|
while (mstp_port->SilenceTimer < turnaround_time) {
|
||||||
/* do nothing - wait for timer to increment */
|
/* do nothing - wait for timer to increment */
|
||||||
|
sched_yield();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -86,6 +86,7 @@ uint16_t dlmstp_put_receive(
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if 1
|
||||||
static const uint8_t CRC_Table[256] =
|
static const uint8_t CRC_Table[256] =
|
||||||
{
|
{
|
||||||
0000, 0xfe, 0xff, 0x01, 0xfd, 0x03, 0x02, 0xfc,
|
0000, 0xfe, 0xff, 0x01, 0xfd, 0x03, 0x02, 0xfc,
|
||||||
@@ -122,7 +123,7 @@ static const uint8_t CRC_Table[256] =
|
|||||||
0xa9, 0x57, 0x56, 0xa8, 0x54, 0xaa, 0xab, 0x55
|
0xa9, 0x57, 0x56, 0xa8, 0x54, 0xaa, 0xab, 0x55
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint8_t calculate_header_CRC_table(
|
static uint8_t calculate_header_CRC(
|
||||||
volatile struct mstp_port_struct_t *mstp_port)
|
volatile struct mstp_port_struct_t *mstp_port)
|
||||||
{
|
{
|
||||||
uint8_t crc8 = 0xFF; /* used to calculate the crc value */
|
uint8_t crc8 = 0xFF; /* used to calculate the crc value */
|
||||||
@@ -136,6 +137,7 @@ static uint8_t calculate_header_CRC_table(
|
|||||||
return (~crc8);
|
return (~crc8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
static uint8_t calculate_header_CRC(
|
static uint8_t calculate_header_CRC(
|
||||||
volatile struct mstp_port_struct_t *mstp_port)
|
volatile struct mstp_port_struct_t *mstp_port)
|
||||||
{
|
{
|
||||||
@@ -149,6 +151,7 @@ static uint8_t calculate_header_CRC(
|
|||||||
|
|
||||||
return (~crc8);
|
return (~crc8);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static void print_received_packet(
|
static void print_received_packet(
|
||||||
volatile struct mstp_port_struct_t *mstp_port,
|
volatile struct mstp_port_struct_t *mstp_port,
|
||||||
@@ -178,8 +181,6 @@ static void print_received_packet(
|
|||||||
if (print_crc_flag) {
|
if (print_crc_flag) {
|
||||||
crc8 = calculate_header_CRC(mstp_port);
|
crc8 = calculate_header_CRC(mstp_port);
|
||||||
fprintf(stderr, "[%02X] ", crc8);
|
fprintf(stderr, "[%02X] ", crc8);
|
||||||
crc8 = calculate_header_CRC_table(mstp_port);
|
|
||||||
fprintf(stderr, "[%02X] ", crc8);
|
|
||||||
}
|
}
|
||||||
if (mstp_port->DataLength) {
|
if (mstp_port->DataLength) {
|
||||||
for (i = 0; i < mstp_port->DataLength; i++) {
|
for (i = 0; i < mstp_port->DataLength; i++) {
|
||||||
|
|||||||
@@ -409,8 +409,8 @@ int main(void)
|
|||||||
|
|
||||||
RS485_Set_Interface("COM4");
|
RS485_Set_Interface("COM4");
|
||||||
RS485_Set_Baud_Rate(38400);
|
RS485_Set_Baud_Rate(38400);
|
||||||
dlmstp_set_my_address(0x05);
|
|
||||||
dlmstp_init();
|
dlmstp_init();
|
||||||
|
dlmstp_set_my_address(0x05);
|
||||||
hThread = _beginthread(test_millisecond_task,4096,&arg_value);
|
hThread = _beginthread(test_millisecond_task,4096,&arg_value);
|
||||||
if (hThread == 0) {
|
if (hThread == 0) {
|
||||||
fprintf(stderr, "Failed to start timer task\n");
|
fprintf(stderr, "Failed to start timer task\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user