From b6636284af526f42cf108374b2b156ad516d103d Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Fri, 26 Mar 2021 12:33:58 +0100 Subject: [PATCH] demos/RT-TEENSY4_1: enable USB shell / tests in demo --- demos/MIMXRT1062/RT-TEENSY4_1/Makefile | 6 +- demos/MIMXRT1062/RT-TEENSY4_1/halconf.h | 4 +- demos/MIMXRT1062/RT-TEENSY4_1/main.c | 109 ++++++++++++++++++++++-- demos/MIMXRT1062/RT-TEENSY4_1/mcuconf.h | 71 +-------------- 4 files changed, 113 insertions(+), 77 deletions(-) diff --git a/demos/MIMXRT1062/RT-TEENSY4_1/Makefile b/demos/MIMXRT1062/RT-TEENSY4_1/Makefile index fbe64db3..f63b5f08 100644 --- a/demos/MIMXRT1062/RT-TEENSY4_1/Makefile +++ b/demos/MIMXRT1062/RT-TEENSY4_1/Makefile @@ -104,6 +104,10 @@ include $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/port_v7m.mk include $(CHIBIOS)/test/lib/test.mk include $(CHIBIOS)/test/rt/rt_test.mk include $(CHIBIOS)/test/oslib/oslib_test.mk +# for printf +include $(CHIBIOS)/os/hal/lib/streams/streams.mk +include $(CHIBIOS)/os/various/shell/shell.mk + # Define linker script file here LDSCRIPT= $(STARTUPLD_CONTRIB)/MIMXRT1062.ld @@ -112,7 +116,7 @@ LDSCRIPT= $(STARTUPLD_CONTRIB)/MIMXRT1062.ld # setting. CSRC = $(ALLCSRC) \ $(TESTSRC) \ - main.c + main.c usbcfg.c $(CHIBIOS)/os/various/syscalls.c # C++ sources that can be compiled in ARM or THUMB mode depending on the global # setting. diff --git a/demos/MIMXRT1062/RT-TEENSY4_1/halconf.h b/demos/MIMXRT1062/RT-TEENSY4_1/halconf.h index 60587e3b..ab2e9a7b 100644 --- a/demos/MIMXRT1062/RT-TEENSY4_1/halconf.h +++ b/demos/MIMXRT1062/RT-TEENSY4_1/halconf.h @@ -149,7 +149,7 @@ * @brief Enables the SERIAL over USB subsystem. */ #if !defined(HAL_USE_SERIAL_USB) || defined(__DOXYGEN__) -#define HAL_USE_SERIAL_USB FALSE +#define HAL_USE_SERIAL_USB TRUE #endif /** @@ -184,7 +184,7 @@ * @brief Enables the USB subsystem. */ #if !defined(HAL_USE_USB) || defined(__DOXYGEN__) -#define HAL_USE_USB FALSE +#define HAL_USE_USB TRUE #endif /** diff --git a/demos/MIMXRT1062/RT-TEENSY4_1/main.c b/demos/MIMXRT1062/RT-TEENSY4_1/main.c index f4c23af0..9dac38aa 100644 --- a/demos/MIMXRT1062/RT-TEENSY4_1/main.c +++ b/demos/MIMXRT1062/RT-TEENSY4_1/main.c @@ -19,6 +19,32 @@ #include "ch_test.h" #include "rt_test_root.h" #include "oslib_test_root.h" +#include "chprintf.h" +#include "shell.h" +#include + +#include "usbcfg.h" + +/* This demo can be customized in scope for easier debugging. + * + * Define one of the following: */ + +/* Run rt and oslib test suite to serial console: */ +//#define DEMO_MODE_TESTS_TO_SERIAL + +/* Provide an interactive shell on serial console. + * You can run the tests using the test command: */ +//#define DEMO_MODE_SHELL_ON_SERIAL + +/* Provide an interactive shell on serial console over USB. + * This mode does not require any extra serial hardware: */ +#define DEMO_MODE_SHELL_ON_USB_SERIAL + +/* + * Serial 1 (LPUART1) corresponds to Pin 24 on the Teensy 4.1, or to the built-in + * usb-to-serial on the debug probe of the MIMXRT1060-EVK. + */ +#define MYSERIAL &SD1 /* * LED blinker thread. @@ -30,10 +56,42 @@ static THD_FUNCTION(Thread1, arg) { chRegSetThreadName("LEDBlinker"); while (true) { palTogglePad(TEENSY_PIN13_IOPORT, TEENSY_PIN13); - chThdSleepMilliseconds(500); + chThdSleepSeconds(1); } } +#define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048) + +static const ShellCommand commands[] = {{NULL, NULL}}; + +static const ShellConfig shell_cfg1 = { +#ifdef DEMO_MODE_SHELL_ON_USB_SERIAL + (BaseSequentialStream *)&SDU1, +#else + (BaseSequentialStream *)MYSERIAL, +#endif + + commands +}; + +char buf[1024]; + +#include "fsl_lpuart.h" +void printf_debug(const char *format, ...) +{ + va_list args; + va_start(args, format); + int n = chvsnprintf(buf, sizeof(buf), format, args); + // Directly write to serial instead of using SD4 BaseSequentialStream, because + // the latter does not work from within a locked section (e.g. usbStart grabs + // a lock). + buf[n] = '\r'; + buf[n+1] = '\n'; + buf[n+2] = '\0'; + LPUART_WriteBlocking(LPUART1, (unsigned char*)buf, n+2); + va_end(args); +} + /* * Application entry point. */ @@ -50,20 +108,59 @@ int main(void) { chSysInit(); /* - * Activates serial 1 (UART0) using the driver default configuration. + * Activates MYSERIAL with 115200 baud. */ - sdStart(&SD1, NULL); + + const SerialConfig sc = { + .sc_speed = 115200, + }; + sdStart(MYSERIAL, &sc); + + chprintf((BaseSequentialStream*)MYSERIAL, "ChibiOS Teensy 4.1 demo\r\n"); /* * Creates the blinker thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); - test_execute((BaseSequentialStream *)&SD1, &rt_test_suite); - test_execute((BaseSequentialStream *)&SD1, &oslib_test_suite); +#if defined(DEMO_MODE_TESTS_TO_SERIAL) + test_execute((BaseSequentialStream *)MYSERIAL, &rt_test_suite); + test_execute((BaseSequentialStream *)MYSERIAL, &oslib_test_suite); +#elif defined(DEMO_MODE_SHELL_ON_SERIAL) while (true) { - chThdSleepMilliseconds(1000); + chprintf((BaseSequentialStream*)MYSERIAL, "Starting serial shell\r\n"); + thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", NORMALPRIO + 1, shellThread, (void *)&shell_cfg1); + chThdWait(shelltp); /* Waiting termination. */ } +#elif defined(DEMO_MODE_SHELL_ON_USB_SERIAL) + chprintf((BaseSequentialStream*)MYSERIAL, "Starting USB serial\r\n"); + /* + * Initializes a serial-over-USB CDC driver. + */ + sduObjectInit(&SDU1); + sduStart(&SDU1, &serusbcfg); + + /* + * Activates the USB driver and then the USB bus pull-up on D+. + * Note, a delay is inserted in order to not have to disconnect the cable + * after a reset. + */ + usbDisconnectBus(serusbcfg.usbp); + chThdSleepMilliseconds(1500); + usbStart(serusbcfg.usbp, &usbcfg); + usbConnectBus(serusbcfg.usbp); + + while (true) { + if (SDU1.config->usbp->state == USB_ACTIVE) { + chprintf((BaseSequentialStream*)MYSERIAL, "Starting serial-over-USB CDC Shell\r\n"); + thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", NORMALPRIO + 1, shellThread, (void *)&shell_cfg1); + chThdWait(shelltp); /* Waiting termination. */ + } + chThdSleepSeconds(1); + } +#else +#error One of DEMO_MODE_TESTS_TO_SERIAL, DEMO_MODE_SHELL_ON_SERIAL or DEMO_MODE_SHELL_ON_USB_SERIAL must be defined +#endif return 0; } diff --git a/demos/MIMXRT1062/RT-TEENSY4_1/mcuconf.h b/demos/MIMXRT1062/RT-TEENSY4_1/mcuconf.h index 8712b157..1b4c9c64 100644 --- a/demos/MIMXRT1062/RT-TEENSY4_1/mcuconf.h +++ b/demos/MIMXRT1062/RT-TEENSY4_1/mcuconf.h @@ -19,73 +19,8 @@ #define MIMXRT1062_MCUCONF -/* - * HAL driver system settings. - */ - -/* Select the MCU clocking mode below by enabling the appropriate block. */ - -/* PEE mode - 180 MHz system clock driving by 16 MHz xtal */ -#if 1 -#define KINETIS_MCG_MODE KINETIS_MCG_MODE_PEE -#define KINETIS_PLLCLK_FREQUENCY 180000000UL -#define KINETIS_SYSCLK_FREQUENCY KINETIS_PLLCLK_FREQUENCY -#define KINETIS_BUSCLK_FREQUENCY 60000000UL -#define KINETIS_FLASHCLK_FREQUENCY 28000000UL - -#define KINETIS_CLKDIV1_OUTDIV1 1 // -> 0 -#define KINETIS_CLKDIV1_OUTDIV2 3 // -> 2 -#define KINETIS_CLKDIV1_OUTDIV4 7 // -> 6 -#endif - -/* PEE mode - 48MHz system clock driven by external crystal. */ -#if 0 -#define KINETIS_MCG_MODE KINETIS_MCG_MODE_PEE -#define KINETIS_PLLCLK_FREQUENCY 96000000UL -#define KINETIS_SYSCLK_FREQUENCY 48000000UL -#endif - -/* FEI mode (~48MHz) */ -#if 0 -#define KINETIS_MCG_MODE KINETIS_MCG_MODE_FEI -#define KINETIS_MCG_FLL_DMX32 1 /* Fine-tune for 32.768 kHz */ -#define KINETIS_MCG_FLL_DRS 1 /* 1464x FLL factor */ -#define KINETIS_SYSCLK_FREQUENCY 47972352UL /* 32.768 kHz * 1464 (~48 MHz) */ -#define KINETIS_CLKDIV1_OUTDIV1 1 /* Divide MCGCLKOUT (~48MHz) by 1 to SYSCLK */ -#define KINETIS_CLKDIV1_OUTDIV2 1 /* Divide by 1 for (~48MHz) peripheral clock */ -#define KINETIS_CLKDIV1_OUTDIV4 2 /* Divide by 2 for (~24MHz) flash clock */ -#define KINETIS_BUSCLK_FREQUENCY KINETIS_SYSCLK_FREQUENCY -#define KINETIS_FLASHCLK_FREQUENCY KINETIS_SYSCLK_FREQUENCY/2 -#endif /* 0 */ - -/* FEE mode - 24 MHz with external 32.768 kHz crystal */ -/* not implemented */ -#if 0 -#define KINETIS_MCG_MODE KINETIS_MCG_MODE_FEE -#define KINETIS_MCG_FLL_DMX32 1 /* Fine-tune for 32.768 kHz */ -#define KINETIS_MCG_FLL_DRS 0 /* 732x FLL factor */ -#define KINETIS_CLKDIV1_OUTDIV1 1 /* Divide 48 MHz FLL by 1 => 24 MHz */ -#define KINETIS_CLKDIV1_OUTDIV4 2 /* Divide OUTDIV1 output by 2 => 12 MHz */ -#define KINETIS_SYSCLK_FREQUENCY 23986176UL /* 32.768 kHz*732 (~24 MHz) */ -#define KINETIS_UART0_CLOCK_FREQ (32768 * 732) /* FLL output */ -#define KINETIS_UART0_CLOCK_SRC 1 /* Select FLL clock */ -#define KINETIS_BUSCLK_FREQUENCY (KINETIS_SYSCLK_FREQUENCY / KINETIS_CLKDIV1_OUTDIV4) -#endif /* 0 */ - -/* FEE mode - 48 MHz */ -/* not implemented */ -#if 0 -#define KINETIS_MCG_MODE KINETIS_MCG_MODE_FEE -#define KINETIS_MCG_FLL_DMX32 1 /* Fine-tune for 32.768 kHz */ -#define KINETIS_MCG_FLL_DRS 1 /* 1464x FLL factor */ -#define KINETIS_CLKDIV1_OUTDIV1 1 /* Divide 48 MHz FLL by 1 => 48 MHz */ -#define KINETIS_CLKDIV1_OUTDIV4 2 /* Divide OUTDIV1 output by 2 => 24 MHz */ -#define KINETIS_SYSCLK_FREQUENCY 47972352UL /* 32.768 kHz * 1464 (~48 MHz) */ -#endif /* 0 */ - -/* - * SERIAL driver system settings. - */ -#define KINETIS_SERIAL_USE_UART0 TRUE +/* The NXP USB stack uses more stack space than the default 256 byte of thread + * working area for the test command can fit, so make some more room: */ +#define SHELL_CMD_TEST_WA_SIZE THD_WORKING_AREA_SIZE(1024) #endif /* _MCUCONF_H_ */