]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blob - fx2lib/lib/serial.c
Add Hantek PSO2020 firmware support
[sigrok-firmware-fx2lafw.git] / fx2lib / lib / serial.c
1 /**
2  * Copyright (C) 2009 Ubixum, Inc. 
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16  **/
17
18
19 #include <fx2regs.h>
20 #include <fx2macros.h>
21 #include <serial.h>
22
23
24 /**
25  * using the comp port implies that timer 2 will be used as
26  * a baud rate generator.  (Don't use timer 2)
27  **/
28 void sio0_init( WORD baud_rate ) __critical { // baud_rate max should be 57600 since int=2 bytes
29         
30     WORD hl; // hl value for reload     
31         BYTE mult; // multiplier for clock speed
32     DWORD tmp; // scratch for mult/divide
33
34     // 0 = 12mhz, 1=24mhz, 2=48mhz    
35         mult = CPUFREQ == CLK_12M ? 1 :
36            CPUFREQ == CLK_24M ? 2 : 4; // since only 3 clock speeds, fast switch instead of doing 2^clock speed pow(2,clkspd)
37
38         // set the clock rate
39         // use clock 2
40         RCLK=1;TCLK=1;
41
42 //    RCAP2H:L = 0xFFFF - CLKOUT / 32 x baud_rate
43
44     // in order to round to nearest value..
45     // tmp * 2 // double
46     // tmp / rate // do the divide
47     // tmp + 1 // add one (which is like adding 1/2)
48     // tmp / 2 // back to original rounded 
49     tmp = mult * 375000L * 2 ;
50     tmp /= baud_rate;
51     tmp += 1;
52     tmp /= 2;
53
54     hl = 0xFFFF - (WORD)tmp;
55
56         RCAP2H= MSB(hl);
57         // seems that the 24/48mhz calculations are always one less than suggested values    
58     // trm table 14-16
59         RCAP2L= LSB(hl) + (mult>0?1:0);
60         TR2=1; // start the timer
61         
62         // set up the serial port       
63         SM0 = 0; SM1=1;// serial mode 1 (asyncronous)   
64         SM2 = 0 ; // has to do with receiving
65         REN = 1 ; // to enable receiving
66     PCON |= 0x80; // SET SMOD0, baud rate doubler
67     TI = 1; // we send initial byte
68
69 }
70
71 char getchar(void) {
72   char c;
73   while (!RI)
74     ;  
75   c=SBUF0;
76   RI=0;
77   return c;
78 }
79
80 void _transchar(char c) {
81  while ( !TI ); // wait for TI=1 
82  TI=0;
83  SBUF0=c;
84 }
85
86 void putchar (char c) {
87   if (c=='\n') _transchar('\r'); // transmit \r\n
88   _transchar(c);  
89   if (c == '\r' ) _transchar('\n'); // transmit \r\n
90 }
91