summaryrefslogtreecommitdiff
path: root/board/discovery-stm32f072/echo.c
blob: 56fbef7bbe0fde12edde38e033c6502544888906 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
/*
 * Task to echo any characters from the three non-console USARTs back to all
 * non-console USARTs.
 */

#include "atomic.h"
#include "common.h"
#include "compile_time_macros.h"
#include "console.h"
#include "panic.h"
#include "task.h"
#include "stream_adaptor.h"
#include "timer.h"
#include "usart-stm32f0.h"
#include "usb-stream.h"
#include "util.h"

static void in_ready(struct in_stream const *stream)
{
	task_wake(TASK_ID_ECHO);
}

static void out_ready(struct out_stream const *stream)
{
	task_wake(TASK_ID_ECHO);
}

#define USART_STREAM_CONFIG(NAME,					\
			    HW,						\
			    BAUD,					\
			    RX_SIZE,					\
			    TX_SIZE,					\
			    IN_READY,					\
			    OUT_READY)					\
									\
	QUEUE_CONFIG(CONCAT2(NAME, _rx_queue), RX_SIZE, uint8_t);	\
	QUEUE_CONFIG(CONCAT2(NAME, _tx_queue), TX_SIZE, uint8_t);	\
									\
	struct usart_config const NAME;					\
									\
	IN_STREAM_FROM_PRODUCER(CONCAT2(NAME, _in),			\
				NAME.producer,				\
				CONCAT2(NAME, _rx_queue),		\
				IN_READY)				\
	OUT_STREAM_FROM_CONSUMER(CONCAT2(NAME, _out),			\
				 NAME.consumer,				\
				 CONCAT2(NAME, _tx_queue),		\
				 OUT_READY)				\
									\
	USART_CONFIG(NAME,						\
		     HW,						\
		     BAUD,						\
		     CONCAT2(NAME, _rx_queue),				\
		     CONCAT2(NAME, _tx_queue),				\
		     CONCAT2(NAME, _in).consumer,			\
		     CONCAT2(NAME, _out).producer)

USART_STREAM_CONFIG(usart1, usart1_hw, 115200, 64, 64, in_ready, NULL);
USART_STREAM_CONFIG(usart3, usart3_hw, 115200, 64, 64, in_ready, NULL);
USART_STREAM_CONFIG(usart4, usart4_hw, 115200, 64, 64, in_ready, NULL);

QUEUE_CONFIG(usb_rx_queue, 256, uint8_t);
QUEUE_CONFIG(usb_tx_queue, 256, uint8_t);

struct usb_stream_config const usb_stream1;

IN_STREAM_FROM_PRODUCER(usb_in, usb_stream1.producer, usb_rx_queue, in_ready)
OUT_STREAM_FROM_CONSUMER(usb_out, usb_stream1.consumer, usb_tx_queue, out_ready)

USB_STREAM_CONFIG(usb_stream1,
		  USB_IFACE_STREAM,
		  USB_STR_STREAM_NAME,
		  USB_EP_STREAM,
		  64,
		  64,
		  usb_rx_queue,
		  usb_tx_queue,
		  usb_in.consumer,
		  usb_out.producer)

struct stream_console_state {
	size_t wrote;
};

struct stream_console_config {
	struct stream_console_state *state;

	struct in_stream  const *in;
	struct out_stream const *out;
};

#define STREAM_CONSOLE_CONFIG(NAME, IN, OUT)			\
	static struct stream_console_state NAME##_state;	\
	struct stream_console_config const NAME = {		\
		.state = &NAME##_state,				\
		.in    = IN,					\
		.out   = OUT,					\
	};

STREAM_CONSOLE_CONFIG(usart1_stream_console, &usart1_in.in, &usart1_out.out)
STREAM_CONSOLE_CONFIG(usart3_stream_console, &usart3_in.in, &usart3_out.out)
STREAM_CONSOLE_CONFIG(usart4_stream_console, &usart4_in.in, &usart4_out.out)
STREAM_CONSOLE_CONFIG(usb_stream1_console, &usb_in.in, &usb_out.out)

static struct stream_console_config const *const consoles[] = {
	&usart1_stream_console,
	&usart3_stream_console,
	&usart4_stream_console,
	&usb_stream1_console,
};

static size_t echo(struct stream_console_config const *const consoles[],
		   size_t consoles_count)
{
	size_t total = 0;
	size_t i;

	for (i = 0; i < consoles_count; ++i) {
		size_t  j;
		uint8_t buffer[64];
		size_t  remaining = 0;
		size_t  count     = in_stream_read(consoles[i]->in,
						   buffer,
						   sizeof(buffer));

		if (count == 0)
			continue;

		for (j = 0; j < consoles_count; ++j)
			consoles[j]->state->wrote = 0;

		do {
			remaining = 0;

			for (j = 0; j < consoles_count; ++j) {
				size_t wrote = consoles[j]->state->wrote;

				if (count == wrote)
					continue;

				wrote += out_stream_write(consoles[j]->out,
							  buffer + wrote,
							  count - wrote);

				consoles[j]->state->wrote = wrote;

				remaining += count - wrote;
			}
		} while (remaining);

		total += count;
	}

	return total;
}

void echo_task(void)
{
	usart_init(&usart1);
	usart_init(&usart3);
	usart_init(&usart4);

	while (1) {
		while (echo(consoles, ARRAY_SIZE(consoles))) {
			/*
			 * Make sure other tasks, like the HOOKS get to run.
			 */
			msleep(1);
		}

		/*
		 * There was nothing left to echo, go to sleep and be
		 * woken up by the next input.
		 */
		task_wait_event(-1);
	}
}

static int command_echo_info(int argc, char **argv)
{
	char const message[] = "Hello World!\r\n";
	size_t     i;

	ccprintf("USART1 RX dropped %d bytes\n",
		 atomic_read_clear((uint32_t *) &(usart1.state->rx_dropped)));

	ccprintf("USART3 RX dropped %d bytes\n",
		 atomic_read_clear((uint32_t *) &(usart3.state->rx_dropped)));

	ccprintf("USART4 RX dropped %d bytes\n",
		 atomic_read_clear((uint32_t *) &(usart4.state->rx_dropped)));

	for (i = 0; i < ARRAY_SIZE(consoles); ++i)
		out_stream_write(consoles[i]->out, message, strlen(message));

	return EC_SUCCESS;
}

DECLARE_CONSOLE_COMMAND(echo_info,
			command_echo_info,
			NULL,
			"Dump echo task debug info",
			NULL);