aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios-contrib/testhal/KINETIS/MCHCK/USB_SERIAL/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios-contrib/testhal/KINETIS/MCHCK/USB_SERIAL/main.c')
-rw-r--r--lib/chibios-contrib/testhal/KINETIS/MCHCK/USB_SERIAL/main.c161
1 files changed, 161 insertions, 0 deletions
diff --git a/lib/chibios-contrib/testhal/KINETIS/MCHCK/USB_SERIAL/main.c b/lib/chibios-contrib/testhal/KINETIS/MCHCK/USB_SERIAL/main.c
new file mode 100644
index 000000000..285954d05
--- /dev/null
+++ b/lib/chibios-contrib/testhal/KINETIS/MCHCK/USB_SERIAL/main.c
@@ -0,0 +1,161 @@
1/*
2 (C) 2015-2016 flabbergast <[email protected]>
3 Based on ChibiOS USB_CDC demo - Copyright (C) 2006..2016 Giovanni Di Sirio
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16*/
17
18#include <stdio.h>
19#include <string.h>
20
21#include "ch.h"
22#include "hal.h"
23
24#include "shell.h"
25#include "chprintf.h"
26
27#include "usbcfg.h"
28
29/*===========================================================================*/
30/* Command line related. */
31/*===========================================================================*/
32
33#define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048)
34
35/* Can be measured using dd if=/dev/xxxx of=/dev/null bs=512 count=10000.*/
36static void cmd_write(BaseSequentialStream *chp, int argc, char *argv[]) {
37 static uint8_t buf[] =
38 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
39 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
40 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
41 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
42 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
43 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
44 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
45 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
46 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
47 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
48 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
49 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
50 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
51 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
52 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
53 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
54
55 (void)argv;
56 if (argc > 0) {
57 chprintf(chp, "Usage: write\r\n");
58 return;
59 }
60
61 while (chnGetTimeout((BaseChannel *)chp, TIME_IMMEDIATE) == Q_TIMEOUT) {
62#if 1
63 /* Writing in channel mode.*/
64 chnWrite(&SDU1, buf, sizeof buf - 1);
65#else
66 /* Writing in buffer mode.*/
67 (void) obqGetEmptyBufferTimeout(&SDU1.obqueue, TIME_INFINITE);
68 memcpy(SDU1.obqueue.ptr, buf, SERIAL_USB_BUFFERS_SIZE);
69 obqPostFullBuffer(&SDU1.obqueue, SERIAL_USB_BUFFERS_SIZE);
70#endif
71 }
72 chprintf(chp, "\r\n\nstopped\r\n");
73}
74
75static const ShellCommand commands[] = {
76 {"write", cmd_write},
77 {NULL, NULL}
78};
79
80static const ShellConfig shell_cfg1 = {
81 (BaseSequentialStream *)&SDU1,
82 commands
83};
84
85/*===========================================================================*/
86/* Generic code. */
87/*===========================================================================*/
88
89/*
90 * Red LED blinker thread, times are in milliseconds.
91 */
92static THD_WORKING_AREA(waThread1, 128);
93static THD_FUNCTION(Thread1, arg) {
94
95 (void)arg;
96 chRegSetThreadName("blinker");
97 while (true) {
98 systime_t time;
99
100 time = serusbcfg.usbp->state == USB_ACTIVE ? 250 : 500;
101 palClearPad(GPIOB, GPIOB_LED);
102 chThdSleepMilliseconds(time);
103 palSetPad(GPIOB, GPIOB_LED);
104 chThdSleepMilliseconds(time);
105 }
106}
107
108/*
109 * Application entry point.
110 */
111int main(void) {
112
113 /*
114 * System initializations.
115 * - HAL initialization, this also initializes the configured device drivers
116 * and performs the board-specific initializations.
117 * - Kernel initialization, the main() function becomes a thread and the
118 * RTOS is active.
119 */
120 halInit();
121 chSysInit();
122
123 /*
124 * Initializes a serial-over-USB CDC driver.
125 */
126 sduObjectInit(&SDU1);
127 sduStart(&SDU1, &serusbcfg);
128
129 /*
130 * Activates the USB driver and then the USB bus pull-up on D+.
131 * Note, a delay is inserted in order to not have to disconnect the cable
132 * after a reset.
133 */
134 usbDisconnectBus(serusbcfg.usbp);
135 chThdSleepMilliseconds(1500);
136 usbStart(serusbcfg.usbp, &usbcfg);
137 usbConnectBus(serusbcfg.usbp);
138
139 /*
140 * Shell manager initialization.
141 */
142 shellInit();
143
144 /*
145 * Creates the blinker thread.
146 */
147 chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
148
149 /*
150 * Normal main() thread activity, spawning shells.
151 */
152 while (true) {
153 if (SDU1.config->usbp->state == USB_ACTIVE) {
154 thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
155 "shell", NORMALPRIO + 1,
156 shellThread, (void *)&shell_cfg1);
157 chThdWait(shelltp); /* Waiting termination. */
158 }
159 chThdSleepMilliseconds(1000);
160 }
161}