summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/m68k/idp/console/duart.c
blob: fe4b657b376f2dbfd906fe8c6466fb7ca0065bb2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#
#  $Id$
#

/*#########################################################
#
# This code is a modified version of what you will find at the
# end of the IDP User's manual.  The original code is copyrighted
# by Motorola and Motorola Semiconductor Products as well as
# Motorola Software products group.
#
# Modifications to the original IDP code by Doug McBride, Colorado
# Space Grant College.  Modifications include a means of accessing
# port B of the duart as well as port A as well as modifications for
# buffering and RTEMS support.  Modifications are provided
# as is and may not be correct.
#
# Rob Savoye provided the format for the mc68681 header file
#
# Joel Sherrill provided inspiration for recoding my original assembly
# for this file into C (a good idea)
#
##########################################################*/

#include "mc68230.h"
#include "mc68681.h"
#include "ringbuf.h"
#include "rtems.h"
#include "bsp.h"

rtems_isr C_Receive_ISR(rtems_vector_number vector);
extern Ring_buffer_t  Buffer[];

extern unsigned char inbuf[];
extern unsigned char inbuf_portb[];
extern unsigned tail;
extern unsigned tail_portb;
unsigned char Pit_initialized = 0;

/*#####################################################################
# The volatile routine to initialize the duart -- port a and port b
######################################################################*/
volatile void init_pit()
{
	/* Disable ports A & B while configuring PIT */
	MC68681_WRITE(DUART_IMR, 0x00);		/* disable imr */
	MC68681_WRITE(DUART_CRA, 0x08);		/* disable port a transmitter */
	MC68681_WRITE(DUART_CRA, 0x02);		/* disable port a receiver */
	MC68681_WRITE(DUART_CRB, 0x08);		/* disable port b transmitter */
	MC68681_WRITE(DUART_CRB, 0x02);		/* disable port b receiver */

	/* install ISR for ports A and B */
	set_vector(C_Receive_ISR, (VECT+H3VECT), 1);

	/* initialize pit */
	MC68230_WRITE(PGCR, 0x00);  /* set mode to 0 -- disable all ports */
	MC68230_WRITE(PSRR, 0x18);	/* set up pirq and piack */
	MC68230_WRITE(PBDDR, 0x00);	/* all pins on port b are input */
	MC68230_WRITE(PBCR, 0x82);	/* submode 1x, h3 interrupt enabled */
	MC68230_WRITE(PIVR, VECT);	/* setup pivr */
	MC68230_WRITE(PGCR, 0x20);	/* turn on all ports */

	/* For some reason, the reset of receiver/transmitter only works for
       the first time around -- it garbles the output otherwise (e.g., sp21) */
	if (!Pit_initialized)
	{
	   /* now initialize the duart registers on port b */
	   /* WARNING:OPTIMIZER MAY ONLY EXECUTE THIRD STATEMENT IF NOT VOLATILE */
	   MC68681_WRITE(DUART_CRB, 0x30);		/* reset tx, channel b */
	   MC68681_WRITE(DUART_CRB, 0x20);		/* reset rx, channel b */
	   MC68681_WRITE(DUART_CRB, 0x10);		/* reset mr pointer, channel b */

	   /* now initialize the duart registers on port a */
	   /* WARNING:OPTIMIZER MAY ONLY EXECUTE THIRD STATEMENT IF NOT VOLATILE */
	   MC68681_WRITE(DUART_CRA, 0x30);		/* reset tx, channel a */
	   MC68681_WRITE(DUART_CRA, 0x20);		/* reset rx, channel a */
	   MC68681_WRITE(DUART_CRA, 0x10);		/* reset mr pointer, channel a */
	   Pit_initialized = 1;
	}

	/* init the general registers of the duart */
	MC68681_WRITE(DUART_IVR, 0x0f);		/* init ivr */
	MC68681_WRITE(DUART_IMR, 0x22);		/* init imr */
	MC68681_WRITE(DUART_ACR, 0x00);		/* init acr */
	MC68681_WRITE(DUART_CTUR, 0x00);	/* init ctur */
	MC68681_WRITE(DUART_CTLR, 0x02);	/* init ctlr */
	MC68681_WRITE(DUART_OPCR, 0x00);	/* init opcr */
	MC68681_WRITE(DUART_OPRSET, 0x01);	/* init cts */

	/* init the actual serial port for port a */
	MC68681_WRITE(DUART_CSRA, 0xbb);	/* init csra -- 9600 baud */
	MC68681_WRITE(DUART_MR1A, 0x13);	/* init mr1a */
	MC68681_WRITE(DUART_MR2A, 0x07);	/* init mr2a */
	MC68681_WRITE(DUART_CRA, 0x05);		/* init cra */

	/* init the actual serial port for port b */
	MC68681_WRITE(DUART_CSRB, 0xbb);	/* init csrb -- 9600 baud */
#define EIGHT_BITS_NO_PARITY
#ifdef EIGHT_BITS_NO_PARITY
	MC68681_WRITE(DUART_MR1B, 0x13);	/* init mr1b */
#else /* 7 bits, even parity */
	MC68681_WRITE(DUART_MR1B, 0x02);	/* init mr1b */
#endif
	MC68681_WRITE(DUART_MR2B, 0x07);	/* init mr2b -- one stop bit */
	MC68681_WRITE(DUART_CRB, 0x05);		/* init crb */
}

/*#####################################################################
# interrupt handler for receive of character from duart on ports A & B
#####################################################################*/
rtems_isr C_Receive_ISR(rtems_vector_number vector)
{
	volatile unsigned char *_addr;

	_addr = (unsigned char *) (PIT_ADDR + PITSR);
	*_addr = 0x04;		/* clear pit interrupt */

	/* Let's check port A first for input */
	_addr = (unsigned char *) (DUART_ADDR + DUART_SRA);
	if (*_addr & 0x01)	/* extract rcvrdy on port A */
	{
		/* Read input on port A */
		_addr = (unsigned char *) (DUART_ADDR + DUART_RBA);
		Ring_buffer_Add_character( &Buffer[ 0 ], *_addr );
	}
	else	/* If not on port A, let's check port B */
	{
		_addr = (unsigned char *) (DUART_ADDR + DUART_SRB);
		if (*_addr & 0x01)	/* extract rcvrdy on port B */
		{
			/* Read input on port B */
			_addr = (unsigned char *) (DUART_ADDR + DUART_RBB);
			Ring_buffer_Add_character( &Buffer[ 1 ], *_addr );
		}
		/* if not ready on port A or port B, must be an error */
		/* if error, get out so that fifo is undisturbed */
	}
}

/*#####################################################################
# This is the routine that actually transmits a character one at a time
# This routine transmits on port A of the IDP board
#####################################################################*/
void transmit_char(char ch)
{
	volatile unsigned char *_addr;

	/* Get SRA (extract txrdy) */
	_addr = (unsigned char *) (DUART_ADDR + DUART_SRA);
	while (!(*_addr & 0x04))
	{
	}

	/* transmit character over port A */
	MC68681_WRITE(DUART_TBA, ch);
}

/*#####################################################################
# This is the routine that actually transmits a character one at a time
# This routine transmits on port B of the IDP board
#####################################################################*/
void transmit_char_portb(char ch)
{
	volatile unsigned char *_addr;

	/* Get SRB (extract txrdy) */
	_addr = (unsigned char *) (DUART_ADDR + DUART_SRB);
	while (!(*_addr & 0x04))
	{
	}

	/* transmit character over port B */
	MC68681_WRITE(DUART_TBB, ch);
}