/* * * mur.sat * * Somewhen in the year 2012, mur.at will have a nano satellite launched * into a low earth orbit (310 km above the surface of our planet). The * satellite itself is a TubeSat personal satellite kit, developed and * launched by interorbital systems. mur.sat is a joint venture of mur.at, * ESC im Labor and realraum. * * Please visit the project hompage at sat.mur.at for further information. * * * Copyright (C) 2013 Christian Pointner * * This file is part of mur.sat. * * mur.sat is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * mur.sat is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with mur.sat. If not, see . * */ // this heavily leans on the USB-CDC testhal Demo from chibiOS /* ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include #include #include "ch.h" #include "hal.h" #include "test.h" #include "shell.h" #include "chprintf.h" #include "usb-cdc-shell.h" #include "usb-cdc-descriptor.h" static SerialUSBDriver SDU1; /* * Handles the GET_DESCRIPTOR callback. All required descriptors must be * handled here. */ static const USBDescriptor *get_descriptor(USBDriver *usbp, uint8_t dtype, uint8_t dindex, uint16_t lang) { (void)usbp; (void)lang; switch (dtype) { case USB_DESCRIPTOR_DEVICE: return &vcom_device_descriptor; case USB_DESCRIPTOR_CONFIGURATION: return &vcom_configuration_descriptor; case USB_DESCRIPTOR_STRING: if (dindex < 4) return &vcom_strings[dindex]; } return NULL; } static USBInEndpointState ep1instate; static USBOutEndpointState ep1outstate; static const USBEndpointConfig ep1config = { USB_EP_MODE_TYPE_BULK, NULL, sduDataTransmitted, sduDataReceived, 0x0040, 0x0040, &ep1instate, &ep1outstate, 1, NULL }; static USBInEndpointState ep2instate; static const USBEndpointConfig ep2config = { USB_EP_MODE_TYPE_INTR, NULL, sduInterruptTransmitted, NULL, 0x0010, 0x0000, &ep2instate, NULL, 1, NULL }; /* * Handles the USB driver global events. */ static void usb_event(USBDriver *usbp, usbevent_t event) { switch (event) { case USB_EVENT_RESET: return; case USB_EVENT_ADDRESS: return; case USB_EVENT_CONFIGURED: chSysLockFromIsr(); /* Enables the endpoints specified into the configuration. Note, this callback is invoked from an ISR so I-Class functions must be used.*/ usbInitEndpointI(usbp, USBD1_DATA_REQUEST_EP, &ep1config); usbInitEndpointI(usbp, USBD1_INTERRUPT_REQUEST_EP, &ep2config); /* Resetting the state of the CDC subsystem.*/ sduConfigureHookI(&SDU1); chSysUnlockFromIsr(); return; case USB_EVENT_SUSPEND: return; case USB_EVENT_WAKEUP: return; case USB_EVENT_STALLED: return; } return; } /* * USB driver configuration. */ static const USBConfig usbcfg = { usb_event, get_descriptor, sduRequestsHook, NULL }; /* * Serial over USB driver configuration. */ static const SerialUSBConfig serusbcfg = { &USBD1, USBD1_DATA_REQUEST_EP, USBD1_DATA_AVAILABLE_EP, USBD1_INTERRUPT_REQUEST_EP }; /*===========================================================================*/ /* Command line related. */ /*===========================================================================*/ static int8_t shell_return_code; #define SHELL_WA_SIZE THD_WA_SIZE(2048) #define TEST_WA_SIZE THD_WA_SIZE(256) static void cmd_mem(BaseSequentialStream *chp, int argc, char *argv[]) { size_t n, size; (void)argv; if (argc > 0) { chprintf(chp, "Usage: mem\r\n"); return; } n = chHeapStatus(NULL, &size); chprintf(chp, "core free memory : %u bytes\r\n", chCoreStatus()); chprintf(chp, "heap fragments : %u\r\n", n); chprintf(chp, "heap free total : %u bytes\r\n", size); } static void cmd_threads(BaseSequentialStream *chp, int argc, char *argv[]) { static const char *states[] = {THD_STATE_NAMES}; Thread *tp; (void)argv; if (argc > 0) { chprintf(chp, "Usage: threads\r\n"); return; } chprintf(chp, " addr stack prio refs state time name\r\n"); tp = chRegFirstThread(); do { chprintf(chp, "%.8lx %.8lx %4lu %4lu %9s %8lu %s\r\n", (uint32_t)tp, (uint32_t)tp->p_ctx.r13, (uint32_t)tp->p_prio, (uint32_t)(tp->p_refs - 1), states[tp->p_state], (uint32_t)tp->p_time, (tp->p_name == NULL) ? "-" : tp->p_name); tp = chRegNextThread(tp); } while (tp != NULL); } static void cmd_test(BaseSequentialStream *chp, int argc, char *argv[]) { Thread *tp; (void)argv; if (argc > 0) { chprintf(chp, "Usage: test\r\n"); return; } tp = chThdCreateFromHeap(NULL, TEST_WA_SIZE, chThdGetPriority(), TestThread, chp); if (tp == NULL) { chprintf(chp, "out of memory\r\n"); return; } chThdWait(tp); } static void cmd_flash(BaseSequentialStream *chp, int argc, char *argv[]) { (void)argv; if (argc != 1) { chprintf(chp, "Usage: flash (ihu|ctr|bat)\r\n"); return; } chprintf(chp, "connecting to bootloader of '%s' .. ", argv[0]); if(!strcmp("ihu", argv[0])) { // TODO: prepare IHU chprintf(chp, "done\r\n"); } else if(!strcmp("ctr",argv[0])) { // TODO: prepare IHU chprintf(chp, "done\r\n"); } else if(!strcmp("bat", argv[0])) { // TODO: prepare IHU chprintf(chp, "done\r\n"); } else { chprintf(chp, "error: target unknown\r\n"); return; } chprintf(chp, " after downloading and flashing has been finished\r\n", argv[0]); chprintf(chp, " disconnect and reconnect the USB cable to restart shell\r\n", argv[0]); } static void cmd_halt(BaseSequentialStream *chp, int argc, char *argv[]) { (void)argv; if (argc > 0) { chprintf(chp, "Usage: halt\r\n"); return; } shell_return_code = MPU_HALT; chprintf(chp, "halting system.\r\n"); shellExit(RDY_OK); } static void cmd_reboot(BaseSequentialStream *chp, int argc, char *argv[]) { (void)argv; if (argc > 0) { chprintf(chp, "Usage: reboot\r\n"); return; } shell_return_code = MPU_REBOOT; chprintf(chp, "rebooting system ...\r\n"); shellExit(RDY_OK); } static void cmd_bootloader(BaseSequentialStream *chp, int argc, char *argv[]) { (void)argv; if (argc > 0) { chprintf(chp, "Usage: bootloader\r\n"); return; } shell_return_code = MPU_BOOTLOADER; chprintf(chp, "rebooting to bootloader ... ignored - not implemented!!\r\n"); /* shellExit(RDY_OK); */ } static const ShellCommand commands[] = { {"mem", cmd_mem}, {"threads", cmd_threads}, {"test", cmd_test}, {"flash", cmd_flash}, {"halt", cmd_halt}, {"reboot", cmd_reboot}, {"bootloader", cmd_bootloader}, {NULL, NULL} }; static const ShellConfig shell_cfg1 = { (BaseSequentialStream *)&SDU1, commands }; /*===========================================================================*/ /* Public Interface. */ /*===========================================================================*/ int8_t usbCDCShellRun(void) { sduObjectInit(&SDU1); sduStart(&SDU1, &serusbcfg); palSetPad(GPIOA, GPIOA_USB_DISC); usbDisconnectBus(serusbcfg.usbp); chThdSleepMilliseconds(100); usbStart(serusbcfg.usbp, &usbcfg); usbConnectBus(serusbcfg.usbp); palClearPad(GPIOA, GPIOA_USB_DISC); shellInit(); shell_return_code = MPU_CONTINUE; Thread *shelltp = NULL; while (TRUE) { if (!shelltp && (SDU1.config->usbp->state == USB_ACTIVE)) shelltp = shellCreate(&shell_cfg1, SHELL_WA_SIZE, NORMALPRIO); else if (chThdTerminated(shelltp)) { chThdRelease(shelltp); /* Recovers memory of the previous shell. */ shelltp = NULL; /* Triggers spawning of a new shell. */ if (shell_return_code != MPU_CONTINUE) break; } chThdSleepMilliseconds(1000); } usbStop(serusbcfg.usbp); sduStop(&SDU1); palSetPad(GPIOA, GPIOA_USB_DISC); return shell_return_code; }