Imported Upstream version 2.9.0
[debian/cc1111] / support / regression / ports / pic16 / support.c
1 /*-------------------------------------------------------------------------
2   support.c - startup for PIC16 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 #pragma stack 0x200 255 /* set stack size to 255 bytes */
27
28 #include <pic18f452.h>
29
30
31 void
32 _putchar(char c)
33 {
34   while (!PIR1bits.TXIF)
35     ;
36   TXREG = c;
37 }
38
39
40 void
41 _initEmu(void)
42 {
43   /* load and configure the libgpsim_modules module */
44   _asm
45     ;; Set frequency to 20MHz
46     .direct "e", ".frequency=20e6"
47     
48     ;; Load the USART library and module
49     .direct "e", "module library libgpsim_modules"
50     .direct "e", "module load usart U1"
51
52     ;; Define a node
53     .direct "e", "node PIC_tx"
54
55     ;; Tie the USART module to the PIC
56     .direct "e", "attach PIC_tx portc6 U1.RXPIN"
57
58     ;; Set the USART module's Baud Rate
59     .direct "e", "U1.rxbaud = 9600"
60
61     ;; Display the received character on terminal
62     .direct "e", "U1.console = true"
63   _endasm;
64
65   /* USART initialization */
66   PORTCbits.TX = 1;     // Set TX pin to 1
67   TRISCbits.TRISC6 = 0; // TX pin is output
68
69   TXSTA = 0;            // Reset USART registers to POR state
70   RCSTA = 0;
71
72   //1. Initialize the SPBRG register for the appropriate
73   //   baud rate. If a high speed baud rate is desired,
74   //   set bit BRGH (Section 16.1).
75   TXSTAbits.BRGH = 1;
76   SPBRG = 129;
77
78   //2. Enable the asynchronous serial port by clearing
79   //   bit SYNC and setting bit SPEN.
80   RCSTAbits.SPEN = 1;
81
82   //3. If interrupts are desired, set enable bit TXIE.
83   //4. If 9-bit transmission is desired, set transmit bit
84   //   TX9. Can be used as address/data bit.
85   //5. Enable the transmission by setting bit TXEN,
86   //   which will also set bit TXIF.
87   TXSTAbits.TXEN = 1;
88
89   //6. If 9-bit transmission is selected, the ninth bit
90   //   should be loaded in bit TX9D.
91   //7. Load data to the TXREG register (starts
92   //   transmission).
93 }
94
95
96 void
97 _exitEmu(void)
98 {
99   /* wait until the transmit buffer is empty */
100   while (!TXSTAbits.TRMT)
101     ;
102
103   /* set the breakpoint */
104   _asm
105    .direct "a", "\"\""
106   _endasm;
107 }