Added MS/TP to Linux using the USB->RS485 from SerialGear. Tested Receive only using the rx_fsm.c and rx_fsm.mak functional test. Works as an MS/TP network monitor.

This commit is contained in:
skarg
2007-06-29 21:22:46 +00:00
parent b3fda233cf
commit 250cc92e00
9 changed files with 3042 additions and 26 deletions
+206 -26
View File
@@ -1,6 +1,6 @@
/*####COPYRIGHTBEGIN####
-------------------------------------------
Copyright (C) 2004 Steve Karg
Copyright (C) 2007 Steve Karg
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
@@ -40,54 +40,234 @@
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
/* Linux includes */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
/* Local includes */
#include "mstp.h"
/* handle returned from open() */
static int RS485_Handle = -1;
/* baudrate settings are defined in <asm/termbits.h>, which is
included by <termios.h> */
static unsigned int RS485_Baud = B38400;
/* serial port name, /dev/ttyS0 */
static char *RS485_Port_Name = "/dev/ttyS0";
/* serial I/O settings */
static struct termios RS485_oldtio;
#define _POSIX_SOURCE 1 /* POSIX compliant source */
/*********************************************************************
* DESCRIPTION: Configures the interface name
* RETURN: none
* ALGORITHM: none
* NOTES: none
*********************************************************************/
void RS485_Set_Interface(char *ifname)
{
/* note: expects a constant char, or char from the heap */
RS485_Port_Name = ifname;
}
/****************************************************************************
* DESCRIPTION: Returns the baud rate that we are currently running at
* RETURN: none
* ALGORITHM: none
* NOTES: none
*****************************************************************************/
uint32_t RS485_Get_Baud_Rate(void)
{
switch (RS485_Baud) {
case B19200: return 19200;
case B38400: return 38400;
case B57600: return 57600;
default:
case B9600: return 9600;
}
}
/****************************************************************************
* DESCRIPTION: Sets the baud rate for the chip USART
* RETURN: none
* ALGORITHM: none
* NOTES: none
*****************************************************************************/
bool RS485_Set_Baud_Rate(uint32_t baud)
{
bool valid = true;
switch (baud) {
case 9600:
RS485_Baud = B9600;
break;
case 19200:
RS485_Baud = B19200;
break;
case 38400:
RS485_Baud = B38400;
break;
default:
valid = false;
break;
}
if (valid) {
/* FIXME: store the baud rate */
}
return valid;
}
void RS485_Initialize(void)
{
struct termios newtio;
/*
Open device for reading and writing and not as controlling tty
because we don't want to get killed if linenoise sends CTRL-C.
*/
RS485_Handle = open(RS485_Port_Name, O_RDWR | O_NOCTTY );
if (RS485_Handle < 0) {
perror(RS485_Port_Name);
exit(-1);
}
tcgetattr(RS485_Handle,&RS485_oldtio); /* save current serial port settings */
bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */
/*
BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
CRTSCTS : output hardware flow control (only used if the cable has
all necessary lines. See sect. 7 of Serial-HOWTO)
CS8 : 8n1 (8bit,no parity,1 stopbit)
CLOCAL : local connection, no modem contol
CREAD : enable receiving characters
*/
newtio.c_cflag = RS485_Baud | CRTSCTS | CS8 | CLOCAL | CREAD;
/*
IGNPAR : ignore bytes with parity errors
ICRNL : map CR to NL (otherwise a CR input on the other computer
will not terminate input)
otherwise make device raw (no other input processing)
*/
newtio.c_iflag = IGNPAR | ICRNL;
/*
Raw output.
*/
newtio.c_oflag = 0;
/*
ICANON : enable canonical input
disable all echo functionality, and don't send signals to calling program
*/
newtio.c_lflag = ICANON;
/*
now clean the modem line and activate the settings for the port
*/
tcflush(RS485_Handle, TCIFLUSH);
tcsetattr(RS485_Handle,TCSANOW,&newtio);
}
/* Transmits a Frame on the wire */
void RS485_Send_Frame(struct mstp_port_struct_t *mstp_port, /* port specific data */
uint8_t * buffer, /* frame to send (up to 501 bytes of data) */
uint16_t nbytes)
{ /* number of bytes of data (up to 501) */
void RS485_Send_Frame(
struct mstp_port_struct_t *mstp_port, /* port specific data */
uint8_t * buffer, /* frame to send (up to 501 bytes of data) */
uint16_t nbytes) /* number of bytes of data (up to 501) */
{
uint8_t turnaround_time;
uint32_t baud;
ssize_t written = 0;
/* in order to avoid line contention */
while (mstp_port->Turn_Around_Waiting) {
/* wait, yield, or whatever */
if (mstp_port) {
baud = RS485_Get_Baud_Rate();
/* wait about 40 bit times since reception */
if (baud == 9600)
turnaround_time = 4;
else if (baud == 19200)
turnaround_time = 2;
else
turnaround_time = 1;
while (mstp_port->SilenceTimer < turnaround_time) {
/* do nothing - wait for timer to increment */
};
}
/*
On success, the number of bytes written are returned (zero indicates
nothing was written). On error, -1 is returned, and errno is set
appropriately. If count is zero and the file descriptor refers to a
regular file, 0 will be returned without causing any other effect. For
a special file, the results are not portable.
*/
written = write(RS485_Handle, buffer, nbytes);
/* Disable the receiver, and enable the transmit line driver. */
while (nbytes) {
putc(*buffer, stderr);
buffer++;
nbytes--;
/* per MSTP spec, sort of */
if (mstp_port) {
mstp_port->SilenceTimer = 0;
}
/* Wait until the final stop bit of the most significant CRC octet */
/* has been transmitted but not more than Tpostdrive. */
/* Disable the transmit line driver. */
return;
}
/* called by timer, interrupt(?) or other thread */
void RS485_Check_UART_Data(struct mstp_port_struct_t *mstp_port)
{
uint8_t buf[1];
int count;
if (mstp_port->ReceiveError == true) {
/* wait for state machine to clear this */
}
/* wait for state machine to read from the DataRegister */
else if (mstp_port->DataAvailable == false) {
/* check for data */
count = read(RS485_Handle,buf,sizeof(buf));
/* if error, */
/* ReceiveError = TRUE; */
/* return; */
mstp_port->DataRegister = 0; /* FIXME: Get this data from UART or buffer */
/* if data is ready, */
/* DataAvailable = TRUE; */
/* return; */
if (count) {
mstp_port->DataRegister = buf[0];
/* if data is ready, */
mstp_port->DataAvailable = true;
}
}
}
#ifdef TEST_RS485
int main(void)
{
uint8_t buf[8];
unsigned i = 0;
int res;
RS485_Set_Interface("/dev/ttyS0");
RS485_Set_Baud_Rate(38400);
RS485_Initialize();
for (;;) {
res = read(RS485_Handle,buf,sizeof(buf));
/* print any characters received */
if (res) {
for (i = 0; i < res; i++) {
fprintf(stderr,"%02X ",buf[i]);
}
}
res = 0;
}
/* restore the old port settings */
tcsetattr(RS485_Handle,TCSANOW,&RS485_oldtio);
return 0;
}
#endif