summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/m68k/ods68302/startup/debugport.c
blob: be9da857b507928d3cfdcd0c4f1dd97b5a91b083 (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
/*****************************************************************************/
/*
  High Level Debug Port Functions

  $Id$
  
 */
/*****************************************************************************/

#include <stdio.h>
#include <stdarg.h>

#include "debugport.h"
#include "m68302scc.h"
#include "bsp.h"

static int initialised;

void debug_port_initialise(void)
{
  scc_initialise(CONSOLE_PORT, CONSOLE_BAUD, FALSE);
#if defined(DEBUG_PORT)
  scc_initialise(DEBUG_PORT, DEBUG_BAUD, FALSE);
#endif
}

unsigned char debug_port_status(const unsigned char status)
{
  if (!initialised)
  {
    initialised = 1;    
    debug_port_initialise();
  }
  
  return scc_status(CONSOLE_PORT, status);
}

unsigned char debug_port_in(void)
{
  if (!initialised)
  {
    initialised = 1;    
    debug_port_initialise();
  }
  
  return scc_in(CONSOLE_PORT);
}

void debug_port_out(const unsigned char character)
{
  if (!initialised)
  {
    initialised = 1;    
    debug_port_initialise();
  }
  
  scc_out(CONSOLE_PORT, character);
}

void debug_port_write(const char *buffer)
{
   while (*buffer != '\0')
   {
     debug_port_out(*buffer++);
   }
}

void debug_port_write_buffer(const char *buffer, unsigned int size)
{
   unsigned int count;
   for (count = 0; count < size; count++)
   {
     debug_port_out(buffer[count]);
   }
}

void debug_port_write_hex_uint(const unsigned int value)
{
   unsigned int bits = sizeof(value) * 8;
   unsigned char c;

   do
   {
     bits -= 4;
     c = (unsigned char) ((value >> bits) & 0x0F);
     if (c < 10)
     {
       c += '0';
     }
     else
     {
       c += 'a' - 10;
     }
     debug_port_out((char) c);
   }
   while (bits);
}

void debug_port_write_hex_ulong(const unsigned long value)
{
   unsigned int bits = sizeof(value) * 8;
   unsigned char c;

   do
   {
     bits -= 4;
     c = (unsigned char) ((value >> bits) & 0x0F);
     if (c < 10)
     {
       c += '0';
     }
     else
     {
       c += 'a' - 10;
     }
     debug_port_out((char) c);
   }
   while (bits);
}

#define BUFFER_SIZE (256)
static char buffer[BUFFER_SIZE];

void debug_port_printf(const char *format, ...)
{
  va_list args;
  int written;

  /*  gain access to the argument list */
  va_start(args, format);

  /* set the trap    */
  buffer[BUFFER_SIZE - 2] = '\xAA';
  buffer[BUFFER_SIZE - 1] = '\x55';

  /* format the string and send to stdout */
  written = vsprintf(buffer, format, args);

  /* try an trap format buffer overflows */
  if ((buffer[BUFFER_SIZE - 2] != '\xAA') || 
      (buffer[BUFFER_SIZE - 1] != '\x55'))
  {
    debug_port_write("debug port buffer overflow, halting...");
    DISABLE_WATCHDOG();
    while (1 == 1);
  }

  /* see if an error occurred, if not flush the output buffer */
  if (written != -1)
  {
    debug_port_write_buffer(buffer, written);
  }
}

void debug_port_banner(void)
{
#define CARD_LABEL "ods68302-" #VARIANT
  
  debug_port_write("\n\n\r");
  debug_port_write(_Copyright_Notice);
  debug_port_write("\n\r  " CARD_ID "\n\r");
}