//--------------------------------------------------------------------------- // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. // // 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 DALLAS SEMICONDUCTOR 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. // // Except as contained in this notice, the name of Dallas Semiconductor // shall not be used except as stated in the Dallas Semiconductor // Branding Policy. //--------------------------------------------------------------------------- // // ds390lnk.C - Serial functions using 8051 to be used as a test // for DS2480 based Universal Serial Adapter 'U' // functions. // // Version: 1.00 // // TODO: 1) Clean up ugly code. // 2) Allow for compile-time choice of using Timer2 for baud rate of Serial1 // 3) Package Serial0 and Serial1 SFRs into nice structs, hopefully to allow // for removal of all the if-else statements in each function. // 4) Do the math and write a better msDelay function. // #include "microser.h" #if USE_SERIAL_INTERRUPTS // this is a ring buffer and can overflow at anytime! static volatile unsigned char receiveBuffer0[SERIAL_RECEIVE_BUFFER_SIZE]; static volatile int receiveBufferHead0 = 0; static volatile int receiveBufferTail0 = 0; static volatile unsigned char receiveBuffer1[SERIAL_RECEIVE_BUFFER_SIZE]; static volatile int receiveBufferHead1 = 0; static volatile int receiveBufferTail1 = 0; // no buffering for transmit static volatile char transmitIsBusy0 = 0; static volatile char transmitIsBusy1 = 0; #endif //--------------------------------------------------------------------------- //-------- COM required functions for MLANU //--------------------------------------------------------------------------- /* exportable functions */ SMALLINT OpenCOM(int, char*); SMALLINT WriteCOM(int, int, uchar*); void CloseCOM(int); void FlushCOM(int); int ReadCOM(int, int, uchar*); void BreakCOM(int); void SetBaudCOM(int, uchar); void msDelay(int); long msGettick(void); //--------------------------------------------------------------------------- // Attempt to open a com port. Keep the handle in ComID. // Set the starting baud rate to 9600. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number provided will // be used to indicate the port number desired when calling // all other functions in this library. // // 'port_zstr' - zero terminate port name. NOT USED. // // // Returns: TRUE(1) - success, COM port opened // FALSE(0) - failure, could not open specified port // SMALLINT OpenCOM(int portnum, char *port_zstr) { port_zstr = 0; //to silence compiler EA = 0; //Disable interrupts #if USE_TIMER2_FOR_SERIAL0 if(portnum==0) { TH2 = RCAP2H = 0xFF; T2CON |= 0x30; //enable Timer2 control for serial1 T2MOD = (T2MOD&0x0f) | 0x20; // timer 2 is an 8bit auto-reload counter } else { #else T2CON &= 0xCF; //disable Timer2 control for serial0 #endif //Setup Timer1 for baud generation TMOD = (TMOD&0x0f) | 0x20; // timer 1 is an 8bit auto-reload counter #if USE_TIMER2_FOR_SERIAL0 } #endif if(portnum==0) { ES0 = 0; // disable serial channel 0 interrupt PCON |= 0x80; // clock is 16x bitrate for serial0 // set 8 bit uart with variable baud from timer 1 // enable receiver and clear RI and TI SCON0 = 0x50; #if USE_SERIAL_INTERRUPTS ES0 = 1; // enable serial channel 0 interrupt #endif } else if(portnum==1) { ES1 = 0; // disable serial channel 1 interrupt WDCON |= 0x80; // clock is 16x bitrate for serial1 // set 8 bit uart with variable baud from timer 1 // enable receiver and clear RI and TI SCON1 = 0x50; #if USE_SERIAL_INTERRUPTS ES1 = 1; // enable serial channel 1 interrupt #endif } SetBaudCOM(portnum,PARMSET_9600); FlushCOM(portnum); EA = 1; //Enable interrupts return TRUE; } // function called by startup390 in sdcc void Serial390Init (void) { OpenCOM(0, ""); } //--------------------------------------------------------------------------- // Closes the connection to the port. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to // OpenCOM to indicate the port number. // void CloseCOM(int portnum) { if(portnum==0) { ES0 = 0; //disable serial interrupt RI_0 = 0; // receive buffer empty #if USE_SERIAL_INTERRUPTS receiveHead0 = receiveTail0 = 0; transmitIsBusy0 = 0; TI_0 = 0; #else TI_0 = 1; // transmit buffer empty #endif } else if(portnum==1) { ES1 = 0; //disable serial interrup RI_1 = 0; #if USE_SERIAL_INTERRUPTS receiveHead1 = receiveTail1 = 0; transmitIsBusy1 = 0; TI_1 = 0; #else TI_1 = 1; // transmit buffer empty #endif } } //--------------------------------------------------------------------------- // Flush the rx and tx buffers // // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to // OpenCOM to indicate the port number. // void FlushCOM(int portnum) { if(portnum==0) { ES0 = 0; RI_0 = 0; // receive buffer empty #if USE_SERIAL_INTERRUPTS receiveHead0 = receiveTail0 = 0; transmitIsBusy0 = 0; TI_0 = 0; ES0 = 1; #else TI_0 = 1; // transmit buffer empty #endif } else if(portnum==1) { ES1 = 0; RI_1 = 0; #if USE_SERIAL_INTERRUPTS receiveHead1 = receiveTail1 = 0; transmitIsBusy1 = 0; TI_1 = 0; ES1 = 1; #else TI_1 = 1; // transmit buffer empty #endif } } //-------------------------------------------------------------------------- // Write an array of bytes to the COM port, verify that it was // sent out. Assume that baud rate has been set. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to // OpenCOM to indicate the port number. // 'outlen' - number of bytes to write to COM port // 'outbuf' - pointer ot an array of bytes to write // // Returns: TRUE(1) - success // FALSE(0) - failure // int WriteCOM(int portnum, int outlen, uchar *outbuf) { int i = 0; if(portnum==0) { for(; i0 ) msDelay(10); if(!serial0_peek()) quit = TRUE; else inbuf[i++] = serial0_getchar(); } } else { for(; !quit && i0 ) msDelay(6); if(!serial1_peek()) quit = TRUE; else inbuf[i++] = serial1_getchar(); } } return i; } //-------------------------------------------------------------------------- // Send a break on the com port for at least 2 ms // // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to // OpenCOM to indicate the port number. // void BreakCOM(int portnum) { if(portnum==0) { //TODO: Check this port pin //hold P3.1 low ES0 = 0; P3 &= 0xFE; msDelay(3); P3 |= 0x01; ES0 = 1; } else { //hold P5.3 low ES1 = 0; P5 &= 0xF7; msDelay(3); P5 |= 0x08; ES1 = 1; } } //-------------------------------------------------------------------------- // Set the baud rate on the com port. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to // OpenCOM to indicate the port number. // 'new_baud' - new baud rate defined as // PARMSET_9600 0x00 // PARMSET_19200 0x02 // PARMSET_57600 0x04 // PARMSET_115200 0x06 // void SetBaudCOM(int portnum, uchar new_baud) { int reload_value; switch (new_baud) { case PARMSET_9600: reload_value = BAUD9600_TIMER_RELOAD_VALUE; break; case PARMSET_19200: reload_value = BAUD19200_TIMER_RELOAD_VALUE; break; case PARMSET_57600: reload_value = BAUD57600_TIMER_RELOAD_VALUE; break; case PARMSET_115200: reload_value = BAUD115200_TIMER_RELOAD_VALUE; break; default: return; } #if USE_TIMER2_FOR_SERIAL0 if(portnum==0) { TR2 = 0; // stop timer 2 TL2 = RCAP2L = reload_value; TH2 = RCAP2H = reload_value>>8; TF2 = 0; // clear timer 2 overflow flag TR2 = 1; // start timer 2 } else { #else portnum = 0; #endif TR1 = 0; // stop timer 1 TL1 = TH1 = reload_value; TF1 = 0; // clear timer 1 overflow flag TR1 = 1; // start timer 1 #if USE_TIMER2_FOR_SERIAL0 } #endif } //-------------------------------------------------------------------------- // Description: // Delay for at least 'len' ms // void msDelay(int len) { int i,j; for(i=0; i