aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios-contrib/testhal/KINETIS/FRDM-KL25Z/USB_SERIAL/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios-contrib/testhal/KINETIS/FRDM-KL25Z/USB_SERIAL/main.c')
-rw-r--r--lib/chibios-contrib/testhal/KINETIS/FRDM-KL25Z/USB_SERIAL/main.c168
1 files changed, 168 insertions, 0 deletions
diff --git a/lib/chibios-contrib/testhal/KINETIS/FRDM-KL25Z/USB_SERIAL/main.c b/lib/chibios-contrib/testhal/KINETIS/FRDM-KL25Z/USB_SERIAL/main.c
new file mode 100644
index 000000000..449db3160
--- /dev/null
+++ b/lib/chibios-contrib/testhal/KINETIS/FRDM-KL25Z/USB_SERIAL/main.c
@@ -0,0 +1,168 @@
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 systime_t time;
95
96 (void)arg;
97
98 chRegSetThreadName("blinker");
99 while (true) {
100 time = serusbcfg.usbp->state == USB_ACTIVE ? 250 : 500;
101 palClearPad(GPIO_LED_RED, PIN_LED_RED);
102 chThdSleepMilliseconds(time);
103 palSetPad(GPIO_LED_RED, PIN_LED_RED);
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 * Turn off the RGB LED.
125 */
126 palSetPad(GPIO_LED_RED, PIN_LED_RED); /* red */
127 palSetPad(GPIO_LED_GREEN, PIN_LED_GREEN); /* green */
128 palSetPad(GPIO_LED_BLUE, PIN_LED_BLUE); /* blue */
129
130 /*
131 * Initializes a serial-over-USB CDC driver.
132 */
133 sduObjectInit(&SDU1);
134 sduStart(&SDU1, &serusbcfg);
135
136 /*
137 * Activates the USB driver and then the USB bus pull-up on D+.
138 * Note, a delay is inserted in order to not have to disconnect the cable
139 * after a reset.
140 */
141 usbDisconnectBus(serusbcfg.usbp);
142 chThdSleepMilliseconds(1500);
143 usbStart(serusbcfg.usbp, &usbcfg);
144 usbConnectBus(serusbcfg.usbp);
145
146 /*
147 * Shell manager initialization.
148 */
149 shellInit();
150
151 /*
152 * Creates the blinker thread.
153 */
154 chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
155
156 /*
157 * Normal main() thread activity, spawning shells.
158 */
159 while (true) {
160 if (SDU1.config->usbp->state == USB_ACTIVE) {
161 thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
162 "shell", NORMALPRIO + 1,
163 shellThread, (void *)&shell_cfg1);
164 chThdWait(shelltp); /* Waiting termination. */
165 }
166 chThdSleepMilliseconds(1000);
167 }
168}