Imported Upstream version 2.9.0
[debian/cc1111] / support / regression / ports / pic14 / support.c
1 /*-------------------------------------------------------------------------
2   support.c - startup for PIC14 regression tests with gpsim
3   
4   Copyright (c) 2006 Borut Razem
5     
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15    
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20   In other words, you are welcome to use, share and improve this program.
21   You are forbidden to forbid anyone else to use, share and improve
22   what you give them.   Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
24
25 #pragma preproc_asm -
26
27 #include <pic16f877.h>
28
29
30 void
31 _putchar(char c)
32 {
33   while (!TXIF)
34     ;
35   TXREG = c;
36 }
37
38
39 void
40 _initEmu(void)
41 {
42   /* load and configure the libgpsim_modules module */
43   _asm
44     ;; Set frequency to 20MHz
45     .direct "e", ".frequency=20e6"
46     
47     ;; Load the USART library and module
48     .direct "e", "module library libgpsim_modules"
49     .direct "e", "module load usart U1"
50
51     ;; Define a node
52     .direct "e", "node PIC_tx"
53
54     ;; Tie the USART module to the PIC
55     .direct "e", "attach PIC_tx portc6 U1.RXPIN"
56
57     ;; Set the USART module's Baud Rate
58     .direct "e", "U1.rxbaud = 9600"
59
60     ;; Display the received character on terminal
61     .direct "e", "U1.console = true"
62   _endasm;
63
64   /* USART initialization */
65   PORTC |= 0x40;     // Set TX pin to 1
66   TRISC &= ~0x40; // TX pin is output
67
68   //1. Initialize the SPBRG register for the appropriate
69   //   baud rate. If a high speed baud rate is desired,
70   //   set bit BRGH (Section 16.1).
71   BRGH = 1;
72   SPBRG = 129;
73
74   //2. Enable the asynchronous serial port by clearing
75   //   bit SYNC and setting bit SPEN.
76   SPEN = 1;
77
78   //3. If interrupts are desired, set enable bit TXIE.
79   //4. If 9-bit transmission is desired, set transmit bit
80   //   TX9. Can be used as address/data bit.
81   //5. Enable the transmission by setting bit TXEN,
82   //   which will also set bit TXIF.
83   TXEN = 1;
84
85   //6. If 9-bit transmission is selected, the ninth bit
86   //   should be loaded in bit TX9D.
87   //7. Load data to the TXREG register (starts
88   //   transmission).
89 }
90
91
92 void
93 _exitEmu(void)
94 {
95   /* wait until the transmit buffer is empty */
96   while (!TRMT)
97     ;
98
99   /* set the breakpoint */
100   _asm
101    .direct "a", "\"\""
102   _endasm;
103 }