static void deinit(vfs_dev_t *dev) { proto_deinit(dev); usb_deinit(dev); free(dev->scanline_buf); }
int main(int argc, char **argv) { signal(SIGHUP, sighandler); signal(SIGINT, sighandler); signal(SIGTERM, sighandler); signal(SIGPIPE, SIG_IGN); // Setting default file permissions to o+rw umask(~(S_IRUSR | S_IWUSR)); // Turning off buffering setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); processcommandline(argc, argv); if (!config_init(configfilename)) return 1; daemon_poll_init(); if (!usb_init()) return 2; while (!nai_flags.sigexit) { if (!usb_process()) break; if (!daemon_poll_process()) break; if (!nai_process()) break; } usb_deinit(); daemon_poll_deinit(); config_deinit(); printf("main: exiting.\n"); return 0; }
/* Jump to the internal STM32 bootloader. The way this works is that we * set a magic number in memory that our startup code looks for (see startup.s). * RAM is preserved across system reset, so when it finds this magic number, it will go * to the bootloader code rather than the application code. */ void jump_to_bootloader(void){ uint16_t i; volatile uint32_t delayCounter; /* Disable USB in advance: this will give the computer time to * recognise it's been disconnected, so when the system bootloader * comes online it will get re-enumerated. */ usb_deinit(); /* Disable all interrupts */ RCC->CIR = 0x00000000; /* Blink LEDs */ setLed(RED); for(i = 0; i < 3; i++) { for(delayCounter = 0; delayCounter < 2000000; delayCounter++); setLed(NONE); for(delayCounter = 0; delayCounter < 2000000; delayCounter++); setLed(RED); } dfu_reboot(); }
/** * \brief SAMD21 WINO SAM-BA Main loop. * \return Unused (ANSI-C compatibility). */ int main(void) { /* Save the value of the boot key memory before it is overwritten */ uint32_t bootKeyPtrVal = *bootKeyPtr; *bootKeyPtr = 0; P_USB_CDC pCdc; DEBUG_PIN_HIGH; volatile PortGroup *esp8266_port = (volatile PortGroup *)(&(PORT->Group[ESP8266_CH_PD_PIN / 32])); /* Enable the ouput on the ESP8266 power down Pin and drive it to low */ esp8266_port->DIRSET.reg = ESP8266_CH_PD_PIN_MASK; esp8266_port->PINCFG[ESP8266_CH_PD_PIN & 0x1F].reg = PORT_PINCFG_INEN; esp8266_port->OUTCLR.reg = ESP8266_CH_PD_PIN_MASK; /* Enable the ouput on the LED Pin and drive it to high */ PORT->Group[LED_PORT].DIRSET.reg = LED_PIN_MASK; LED_ON; /* System initialization */ system_init(); cpu_irq_enable(); usart_open(); pCdc = (P_USB_CDC)usb_init(); DEBUG_USART_OPEN; /* Pointer to store application start address */ uint32_t *data_ptr = (uint32_t *)0x20000000; /* Store the application start address @0x20000000 */ *data_ptr = APP_START_ADDRESS; /* Jump in application if condition is satisfied */ static uint32_t app_start_address; /* Load the Reset Handler address of the application */ app_start_address = *(uint32_t *)(APP_START_ADDRESS + 4); int waiting = WAIT_SHORT; int elapsed; /* Test reset vector of application @APP_START_ADDRESS+4 * If *(APP_START+0x4) == 0xFFFFFFFF then set waiting * Application erased condition */ if (app_start_address == 0xFFFFFFFF) { waiting = -1; } else if (bootKeyPtrVal == bootKey) { waiting = WAIT_LONG; } DEBUG_PIN_LOW; *bootKeyPtr = bootKey; /* Wait for a complete enum on usb or a '#' char on serial line. * If there is no user application in the device's memory, * this loop will not end, in the other case, it will wait until timeout. */ while (waiting) { if (pCdc->IsConfigured(pCdc)) { sam_ba_monitor_init(SAM_BA_INTERFACE_USBCDC); main_b_cdc_enable = true; break; } if (usart_sharp_received()) { sam_ba_monitor_init(SAM_BA_INTERFACE_USART); break; } if (PORT->Group[LED_PORT].OUT.reg == LED_PIN_MASK) { if (elapsed > 3000) { LED_OFF; elapsed = 0; } } else { if (elapsed > 30000) { LED_ON; elapsed = 0; } } if (waiting > 0) waiting--; elapsed++; } *bootKeyPtr = 0; while (waiting) { uint32_t tmp = do_sam_ba_monitor(); if (PORT->Group[LED_PORT].OUT.reg == LED_PIN_MASK) { if (elapsed > (1 ? 5000 : 1)) { LED_OFF; elapsed = 0; } } else { if (elapsed > (1 ? 50000 : 2)) { LED_ON; elapsed = 0; } } if (waiting > 0) { /* If there is no activity, decrement timeout */ /* Else set WAIT_SHORT timeout */ if (tmp == 0) waiting--; else waiting = WAIT_SHORT; } elapsed++; } /* Reset LED pin */ LED_OFF; PORT->Group[LED_PORT].DIRCLR.reg = LED_PIN_MASK; usart_close(); usb_deinit(); // reset clocks //SYSCTRL->OSC8M.reg = SYSCTRL_OSC8M_RESETVALUE; //SYSCTRL->OSC32K.reg = SYSCTRL_OSC32K_RESETVALUE; //SYSCTRL->XOSC32K.reg = SYSCTRL_XOSC32K_RESETVALUE; //SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_RESETVALUE; //SYSCTRL->DFLLMUL.reg = SYSCTRL_DFLLMUL_RESETVALUE; //SYSCTRL->DFLLVAL.reg = SYSCTRL_DFLLVAL_RESETVALUE; /* Jump to the application */ /* Rebase the Stack Pointer */ __set_MSP(*(uint32_t *) APP_START_ADDRESS); /* Rebase the vector table base address */ SCB->VTOR = ((uint32_t) APP_START_ADDRESS & SCB_VTOR_TBLOFF_Msk); /* Jump to application Reset Handler in the application */ asm("bx %0"::"r"(app_start_address)); }