diff --git a/bacnet-stack/ports/dos/bacnet.prj b/bacnet-stack/ports/dos/bacnet.prj index 36184de8..c860410c 100644 Binary files a/bacnet-stack/ports/dos/bacnet.prj and b/bacnet-stack/ports/dos/bacnet.prj differ diff --git a/bacnet-stack/ports/dos/rs485.c b/bacnet-stack/ports/dos/rs485.c index b0595daa..8c4cb9fb 100644 --- a/bacnet-stack/ports/dos/rs485.c +++ b/bacnet-stack/ports/dos/rs485.c @@ -35,10 +35,11 @@ #include "timer.h" /* This file has been customized for use with DOS */ -#include "hardware.h" +#include +#include "serial.h" /* UART */ -static volatile AT91S_USART *RS485_Interface = AT91C_BASE_US0; +static char RS485_Port = '2'; /* baud rate */ static int RS485_Baud = 38400; @@ -64,42 +65,10 @@ static int RS485_Baud = 38400; void RS485_Initialize( void) { - /* Enable the USART0 clock in the Power Management Controller */ - volatile AT91PS_PMC pPMC = AT91C_BASE_PMC; - pPMC->PMC_PCER = pPMC->PMC_PCSR | (1 << AT91C_ID_US0); - - /* Disable and clear USART0 interrupt - in AIC Interrupt Disable Command Register */ - volatile AT91PS_AIC pAIC = AT91C_BASE_AIC; - pAIC->AIC_IDCR = (1 << AT91C_ID_US0); - pAIC->AIC_ICCR = (1 << AT91C_ID_US0); - - /* enable the peripheral by disabling the pin in the PIO controller */ - *AT91C_PIOA_PDR = AT91C_PA5_RXD0 | AT91C_PA6_TXD0 | AT91C_PA7_RTS0; - - RS485_Interface->US_CR = AT91C_US_RSTRX | /* Reset Receiver */ - AT91C_US_RSTTX | /* Reset Transmitter */ - AT91C_US_RSTSTA | /* Clear status register */ - AT91C_US_RXDIS | /* Receiver Disable */ - AT91C_US_TXDIS; /* Transmitter Disable */ - - RS485_Interface->US_MR = AT91C_US_USMODE_RS485 | /* RS-485 Mode - RTS auto assert */ - AT91C_US_CLKS_CLOCK | /* Clock = MCK */ - AT91C_US_CHRL_8_BITS | /* 8-bit Data */ - AT91C_US_PAR_NONE | /* No Parity */ - AT91C_US_NBSTOP_1_BIT; /* 1 Stop Bit */ - - /* set the Time Guard to release RTS after x bit times */ - RS485_Interface->US_TTGR = 1; - - /* Receiver Time-out disabled */ - RS485_Interface->US_RTOR = 0; - /* baud rate */ - RS485_Interface->US_BRGR = MCK / 16 / RS485_Baud; - - RS485_Interface->US_CR = AT91C_US_RXEN | /* Receiver Enable */ - AT91C_US_TXEN; /* Transmitter Enable */ + OpenComPort(RS485_Port); + /* FIXME: change to numeric parameters */ + InitComPort ( "38400", '8', 'N', '1' ); return; } @@ -141,8 +110,7 @@ bool RS485_Set_Baud_Rate( case 76800: case 115200: RS485_Baud = baud; - RS485_Interface->US_BRGR = MCK / 16 / baud; - /* FIXME: store the baud rate */ + /* FIXME: store the baud rate */ break; default: valid = false; @@ -176,7 +144,7 @@ void RS485_Turnaround_Delay( * DESCRIPTION: Enable or disable the transmitter * RETURN: none * ALGORITHM: none -* NOTES: The Atmel ARM7 has an automatic enable/disable in RS485 mode. +* NOTES: none *****************************************************************************/ void RS485_Transmitter_Enable( bool enable) @@ -194,19 +162,8 @@ void RS485_Send_Data( uint8_t * buffer, /* data to send */ uint16_t nbytes) { /* number of bytes of data */ - /* LED on send */ - volatile AT91PS_PIO pPIO = AT91C_BASE_PIOA; - /* LED ON */ - pPIO->PIO_CODR = LED1; /* send all the bytes */ - while (nbytes) { - while (!(RS485_Interface->US_CSR & AT91C_US_TXRDY)) { - /* do nothing - wait until Tx buffer is empty */ - } - RS485_Interface->US_THR = *buffer; - buffer++; - nbytes--; - } + ComSendData(buffer, nbytes); while (!(RS485_Interface->US_CSR & AT91C_US_TXRDY)) { /* do nothing - wait until Tx buffer is empty */ } diff --git a/bacnet-stack/ports/dos/serial.c b/bacnet-stack/ports/dos/serial.c index 19ca9a57..2ca1efb9 100644 --- a/bacnet-stack/ports/dos/serial.c +++ b/bacnet-stack/ports/dos/serial.c @@ -43,6 +43,37 @@ void (INTERRUPT FAR *oldvector_serial )(); 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; @@ -105,53 +136,25 @@ int OpenComPort ( char Port ) /* install int. handler */ outp ( 0x21, (unsigned char) old_comm_params.int_cntrl & (unsigned char) status ); + + atexit(CloseComPort); + return retval; } -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; -} - void InitComPort ( char Baud[], char Databits, char Parity, char Stopbits ) { int status; - long baudrate; unsigned divisor; + long baudrate; /* set baud rate */ status = inp ( ComBase + LINE_CNTRL ); outp ( ComBase + LINE_CNTRL, (unsigned char) status | 0x80 ); - baudrate = atol ( Baud ); + baudrate = atol(Baud); if ( baudrate == 0) baudrate = 2400L; divisor = (unsigned) ( 115200L / baudrate); @@ -266,6 +269,24 @@ int ComSendString ( char *string ) 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; diff --git a/bacnet-stack/ports/dos/serial.h b/bacnet-stack/ports/dos/serial.h index 9b4b511e..4751146f 100644 --- a/bacnet-stack/ports/dos/serial.h +++ b/bacnet-stack/ports/dos/serial.h @@ -98,6 +98,7 @@ 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 */