-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlog_interface.c
63 lines (53 loc) · 1.65 KB
/
log_interface.c
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
// Copyright 2014 Technical Machine, Inc. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#include "usb/tessel_usb.h"
#include "LPC18xx.h"
#define debug_buf_size 1024
static char debug_buf[2][debug_buf_size];
static volatile unsigned debug_pos = 0;
static volatile unsigned debug_bank = 0;
static volatile bool debug_connected = false;
bool usb_debug_set_interface(uint16_t setting) {
if (setting == 0) {
debug_connected = false;
return true;
} else if (setting == 1) {
usb_enable_ep(debug_in_ep, USB_EP_TYPE_BULK, 512);
debug_connected = true;
return true;
}
return false;
}
void debug_try_to_send() {
if (usb_ep_ready(debug_in_ep) && debug_pos > 0) {
usb_ep_start_in(debug_in_ep, (uint8_t*) debug_buf[debug_bank], debug_pos, false);
debug_pos = 0;
debug_bank ^= 1;
}
}
void usb_log_write(char level, const char* s, int len) {
if (!debug_connected) {
return;
}
if (len + 2 > debug_buf_size) len = debug_buf_size - 2;
if ((debug_pos + len + 2) > debug_buf_size) {
while ((debug_pos + len) >= debug_buf_size) { }
}
__disable_irq();
debug_buf[debug_bank][debug_pos++] = 1;
debug_buf[debug_bank][debug_pos++] = level;
for (int i=0; i<len; i++) {
debug_buf[debug_bank][debug_pos++] = s[i];
}
__enable_irq();
debug_try_to_send();
}
void tm_log(char level, const char* s, int len) {
usb_log_write(level, s, len);
}