summaryrefslogtreecommitdiff
path: root/openpicc/application/usb_print.c
blob: 5e4372b984f496918c0a0d6b18526cd104000943 (plain)
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
/* Buffered USB debug output
 * (C) 2007 Henryk Plötz <henryk@ploetzli.ch>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <FreeRTOS.h>
#include <task.h> 
#include <semphr.h>
#include <USB-CDC.h>
#include <string.h>

#include "usb_print.h"

#define BUFLEN (2*1024)

static char ringbuffer[BUFLEN];
static int ringstart, ringstop;
static int default_flush = 1, forced_silence = 0;
static xSemaphoreHandle print_semaphore;

void usb_print_buffer(const char* buffer, int start, int stop) {
	usb_print_buffer_f(buffer,start,stop,default_flush);
}
int usb_print_buffer_f(const char* buffer, int start, int stop, int flush)
{
	int pos=start, endpos=stop;
	while(pos<stop) {
		int available = BUFLEN-1 - (((ringstop+BUFLEN)-ringstart) % BUFLEN);
		if(available == 0 && flush) { 
			usb_print_flush();
			continue;
		}
		if((endpos-pos)>available) endpos=pos+available;
		
		while(pos < endpos) {
			ringbuffer[ringstop] = buffer[pos];
			pos++;
			available--;
			ringstop = (ringstop+1) % BUFLEN;
		}
		
		if(flush) usb_print_flush();
		else if(available==0)
			return 0;
		endpos=stop;
		if(pos>=stop)
			return (available > 0);
	}
	return 0;
}

void usb_print_string(const char *string) {
	usb_print_string_f(string, default_flush);
}
int usb_print_string_f(const char* string, int flush)
{
	int start = 0, stop = strlen(string);
	return usb_print_buffer_f(string, start, stop, flush);
}

void usb_print_char(const char c) {
	usb_print_char_f(c, default_flush);
}
int usb_print_char_f(const char c, int flush)
{
	return usb_print_buffer_f(&c, 0, 1, flush);
}

int usb_print_get_default_flush(void)
{
	return default_flush;
}

int usb_print_set_default_flush(int flush)
{
	int old_flush = default_flush;
	default_flush = flush;
	return old_flush;
}

int usb_print_set_force_silence(int silence)
{
	int old_silence = forced_silence;
	forced_silence = silence;
	return old_silence;
}


/* Must NOT be called from ISR context */
void usb_print_flush(void)
{
	int oldstop, newstart;
	if(forced_silence) return;
	
	taskENTER_CRITICAL();
	if(print_semaphore == NULL)
		usb_print_init();
	if(print_semaphore == NULL) {
		taskEXIT_CRITICAL();
		return;
	}
	taskEXIT_CRITICAL();
	
	xSemaphoreTake(print_semaphore, portMAX_DELAY);
	
	taskENTER_CRITICAL();
	oldstop = ringstop;
	newstart = ringstart;
	taskEXIT_CRITICAL();
	 
	while(newstart != oldstop) {
		vUSBSendByte_blocking(ringbuffer[newstart], 5*portTICK_RATE_MS);
		newstart = (newstart+1) % BUFLEN;
	}
	
	taskENTER_CRITICAL();
	ringstart = newstart;
	taskEXIT_CRITICAL();
	
	xSemaphoreGive(print_semaphore);
}

void usb_print_init(void)
{
	memset(ringbuffer, 0, BUFLEN);
	ringstart = ringstop = 0;
	vSemaphoreCreateBinary( print_semaphore );
}
personal git repositories of Harald Welte. Your mileage may vary