Imported Upstream version 2.9.0
[debian/cc1111] / device / lib / _autobaud.c
1 /*-------------------------------------------------------------------------
2
3   _autobaud.c - automatic baud rate detection routine. Adapted for
4   sdcc compiler from Paul Stoffregen's  <paul@ece.orst.edu> autobaud.asm
5   the original assembly code can be found at 
6   http://www.ece.orst.edu/~paul/8051-goodies/autobaud.html  
7   
8   written By -  Sandeep Dutta . sandeep.dutta@usa.net (1999)
9
10   This library is free software; you can redistribute it and/or modify it
11   under the terms of the GNU Library General Public License as published by the
12   Free Software Foundation; either version 2, or (at your option) any
13   later version.
14   
15   This library is distributed in the hope that it will be useful,
16   but WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   GNU Library General Public License for more details.
19   
20   You should have received a copy of the GNU Library General Public License
21   along with this program; if not, write to the Free Software
22   Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23   
24   In other words, you are welcome to use, share and improve this program.
25   You are forbidden to forbid anyone else to use, share and improve
26   what you give them.   Help stamp out software-hoarding!
27   -------------------------------------------------------------------------*/
28 #include <8051.h>
29 /*
30 ; To set the baud rate, use this formula or use autobaud()
31 ; baud_const = 256 - (crystal / (12 * 16 * baud)) */
32
33 /*
34 ;to do automatic baud rate detection, we assume the user will
35 ;press the carriage return, which will cause this bit pattern
36 ;to appear on port 3 pin 0 (CR = ascii code 13, assume 8N1 format)
37 ;
38 ;              0 1 0 1 1 0 0 0 0 1
39 ;              | |             | |
40 ; start bit----+ +--lsb   msb--+ +----stop bit
41 ;
42 ;we'll start timer #1 in 16 bit mode at the transition between the
43 ;start bit and the LSB and stop it between the MBS and stop bit.
44 ;That will give approx the number of cpu cycles for 8 bits.  Divide
45 ;by 8 for one bit and by 16 since the built-in UART takes 16 timer
46 ;overflows for each bit.  We need to be careful about roundoff during
47 ;division and the result has to be inverted since timer #1 counts up.  Of
48 ;course, timer #1 gets used in 8-bit auto reload mode for generating the
49 ;built-in UART's baud rate once we know what the reload value should be.
50 */
51
52 void autobaud ()
53 {
54
55         /* get timer #1 ready for action (16 bit mode) */
56         TMOD=0x11;
57         TCON = 0;
58         TH1 = TL1 = 0;
59         
60         /* wait for start bit */
61 autobaud2:
62         while(RXD) ; 
63
64         /*  check it a few more times to make
65              sure we don't trigger on some noise*/
66         if (RXD) goto autobaud2;
67         if (RXD) goto autobaud2;
68         if (RXD) goto autobaud2;
69         if (RXD) goto autobaud2;
70
71         /* wait for bit #0 to begin      */
72         while (!RXD);
73         TR1 = 1; /* start the timer */
74         while (RXD);             // wait for bit #1 to begin
75         while(!RXD);             // wait for bit #2 to begin
76         while(RXD);              // wait for bit #4 to begin
77         while (!RXD);            // wait for stop bit to begin
78         TR1 = 0;                 // stop timing
79
80         /* ;grab bit 7... it's the lsb we want */
81         TH1 = (TH1 << 1) | (TL1 >> 7);
82
83         /* round off if necessary */
84         TH1 = (TH1 << 1) | ((TL1 >> 6) & 0x01);
85
86         /* invert since timer #1 will count up */
87         TH1 = ~TH1;
88
89         /* now TH1 has the correct reload value (I hope) */
90         TH1++ ;
91
92         TL1 = TH1;
93         TMOD =  0x21     ;      // set timer #1 for 8 bit auto-reload
94         PCON =  0x80     ;      // configure built-in uart
95         SCON =  0x52     ;
96 }
97