2 * Copyright (C) 2009 Ubixum, Inc.
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.
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.
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/>.
20 #include <fx2macros.h>
25 * using the comp port implies that timer 2 will be used as
26 * a baud rate generator. (Don't use timer 2)
28 void sio0_init( WORD baud_rate ) __critical { // baud_rate max should be 57600 since int=2 bytes
30 WORD hl; // hl value for reload
31 BYTE mult; // multiplier for clock speed
32 DWORD tmp; // scratch for mult/divide
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)
42 // RCAP2H:L = 0xFFFF - CLKOUT / 32 x baud_rate
44 // in order to round to nearest value..
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 ;
54 hl = 0xFFFF - (WORD)tmp;
57 // seems that the 24/48mhz calculations are always one less than suggested values
59 RCAP2L= LSB(hl) + (mult>0?1:0);
60 TR2=1; // start the timer
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
80 void _transchar(char c) {
81 while ( !TI ); // wait for TI=1
86 void putchar (char c) {
87 if (c=='\n') _transchar('\r'); // transmit \r\n
89 if (c == '\r' ) _transchar('\n'); // transmit \r\n