blob: ca45404767d25647bd7bdfc53df2159283b75879 [file] [log] [blame]
Ajay Dudani168f6cb2009-12-07 19:04:02 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <jtag.h>
30
31#define STATUS_NOMSG 0
32#define STATUS_OKAY 1
33#define STATUS_FAIL 2
34#define STATUS_PRINT 3
35
36volatile unsigned _jtag_cmd = 0;
37volatile unsigned _jtag_msg = 0;
38unsigned char _jtag_cmd_buffer[128];
39unsigned char _jtag_msg_buffer[128];
40
41volatile unsigned _jtag_arg0 = 0;
42volatile unsigned _jtag_arg1 = 0;
43volatile unsigned _jtag_arg2 = 0;
44
45
46void jtag_hook(void);
47
48static void jtag_msg(unsigned status, const char *msg)
49{
50 unsigned char *out = _jtag_msg_buffer;
51 while((*out++ = *msg++) != 0) ;
52 _jtag_msg = status;
53 do {
54 jtag_hook();
55 } while(_jtag_msg != 0);
56}
57
58void jtag_okay(const char *msg)
59{
60 if(msg == 0) msg = "OKAY";
61 jtag_msg(STATUS_OKAY, msg);
62}
63
64void jtag_fail(const char *msg)
65{
66 if(msg == 0) msg = "FAIL";
67 jtag_msg(STATUS_FAIL, msg);
68}
69
70int jtag_cmd_pending()
71{
72 jtag_hook();
73 return (int) _jtag_cmd;
74}
75
76void jtag_cmd_loop(void (*do_cmd)(const char *, unsigned, unsigned, unsigned))
77{
78 unsigned n;
79 for(;;) {
80 if(jtag_cmd_pending()){
81 do_cmd((const char*) _jtag_cmd_buffer, _jtag_arg0, _jtag_arg1, _jtag_arg2);
82 for(n = 0; n < 256; n++) _jtag_cmd_buffer[n] = 0;
83 _jtag_arg0 = 0;
84 _jtag_arg1 = 0;
85 _jtag_arg2 = 0;
86 _jtag_cmd = 0;
87 }
88 }
89}
90
91static char jtag_putc_buffer[128];
92static unsigned jtag_putc_count = 0;
93
94static void jtag_push_buffer(void)
95{
96 jtag_putc_buffer[jtag_putc_count] = 0;
97 jtag_putc_count = 0;
98 jtag_msg(STATUS_PRINT, jtag_putc_buffer);
99}
100
101void jtag_dputc(unsigned c)
102{
103 if((c < 32) || (c > 127)) {
104 if(c == '\n') {
105 jtag_push_buffer();
106 }
107 return;
108 }
109
110 jtag_putc_buffer[jtag_putc_count++] = c;
111 if(jtag_putc_count == 127) {
112 jtag_push_buffer();
113 }
114}
115