update credits
[librepilot.git] / flight / libraries / dcc_stdio.c
blob1a522e9a0ded69c8ab18dd68a2fc7ad6404191f8
1 /***************************************************************************
2 * Copyright (C) 2008 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * Copyright (C) 2008 by Spencer Oliver *
5 * spen@spen-soft.co.uk *
6 * Copyright (C) 2008 by Frederik Kriewtz *
7 * frederik@kriewitz.eu *
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU General Public License *
20 * along with this program; if not, write to the *
21 * Free Software Foundation, Inc., *
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23 ***************************************************************************/
25 #include "inc/dcc_stdio.h"
27 #define TARGET_REQ_TRACEMSG 0x00
28 #define TARGET_REQ_DEBUGMSG_ASCII 0x01
29 #define TARGET_REQ_DEBUGMSG_HEXMSG(size) (0x01 | ((size & 0xff) << 8))
30 #define TARGET_REQ_DEBUGCHAR 0x02
32 /* we use the cortex_m3 DCRDR reg to simulate a arm7_9 dcc channel
33 * DCRDR[7:0] is used by target for status
34 * DCRDR[15:8] is used by target for write buffer
35 * DCRDR[23:16] is used for by host for status
36 * DCRDR[31:24] is used for by host for write buffer */
38 #define NVIC_DBG_DATA_R (*((volatile unsigned short *)0xE000EDF8))
40 #define BUSY 1
42 void dbg_write(unsigned long dcc_data)
44 int len = 4;
46 while (len--) {
47 /* wait for data ready */
48 while (NVIC_DBG_DATA_R & BUSY) {
52 /* write our data and set write flag - tell host there is data*/
53 NVIC_DBG_DATA_R = (unsigned short)(((dcc_data & 0xff) << 8) | BUSY);
54 dcc_data >>= 8;
58 void dbg_trace_point(unsigned long number)
60 dbg_write(TARGET_REQ_TRACEMSG | (number << 8));
63 void dbg_write_u32(const unsigned long *val, long len)
65 dbg_write(TARGET_REQ_DEBUGMSG_HEXMSG(4) | ((len & 0xffff) << 16));
67 while (len > 0) {
68 dbg_write(*val);
70 val++;
71 len--;
75 void dbg_write_u16(const unsigned short *val, long len)
77 unsigned long dcc_data;
79 dbg_write(TARGET_REQ_DEBUGMSG_HEXMSG(2) | ((len & 0xffff) << 16));
81 while (len > 0) {
82 dcc_data = val[0]
83 | ((len > 1) ? val[1] << 16 : 0x0000);
85 dbg_write(dcc_data);
87 val += 2;
88 len -= 2;
92 void dbg_write_u8(const unsigned char *val, long len)
94 unsigned long dcc_data;
96 dbg_write(TARGET_REQ_DEBUGMSG_HEXMSG(1) | ((len & 0xffff) << 16));
98 while (len > 0) {
99 dcc_data = val[0]
100 | ((len > 1) ? val[1] << 8 : 0x00)
101 | ((len > 2) ? val[2] << 16 : 0x00)
102 | ((len > 3) ? val[3] << 24 : 0x00);
104 dbg_write(dcc_data);
106 val += 4;
107 len -= 4;
111 void dbg_write_str(const char *msg)
113 long len;
114 unsigned long dcc_data;
116 for (len = 0; msg[len] && (len < 65536); len++) {
120 dbg_write(TARGET_REQ_DEBUGMSG_ASCII | ((len & 0xffff) << 16));
122 while (len > 0) {
123 dcc_data = msg[0]
124 | ((len > 1) ? msg[1] << 8 : 0x00)
125 | ((len > 2) ? msg[2] << 16 : 0x00)
126 | ((len > 3) ? msg[3] << 24 : 0x00);
127 dbg_write(dcc_data);
129 msg += 4;
130 len -= 4;
134 void dbg_write_char(char msg)
136 dbg_write(TARGET_REQ_DEBUGCHAR | ((msg & 0xff) << 16));
139 void dbg_write_hex32(const unsigned long val)
141 static const char hextab[] = {
142 '0', '1', '2', '3', '4', '5', '6', '7',
143 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
146 for (int shift = 28; shift >= 0; shift -= 4) {
147 dbg_write_char(hextab[(val >> shift) & 0xf]);