/* * * mur.sat * * Somewhen in the year 20xx, 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-2015 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 #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" #include "board-utils.h" #include "utils.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 const SDCConfig sdccfg = { 0 }; static char* blkstate_to_string(blkstate_t state) { switch(state) { case BLK_UNINIT: return "Not initialized."; case BLK_STOP: return "Stopped."; case BLK_ACTIVE: return "Interface active."; case BLK_CONNECTING: return "Connection in progress."; case BLK_DISCONNECTING: return "Disconnection in progress."; case BLK_READY: return "Device ready."; case BLK_READING: return "Read operation in progress."; case BLK_WRITING: return "Write operation in progress."; case BLK_SYNCING: return "Sync. operation in progress. "; } return "unkown"; } static void sdc_read_test(BaseSequentialStream *chp, const uint32_t sector) { chprintf(chp, "Running SDCard Read Test\r\n"); chprintf(chp, "------------------------\r\n\r\n"); unsigned int n; chprintf(chp, "waiting for card ."); for(n = 0; n < 20; n++) { if(blkIsInserted(&SDCD1)) { chprintf(chp, " found\r\n"); chprintf(chp, "write protection: %s\r\n", blkIsWriteProtected(&SDCD1) ? "ON" : "OFF"); // Connect if(blkConnect(&SDCD1) == CH_SUCCESS) { chprintf(chp, "connecting ..."); while(blkGetDriverState(&SDCD1) == BLK_CONNECTING) { chThdSleepMilliseconds(100); chprintf(chp, "."); } if(blkGetDriverState(&SDCD1) != BLK_READY) { chprintf(chp, "error.\r\nERROR driver not ready (state now: %s)\r\n", blkstate_to_string((blkGetDriverState(&SDCD1)))); return; } chprintf(chp, "done.\r\n"); BlockDeviceInfo bi; if(blkGetInfo(&SDCD1, &bi) == CH_FAILED) { chprintf(chp, "ERROR: reading device info\r\n"); return; } chprintf(chp, "Device: blocksize=%d, blocknum=%d\r\n", bi.blk_size, bi.blk_num); if(bi.blk_num <= sector) { chprintf(chp, "Requested sector (%d) is after end of device\r\n", sector); return; } // Read chprintf(chp, "\r\nReading Block %ld: \r\n", sector); uint8_t buf[1024]; if(sizeof(buf) < bi.blk_size) { chprintf(chp, "Your read buffer ist too small - aborting...\r\n"); return; } memset(buf, 0, sizeof(buf)); if(blkRead(&SDCD1, sector, buf, 1) == CH_FAILED) { chprintf(chp, "ERROR: blkRead returned with error\r\n"); return; } uint32_t i; for(i=0; i\r\n"); return; } sdcStart(&SDCD1, &sdccfg); sdc_read_test(chp, strtoul(argv[0], NULL, 10)); sdcStop(&SDCD1); } static void sdc_write_test(BaseSequentialStream *chp, const uint32_t sector) { chprintf(chp, "Running SDCard Write Test\r\n"); chprintf(chp, "-------------------------\r\n\r\n"); unsigned int n; chprintf(chp, "waiting for card ."); for(n = 0; n < 20; n++) { if(blkIsInserted(&SDCD1)) { chprintf(chp, " found\r\n"); chprintf(chp, "write protection: %s\r\n", blkIsWriteProtected(&SDCD1) ? "ON" : "OFF"); if(blkIsWriteProtected(&SDCD1)) { chprintf(chp, "aborting due to write protection!\r\n"); return; } // Connect if(blkConnect(&SDCD1) == CH_SUCCESS) { chprintf(chp, "connecting ..."); while(blkGetDriverState(&SDCD1) == BLK_CONNECTING) { chThdSleepMilliseconds(100); chprintf(chp, "."); } if(blkGetDriverState(&SDCD1) != BLK_READY) { chprintf(chp, "error.\r\nERROR driver not ready (state now: %s)\r\n", blkstate_to_string((blkGetDriverState(&SDCD1)))); return; } chprintf(chp, "done.\r\n"); BlockDeviceInfo bi; if(blkGetInfo(&SDCD1, &bi) == CH_FAILED) { chprintf(chp, "ERROR: reading device info\r\n"); return; } chprintf(chp, "Device: blocksize=%d, blocknum=%d\r\n", bi.blk_size, bi.blk_num); if(bi.blk_num <= sector) { chprintf(chp, "Requested sector (%d) is after end of device\r\n", sector); return; } // Write chprintf(chp, "\r\nWriting Block %ld: \r\n", sector); uint8_t buf[1024]; if(sizeof(buf) < bi.blk_size) { chprintf(chp, "Your write buffer ist too small - aborting...\r\n"); return; } uint32_t i; for(i=0; i\r\n"); return; } sdcStart(&SDCD1, &sdccfg); sdc_write_test(chp, strtoul(argv[0], NULL, 10)); sdcStop(&SDCD1); } 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 CTR chprintf(chp, "done\r\n"); } else if(!strcmp("bat", argv[0])) { // TODO: prepare BAT 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[] = { {"sdcr", cmd_sdcr}, {"sdcw", cmd_sdcw}, {"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); usbStart(serusbcfg.usbp, &usbcfg); usbConnect(serusbcfg.usbp); 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); } usbDisconnect(serusbcfg.usbp); usbStop(serusbcfg.usbp); sduStop(&SDU1); return shell_return_code; }