summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/m68k/idp/console/duart.c
blob: c8f0d20edb9b3a6fcecc5bdbd81045959556259d (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 *  $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 <bsp.h>
#include <rtems/ringbuf.h>

rtems_isr C_Receive_ISR(rtems_vector_number vector);
extern Ring_buffer_t  Console_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()
{
  /*
   *  ports A & B while configuring PIT by:
   *
   *    + disable Interrupt Mask Register
   *    + disable port A transmitter
   *    + disable port A receiver
   *    + disable port B transmitter
   *    + disable port B receiver
   */

  MC68681_WRITE(DUART_ADDR, MC68681_INTERRUPT_MASK_REG, 0x00);
  MC68681_WRITE(DUART_ADDR, MC68681_COMMAND_REG_A ,MC68681_MODE_REG_DISABLE_TX);
  MC68681_WRITE(DUART_ADDR, MC68681_COMMAND_REG_A, MC68681_MODE_REG_DISABLE_RX);
  MC68681_WRITE(DUART_ADDR, MC68681_COMMAND_REG_B, MC68681_MODE_REG_DISABLE_TX);
  MC68681_WRITE(DUART_ADDR, MC68681_COMMAND_REG_B, MC68681_MODE_REG_DISABLE_RX);

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

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

  /*
   *  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)
  {
    /*
     * initialize the duart registers on port b
     * WARNING:OPTIMIZER MAY ONLY EXECUTE THIRD STATEMENT IF NOT VOLATILE
     *
     *  reset tx, channel b
     *  reset rx, channel b
     *  reset mr pointer, ch
     */
     MC68681_WRITE(DUART_ADDR, MC68681_COMMAND_REG_B, MC68681_MODE_REG_RESET_TX);
     MC68681_WRITE(DUART_ADDR, MC68681_COMMAND_REG_B, MC68681_MODE_REG_RESET_RX);
     MC68681_WRITE(DUART_ADDR, MC68681_COMMAND_REG_B, MC68681_MODE_REG_RESET_MR_PTR);

    /*
     * initialize the duart registers on port a
     * WARNING:OPTIMIZER MAY ONLY EXECUTE THIRD STATEMENT IF NOT VOLATILE
     *
     *  reset tx, channel a
     *  reset rx, channel a
     *  reset mr pointer, ch
     */
     MC68681_WRITE(DUART_ADDR, MC68681_COMMAND_REG_A, MC68681_MODE_REG_RESET_TX);
     MC68681_WRITE(DUART_ADDR, MC68681_COMMAND_REG_A, MC68681_MODE_REG_RESET_RX);
     MC68681_WRITE(DUART_ADDR, MC68681_COMMAND_REG_A, MC68681_MODE_REG_RESET_MR_PTR);

     Pit_initialized = 1;
  }

  /*
   * Init the general registers of the duart
   *
   * init ivr
   * init imr
   * init acr
   * init ctur
   * init ctlr
   * init opcr
   * init cts
   */
  MC68681_WRITE(DUART_ADDR, MC68681_INTERRUPT_VECTOR_REG,
                MC68681_INTERRUPT_VECTOR_INIT);
  MC68681_WRITE(DUART_ADDR, MC68681_INTERRUPT_MASK_REG,
                MC68681_IR_RX_READY_A | MC68681_IR_RX_READY_B);
  MC68681_WRITE(DUART_ADDR, MC68681_AUX_CTRL_REG, MC68681_CLEAR);
  MC68681_WRITE(DUART_ADDR, MC68681_COUNTER_TIMER_UPPER_REG, 0x00);
  MC68681_WRITE(DUART_ADDR, MC68681_COUNTER_TIMER_LOWER_REG, 0x02);
  MC68681_WRITE(DUART_ADDR, MC68681_OUTPUT_PORT_CONFIG_REG, MC68681_CLEAR);
  MC68681_WRITE(DUART_ADDR, MC68681_OUTPUT_PORT_SET_REG, 0x01);

  /*
   * init the actual serial port for port a
   *
   * Set Baud Rate to 9600
   * Set Stop bit length of 1
   * enable Transmit and receive
   */
  MC68681_WRITE(DUART_ADDR, MC68681_CLOCK_SELECT_REG_A, MC68681_BAUD_RATE_MASK_9600);
  MC68681_WRITE(DUART_ADDR, MC68681_MODE_REG_1A,
                (MC68681_8BIT_CHARS | MC68681_NO_PARITY));
  MC68681_WRITE(DUART_ADDR, MC68681_MODE_REG_2A,MC68681_STOP_BIT_LENGTH_1);
  MC68681_WRITE(DUART_ADDR, MC68681_COMMAND_REG_A,
                (MC68681_MODE_REG_ENABLE_TX | MC68681_MODE_REG_ENABLE_RX));

  /*
   * init the actual serial port for port b
   * init csrb -- 9600 baud
   */
  MC68681_WRITE(DUART_ADDR, MC68681_CLOCK_SELECT_REG_B, MC68681_BAUD_RATE_MASK_9600);

#define EIGHT_BITS_NO_PARITY
#ifdef EIGHT_BITS_NO_PARITY
  /*
   * Set 8 Bit characters with no parity
   */
  MC68681_WRITE(DUART_ADDR, MC68681_MODE_REG_1B,
                (MC68681_NO_PARITY | MC68681_8BIT_CHARS) );
#else
  /*
   * Set 7 Bit Characters with parity
   */
  MC68681_WRITE(DUART_ADDR, MC68681_MODE_REG_1B,
                (MC68681_WITH_PARITY |  MC68681_7BIT_CHARS) );
#endif

  /*
   * Set Stop Bit length to 1
   * Disable Recieve and transmit on B
   */
  MC68681_WRITE(DUART_ADDR, MC68681_MODE_REG_2B,MC68681_STOP_BIT_LENGTH_1);
  MC68681_WRITE(DUART_ADDR, MC68681_COMMAND_REG_B,
                (MC68681_MODE_REG_ENABLE_TX | MC68681_MODE_REG_ENABLE_RX) );
}

/*#####################################################################
# 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;

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

  /*
   *  check port A first for input
   *     extract rcvrdy on port B
   *     set ptr to recieve buffer and read character into ring buffer
   */
  _addr = (unsigned char *) (DUART_ADDR + MC68681_STATUS_REG_A);
  if (*_addr & MC68681_RX_READY)  /* extract rcvrdy on port A */
  {
    _addr = (unsigned char *) (DUART_ADDR + MC68681_RECEIVE_BUFFER_A);
    Ring_buffer_Add_character( &Console_Buffer[ 0 ], *_addr );
  }

  /*
   *  If not on port A, let's check port B
   *     extract rcvrdy on port B
   *     set ptr to recieve buffer and read character into ring buffer
   */
  else
  {
    _addr = (unsigned char *) (DUART_ADDR + MC68681_STATUS_REG_B);
    if (*_addr & MC68681_RX_READY)  /* extract rcvrdy on port B */
    {
      _addr = (unsigned char *) (DUART_ADDR + MC68681_RECEIVE_BUFFER_B);
      Ring_buffer_Add_character( &Console_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 + MC68681_STATUS_REG_A);
  while (!(*_addr & MC68681_TX_READY))
  {
  }

  /*
   * transmit character over port A
   */
  MC68681_WRITE(DUART_ADDR, MC68681_TRANSMIT_BUFFER_A, 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 + MC68681_STATUS_REG_B);
  while (!(*_addr &  MC68681_TX_READY))
  {
  }

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