1) Created MSTP_Create_And_Send_Frame function.
2) Moved MSTP_Create_Frame call out of rs485.c and into mstp.c 3) Modified all previous calls to RS485_Send_Frame calls to call MSTP_Create_And_Send_Frame instead.
This commit is contained in:
+51
-27
@@ -128,6 +128,14 @@ const unsigned Tusage_delay = 15;
|
|||||||
// larger values for this timeout, not to exceed 100 milliseconds.)
|
// larger values for this timeout, not to exceed 100 milliseconds.)
|
||||||
const unsigned Tusage_timeout = 20;
|
const unsigned Tusage_timeout = 20;
|
||||||
|
|
||||||
|
void MSTP_Create_And_Send_Frame(
|
||||||
|
struct mstp_port_struct_t *mstp_port, // port to send from
|
||||||
|
uint8_t frame_type, // type of frame to send - see defines
|
||||||
|
uint8_t destination, // destination address
|
||||||
|
uint8_t source, // source address
|
||||||
|
uint8_t *data, // any data to be sent - may be null
|
||||||
|
unsigned data_len); // number of bytes of data (up to 501)
|
||||||
|
|
||||||
// Millisecond Timer - called every millisecond
|
// Millisecond Timer - called every millisecond
|
||||||
void MSTP_Millisecond_Timer(struct mstp_port_struct_t *mstp_port)
|
void MSTP_Millisecond_Timer(struct mstp_port_struct_t *mstp_port)
|
||||||
{
|
{
|
||||||
@@ -572,7 +580,7 @@ void MSTP_Master_Node_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
else if ((mstp_port->DestinationAddress == mstp_port->This_Station) &&
|
else if ((mstp_port->DestinationAddress == mstp_port->This_Station) &&
|
||||||
(mstp_port->FrameType == FRAME_TYPE_POLL_FOR_MASTER))
|
(mstp_port->FrameType == FRAME_TYPE_POLL_FOR_MASTER))
|
||||||
{
|
{
|
||||||
RS485_Send_Frame(
|
MSTP_Create_And_Send_Frame(
|
||||||
mstp_port,
|
mstp_port,
|
||||||
FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER,
|
FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER,
|
||||||
mstp_port->SourceAddress,
|
mstp_port->SourceAddress,
|
||||||
@@ -627,7 +635,7 @@ void MSTP_Master_Node_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
// or a proprietary type that does not expect a reply,
|
// or a proprietary type that does not expect a reply,
|
||||||
// {
|
// {
|
||||||
// // transmit the data frame
|
// // transmit the data frame
|
||||||
// RS485_Send_Frame(?????????????);
|
// MSTP_Create_And_Send_Frame(?????????????);
|
||||||
// FrameCount++;
|
// FrameCount++;
|
||||||
// mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN;
|
// mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN;
|
||||||
// }
|
// }
|
||||||
@@ -637,7 +645,7 @@ void MSTP_Master_Node_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
// a proprietary type that expects a reply,
|
// a proprietary type that expects a reply,
|
||||||
// {
|
// {
|
||||||
// // transmit the data frame
|
// // transmit the data frame
|
||||||
// RS485_Send_Frame();
|
// MSTP_Create_And_Send_Frame();
|
||||||
// FrameCount++;
|
// FrameCount++;
|
||||||
// mstp_port->master_state = MSTP_MASTER_STATE_WAIT_FOR_REPLY;
|
// mstp_port->master_state = MSTP_MASTER_STATE_WAIT_FOR_REPLY;
|
||||||
// }
|
// }
|
||||||
@@ -748,7 +756,7 @@ void MSTP_Master_Node_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
{
|
{
|
||||||
mstp_port->TokenCount++;
|
mstp_port->TokenCount++;
|
||||||
// transmit a Token frame to NS
|
// transmit a Token frame to NS
|
||||||
RS485_Send_Frame(
|
MSTP_Create_And_Send_Frame(
|
||||||
mstp_port,
|
mstp_port,
|
||||||
FRAME_TYPE_TOKEN,
|
FRAME_TYPE_TOKEN,
|
||||||
mstp_port->Next_Station,
|
mstp_port->Next_Station,
|
||||||
@@ -764,7 +772,7 @@ void MSTP_Master_Node_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
((uint8_t)((mstp_port->Poll_Station + 1) % (mstp_port->Nmax_master + 1)) != mstp_port->Next_Station))
|
((uint8_t)((mstp_port->Poll_Station + 1) % (mstp_port->Nmax_master + 1)) != mstp_port->Next_Station))
|
||||||
{
|
{
|
||||||
mstp_port->Poll_Station = (mstp_port->Poll_Station + 1) % (mstp_port->Nmax_master + 1);
|
mstp_port->Poll_Station = (mstp_port->Poll_Station + 1) % (mstp_port->Nmax_master + 1);
|
||||||
RS485_Send_Frame(
|
MSTP_Create_And_Send_Frame(
|
||||||
mstp_port,
|
mstp_port,
|
||||||
FRAME_TYPE_POLL_FOR_MASTER,
|
FRAME_TYPE_POLL_FOR_MASTER,
|
||||||
mstp_port->Poll_Station,
|
mstp_port->Poll_Station,
|
||||||
@@ -781,7 +789,7 @@ void MSTP_Master_Node_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
{
|
{
|
||||||
mstp_port->Poll_Station = mstp_port->This_Station;
|
mstp_port->Poll_Station = mstp_port->This_Station;
|
||||||
// transmit a Token frame to NS
|
// transmit a Token frame to NS
|
||||||
RS485_Send_Frame(
|
MSTP_Create_And_Send_Frame(
|
||||||
mstp_port,
|
mstp_port,
|
||||||
FRAME_TYPE_TOKEN,
|
FRAME_TYPE_TOKEN,
|
||||||
mstp_port->Next_Station,
|
mstp_port->Next_Station,
|
||||||
@@ -799,7 +807,7 @@ void MSTP_Master_Node_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
(mstp_port->SoleMaster == true))
|
(mstp_port->SoleMaster == true))
|
||||||
{
|
{
|
||||||
mstp_port->Poll_Station = (mstp_port->Next_Station +1) % (mstp_port->Nmax_master + 1);
|
mstp_port->Poll_Station = (mstp_port->Next_Station +1) % (mstp_port->Nmax_master + 1);
|
||||||
RS485_Send_Frame(
|
MSTP_Create_And_Send_Frame(
|
||||||
mstp_port,
|
mstp_port,
|
||||||
FRAME_TYPE_POLL_FOR_MASTER,
|
FRAME_TYPE_POLL_FOR_MASTER,
|
||||||
mstp_port->Poll_Station,
|
mstp_port->Poll_Station,
|
||||||
@@ -830,7 +838,7 @@ void MSTP_Master_Node_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
{
|
{
|
||||||
mstp_port->RetryCount++;
|
mstp_port->RetryCount++;
|
||||||
// Transmit a Token frame to NS
|
// Transmit a Token frame to NS
|
||||||
RS485_Send_Frame(
|
MSTP_Create_And_Send_Frame(
|
||||||
mstp_port,
|
mstp_port,
|
||||||
FRAME_TYPE_TOKEN,
|
FRAME_TYPE_TOKEN,
|
||||||
mstp_port->Next_Station,
|
mstp_port->Next_Station,
|
||||||
@@ -847,7 +855,7 @@ void MSTP_Master_Node_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
// Assume that NS has failed.
|
// Assume that NS has failed.
|
||||||
mstp_port->Poll_Station = (mstp_port->Next_Station + 1) % (mstp_port->Nmax_master + 1);
|
mstp_port->Poll_Station = (mstp_port->Next_Station + 1) % (mstp_port->Nmax_master + 1);
|
||||||
// Transmit a Poll For Master frame to PS.
|
// Transmit a Poll For Master frame to PS.
|
||||||
RS485_Send_Frame(
|
MSTP_Create_And_Send_Frame(
|
||||||
mstp_port,
|
mstp_port,
|
||||||
FRAME_TYPE_POLL_FOR_MASTER,
|
FRAME_TYPE_POLL_FOR_MASTER,
|
||||||
mstp_port->Poll_Station,
|
mstp_port->Poll_Station,
|
||||||
@@ -883,7 +891,7 @@ void MSTP_Master_Node_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
// on the network and is empowered to create a token.
|
// on the network and is empowered to create a token.
|
||||||
mstp_port->Poll_Station = (mstp_port->This_Station + 1) % (mstp_port->Nmax_master + 1);
|
mstp_port->Poll_Station = (mstp_port->This_Station + 1) % (mstp_port->Nmax_master + 1);
|
||||||
// Transmit a Poll For Master frame to PS.
|
// Transmit a Poll For Master frame to PS.
|
||||||
RS485_Send_Frame(
|
MSTP_Create_And_Send_Frame(
|
||||||
mstp_port,
|
mstp_port,
|
||||||
FRAME_TYPE_POLL_FOR_MASTER,
|
FRAME_TYPE_POLL_FOR_MASTER,
|
||||||
mstp_port->Poll_Station,
|
mstp_port->Poll_Station,
|
||||||
@@ -911,7 +919,7 @@ void MSTP_Master_Node_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
mstp_port->Next_Station = mstp_port->SourceAddress;
|
mstp_port->Next_Station = mstp_port->SourceAddress;
|
||||||
mstp_port->EventCount = 0;
|
mstp_port->EventCount = 0;
|
||||||
// Transmit a Token frame to NS
|
// Transmit a Token frame to NS
|
||||||
RS485_Send_Frame(
|
MSTP_Create_And_Send_Frame(
|
||||||
mstp_port,
|
mstp_port,
|
||||||
FRAME_TYPE_TOKEN,
|
FRAME_TYPE_TOKEN,
|
||||||
mstp_port->Next_Station,
|
mstp_port->Next_Station,
|
||||||
@@ -956,7 +964,7 @@ void MSTP_Master_Node_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
// poll for a master at address PS.
|
// poll for a master at address PS.
|
||||||
mstp_port->EventCount = 0;
|
mstp_port->EventCount = 0;
|
||||||
// transmit a Token frame to NS
|
// transmit a Token frame to NS
|
||||||
RS485_Send_Frame(
|
MSTP_Create_And_Send_Frame(
|
||||||
mstp_port,
|
mstp_port,
|
||||||
FRAME_TYPE_TOKEN,
|
FRAME_TYPE_TOKEN,
|
||||||
mstp_port->Next_Station,
|
mstp_port->Next_Station,
|
||||||
@@ -976,7 +984,7 @@ void MSTP_Master_Node_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
mstp_port->Poll_Station =
|
mstp_port->Poll_Station =
|
||||||
(mstp_port->Poll_Station + 1) % (mstp_port->Nmax_master + 1);
|
(mstp_port->Poll_Station + 1) % (mstp_port->Nmax_master + 1);
|
||||||
// Transmit a Poll For Master frame to PS.
|
// Transmit a Poll For Master frame to PS.
|
||||||
RS485_Send_Frame(
|
MSTP_Create_And_Send_Frame(
|
||||||
mstp_port,
|
mstp_port,
|
||||||
FRAME_TYPE_POLL_FOR_MASTER,
|
FRAME_TYPE_POLL_FOR_MASTER,
|
||||||
mstp_port->Poll_Station,
|
mstp_port->Poll_Station,
|
||||||
@@ -1012,7 +1020,7 @@ void MSTP_Master_Node_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
// within Treply_delay after the reception of the
|
// within Treply_delay after the reception of the
|
||||||
// final octet of the requesting frame
|
// final octet of the requesting frame
|
||||||
// (the mechanism used to determine this is a local matter),
|
// (the mechanism used to determine this is a local matter),
|
||||||
// then call RS485_Send_Frame to transmit the reply frame
|
// then call MSTP_Create_And_Send_Frame to transmit the reply frame
|
||||||
// and enter the IDLE state to wait for the next frame.
|
// and enter the IDLE state to wait for the next frame.
|
||||||
|
|
||||||
// Test Request
|
// Test Request
|
||||||
@@ -1028,7 +1036,7 @@ void MSTP_Master_Node_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
// then no response shall be returned.
|
// then no response shall be returned.
|
||||||
if (mstp_port->FrameType == FRAME_TYPE_TEST_REQUEST)
|
if (mstp_port->FrameType == FRAME_TYPE_TEST_REQUEST)
|
||||||
{
|
{
|
||||||
RS485_Send_Frame(
|
MSTP_Create_And_Send_Frame(
|
||||||
mstp_port,
|
mstp_port,
|
||||||
FRAME_TYPE_TEST_RESPONSE,
|
FRAME_TYPE_TEST_RESPONSE,
|
||||||
mstp_port->SourceAddress,
|
mstp_port->SourceAddress,
|
||||||
@@ -1047,12 +1055,12 @@ void MSTP_Master_Node_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
// used to determine this is a local matter),
|
// used to determine this is a local matter),
|
||||||
// then an immediate reply is not possible.
|
// then an immediate reply is not possible.
|
||||||
// Any reply shall wait until this node receives the token.
|
// Any reply shall wait until this node receives the token.
|
||||||
// Call RS485_Send_Frame to transmit a Reply Postponed frame,
|
// Call MSTP_Create_And_Send_Frame to transmit a Reply Postponed frame,
|
||||||
// and enter the IDLE state.
|
// and enter the IDLE state.
|
||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
RS485_Send_Frame(
|
MSTP_Create_And_Send_Frame(
|
||||||
mstp_port,
|
mstp_port,
|
||||||
FRAME_TYPE_REPLY_POSTPONED,
|
FRAME_TYPE_REPLY_POSTPONED,
|
||||||
mstp_port->SourceAddress,
|
mstp_port->SourceAddress,
|
||||||
@@ -1170,6 +1178,28 @@ unsigned MSTP_Create_Frame(
|
|||||||
return index; // returns the frame length
|
return index; // returns the frame length
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MSTP_Create_And_Send_Frame(
|
||||||
|
struct mstp_port_struct_t *mstp_port, // port to send from
|
||||||
|
uint8_t frame_type, // type of frame to send - see defines
|
||||||
|
uint8_t destination, // destination address
|
||||||
|
uint8_t source, // source address
|
||||||
|
uint8_t *data, // any data to be sent - may be null
|
||||||
|
unsigned data_len) // number of bytes of data (up to 501)
|
||||||
|
{
|
||||||
|
uint8_t buffer[INPUT_BUFFER_SIZE] = {0};
|
||||||
|
uint16_t len = 0; // number of bytes to send
|
||||||
|
|
||||||
|
len = (uint16_t)MSTP_Create_Frame(
|
||||||
|
buffer, // where frame is loaded
|
||||||
|
sizeof(buffer), // amount of space available
|
||||||
|
frame_type, // type of frame to send - see defines
|
||||||
|
destination, // destination address
|
||||||
|
source, // source address
|
||||||
|
data, // any data to be sent - may be null
|
||||||
|
data_len); // number of bytes of data (up to 501)
|
||||||
|
|
||||||
|
RS485_Send_Frame(mstp_port, buffer, len);
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef TEST
|
#ifdef TEST
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
@@ -1179,18 +1209,12 @@ unsigned MSTP_Create_Frame(
|
|||||||
// test stub functions
|
// test stub functions
|
||||||
void RS485_Send_Frame(
|
void RS485_Send_Frame(
|
||||||
struct mstp_port_struct_t *mstp_port, // port specific data
|
struct mstp_port_struct_t *mstp_port, // port specific data
|
||||||
uint8_t frame_type, // type of frame to send - see defines
|
uint8_t *buffer, // frame to send (up to 501 bytes of data)
|
||||||
uint8_t destination, // destination address
|
uint16_t nbytes) // number of bytes of data (up to 501)
|
||||||
uint8_t source, // source address
|
|
||||||
uint8_t *data, // any data to be sent - may be null
|
|
||||||
unsigned data_len) // number of bytes of data (up to 501)
|
|
||||||
{
|
{
|
||||||
(void)mstp_port;
|
(void)mstp_port;
|
||||||
(void)frame_type;
|
(void)buffer;
|
||||||
(void)destination;
|
(void)nbytes;
|
||||||
(void)source;
|
|
||||||
(void)data;
|
|
||||||
(void)data_len;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define RING_BUFFER_DATA_SIZE 1
|
#define RING_BUFFER_DATA_SIZE 1
|
||||||
|
|||||||
@@ -231,13 +231,4 @@ void MSTP_Millisecond_Timer(struct mstp_port_struct_t *mstp_port);
|
|||||||
void MSTP_Receive_Frame_FSM(struct mstp_port_struct_t *mstp_port);
|
void MSTP_Receive_Frame_FSM(struct mstp_port_struct_t *mstp_port);
|
||||||
void MSTP_Master_Node_FSM(struct mstp_port_struct_t *mstp_port);
|
void MSTP_Master_Node_FSM(struct mstp_port_struct_t *mstp_port);
|
||||||
|
|
||||||
unsigned MSTP_Create_Frame(
|
|
||||||
uint8_t *buffer, // where frame is loaded
|
|
||||||
unsigned buffer_len, // amount of space available
|
|
||||||
uint8_t frame_type, // type of frame to send - see defines
|
|
||||||
uint8_t destination, // destination address
|
|
||||||
uint8_t source, // source address
|
|
||||||
uint8_t *data, // any data to be sent - may be null
|
|
||||||
unsigned data_len); // number of bytes of data (up to 501)
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -45,16 +45,10 @@
|
|||||||
|
|
||||||
// Transmits a Frame on the wire
|
// Transmits a Frame on the wire
|
||||||
void RS485_Send_Frame(
|
void RS485_Send_Frame(
|
||||||
struct mstp_port_struct_t *mstp_port, // port to send from
|
struct mstp_port_struct_t *mstp_port, // port specific data
|
||||||
uint8_t frame_type, // type of frame to send - see defines
|
uint8_t *buffer, // frame to send (up to 501 bytes of data)
|
||||||
uint8_t destination, // destination address
|
uint16_t nbytes) // number of bytes of data (up to 501)
|
||||||
uint8_t source, // source address
|
|
||||||
uint8_t *data, // any data to be sent - may be null
|
|
||||||
unsigned data_len) // number of bytes of data (up to 501)
|
|
||||||
{
|
{
|
||||||
uint8_t buffer[INPUT_BUFFER_SIZE] = {0};
|
|
||||||
uint8_t *pbuf = NULL; // used for pointer arithmatic
|
|
||||||
unsigned len = 0; // number of bytes to send
|
|
||||||
|
|
||||||
// in order to avoid line contention
|
// in order to avoid line contention
|
||||||
while (mstp_port->Turn_Around_Waiting)
|
while (mstp_port->Turn_Around_Waiting)
|
||||||
@@ -64,21 +58,11 @@ void RS485_Send_Frame(
|
|||||||
|
|
||||||
// Disable the receiver, and enable the transmit line driver.
|
// Disable the receiver, and enable the transmit line driver.
|
||||||
|
|
||||||
len = MSTP_Create_Frame(
|
while (nbytes)
|
||||||
buffer, // where frame is loaded
|
|
||||||
sizeof(buffer), // amount of space available
|
|
||||||
frame_type, // type of frame to send - see defines
|
|
||||||
destination, // destination address
|
|
||||||
source, // source address
|
|
||||||
data, // any data to be sent - may be null
|
|
||||||
data_len); // number of bytes of data (up to 501)
|
|
||||||
|
|
||||||
pbuf = &buffer[0];
|
|
||||||
while (len)
|
|
||||||
{
|
{
|
||||||
putc(*pbuf,stderr);
|
putc(*buffer,stderr);
|
||||||
pbuf++;
|
buffer++;
|
||||||
len--;
|
nbytes--;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait until the final stop bit of the most significant CRC octet
|
// Wait until the final stop bit of the most significant CRC octet
|
||||||
|
|||||||
@@ -44,20 +44,15 @@
|
|||||||
|
|
||||||
// Transmits a Frame on the wire
|
// Transmits a Frame on the wire
|
||||||
void RS485_Send_Frame(
|
void RS485_Send_Frame(
|
||||||
struct mstp_port_struct_t *mstp_port, // port to send from
|
struct mstp_port_struct_t *mstp_port, // port specific data
|
||||||
uint8_t frame_type, // type of frame to send - see defines
|
uint8_t *buffer, // frame to send (up to 501 bytes of data)
|
||||||
uint8_t destination, // destination address
|
uint16_t nbytes) // number of bytes of data (up to 501)
|
||||||
uint8_t source, // source address
|
|
||||||
uint8_t *data, // any data to be sent - may be null
|
|
||||||
unsigned data_len) // number of bytes of data (up to 501)
|
|
||||||
{
|
{
|
||||||
uint8_t HeaderCRC; // used for running CRC calculation
|
uint8_t HeaderCRC; // used for running CRC calculation
|
||||||
|
|
||||||
(void)frame_type; // FIXME: temp until we implement this code
|
(void)buffer; // FIXME: temp until we implement this code
|
||||||
(void)destination; // FIXME: temp until we implement this code
|
(void)nbytes; // FIXME: temp until we implement this code
|
||||||
(void)source; // FIXME: temp until we implement this code
|
|
||||||
(void)data; // FIXME: temp until we implement this code
|
|
||||||
(void)data_len; // FIXME: temp until we implement this code
|
|
||||||
// in order to avoid line contention
|
// in order to avoid line contention
|
||||||
while (mstp_port->SilenceTimer < Tturnaround)
|
while (mstp_port->SilenceTimer < Tturnaround)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -41,11 +41,8 @@
|
|||||||
|
|
||||||
void RS485_Send_Frame(
|
void RS485_Send_Frame(
|
||||||
struct mstp_port_struct_t *mstp_port, // port specific data
|
struct mstp_port_struct_t *mstp_port, // port specific data
|
||||||
uint8_t frame_type, // type of frame to send - see defines
|
uint8_t *buffer, // frame to send (up to 501 bytes of data)
|
||||||
uint8_t destination, // destination address
|
uint16_t nbytes); // number of bytes of data (up to 501)
|
||||||
uint8_t source, // source address
|
|
||||||
uint8_t *data, // any data to be sent - may be null
|
|
||||||
unsigned data_len); // number of bytes of data (up to 501)
|
|
||||||
|
|
||||||
void RS485_Check_UART_Data(
|
void RS485_Check_UART_Data(
|
||||||
struct mstp_port_struct_t *mstp_port); // port specific data
|
struct mstp_port_struct_t *mstp_port); // port specific data
|
||||||
|
|||||||
Reference in New Issue
Block a user