/* * Application entry point. */ int main(void) { /* * System initializations. * - HAL initialization, this also initializes the configured device drivers * and performs the board-specific initializations. * - Kernel initialization, the main() function becomes a thread and the * RTOS is active. */ halInit(); chSysInit(); /* * Activates the serial driver 2 and SDC driver 1 using default * configuration. */ sdStart(&SD6, NULL); BaseSequentialStream *chp = (BaseSequentialStream *)&SD6; chprintf(chp, "running main()\r\n"); chThdSleepMilliseconds(50); #if STM32_USB_USE_OTG2 USBDriver *usb_driver = &USBD2; #else USBDriver *usb_driver = &USBD1; #endif /* * Activates the card insertion monitor. */ init_sd(); chprintf(chp, "done starting SDC\r\n"); const bool_t sdcConnectStatus = sdcConnect(&SDCD1); if( sdcConnectStatus != CH_SUCCESS ) { chprintf(chp, "failed to connect to SD Card, sdcConnectStatus = %u\r\n", sdcConnectStatus); for(;;) { chThdSleepMilliseconds(3000); } } chprintf(chp, "setting up MSD\r\n"); const usb_msd_driver_state_t msd_driver_state = msdInit(usb_driver, (BaseBlockDevice*)&SDCD1, &UMSD1, USB_MS_DATA_EP, USB_MSD_INTERFACE_NUMBER); if( msd_driver_state != USB_MSD_DRIVER_OK ) { chprintf(chp, "Error initing USB MSD, %d %s\r\n", msd_driver_state, usb_msd_driver_state_t_to_str(msd_driver_state)); } UMSD1.chp = chp; chprintf(chp, "Initializing SDU1...\r\n"); serusbcfg.usbp = usb_driver; sduObjectInit(&SDU1); /*Disconnect the USB Bus*/ usbDisconnectBus(usb_driver); chThdSleepMilliseconds(200); /*Start the useful functions*/ sduStart(&SDU1, &serusbcfg); msdStart(&UMSD1); usbStart(usb_driver, &msd_usb_config); /*Connect the USB Bus*/ usbConnectBus(usb_driver); /* * Creates the blinker thread. */ chprintf(chp, "starting blinker thread\r\n"); chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); while (TRUE) { palTogglePad(GPIOC, GPIOC_LED); chThdSleepMilliseconds(500); } }
static msg_t thdUsbStorage(void *arg) { (void) arg; // unused chRegSetThreadName("UsbStorage:polling"); uint antiBounce=5; EventListener connected; // Should use EXTI interrupt instead of active polling, // but in the chibios_opencm3 implementation, since EXTI is // used via libopencm3, ISR are routed on pprz/opencm3 and cannot // be used concurrently by chibios api // Should be fixed when using chibios-rt branch while (!chThdShouldTerminate() && antiBounce) { const bool_t usbConnected = palReadPad (GPIOA, GPIOA_OTG_FS_VBUS); if (usbConnected) antiBounce--; else antiBounce=5; chThdSleepMilliseconds(20); } isRunning = true; chRegSetThreadName("UsbStorage:connected"); /* Stop the logs*/ chibios_logFinish (true); /* connect sdcard sdc interface sdio */ if (sdioConnect () == false) chThdExit (RDY_TIMEOUT); /* initialize the USB mass storage driver */ msdInit(&UMSD1); /* start the USB mass storage service */ msdStart(&UMSD1, &msdConfig); /* start the USB driver */ usbDisconnectBus(&USBD1); chThdSleepMilliseconds(1000); usbStart(&USBD1, &usbConfig); usbConnectBus(&USBD1); /* wait for a real usb storage connexion before shutting down autopilot */ chEvtRegisterMask(&UMSD1.evt_connected, &connected, EVENT_MASK(1)); chEvtWaitOne(EVENT_MASK(1)); /* stop autopilot */ if (pprzThdPtr != NULL) { chThdTerminate (pprzThdPtr); chThdWait (pprzThdPtr); pprzThdPtr = NULL; } /* wait until usb-storage is unmount and usb cable is unplugged*/ while (!chThdShouldTerminate() && palReadPad (GPIOA, GPIOA_OTG_FS_VBUS)) { chThdSleepMilliseconds(10); } /* then close open descriptors and reboot autopilot */ usbDisconnectBus(&USBD1); chThdSleepMilliseconds(500); msdStop(&UMSD1); sdioDisconnect (); MCU_RESTART(); return RDY_OK; }
int main(void) { // copy vector table memcpy((char *)0x20000000, (const char *)&_vectors, 0x200); // remap SRAM1 to 0x00000000 SYSCFG->MEMRMP |= 0x03; /* system & hardware initialization */ halInit(); // float usb inputs, hope the host notices detach... palSetPadMode(GPIOA, 11, PAL_MODE_INPUT); palSetPadMode(GPIOA, 12, PAL_MODE_INPUT); // setup LEDs palSetPadMode(LED1_PORT,LED1_PIN,PAL_MODE_OUTPUT_PUSHPULL); palSetPadMode(LED2_PORT, LED2_PIN, PAL_MODE_OUTPUT_PUSHPULL); palClearPad(LED1_PORT,LED1_PIN); palSetPad(LED2_PORT,LED2_PIN); chSysInit(); palSetPadMode(GPIOA, 11, PAL_MODE_ALTERNATE(10)); palSetPadMode(GPIOA, 12, PAL_MODE_ALTERNATE(10)); palSetPadMode(GPIOC, 8, PAL_MODE_ALTERNATE(12) | PAL_STM32_OSPEED_HIGHEST); palSetPadMode(GPIOC, 9, PAL_MODE_ALTERNATE(12) | PAL_STM32_OSPEED_HIGHEST); palSetPadMode(GPIOC, 10, PAL_MODE_ALTERNATE(12) | PAL_STM32_OSPEED_HIGHEST); palSetPadMode(GPIOC, 11, PAL_MODE_ALTERNATE(12) | PAL_STM32_OSPEED_HIGHEST); palSetPadMode(GPIOC, 12, PAL_MODE_ALTERNATE(12) | PAL_STM32_OSPEED_HIGHEST); palSetPadMode(GPIOD, 2, PAL_MODE_ALTERNATE(12) | PAL_STM32_OSPEED_HIGHEST); chThdSleepMilliseconds(50); /* initialize the SD card */ sdcStart(&SDCD1, NULL); sdcConnect(&SDCD1); /* initialize the USB mass storage driver */ msdInit(&UMSD1); /* turn off green LED, turn on red LED */ palSetPadMode(LED1_PORT, LED1_PIN, PAL_MODE_OUTPUT_PUSHPULL); palClearPad(LED1_PORT, LED1_PIN); palSetPadMode(LED2_PORT, LED2_PIN, PAL_MODE_OUTPUT_PUSHPULL); palSetPad(LED2_PORT, LED2_PIN); /* start the USB mass storage service */ int ret = msdStart(&UMSD1, &msdConfig); if (ret != 0) { /* no media found : bye bye !*/ usbDisconnectBus(&USBD1); chThdSleepMilliseconds(1000); NVIC_SystemReset(); } /* watch the mass storage events */ EventListener connected; EventListener ejected; chEvtRegisterMask(&UMSD1.evt_connected, &connected, EVENT_MASK(1)); chEvtRegisterMask(&UMSD1.evt_ejected, &ejected, EVENT_MASK(2)); /* start the USB driver */ usbDisconnectBus(&USBD1); chThdSleepMilliseconds(1000); usbStart(&USBD1, &usbConfig); usbConnectBus(&USBD1); while (TRUE) { eventmask_t event = chEvtWaitOne(EVENT_MASK(1) | EVENT_MASK(2)); if (event == EVENT_MASK(1)) { /* media connected */ } else if (event == EVENT_MASK(2)) { /* media ejected : bye bye !*/ usbDisconnectBus(&USBD1); chThdSleepMilliseconds(1000); NVIC_SystemReset(); } } return 0; }