adjust root folder
This commit is contained in:
Binary file not shown.
+1328
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,131 @@
|
||||
/*====================================================================
|
||||
|
||||
_MSC_VER Microsoft C 6.0 and later
|
||||
_QC Microsoft Quick C 2.51 and later
|
||||
__TURBOC__ Borland Turbo C, Turbo C++ and BC++
|
||||
__BORLANDC__ Borland C++
|
||||
__ZTC__ Zortech C and C++
|
||||
__SC__ Symantec C++
|
||||
__WATCOMC__ WATCOM C
|
||||
__POWERC Mix Power C
|
||||
__GNUC__ Gnu C
|
||||
|
||||
Revised:
|
||||
|
||||
25-Sep-95 Bob Stout Original from PC-PORT.H
|
||||
30-Mar-96 Ed Blackman OS/2 mods for OS/2 ver 2.0 and up
|
||||
30-May-96 Andrew Clarke Added support for WATCOM C/C++ __NT__ macro.
|
||||
17-Jun-96 Bob Stout Added __FLAT__ macros support
|
||||
20-Aug-96 Bob Stout Eliminate Win32 conflicts
|
||||
======================================================================*/
|
||||
|
||||
|
||||
/* prevent multiple inclusions of this header file */
|
||||
|
||||
#ifndef EXTKWORD__H
|
||||
#define EXTKWORD__H
|
||||
|
||||
#include <limits.h> /* For INT_MAX, LONG_MAX */
|
||||
|
||||
/*
|
||||
** Watcom defines __FLAT__ for 32-bit environments and so will we
|
||||
*/
|
||||
|
||||
#if !defined(__FLAT__) && !defined(__WATCOMC__) && !defined(_MSC_VER)
|
||||
#if defined(__GNUC__)
|
||||
#define __FLAT__ 1
|
||||
#elif defined (_WIN32) || defined(WIN32) || defined(__NT__)
|
||||
#define __FLAT__ 1
|
||||
#elif defined(__INTSIZE)
|
||||
#if (4 == __INTSIZE)
|
||||
#define __FLAT__ 1
|
||||
#endif
|
||||
#elif (defined(__ZTC__) && !defined(__SC__)) || defined(__TURBOC__)
|
||||
#if ((INT_MAX != SHRT_MAX) && (SHRT_MAX == 32767))
|
||||
#define __FLAT__ 1
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
** Correct extended keywords syntax
|
||||
*/
|
||||
|
||||
#if defined(__unix__)
|
||||
#if !defined(FAR)
|
||||
#define FAR
|
||||
#endif
|
||||
#if !defined(NEAR)
|
||||
#define NEAR
|
||||
#endif
|
||||
#if !defined(HUGE)
|
||||
#define HUGE
|
||||
#endif
|
||||
#if !defined(PASCAL)
|
||||
#define PASCAL
|
||||
#endif
|
||||
#if !defined(CDECL)
|
||||
#define CDECL
|
||||
#endif
|
||||
#if !defined(INTERRUPT)
|
||||
#define INTERRUPT
|
||||
#endif
|
||||
#elif defined(__OS2__) /* EBB: not sure this works for OS/2 1.x */
|
||||
#include <os2def.h>
|
||||
#define INTERRUPT
|
||||
#ifndef HUGE
|
||||
#define HUGE
|
||||
#endif
|
||||
#elif defined(_WIN32) || defined(WIN32) || defined(__NT__)
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define NOGDI
|
||||
#define NOSERVICE
|
||||
#undef INC_OLE1
|
||||
#undef INC_OLE2
|
||||
#include <windows.h>
|
||||
#define INTERRUPT
|
||||
#ifndef HUGE
|
||||
#define HUGE
|
||||
#endif
|
||||
#else /* ! Win 32 or OS/2 */
|
||||
#if (defined(__POWERC) || (defined(__TURBOC__) && !defined(__BORLANDC__)) \
|
||||
|| (defined(__ZTC__) && !defined(__SC__))) && !defined(__FLAT__)
|
||||
#define FAR far
|
||||
#define NEAR near
|
||||
#define PASCAL pascal
|
||||
#define CDECL cdecl
|
||||
#if (defined(__ZTC__) && !defined(__SC__)) || (defined(__SC__) && \
|
||||
(__SC__ < 0x700))
|
||||
#ifndef HUGE
|
||||
#define HUGE far
|
||||
#endif
|
||||
#define INTERRUPT
|
||||
#else
|
||||
#ifndef HUGE
|
||||
#define HUGE huge
|
||||
#endif
|
||||
#define INTERRUPT interrupt
|
||||
#endif
|
||||
#else
|
||||
#if (defined(__MSDOS__) || defined(MSDOS)) && !defined(__FLAT__)
|
||||
#define FAR _far
|
||||
#define NEAR _near
|
||||
#ifndef HUGE
|
||||
#define HUGE _huge
|
||||
#endif
|
||||
#define PASCAL _pascal
|
||||
#define CDECL _cdecl
|
||||
#define INTERRUPT _interrupt
|
||||
#else
|
||||
#define FAR
|
||||
#define NEAR
|
||||
#ifndef HUGE
|
||||
#define HUGE
|
||||
#endif
|
||||
#define PASCAL
|
||||
#define CDECL
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif /* EXTKWORD__H */
|
||||
@@ -0,0 +1,112 @@
|
||||
/**************************************************************************
|
||||
*
|
||||
* Copyright (C) 2007 Steve Karg <skarg@users.sourceforge.net>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*********************************************************************/
|
||||
/* hardware specific */
|
||||
#include "timer.h"
|
||||
/* standard libraries */
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
/* BACnet */
|
||||
#include "rs485.h"
|
||||
#include "datalink.h"
|
||||
#include "npdu.h"
|
||||
#include "apdu.h"
|
||||
#include "dcc.h"
|
||||
#include "iam.h"
|
||||
#include "handlers.h"
|
||||
#include "device.h"
|
||||
#include "dcc.h"
|
||||
#include "iam.h"
|
||||
#include "txbuf.h"
|
||||
|
||||
static unsigned long DCC_Timer = 1000;
|
||||
|
||||
static void millisecond_timer(
|
||||
void)
|
||||
{
|
||||
while (Timer_Milliseconds) {
|
||||
Timer_Milliseconds--;
|
||||
if (DCC_Timer) {
|
||||
DCC_Timer--;
|
||||
}
|
||||
}
|
||||
/* note: MS/TP silence timer is updated in ISR */
|
||||
}
|
||||
|
||||
static void bacnet_init(
|
||||
void)
|
||||
{
|
||||
#if defined(BACDL_MSTP)
|
||||
RS485_Set_Baud_Rate(38400);
|
||||
dlmstp_set_mac_address(57);
|
||||
dlmstp_set_max_master(127);
|
||||
dlmstp_set_max_info_frames(1);
|
||||
dlmstp_init(NULL);
|
||||
#endif
|
||||
Device_Set_Object_Instance_Number(11111);
|
||||
#ifndef DLMSTP_TEST
|
||||
/* we need to handle who-is to support dynamic device binding */
|
||||
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is);
|
||||
/* Set the handlers for any confirmed services that we support. */
|
||||
/* We must implement read property - it's required! */
|
||||
apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
|
||||
handler_read_property);
|
||||
apdu_set_confirmed_handler(SERVICE_CONFIRMED_REINITIALIZE_DEVICE,
|
||||
handler_reinitialize_device);
|
||||
apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY,
|
||||
handler_write_property);
|
||||
/* handle communication so we can shutup when asked */
|
||||
apdu_set_confirmed_handler(SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL,
|
||||
handler_device_communication_control);
|
||||
#endif
|
||||
}
|
||||
|
||||
int main(
|
||||
void)
|
||||
{
|
||||
uint16_t pdu_len = 0;
|
||||
BACNET_ADDRESS src; /* source address */
|
||||
uint8_t pdu[MAX_MPDU]; /* PDU data */
|
||||
|
||||
Timer_Init();
|
||||
bacnet_init();
|
||||
/* broadcast an I-Am on startup */
|
||||
Send_I_Am(&Handler_Transmit_Buffer[0]);
|
||||
for (;;) {
|
||||
millisecond_timer();
|
||||
if (!DCC_Timer) {
|
||||
dcc_timer_seconds(1);
|
||||
DCC_Timer = 1000;
|
||||
}
|
||||
/* BACnet handling */
|
||||
pdu_len = datalink_receive(&src, &pdu[0], MAX_MPDU, 0);
|
||||
if (pdu_len) {
|
||||
#ifndef DLMSTP_TEST
|
||||
npdu_handler(&src, &pdu[0], pdu_len);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
** MK_FP.H
|
||||
**
|
||||
** Standard header file making sure this pesky Intel macro is defined!
|
||||
*/
|
||||
|
||||
#ifndef MK_FP__H
|
||||
#define MK_FP__H
|
||||
|
||||
#include "extkword.h"
|
||||
|
||||
#if defined(__WATCOMC__)
|
||||
#include <i86.h>
|
||||
#elif !defined(__PACIFIC__)
|
||||
#include <dos.h>
|
||||
#endif
|
||||
|
||||
#if !defined(MK_FP)
|
||||
#define MK_FP(seg,off) \
|
||||
((void FAR *)(((unsigned long)(seg) << 16)|(unsigned)(off)))
|
||||
#endif
|
||||
|
||||
#endif /* MK_FP__H */
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
** PCHWIO.C - SNIPPETS portable hardware I/O access under DOS
|
||||
**
|
||||
** public domain by Bob Stout
|
||||
*/
|
||||
|
||||
#include "pchwio.h"
|
||||
#include "mk_fp.h"
|
||||
|
||||
#if defined(__ZTC__) && !defined(__SC__)
|
||||
|
||||
void FAR *getvect(
|
||||
unsigned intnum)
|
||||
{
|
||||
unsigned seg, off;
|
||||
|
||||
int_getvector(intnum, &off, &seg);
|
||||
return MK_FP(seg, off);
|
||||
}
|
||||
|
||||
void setvect(
|
||||
unsigned intnum,
|
||||
void (INTERRUPT FAR * handler) ())
|
||||
{
|
||||
unsigned seg = FP_SEG(handler), off = FP_OFF(handler);
|
||||
|
||||
int_setvector(intnum, off, seg);
|
||||
}
|
||||
|
||||
#endif /* ZTC getvect(), setvect() */
|
||||
|
||||
|
||||
|
||||
#if defined(_MSC_VER) || defined(__WATCOMC__) || \
|
||||
defined(__ZTC__) || defined(__SC__)
|
||||
|
||||
#if !defined(MK_FP)
|
||||
#define MK_FP(seg,off) ((void far *)(((long)(seg) << 16)|(unsigned)(off)))
|
||||
#endif
|
||||
|
||||
unsigned char Peekb(
|
||||
unsigned seg,
|
||||
unsigned ofs)
|
||||
{
|
||||
unsigned char FAR *ptr;
|
||||
|
||||
ptr = MK_FP(seg, ofs);
|
||||
return *ptr;
|
||||
}
|
||||
|
||||
unsigned short Peekw(
|
||||
unsigned seg,
|
||||
unsigned ofs)
|
||||
{
|
||||
unsigned FAR *ptr;
|
||||
|
||||
ptr = MK_FP(seg, ofs);
|
||||
return *ptr;
|
||||
}
|
||||
|
||||
void Pokeb(
|
||||
unsigned seg,
|
||||
unsigned ofs,
|
||||
unsigned char ch)
|
||||
{
|
||||
unsigned char FAR *ptr;
|
||||
|
||||
ptr = MK_FP(seg, ofs);
|
||||
*ptr = ch;
|
||||
}
|
||||
|
||||
void Pokew(
|
||||
unsigned seg,
|
||||
unsigned ofs,
|
||||
unsigned short num)
|
||||
{
|
||||
unsigned FAR *ptr;
|
||||
|
||||
ptr = MK_FP(seg, ofs);
|
||||
*ptr = num;
|
||||
}
|
||||
|
||||
#endif /* MSC/ZTC/WC Peek(), poke() */
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
** PCHWIO.H - SNIPPETS header file for portable hardware I/O access under DOS
|
||||
**
|
||||
** public domain by Bob Stout
|
||||
*/
|
||||
|
||||
#ifndef PCHWIO__H
|
||||
#define PCHWIO__H
|
||||
|
||||
#include <dos.h>
|
||||
#include "extkword.h"
|
||||
|
||||
|
||||
#if defined(__TURBOC__) || defined(__POWERC)
|
||||
#ifndef inp
|
||||
#define inp inportb
|
||||
#endif
|
||||
#ifndef outp
|
||||
#define outp outportb
|
||||
#endif
|
||||
#ifndef inpw
|
||||
#define inpw inport
|
||||
#endif
|
||||
#ifndef outpw
|
||||
#define outpw outport
|
||||
#endif
|
||||
#elif defined(__ZTC__)
|
||||
#include <int.h>
|
||||
#define enable int_on
|
||||
#define disable int_off
|
||||
#if !defined(__SC__)
|
||||
void FAR *getvect(
|
||||
unsigned intnum);
|
||||
void setvect(
|
||||
unsigned intnum,
|
||||
void (INTERRUPT FAR * handler) ());
|
||||
#else
|
||||
#define getvect _dos_getvect
|
||||
#define setvect _dos_setvect
|
||||
#endif
|
||||
#else /* assume MSC/QC/WC */
|
||||
#include <conio.h>
|
||||
#if defined(__WATCOMC__)
|
||||
#include <i86.h>
|
||||
#endif
|
||||
#define enable _enable
|
||||
#define disable _disable
|
||||
#define getvect _dos_getvect
|
||||
#define setvect _dos_setvect
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(_MSC_VER) || defined(__WATCOMC__) || \
|
||||
defined(__ZTC__) || defined(__SC__)
|
||||
|
||||
unsigned char Peekb(
|
||||
unsigned seg,
|
||||
unsigned ofs); /* PCHWIO.C */
|
||||
unsigned short Peekw(
|
||||
unsigned seg,
|
||||
unsigned ofs); /* PCHWIO.C */
|
||||
void Pokeb(
|
||||
unsigned seg,
|
||||
unsigned ofs,
|
||||
unsigned char ch); /* PCHWIO.C */
|
||||
void Pokew(
|
||||
unsigned seg,
|
||||
unsigned ofs,
|
||||
unsigned short num); /* PCHWIO.C */
|
||||
|
||||
#elif defined(__TURBOC__)
|
||||
#define Peekw peek
|
||||
#define Pokew poke
|
||||
#define Peekb peekb
|
||||
#define Pokeb pokeb
|
||||
#endif /* peek(), poke() */
|
||||
|
||||
#endif /* PCHWIO__H */
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
+----------------------------------------------------+
|
||||
| Thunderbird Software |
|
||||
+----------------------------------------------------+
|
||||
| Filespec : Queue.c |
|
||||
| Date : September 29, 1994 |
|
||||
| Time : 10:16AM |
|
||||
| Revision : 1.0 |
|
||||
+----------------------------------------------------+
|
||||
| Programmer: Scott Andrews |
|
||||
| Address : 5358 Summit RD SW |
|
||||
| City/State: Pataskala, Ohio |
|
||||
| Zip : 43062 |
|
||||
+----------------------------------------------------+
|
||||
| Released to the Public Domain |
|
||||
+----------------------------------------------------+
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "queue.h"
|
||||
|
||||
QUEUE *alloc_queue(
|
||||
int size)
|
||||
{
|
||||
QUEUE *retval;
|
||||
retval = (QUEUE *) malloc(sizeof(QUEUE) + (size_t) size);
|
||||
if ((QUEUE *) 0 != retval) {
|
||||
retval->size = size;
|
||||
retval->head = 0;
|
||||
retval->tail = 0;
|
||||
retval->avail = size;
|
||||
retval->buffer = ((char *) retval) + sizeof(QUEUE);
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
int en_queue(
|
||||
QUEUE * queue,
|
||||
char data)
|
||||
{
|
||||
int retval = -1;
|
||||
if (0 != queue->avail) {
|
||||
*(queue->buffer + queue->head) = data;
|
||||
queue->head += 1;
|
||||
if (queue->head == queue->size)
|
||||
queue->head = 0;
|
||||
queue->avail -= 1;
|
||||
retval = queue->avail;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
int de_queue(
|
||||
QUEUE * queue)
|
||||
{
|
||||
int retval = -1;
|
||||
if (queue->avail != queue->size) {
|
||||
retval = *(queue->buffer + queue->tail);
|
||||
queue->tail += 1;
|
||||
if (queue->tail == queue->size)
|
||||
queue->tail = 0;
|
||||
queue->avail += 1;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
/* End of Queue.c */
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
+----------------------------------------------------+
|
||||
| Thunderbird Software |
|
||||
+----------------------------------------------------+
|
||||
| Filespec : QUEUE.H |
|
||||
| Date : August 30, 1994 |
|
||||
| Time : 5:40 PM |
|
||||
| Revision : 0.0 |
|
||||
+----------------------------------------------------+
|
||||
| Programmer: Scott Andrews |
|
||||
| Address : 5358 Summit RD SW |
|
||||
| City/State: Pataskala, Ohio |
|
||||
| Zip : 43062 |
|
||||
+----------------------------------------------------+
|
||||
| Released to the Public Domain |
|
||||
+----------------------------------------------------+
|
||||
*/
|
||||
|
||||
#ifndef QUEUE__H
|
||||
#define QUEUE__H
|
||||
|
||||
/* Needed by Serial.C */
|
||||
|
||||
typedef struct {
|
||||
int size;
|
||||
int head;
|
||||
int tail;
|
||||
int avail;
|
||||
char *buffer;
|
||||
} QUEUE;
|
||||
|
||||
#define queue_empty(queue) (queue)->head == (queue)->tail
|
||||
#define queue_avail(queue) (queue)->avail
|
||||
|
||||
QUEUE *alloc_queue(
|
||||
int size);
|
||||
int en_queue(
|
||||
QUEUE * queue_ptr,
|
||||
char data);
|
||||
int de_queue(
|
||||
QUEUE * queue_ptr);
|
||||
|
||||
/* End of Queue.H */
|
||||
|
||||
#endif /* QUEUE__H */
|
||||
@@ -0,0 +1,5 @@
|
||||
This is a port to DOS using the BACnet MS/TP datalink layer.
|
||||
It utilizes some serial routines from snippets.org.
|
||||
It was tested and compiled with Turbo C++ 1.01 which is
|
||||
freely available from http://dn.codegear.com/article/21751
|
||||
It was targeting the TS-3100 from Technologic Systems.
|
||||
@@ -0,0 +1,241 @@
|
||||
/**************************************************************************
|
||||
*
|
||||
* Copyright (C) 2007 Steve Karg <skarg@users.sourceforge.net>
|
||||
* RS-485 initialization on AT91SAM7S inspired by Keil Eletronik serial.c
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*********************************************************************/
|
||||
|
||||
/* The module handles sending data out the RS-485 port */
|
||||
/* and handles receiving data from the RS-485 port. */
|
||||
/* Customize this file for your specific hardware */
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "timer.h"
|
||||
|
||||
/* This file has been customized for use with DOS */
|
||||
#include <dos.h>
|
||||
#include "serial.h"
|
||||
|
||||
/* UART */
|
||||
static char RS485_Port = '2';
|
||||
/* baud rate */
|
||||
static int RS485_Baud = 38400;
|
||||
|
||||
/* The minimum time after the end of the stop bit of the final octet of a */
|
||||
/* received frame before a node may enable its EIA-485 driver: 40 bit times. */
|
||||
/* At 9600 baud, 40 bit times would be about 4.166 milliseconds */
|
||||
/* At 19200 baud, 40 bit times would be about 2.083 milliseconds */
|
||||
/* At 38400 baud, 40 bit times would be about 1.041 milliseconds */
|
||||
/* At 57600 baud, 40 bit times would be about 0.694 milliseconds */
|
||||
/* At 76800 baud, 40 bit times would be about 0.520 milliseconds */
|
||||
/* At 115200 baud, 40 bit times would be about 0.347 milliseconds */
|
||||
/* 40 bits is 4 octets including a start and stop bit with each octet */
|
||||
#define Tturnaround (40UL)
|
||||
/* turnaround_time_milliseconds = (Tturnaround*1000UL)/RS485_Baud; */
|
||||
|
||||
/****************************************************************************
|
||||
* DESCRIPTION: Initializes the RS485 hardware and variables, and starts in
|
||||
* receive mode.
|
||||
* RETURN: none
|
||||
* ALGORITHM: none
|
||||
* NOTES: none
|
||||
*****************************************************************************/
|
||||
void RS485_Initialize(
|
||||
void)
|
||||
{
|
||||
/* baud rate */
|
||||
OpenComPort(RS485_Port);
|
||||
/* FIXME: change to numeric parameters */
|
||||
InitComPort("38400", '8', 'N', '1');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void RS485_Cleanup(
|
||||
void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* DESCRIPTION: Returns the baud rate that we are currently running at
|
||||
* RETURN: none
|
||||
* ALGORITHM: none
|
||||
* NOTES: none
|
||||
*****************************************************************************/
|
||||
uint32_t RS485_Get_Baud_Rate(
|
||||
void)
|
||||
{
|
||||
return RS485_Baud;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* 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:
|
||||
case 19200:
|
||||
case 38400:
|
||||
case 57600:
|
||||
case 76800:
|
||||
case 115200:
|
||||
RS485_Baud = baud;
|
||||
/* FIXME: store the baud rate */
|
||||
break;
|
||||
default:
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* DESCRIPTION: Waits on the SilenceTimer for 40 bits.
|
||||
* RETURN: none
|
||||
* ALGORITHM: none
|
||||
* NOTES: none
|
||||
*****************************************************************************/
|
||||
void RS485_Turnaround_Delay(
|
||||
void)
|
||||
{
|
||||
uint16_t turnaround_time;
|
||||
|
||||
/* delay after reception before trasmitting - per MS/TP spec */
|
||||
/* wait a minimum 40 bit times since reception */
|
||||
/* at least 1 ms for errors: rounding, clock tick */
|
||||
turnaround_time = 1 + ((Tturnaround * 1000UL) / RS485_Baud);
|
||||
while (Timer_Silence() < turnaround_time) {
|
||||
/* do nothing - wait for timer to increment */
|
||||
};
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* DESCRIPTION: Enable or disable the transmitter
|
||||
* RETURN: none
|
||||
* ALGORITHM: none
|
||||
* NOTES: none
|
||||
*****************************************************************************/
|
||||
void RS485_Transmitter_Enable(
|
||||
bool enable)
|
||||
{
|
||||
(void) enable;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* DESCRIPTION: Send some data and wait until it is sent
|
||||
* RETURN: none
|
||||
* ALGORITHM: none
|
||||
* NOTES: none
|
||||
*****************************************************************************/
|
||||
void RS485_Send_Data(
|
||||
uint8_t * buffer, /* data to send */
|
||||
uint16_t nbytes)
|
||||
{ /* number of bytes of data */
|
||||
/* send all the bytes */
|
||||
ComSendData(buffer, nbytes);
|
||||
while (!(RS485_Interface->US_CSR & AT91C_US_TXRDY)) {
|
||||
/* do nothing - wait until Tx buffer is empty */
|
||||
}
|
||||
/* per MSTP spec */
|
||||
Timer_Silence_Reset();
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* DESCRIPTION: Return true if a framing or overrun error is present
|
||||
* RETURN: true if error
|
||||
* ALGORITHM: none
|
||||
* NOTES: Clears any error flags.
|
||||
*****************************************************************************/
|
||||
bool RS485_ReceiveError(
|
||||
void)
|
||||
{
|
||||
bool ReceiveError = false;
|
||||
/* LED on send */
|
||||
volatile AT91PS_PIO pPIO = AT91C_BASE_PIOA;
|
||||
|
||||
/* check for data or error */
|
||||
if (RS485_Interface->US_CSR & (AT91C_US_OVRE | AT91C_US_FRAME)) {
|
||||
/* clear the error flag */
|
||||
RS485_Interface->US_CR = AT91C_US_RSTSTA;
|
||||
ReceiveError = true;
|
||||
/* LED ON */
|
||||
pPIO->PIO_CODR = LED2;
|
||||
}
|
||||
|
||||
return ReceiveError;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* DESCRIPTION: Return true if data is available
|
||||
* RETURN: true if data is available, with the data in the parameter set
|
||||
* ALGORITHM: none
|
||||
* NOTES: none
|
||||
*****************************************************************************/
|
||||
bool RS485_DataAvailable(
|
||||
uint8_t * DataRegister)
|
||||
{
|
||||
bool DataAvailable = false;
|
||||
/* LED on send */
|
||||
volatile AT91PS_PIO pPIO = AT91C_BASE_PIOA;
|
||||
|
||||
if (RS485_Interface->US_CSR & AT91C_US_RXRDY) {
|
||||
/* data is available */
|
||||
*DataRegister = RS485_Interface->US_RHR;
|
||||
DataAvailable = true;
|
||||
/* LED ON */
|
||||
pPIO->PIO_CODR = LED2;
|
||||
}
|
||||
|
||||
return DataAvailable;
|
||||
}
|
||||
|
||||
#ifdef TEST_RS485
|
||||
int main(
|
||||
void)
|
||||
{
|
||||
unsigned i = 0;
|
||||
uint8_t DataRegister;
|
||||
|
||||
RS485_Set_Baud_Rate(38400);
|
||||
RS485_Initialize();
|
||||
/* receive task */
|
||||
for (;;) {
|
||||
if (RS485_ReceiveError()) {
|
||||
fprintf(stderr, "ERROR ");
|
||||
} else if (RS485_DataAvailable(&DataRegister)) {
|
||||
fprintf(stderr, "%02X ", DataRegister);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,60 @@
|
||||
/**************************************************************************
|
||||
*
|
||||
* Copyright (C) 2007 Steve Karg <skarg@users.sourceforge.net>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*********************************************************************/
|
||||
|
||||
#ifndef RS485_H
|
||||
#define RS485_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
void RS485_Initialize(
|
||||
void);
|
||||
|
||||
void RS485_Transmitter_Enable(
|
||||
bool enable);
|
||||
|
||||
void RS485_Send_Data(
|
||||
uint8_t * buffer, /* data to send */
|
||||
uint16_t nbytes); /* number of bytes of data */
|
||||
|
||||
bool RS485_ReceiveError(
|
||||
void);
|
||||
bool RS485_DataAvailable(
|
||||
uint8_t * data);
|
||||
|
||||
void RS485_Turnaround_Delay(
|
||||
void);
|
||||
uint32_t RS485_Get_Baud_Rate(
|
||||
void);
|
||||
bool RS485_Set_Baud_Rate(
|
||||
uint32_t baud);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
#endif
|
||||
@@ -0,0 +1,382 @@
|
||||
/*
|
||||
+----------------------------------------------------+
|
||||
| Thunderbird Software |
|
||||
+----------------------------------------------------+
|
||||
| Filespec : Serial.c |
|
||||
| Date : October 24, 1991 |
|
||||
| Time : 15:03 |
|
||||
| Revision : 1.1 |
|
||||
| Update: August 29, 1994 |
|
||||
+----------------------------------------------------+
|
||||
| Programmer: Scott Andrews |
|
||||
| Address : 5358 Summit RD SW |
|
||||
| City/State: Pataskala, Ohio |
|
||||
| Zip : 43062 |
|
||||
+----------------------------------------------------+
|
||||
| Released to the Public Domain |
|
||||
+----------------------------------------------------+
|
||||
*/
|
||||
|
||||
/*
|
||||
+----------------------------------------------------------+
|
||||
| Call open_serial to install the interrupt handler |
|
||||
| You must call close_serial before exiting your program |
|
||||
| or a machine crash will occur! |
|
||||
+----------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <dos.h>
|
||||
#include <string.h>
|
||||
#include "serial.h"
|
||||
#include "queue.h"
|
||||
|
||||
QUEUE *Serial_In_Queue;
|
||||
QUEUE *Serial_Out_Queue;
|
||||
|
||||
OLD_COMM_PARAMS old_comm_params;
|
||||
COMM_STATUS comm_status;
|
||||
|
||||
void (
|
||||
INTERRUPT FAR * oldvector_serial) (
|
||||
);
|
||||
/* save addr for intr handler */
|
||||
|
||||
int ComBase; /* Comm port address */
|
||||
int IrqNum; /* Comm interrupt request */
|
||||
|
||||
void CloseComPort(
|
||||
void)
|
||||
{
|
||||
int status;
|
||||
|
||||
/* restore UART to previous state */
|
||||
|
||||
outp(ComBase + INT_EN, (unsigned char) 0);
|
||||
outp(ComBase + MODEM_CNTRL, (unsigned char) old_comm_params.modem);
|
||||
status = inp(ComBase + LINE_CNTRL);
|
||||
outp(ComBase + LINE_CNTRL, (unsigned char) status | 0x80);
|
||||
outp(ComBase + BAUD_LSB, (unsigned char) old_comm_params.baud_lsb);
|
||||
outp(ComBase + BAUD_MSB, (unsigned char) old_comm_params.baud_msb);
|
||||
outp(ComBase + LINE_CNTRL, (unsigned char) old_comm_params.line);
|
||||
outp(0x21, (unsigned char) old_comm_params.int_cntrl);
|
||||
|
||||
/* restore old interrupt handler */
|
||||
|
||||
setvect(IrqNum + 8, oldvector_serial);
|
||||
|
||||
/* free input and output queues */
|
||||
|
||||
free(Serial_In_Queue);
|
||||
free(Serial_Out_Queue);
|
||||
return;
|
||||
}
|
||||
|
||||
int OpenComPort(
|
||||
char Port)
|
||||
{ /* install int. handler */
|
||||
unsigned status;
|
||||
int retval = -1;
|
||||
|
||||
/* allocate input and output queues */
|
||||
|
||||
Serial_In_Queue = alloc_queue(SerInBufSize);
|
||||
if ((QUEUE *) 0 == Serial_In_Queue)
|
||||
return retval;
|
||||
Serial_Out_Queue = alloc_queue(SerOutBufSize);
|
||||
if ((QUEUE *) 0 == Serial_Out_Queue) {
|
||||
free(Serial_In_Queue);
|
||||
return retval;
|
||||
}
|
||||
retval = 0;
|
||||
|
||||
/* Setup Comm base port address and IRQ number */
|
||||
|
||||
switch (Port) {
|
||||
case '1':
|
||||
ComBase = 0x3F8;
|
||||
IrqNum = 4;
|
||||
break;
|
||||
case '2':
|
||||
ComBase = 0x2F8;
|
||||
IrqNum = 3;
|
||||
break;
|
||||
case '3':
|
||||
ComBase = 0x3E8;
|
||||
IrqNum = 4;
|
||||
break;
|
||||
case '4':
|
||||
ComBase = 0x2E8;
|
||||
IrqNum = 3;
|
||||
break;
|
||||
default:
|
||||
ComBase = 0x3F8;
|
||||
IrqNum = 4;
|
||||
break;
|
||||
}
|
||||
old_comm_params.int_enable = inp(ComBase + INT_EN);
|
||||
outp(ComBase + INT_EN, 0); /* turn off comm interrupts */
|
||||
|
||||
/* save old comm parameters */
|
||||
|
||||
old_comm_params.line = inp(ComBase + LINE_CNTRL);
|
||||
old_comm_params.modem = inp(ComBase + MODEM_CNTRL);
|
||||
status = inp(ComBase + LINE_CNTRL);
|
||||
outp(ComBase + LINE_CNTRL, (unsigned char) status | 0x80);
|
||||
old_comm_params.baud_lsb = inp(ComBase + BAUD_LSB);
|
||||
old_comm_params.baud_msb = inp(ComBase + BAUD_MSB);
|
||||
status = inp(ComBase + LINE_CNTRL);
|
||||
outp(ComBase + LINE_CNTRL, (unsigned char) status | 0x7F);
|
||||
status = OUT2 | DTR; /* DTR/OUT2 must be set! */
|
||||
outp(ComBase + MODEM_CNTRL, (unsigned char) status);
|
||||
|
||||
/* get serial port address/vector */
|
||||
|
||||
oldvector_serial = (void (INTERRUPT FAR *) (void)) getvect(IrqNum + 8);
|
||||
|
||||
/* set our interrupt handler */
|
||||
|
||||
setvect(IrqNum + 8, serial);
|
||||
|
||||
/* save the PIC */
|
||||
|
||||
old_comm_params.int_cntrl = inp(0x21);
|
||||
status = (1 << IrqNum); /* calculate int enable bit */
|
||||
status = ~status;
|
||||
|
||||
/* ok enable comm ints */
|
||||
|
||||
outp(0x21,
|
||||
(unsigned char) old_comm_params.int_cntrl & (unsigned char) status);
|
||||
|
||||
atexit(CloseComPort);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
void InitComPort(
|
||||
char Baud[],
|
||||
char Databits,
|
||||
char Parity,
|
||||
char Stopbits)
|
||||
{
|
||||
int status;
|
||||
unsigned divisor;
|
||||
long baudrate;
|
||||
|
||||
/* set baud rate */
|
||||
|
||||
status = inp(ComBase + LINE_CNTRL);
|
||||
outp(ComBase + LINE_CNTRL, (unsigned char) status | 0x80);
|
||||
baudrate = atol(Baud);
|
||||
if (baudrate == 0)
|
||||
baudrate = 2400L;
|
||||
divisor = (unsigned) (115200L / baudrate);
|
||||
outp(ComBase + BAUD_LSB, (unsigned char) (divisor & 0x00FF));
|
||||
outp(ComBase + BAUD_MSB, (unsigned char) ((divisor >> 8) & 0x00FF));
|
||||
status = 0x00;
|
||||
|
||||
/* set parity */
|
||||
|
||||
switch (Parity) { /* set parity value */
|
||||
case 'O': /* odd parity */
|
||||
case 'o':
|
||||
status = 0x08;
|
||||
break;
|
||||
|
||||
case 'E': /* even parity */
|
||||
case 'e':
|
||||
status = 0x18;
|
||||
break;
|
||||
|
||||
case 'S': /* stick parity */
|
||||
case 's':
|
||||
status = 0x28;
|
||||
break;
|
||||
|
||||
case 'N': /* no parity */
|
||||
case 'n':
|
||||
default:
|
||||
status = 0x00;
|
||||
}
|
||||
|
||||
/* set number data bits */
|
||||
|
||||
switch (Databits) {
|
||||
case '5':
|
||||
break;
|
||||
|
||||
case '6':
|
||||
status = status | 0x01;
|
||||
break;
|
||||
|
||||
case '7':
|
||||
status = status | 0x02;
|
||||
break;
|
||||
|
||||
case '8':
|
||||
default:
|
||||
status = status | 0x03;
|
||||
}
|
||||
|
||||
/* set number stop bits */
|
||||
|
||||
switch (Stopbits) {
|
||||
case '2':
|
||||
status = status | 0x04;
|
||||
break;
|
||||
|
||||
case '1':
|
||||
default:
|
||||
;
|
||||
}
|
||||
outp(ComBase + LINE_CNTRL, (unsigned char) status);
|
||||
status = OUT2 | DTR; /* DTR/OUT2 must be set! */
|
||||
outp(ComBase + MODEM_CNTRL, (unsigned char) status);
|
||||
|
||||
/* enable serial interrupts */
|
||||
|
||||
outp(ComBase + INT_EN, RX_INT | ERR_INT | RS_INT);
|
||||
return;
|
||||
}
|
||||
|
||||
void DropDtr(
|
||||
void)
|
||||
{
|
||||
int status;
|
||||
|
||||
status = inp(ComBase + MODEM_CNTRL);
|
||||
status &= 0xFE; /* turn off DTR bit */
|
||||
outp(ComBase + MODEM_CNTRL, (unsigned char) status);
|
||||
return;
|
||||
}
|
||||
|
||||
void RaiseDtr(
|
||||
void)
|
||||
{
|
||||
int status;
|
||||
|
||||
status = inp(ComBase + MODEM_CNTRL);
|
||||
status |= 0x01; /* turn on DTR bit */
|
||||
outp(ComBase + MODEM_CNTRL, (unsigned char) status);
|
||||
return;
|
||||
}
|
||||
|
||||
int ComRecChar(
|
||||
void)
|
||||
{
|
||||
return de_queue(Serial_In_Queue);
|
||||
}
|
||||
|
||||
int ComSendString(
|
||||
char *string)
|
||||
{
|
||||
int retval;
|
||||
char *pointer;
|
||||
pointer = string;
|
||||
|
||||
while (*pointer) {
|
||||
retval = en_queue(Serial_Out_Queue, *pointer);
|
||||
pointer++;
|
||||
}
|
||||
if (0x0 == (comm_status.modem & 0x40))
|
||||
RaiseDtr();
|
||||
outp(ComBase + INT_EN, RX_INT | TBE_INT | ERR_INT | RS_INT);
|
||||
return retval;
|
||||
}
|
||||
|
||||
int ComSendData(
|
||||
char *buffer,
|
||||
unsigned buffer_length)
|
||||
{
|
||||
int retval;
|
||||
char *pointer;
|
||||
pointer = buffer;
|
||||
unsigned i;
|
||||
|
||||
for (i = 0; i < buffer_length; i++) {
|
||||
retval = en_queue(Serial_Out_Queue, *pointer);
|
||||
pointer++;
|
||||
}
|
||||
if (0x0 == (comm_status.modem & 0x40))
|
||||
RaiseDtr();
|
||||
outp(ComBase + INT_EN, RX_INT | TBE_INT | ERR_INT | RS_INT);
|
||||
return retval;
|
||||
}
|
||||
|
||||
int ComSendChar(
|
||||
char character)
|
||||
{
|
||||
int retval;
|
||||
|
||||
/* interrupt driven send */
|
||||
|
||||
if (0x0 == (comm_status.modem & 0x40))
|
||||
RaiseDtr();
|
||||
retval = en_queue(Serial_Out_Queue, character);
|
||||
if (-1 != retval)
|
||||
outp(ComBase + INT_EN, RX_INT | TBE_INT | ERR_INT | RS_INT);
|
||||
return retval;
|
||||
}
|
||||
|
||||
int ComStatus(
|
||||
void)
|
||||
{
|
||||
unsigned status;
|
||||
unsigned retval;
|
||||
|
||||
retval = inp(ComBase + LINE_STATUS);
|
||||
retval = retval << 8;
|
||||
status = inp(ComBase + MODEM_STATUS);
|
||||
retval = retval | status;
|
||||
if (queue_empty(Serial_In_Queue))
|
||||
retval &= 0xFEFF;
|
||||
else
|
||||
retval |= 0x0100;
|
||||
return (int) retval;
|
||||
}
|
||||
|
||||
void INTERRUPT FAR serial(
|
||||
void)
|
||||
{ /* interrupt handler */
|
||||
int temp;
|
||||
|
||||
disable();
|
||||
while (1) {
|
||||
comm_status.intrupt = inp(ComBase + INT_ID);
|
||||
comm_status.intrupt &= 0x0f;
|
||||
switch (comm_status.intrupt) {
|
||||
case 0x00: /* modem interrupt */
|
||||
comm_status.modem = inp(ComBase + MODEM_STATUS);
|
||||
break;
|
||||
|
||||
case 0x02: /* xmit interrupt */
|
||||
if (queue_empty(Serial_Out_Queue))
|
||||
outp(ComBase + INT_EN, RX_INT | ERR_INT | RS_INT);
|
||||
else {
|
||||
temp = de_queue(Serial_Out_Queue);
|
||||
if (-1 != temp)
|
||||
outp(ComBase + XMIT, temp);
|
||||
}
|
||||
break;
|
||||
|
||||
case 0x04: /* receive interrupt */
|
||||
en_queue(Serial_In_Queue, (char) inp(ComBase + REC));
|
||||
break;
|
||||
|
||||
case 0x06: /* line interrupt */
|
||||
comm_status.line = inp(ComBase + LINE_STATUS);
|
||||
(void) inp(ComBase + REC);
|
||||
en_queue(Serial_In_Queue, '!');
|
||||
break;
|
||||
|
||||
default: /* No Mo` Left */
|
||||
comm_status.modem = inp(ComBase + MODEM_STATUS);
|
||||
outp(0x20, 0x20);
|
||||
enable();
|
||||
return;
|
||||
} /* switch */
|
||||
} /* while */
|
||||
}
|
||||
|
||||
/* End of Serial.C */
|
||||
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
+----------------------------------------------------+
|
||||
| Thunderbird Software |
|
||||
+----------------------------------------------------+
|
||||
| Filespec : Serial.c |
|
||||
| Date : October 24, 1991 |
|
||||
| Time : 15:03 |
|
||||
| Revision : 1.1 |
|
||||
| Update : August 29, 1994 |
|
||||
| Update : March 12, 1995 by Bob Stout |
|
||||
+----------------------------------------------------+
|
||||
| Programmer: Scott Andrews |
|
||||
| Address : 5358 Summit RD SW |
|
||||
| City/State: Pataskala, Ohio |
|
||||
| Zip : 43062 |
|
||||
+----------------------------------------------------+
|
||||
| Released to the Public Domain |
|
||||
+----------------------------------------------------+
|
||||
*/
|
||||
|
||||
#ifndef SERIAL__H
|
||||
#define SERIAL__H
|
||||
|
||||
#include "extkword.h"
|
||||
#include "pchwio.h"
|
||||
|
||||
#define SerInBufSize 4096 /* Size of input buffer */
|
||||
#define SerOutBufSize 512 /* Size of output buffer */
|
||||
|
||||
/* 8250 registers */
|
||||
|
||||
#define REC 0 /* Uart receive reg. */
|
||||
#define XMIT 0 /* Uart transmit reg. */
|
||||
#define INT_EN 1 /* Uart int. enable reg. */
|
||||
#define INT_ID 2 /* Uart int. ident. reg. */
|
||||
#define LINE_CNTRL 3 /* Uart line control reg. */
|
||||
#define MODEM_CNTRL 4 /* Uart modem control reg. */
|
||||
#define LINE_STATUS 5 /* Uart line status reg. */
|
||||
#define MODEM_STATUS 6 /* Uart modem status reg. */
|
||||
#define BAUD_LSB 0 /* Uart baud divisor reg. */
|
||||
#define BAUD_MSB 1 /* Uart baud divisor reg. */
|
||||
|
||||
#define NONE 0 /* Handshake param none */
|
||||
#define HDW 1 /* Handshake param hardware */
|
||||
#define XON 2 /* Handshake param software */
|
||||
|
||||
/* Interrupt enable register */
|
||||
|
||||
#define RX_INT 0x01 /* Receive interrupt mask */
|
||||
#define TBE_INT 0x02 /* Transmit buffer empty mask */
|
||||
#define ERR_INT 0x04 /* Error interrupt mask */
|
||||
#define RS_INT 0x08 /* Line interrupt mask */
|
||||
|
||||
/* Interrupt id register */
|
||||
|
||||
#define OUT2 0x08 /* Out 2 line */
|
||||
#define DTR 0x01 /* DTR high */
|
||||
#define RTS 0x02 /* RTS high */
|
||||
#define CTS 0x10
|
||||
#define DSR 0x20
|
||||
#define XMTRDY 0x20
|
||||
#define TXR 0 /* Transmit register (WRITE) */
|
||||
|
||||
#if !defined TRUE /* Define boolean true/false */
|
||||
#define FALSE 0
|
||||
#define TRUE !FALSE
|
||||
#endif
|
||||
|
||||
extern void (
|
||||
INTERRUPT FAR * oldvector_serial) (
|
||||
void);
|
||||
|
||||
extern int ComBase; /* Comm port address */
|
||||
extern int IrqNum; /* Comm interrupt request */
|
||||
|
||||
typedef struct { /* Save existing comm params */
|
||||
int int_enable; /* old interrupt enable reg value */
|
||||
int line; /* " line control " " */
|
||||
int modem; /* old modem control " " */
|
||||
int baud_lsb; /* old baud rate divisor LSD */
|
||||
int baud_msb; /* " " " " MSD */
|
||||
int int_cntrl; /* old PIC interrupt reg value */
|
||||
} OLD_COMM_PARAMS;
|
||||
extern OLD_COMM_PARAMS old_comm_params;
|
||||
|
||||
typedef struct {
|
||||
int line; /* Uart line status reg. */
|
||||
int modem; /* Uart mode status reg. */
|
||||
int intrupt; /* Uart interrupt reg. */
|
||||
int handshake; /* Handshake status */
|
||||
} COMM_STATUS; /* status, updated, handler */
|
||||
extern COMM_STATUS comm_status;
|
||||
|
||||
int OpenComPort(
|
||||
char Port); /*setup comm for usage */
|
||||
void InitComPort(
|
||||
char Baud[],
|
||||
char Databits,
|
||||
char Parity,
|
||||
char Stop);
|
||||
void CloseComPort(
|
||||
void); /* Restore comm port */
|
||||
void DropDtr(
|
||||
void); /* Lower DTR */
|
||||
void RaiseDtr(
|
||||
void); /* Raise DTR */
|
||||
int ComRecChar(
|
||||
void); /* Fetch character from rcv buf */
|
||||
|
||||
int ComSendChar(
|
||||
char character); /* Put char into xmit buffer */
|
||||
int ComSendString(
|
||||
char *string);
|
||||
int ComSendData(
|
||||
char *buffer,
|
||||
unsigned buffer_length);
|
||||
int ComStatus(
|
||||
void); /* Fetch comm status */
|
||||
void INTERRUPT FAR serial(
|
||||
void); /* interrupt handler */
|
||||
|
||||
/* End of Serial.H */
|
||||
|
||||
#endif /* SERIAL__H */
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef _STDBOOL_H
|
||||
#define _STDBOOL_H
|
||||
|
||||
/* C99 Boolean types for compilers without C99 support */
|
||||
/* http://www.opengroup.org/onlinepubs/009695399/basedefs/stdbool.h.html */
|
||||
#if !defined(__cplusplus)
|
||||
|
||||
#if !defined(__GNUC__)
|
||||
/* _Bool builtin type is included in GCC */
|
||||
typedef enum { _Bool_must_promote_to_int = -1, false = 0, true = 1 } _Bool;
|
||||
#endif
|
||||
|
||||
#define bool _Bool
|
||||
#define true 1
|
||||
#define false 0
|
||||
#define __bool_true_false_are_defined 1
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,31 @@
|
||||
/* Defines the standard integer types that are used in code */
|
||||
/* for the x86 processor and Borland Compiler */
|
||||
|
||||
#ifndef _STDINT_H
|
||||
#define _STDINT_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
typedef unsigned char uint8_t; /* 1 byte 0 to 255 */
|
||||
typedef signed char int8_t; /* 1 byte -127 to 127 */
|
||||
typedef unsigned short uint16_t; /* 2 bytes 0 to 65535 */
|
||||
typedef signed short int16_t; /* 2 bytes -32767 to 32767 */
|
||||
/*typedef unsigned short long uint24_t; // 3 bytes 0 to 16777215 */
|
||||
typedef unsigned long uint32_t; /* 4 bytes 0 to 4294967295 */
|
||||
typedef signed long int32_t; /* 4 bytes -2147483647 to 2147483647 */
|
||||
/* typedef signed long long int64_t; */
|
||||
/* typedef unsigned long long uint64_t; */
|
||||
|
||||
#define INT8_MIN (-128)
|
||||
#define INT16_MIN (-32768)
|
||||
#define INT32_MIN (-2147483647 - 1)
|
||||
|
||||
#define INT8_MAX 127
|
||||
#define INT16_MAX 32767
|
||||
#define INT32_MAX 2147483647
|
||||
|
||||
#define UINT8_MAX 0xff /* 255U */
|
||||
#define UINT16_MAX 0xffff /* 65535U */
|
||||
#define UINT32_MAX 0xffffffff /* 4294967295U */
|
||||
|
||||
#endif /* STDINT_H */
|
||||
@@ -0,0 +1,201 @@
|
||||
/**************************************************************************
|
||||
*
|
||||
* Copyright (C) 2007 Steve Karg <skarg@users.sourceforge.net>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*********************************************************************/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <dos.h>
|
||||
|
||||
/* global variable counts milliseconds */
|
||||
volatile unsigned long Timer_Milliseconds;
|
||||
/* MS/TP Silence Timer */
|
||||
static volatile int SilenceTime;
|
||||
/* counts ticks */
|
||||
volatile unsigned long Timer_Milliseconds;
|
||||
|
||||
#define RTC_CMD_ADDR 0x70 /* RTC internal register offset goes here */
|
||||
#define RTC_DAT_ADDR 0x71 /* RTC internal register R/W access here */
|
||||
|
||||
static uint8_t RTC_RS_Convert(
|
||||
uint16_t hertz)
|
||||
{
|
||||
uint8_t RS = 0;
|
||||
/* from DS12887A datasheet
|
||||
SELECT BITS tPI PERIODIC
|
||||
REGISTER A INTERRUPT SQW OUTPUT
|
||||
RS3 RS2 RS1 RS0 RATE FREQUENCY
|
||||
--- --- --- --- ------------ ----------
|
||||
0 0 0 0 None 0Hz
|
||||
0 0 0 1 3.90625ms 256Hz
|
||||
0 0 1 0 7.8125ms 128Hz
|
||||
0 0 1 1 122.070µs 8192Hz
|
||||
0 1 0 0 244.141µs 4096Hz
|
||||
0 1 0 1 488.281µs 2048Hz
|
||||
0 1 1 0 976.5625µs 1024Hz
|
||||
0 1 1 1 1.953125ms 512Hz
|
||||
1 0 0 0 3.90625ms 256Hz
|
||||
1 0 0 1 7.8125ms 128Hz
|
||||
1 0 1 0 15.625ms 64Hz
|
||||
1 0 1 1 31.25ms 32Hz
|
||||
1 1 0 0 62.5ms 16Hz
|
||||
1 1 0 1 125ms 8Hz
|
||||
1 1 1 0 250ms 4Hz
|
||||
1 1 1 1 500ms 2Hz
|
||||
*/
|
||||
/* FIXME: create a clever formula to replace switch */
|
||||
switch (hertz) {
|
||||
case 8192:
|
||||
RS = 3;
|
||||
break;
|
||||
case 4096:
|
||||
RS = 4;
|
||||
break;
|
||||
case 2048:
|
||||
RS = 5;
|
||||
break;
|
||||
case 1024:
|
||||
RS = 6;
|
||||
break;
|
||||
case 512:
|
||||
RS = 7;
|
||||
break;
|
||||
case 256:
|
||||
RS = 8;
|
||||
break;
|
||||
case 128:
|
||||
RS = 9;
|
||||
break;
|
||||
case 64:
|
||||
RS = 10;
|
||||
break;
|
||||
case 32:
|
||||
RS = 11;
|
||||
break;
|
||||
case 16:
|
||||
RS = 12;
|
||||
break;
|
||||
case 8:
|
||||
RS = 13;
|
||||
break;
|
||||
case 4:
|
||||
RS = 14;
|
||||
break;
|
||||
case 2:
|
||||
RS = 15;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return RS;
|
||||
}
|
||||
|
||||
/* setting for 8192 interrupts per second
|
||||
which is an interrupt every 122uS. */
|
||||
#define INT_FREQ 8192
|
||||
|
||||
static void interrupt Timer_Interrupt_Handler(
|
||||
void)
|
||||
{
|
||||
static uint16_t Timer_Ticks = 0;
|
||||
static uint16_t Elapsed_Milliseconds = 0;
|
||||
uint16_t milliseconds = 0;
|
||||
uint16_t diff = 0;
|
||||
uint8_t temp_reg;
|
||||
|
||||
Timer_Ticks++;
|
||||
milliseconds = (Timer_Ticks * 1000) / INT_FREQ;
|
||||
diff = milliseconds - Elapsed_Milliseconds;
|
||||
if (diff >= 1) {
|
||||
Elapsed_Milliseconds = milliseconds;
|
||||
Timer_Milliseconds++;
|
||||
if (SilenceTime < 60000)
|
||||
SilenceTime++;
|
||||
}
|
||||
/* max resolution */
|
||||
if (Timer_Ticks >= INT_FREQ) {
|
||||
Timer_Ticks = 0;
|
||||
Elapsed_Milliseconds = 0;
|
||||
}
|
||||
|
||||
/* clear interrupt */
|
||||
outportb(RTC_CMD_ADDR, 0x0C); /* select RTC register C */
|
||||
temp_reg = inportb(RTC_DAT_ADDR); /* read RTC register C */
|
||||
/* signal end of interrupt to slave PIC */
|
||||
outportb(0xA0, 0x20);
|
||||
/* signal end of interrupt to master PIC */
|
||||
outportb(0x20, 0x20);
|
||||
}
|
||||
|
||||
/* previous interrrupt vector */
|
||||
static void interrupt(
|
||||
*OldVector) (
|
||||
);
|
||||
|
||||
void Timer_Cleanup(
|
||||
void)
|
||||
{
|
||||
setvect(0x70, OldVector);
|
||||
}
|
||||
|
||||
void Timer_Init(
|
||||
void)
|
||||
{
|
||||
uint8_t RC = RTC_RS_Convert(INT_FREQ);
|
||||
|
||||
/* get old interrupt vector to re-install on exit */
|
||||
OldVector = getvect(0x70);
|
||||
/* disable interrupts */
|
||||
disable();
|
||||
/* set RTC int. vector for our routine */
|
||||
setvect(0x70, Timer_Interrupt_Handler);
|
||||
/* set register B with PIE enabled */
|
||||
outportb(RTC_CMD_ADDR, 0x0B);
|
||||
outportb(RTC_DAT_ADDR, 0x42);
|
||||
/* set register A to our frequency */
|
||||
outportb(RTC_CMD_ADDR, 0x0A);
|
||||
outportb(RTC_DAT_ADDR, (0x20 | (RC & 0x0F)));
|
||||
/* re-enable system interrupts */
|
||||
enable();
|
||||
atexit(Timer_Cleanup);
|
||||
}
|
||||
|
||||
int Timer_Silence(
|
||||
void)
|
||||
{
|
||||
uint16_t time_value;
|
||||
|
||||
disable();
|
||||
time_value = SilenceTime;
|
||||
enable();
|
||||
|
||||
return time_value;
|
||||
}
|
||||
|
||||
void Timer_Silence_Reset(
|
||||
void)
|
||||
{
|
||||
disable();
|
||||
SilenceTime = 0;
|
||||
enable();
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/**************************************************************************
|
||||
*
|
||||
* Copyright (C) 2007 Steve Karg <skarg@users.sourceforge.net>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*********************************************************************/
|
||||
|
||||
#ifndef TIMER_H
|
||||
#define TIMER_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
extern volatile unsigned long Timer_Milliseconds;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
void Timer_Init(
|
||||
void);
|
||||
int Timer_Silence(
|
||||
void);
|
||||
void Timer_Silence_Reset(
|
||||
void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
#endif
|
||||
Reference in New Issue
Block a user