aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios-contrib/demos/MIMXRT1062/RT-TEENSY4_1/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios-contrib/demos/MIMXRT1062/RT-TEENSY4_1/main.c')
-rw-r--r--lib/chibios-contrib/demos/MIMXRT1062/RT-TEENSY4_1/main.c176
1 files changed, 176 insertions, 0 deletions
diff --git a/lib/chibios-contrib/demos/MIMXRT1062/RT-TEENSY4_1/main.c b/lib/chibios-contrib/demos/MIMXRT1062/RT-TEENSY4_1/main.c
new file mode 100644
index 000000000..6ce53805d
--- /dev/null
+++ b/lib/chibios-contrib/demos/MIMXRT1062/RT-TEENSY4_1/main.c
@@ -0,0 +1,176 @@
1/*
2 ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#include "ch.h"
18#include "chthreads.h"
19#include "hal.h"
20#include "ch_test.h"
21#include "rt_test_root.h"
22#include "oslib_test_root.h"
23#include "chprintf.h"
24#include "shell.h"
25#include <string.h>
26
27#include "usbcfg.h"
28
29/* This demo can be customized in scope for easier debugging.
30 *
31 * Define one of the following: */
32
33/* Run rt and oslib test suite to serial console: */
34//#define DEMO_MODE_TESTS_TO_SERIAL
35
36/* Provide an interactive shell on serial console.
37 * You can run the tests using the test command: */
38//#define DEMO_MODE_SHELL_ON_SERIAL
39
40/* Provide an interactive shell on serial console over USB.
41 * This mode does not require any extra serial hardware: */
42#define DEMO_MODE_SHELL_ON_USB_SERIAL
43
44/*
45 * Serial 1 (LPUART1) corresponds to Pin 24 on the Teensy 4.1, or to the built-in
46 * usb-to-serial on the debug probe of the MIMXRT1060-EVK.
47 */
48#define MYSERIAL &SD1
49
50/*
51 * LED blinker thread.
52 */
53static THD_WORKING_AREA(waThread1, 64);
54static THD_FUNCTION(Thread1, arg) {
55
56 (void)arg;
57 chRegSetThreadName("LEDBlinker");
58 while (true) {
59 palTogglePad(TEENSY_PIN13_IOPORT, TEENSY_PIN13);
60 chThdSleepSeconds(1);
61 }
62}
63
64#define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048)
65
66static const ShellCommand commands[] = {{NULL, NULL}};
67
68static const ShellConfig shell_cfg1 = {
69#ifdef DEMO_MODE_SHELL_ON_USB_SERIAL
70 (BaseSequentialStream *)&SDU1,
71#else
72 (BaseSequentialStream *)MYSERIAL,
73#endif
74
75 commands
76};
77
78char buf[1024];
79
80#include "fsl_lpuart.h"
81void printf_debug(const char *format, ...)
82{
83 va_list args;
84 va_start(args, format);
85 int n = chvsnprintf(buf, sizeof(buf), format, args);
86 // Directly write to serial instead of using SD4 BaseSequentialStream, because
87 // the latter does not work from within a locked section (e.g. usbStart grabs
88 // a lock).
89 buf[n] = '\r';
90 buf[n+1] = '\n';
91 buf[n+2] = '\0';
92 LPUART_WriteBlocking(LPUART1, (unsigned char*)buf, n+2);
93 va_end(args);
94}
95
96semaphore_t scls;
97
98/*
99 * Application entry point.
100 */
101int main(void) {
102
103 /*
104 * System initializations.
105 * - HAL initialization, this also initializes the configured device drivers
106 * and performs the board-specific initializations.
107 * - Kernel initialization, the main() function becomes a thread and the
108 * RTOS is active.
109 */
110 halInit();
111 chSysInit();
112
113 chSemObjectInit(&scls, 0);
114
115 /*
116 * Activates MYSERIAL with 115200 baud.
117 */
118
119 const SerialConfig sc = {
120 .sc_speed = 115200,
121 };
122 sdStart(MYSERIAL, &sc);
123
124 chprintf((BaseSequentialStream*)MYSERIAL, "ChibiOS Teensy 4.1 demo\r\n");
125
126 /*
127 * Creates the blinker thread.
128 */
129 chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
130
131#if defined(DEMO_MODE_TESTS_TO_SERIAL)
132 test_execute((BaseSequentialStream *)MYSERIAL, &rt_test_suite);
133 test_execute((BaseSequentialStream *)MYSERIAL, &oslib_test_suite);
134#elif defined(DEMO_MODE_SHELL_ON_SERIAL)
135 while (true) {
136 chprintf((BaseSequentialStream*)MYSERIAL, "Starting serial shell\r\n");
137 thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", NORMALPRIO + 1, shellThread, (void *)&shell_cfg1);
138 chThdWait(shelltp); /* Waiting termination. */
139 }
140#elif defined(DEMO_MODE_SHELL_ON_USB_SERIAL)
141 chprintf((BaseSequentialStream*)MYSERIAL, "Starting USB serial\r\n");
142 /*
143 * Initializes a serial-over-USB CDC driver.
144 */
145 sduObjectInit(&SDU1);
146 sduStart(&SDU1, &serusbcfg);
147
148 /*
149 * Activates the USB driver and then the USB bus pull-up on D+.
150 * Note, a delay is inserted in order to not have to disconnect the cable
151 * after a reset.
152 */
153 usbDisconnectBus(serusbcfg.usbp);
154 chThdSleepMilliseconds(1500);
155 usbStart(serusbcfg.usbp, &usbcfg);
156 usbConnectBus(serusbcfg.usbp);
157
158 while (true) {
159 if (SDU1.config->usbp->state == USB_ACTIVE) {
160 // Wait until sduRequestsHook CDC_SET_CONTROL_LINE_STATE happens, then
161 // sleep for a certain time to give the app a chance to configure flags.
162 chSemWait(&scls);
163 chThdSleepMilliseconds(100);
164
165 chprintf((BaseSequentialStream*)MYSERIAL, "Starting serial-over-USB CDC Shell\r\n");
166 thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", NORMALPRIO + 1, shellThread, (void *)&shell_cfg1);
167 chThdWait(shelltp); /* Waiting termination. */
168 }
169 chThdSleepSeconds(1);
170 }
171#else
172#error One of DEMO_MODE_TESTS_TO_SERIAL, DEMO_MODE_SHELL_ON_SERIAL or DEMO_MODE_SHELL_ON_USB_SERIAL must be defined
173#endif
174
175 return 0;
176}