/* function called when cmd_bootloader is parsed successfully */ static void cmd_bootloader_parsed(void *parsed_result, void *data) { #ifndef HOST_VERSION bootloader(); #else printf("not implemented\n"); #endif }
// Bootloader entry logic // void bl_main(void) { uint8_t i; // Do early hardware init hardware_init(); // Work out why we reset // // Note that if PORSF (bit 1) is set, all other bits are invalid. reset_source = RSTSRC; if (reset_source & (1 << 1)) reset_source = 1 << 1; // Check for app validity app_valid = flash_app_valid(); // Do some simple debouncing on the bootloader-entry // strap/button. debounce_count = 0; for (i = 0; i < 255; i++) { if (BUTTON_BOOTLOAD == BUTTON_ACTIVE) debounce_count++; } // Turn on the LED to indicate the bootloader is running LED_BOOTLOADER = LED_ON; // Boot the application if: // // - The reset was not due to a flash error (the app uses this to reboot // into the bootloader) // - The signature is valid. // - The boot-to-bootloader strap/button is not in the active state. // if (!(reset_source & (1 << 6)) && app_valid) { // The button was not entirely pressed - play it safe // and boot the app. // if (debounce_count < 200) { // Stash board info in SFRs for the application to find later // BOARD_FREQUENCY_REG = board_frequency; BOARD_BL_VERSION_REG = BL_VERSION; // And jump ((void (__code *)(void))FLASH_APP_START)(); } } // Bootloader loop // for (;;) bootloader(); }
int main(void) { unsigned timeout = 0; /* do board-specific initialisation */ board_init(); #ifdef INTERFACE_USART /* XXX sniff for a USART connection to decide whether to wait in the bootloader? */ timeout = BOOTLOADER_DELAY; #endif #ifdef INTERFACE_I2C # error I2C bootloader detection logic not implemented #endif /* if the app left a cookie saying we should wait, then wait */ if (should_wait()) timeout = BOOTLOADER_DELAY; #ifdef BOARD_FORCE_BL_PIN /* if the force-BL pin state matches the state of the pin, wait in the bootloader forever */ if (BOARD_FORCE_BL_VALUE == gpio_get(BOARD_FORCE_BL_PORT, BOARD_FORCE_BL_PIN)) timeout = 0xffffffff; #endif /* look for the magic wait-in-bootloader value in backup register zero */ /* if we aren't expected to wait in the bootloader, try to boot immediately */ if (timeout == 0) { /* try to boot immediately */ jump_to_app(); /* if we returned, there is no app; go to the bootloader and stay there */ timeout = 0; } /* configure the clock for bootloader activity */ rcc_clock_setup_in_hsi_out_24mhz(); /* start the interface */ cinit(BOARD_INTERFACE_CONFIG); while (1) { /* run the bootloader, possibly coming back after the timeout */ bootloader(timeout); /* look to see if we can boot the app */ jump_to_app(); /* boot failed; stay in the bootloader forever next time */ timeout = 0; } }
void main() { setup_adc(ADC_OFF); if (input(BOOTLOADER_PIN) && !input(BOOTLOADER_PIN_BIS)) { output_high(LED1); output_high(LED2); bootloader(); } #asm goto RESET_VECTOR #endasm }
// De-init all the peripherical, // and launch the Nordic USB bootloader located @ 0x7800 void launchBootloader() { void (*bootloader)() = (void (*)())0x7800; //Deactivate the interruptions IEN0 = 0x00; //Deinitialise the USB usbDeinit(); //Deinitialise the radio radioDeinit(); //Call the bootloader bootloader(); }
void main(void) { uint16_t i; // // copy bootloader functions from FLASH to RAM: uint8_t code *psrc = (uint8_t code*)SROM_MC_SRC(CODE_BOOTLOADER); uint8_t xdata *pdest = (uint8_t xdata*)SROM_MC_TRG(CODE_BOOTLOADER); for(i=0;i<SROM_MC_LEN(CODE_BOOTLOADER);i++) { *pdest++ = *psrc++; } // // Copy bootloader constants from FLASH to RAM: psrc = (uint8_t code*)SROM_MC_SRC(CONST_BOOTLOADER); pdest = (uint8_t xdata*)SROM_MC_TRG(CONST_BOOTLOADER); for(i=0;i<SROM_MC_LEN(CONST_BOOTLOADER);i++) { *pdest++ = *psrc++; } bootloader(); // Will never return }
//This is the first thing the micro runs after startup_stm32f0xx.s //Only the SRAM is initialized at this point void bootloaderSwitcher() { uint32_t jumpaddr, tmp; tmp = *BOOTLOADER_MAGIC_ADDR; if (tmp == BOOTLOADER_MAGIC_TOKEN) { *BOOTLOADER_MAGIC_ADDR = 0; //Zero it so we don't loop by accident // For the LQFP48 of STM32F042, the BOOT0 pin is at PF11, // force it high so bootloader doesnt skip to main app __HAL_RCC_GPIOF_CLK_ENABLE(); GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.Pin = GPIO_PIN_11; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); HAL_GPIO_WritePin(GPIOF, GPIO_PIN_11, GPIO_PIN_SET); // setup IWDG so reset occurs after DFU mode exit // RCC->CSR |= (1 << 0); // LSI enable, necessary for IWDG hiwdg.Instance = IWDG; hiwdg.Init.Prescaler = IWDG_PRESCALER_32; // 32KHz/32 --> iwdg @ 1khz hiwdg.Init.Window = 0x0FFF; // no window hiwdg.Init.Reload = 255; // 255ms HAL_IWDG_Init(&hiwdg); // jump into bootloader ROM const uint32_t BOOTLOADER_START_ADDR = ((DBGMCU->IDCODE & 0xFFF) == 0x448) ? BOOTLOADER_START_ADDR_072 : BOOTLOADER_START_ADDR_042; jumpaddr = *(__IO uint32_t*)(BOOTLOADER_START_ADDR + 4); void(*bootloader)(void) = (void(*)(void)) jumpaddr; __set_MSP(*(__IO uint32_t*) BOOTLOADER_START_ADDR); // bye bye bootloader(); //this should never be hit, trap for debugging while (1) {} } }
int main(void) { bootloader(); while(1); }
int main(void) { bool try_boot = true; /* try booting before we drop to the bootloader */ unsigned timeout = BOOTLOADER_DELAY; /* if nonzero, drop out of the bootloader after this time */ /* Enable the FPU before we hit any FP instructions */ SCB_CPACR |= ((3UL << 10*2) | (3UL << 11*2)); /* set CP10 Full Access and set CP11 Full Access */ #if defined(BOARD_VRBRAINV40) || defined(BOARD_VRBRAINV45) if(!board_test_force_pin() && !(board_get_rtc_signature() == BOOT_RTC_SIGNATURE)) { jump_to_app(); } #endif /* do board-specific initialisation */ board_init(); /* * Check the force-bootloader register; if we find the signature there, don't * try booting. */ if (board_get_rtc_signature() == BOOT_RTC_SIGNATURE) { /* * Don't even try to boot before dropping to the bootloader. */ try_boot = false; /* * Don't drop out of the bootloader until something has been uploaded. */ timeout = 0; #if defined(BOARD_VRBRAINV40) || defined(BOARD_VRBRAINV45) // force an erase of the first sector because version 4.x // of the board is not able to reset USB after first boot. // This will force the bootloader to stay until a program has been flashed. flash_unlock(); flash_func_erase_sector(0); flash_lock(); #endif /* * Clear the signature so that if someone resets us while we're * in the bootloader we'll try to boot next time. */ board_set_rtc_signature(0); } #ifdef INTERFACE_USB /* * Check for USB connection - if present, don't try to boot, but set a timeout after * which we will fall out of the bootloader. * * If the force-bootloader pins are tied, we will stay here until they are removed and * we then time out. */ #if defined(BOARD_VRBRAINV40) || defined(BOARD_VRBRAINV45) if (gpio_get(BOARD_PRESENCE_PORT, BOARD_PRESENCE_PIN) != 0 && board_test_force_pin()) { /* don't try booting before we set up the bootloader */ try_boot = false; } #else if (gpio_get(BOARD_PRESENCE_PORT, BOARD_PRESENCE_PIN) != 0) { /* don't try booting before we set up the bootloader */ try_boot = false; } #endif #endif /* Try to boot the app if we think we should just go straight there */ if (try_boot) { /* set the boot-to-bootloader flag so that if boot fails on reset we will stop here */ #ifdef BOARD_BOOT_FAIL_DETECT board_set_rtc_signature(BOOT_RTC_SIGNATURE); #endif /* try to boot immediately */ jump_to_app(); /* booting failed, stay in the bootloader forever */ timeout = 0; } /* configure the clock for bootloader activity */ rcc_clock_setup_hse_3v3(&clock_setup); /* start the interface */ cinit(BOARD_INTERFACE_CONFIG); #if 0 // MCO1/02 gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO8); gpio_set_output_options(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, GPIO8); gpio_set_af(GPIOA, GPIO_AF0, GPIO8); gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9); gpio_set_af(GPIOC, GPIO_AF0, GPIO9); #endif while (1) { /* run the bootloader, come back after an app is uploaded or we time out */ bootloader(timeout); /* if the force-bootloader pins are strapped, just loop back */ if (board_test_force_pin()) continue; /* set the boot-to-bootloader flag so that if boot fails on reset we will stop here */ #ifdef BOARD_BOOT_FAIL_DETECT board_set_rtc_signature(BOOT_RTC_SIGNATURE); #endif /* look to see if we can boot the app */ jump_to_app(); /* launching the app failed - stay in the bootloader forever */ timeout = 0; } }
void action_function(keyrecord_t *record, uint8_t id, uint8_t opt) { if (id == BOOTLOADER) { bootloader(); } }
void CommHandler(void) //UART4 Interrupt handler implementation { int c = GetChar(); if (c >= 0) { LEDon(); switch (c) { case 'a': debugAutoPan ^= 1; print("Autopan messages %s\r\n", debugAutoPan ? "on" : "off"); break; case 'b': print("rebooting into boot loader ...\r\n"); Delay_ms(1000); bootloader(); break; case 'c': debugCnt ^= 1; print("counter messages %s\r\n", debugCnt ? "on" : "off"); break; case 'd': debugPrint ^= 1; print("debug messages %s\r\n", debugPrint ? "on" : "off"); break; case 'g': Delay_ms(100); PutChar('x'); for (int i = 0; i < CONFIGDATASIZE; i++) { uint8_t data = configData[i]; PutChar(data); } break; case 'G': printConfig(); break; #if 0 case 'H': if (CharAvailable() >= CONFIGDATASIZE) { for (int i = 0; i < CONFIGDATASIZE; i++) { uint8_t data = GetChar(); if (data <= LARGEST_CONFIGDATA) { configData[i] = data; } } configSave(); } else { UnGetChar(c); // try again in next loop } break; #endif case 'h': for (int i = 0; i < CONFIGDATASIZE; i++) { int data; while ((data = GetChar()) < 0) ; if (data <= LARGEST_CONFIGDATA) { configData[i] = data; } } configSave(); break; case 'i': ConfigMode = 1; break; case 'j': ConfigMode = 0; break; case 'o': debugOrient ^= 1; print("Orientation messages %s\r\n", debugOrient ? "on" : "off"); break; case 'O': debugGravityVector ^= 1; print("GVector messages %s\r\n", debugGravityVector ? "on" : "off"); break; case 'S': debugSetpoints ^= 1; print("Setpoints messages %s\r\n", debugSetpoints ? "on" : "off"); break; case 'p': debugPerf ^= 1; print("performance messages %s\r\n", debugPerf ? "on" : "off"); break; case 'r': debugRC ^= 1; print("RC messages %s\r\n", debugRC ? "on" : "off"); break; case 'R': print("rebooting...\r\n"); Delay_ms(1000); reboot(); break; case 's': debugSense ^= 1; print("Sensor messages %s\r\n", debugSense ? "on" : "off"); break; case 'u': { extern int bDeviceState; printUSART("\r\nYY bDeviceState %3d VCPConnectMode %d\r\n", bDeviceState, GetVCPConnectMode()); break; } case 'v': print("Version: %s\r\n", __EV_VERSION); break; case '+': testPhase += 0.1; print("test phase output %5.1f\r\n", testPhase); break; case '-': testPhase -= 0.1; print("test phase output %5.1f\r\n", testPhase); break; /* case '?': print("CLI documentation\r\n"); // print("\t'+' test phase output increase (now %5.1f)\r\n", testPhase); //print("\t'-' test phase output decrease (now %5.1f)\r\n", testPhase); print("\t'a' autopan messages display (now %s)\r\n", debugAutoPan ? "on" : "off"); print("\t'b' reboot into bootloader\r\n"); print("\t'c' counter messages display (now %s)\r\n", debugCnt ? "on" : "off"); print("\t'd' debug messages display (now %s)\r\n", debugPrint ? "on" : "off"); print("\t'g' dump configuration (binary)\r\n"); print("\t'G' dump configuration (hexadecimal)\r\n"); // print("\t'h' write and save config array\r\n"); print("\t'i' enter config mode (now %s)\r\n", ConfigMode ? "on" : "off"); print("\t'j' leave config mode (now %s)\r\n", ConfigMode ? "on" : "off"); print("\t'o' orientation messages display (now %s)\r\n", debugOrient ? "on" : "off"); print("\t'p' performance messages display (now %s)\r\n", debugPerf ? "on" : "off"); print("\t'r' RC messages display (now %s)\r\n", debugRC ? "on" : "off"); print("\t'R' reboot\r\n"); print("\t's' toggle sensor messages display (now %s)\r\n", debugSense ? "on" : "off"); print("\t'u' print USB state (bDeviceState %3d VCPConnectMode %d)\r\n", bDeviceState, GetVCPConnectMode()); print("\t'v' print version (%s)\r\n", __EV_VERSION); break; */ default: // TODO break; } } }
void CommHandler(void) //UART4 Interrupt handler implementation { int c = GetChar(); if (c >= 0) { //ConfigMode = 1; LEDon(); //print("got char %02X\r\n", c); switch (c) { case 'b': print("rebooting into boot loader ...\r\n"); Delay_ms(1000); bootloader(); break; case 'c': debugCnt ^= 1; print("counter messages %s\r\n", debugCnt ? "on" : "off"); break; case 'd': debugPrint ^= 1; print("debug messages %s\r\n", debugPrint ? "on" : "off"); break; case 'g': Delay_ms(100); PutChar('x'); for (int i = 0; i < CONFIGDATASIZE; i++) { uint8_t data = configData[i]; PutChar(data); } break; case 'G': printConfig(); break; #if 0 case 'H': if (CharAvailable() >= CONFIGDATASIZE) { for (int i = 0; i < CONFIGDATASIZE; i++) { uint8_t data = GetChar(); if (data <= LARGEST_CONFIGDATA) { configData[i] = data; } } configSave(); } else { UnGetChar(c); // try again in next loop } break; #endif case 'h': for (int i = 0; i < CONFIGDATASIZE; i++) { int data; while ((data = GetChar()) < 0) ; if (data <= LARGEST_CONFIGDATA) { configData[i] = data; } } configSave(); break; case 'i': ConfigMode = 1; break; case 'j': ConfigMode = 0; break; case 'o': debugOrient ^= 1; print("Orientation messages %s\r\n", debugOrient ? "on" : "off"); break; case 'p': debugPerf ^= 1; print("performance messages %s\r\n", debugPerf ? "on" : "off"); break; case 'r': debugRC ^= 1; print("RC messages %s\r\n", debugRC ? "on" : "off"); break; case 'R': print("rebooting...\r\n"); Delay_ms(1000); reboot(); break; case 's': debugSense ^= 1; print("Sensor messages %s\r\n", debugSense ? "on" : "off"); break; case 'u': { extern int bDeviceState; printUSART("\r\nYY bDeviceState %3d VCPConnectMode %d\r\n", bDeviceState, GetVCPConnectMode()); break; } case '+': testPhase += 0.1; print("test phase output %5.1f\r\n", testPhase); break; case '-': testPhase -= 0.1; print("test phase output %5.1f\r\n", testPhase); break; default: // TODO break; } } }
int main(void) { #ifndef HOST_VERSION /* brake */ BRAKE_DDR(); BRAKE_OFF(); /* CPLD reset on PG3 */ DDRG |= 1<<3; PORTG &= ~(1<<3); /* implicit */ /* LEDS */ DDRJ |= 0x0c; DDRL = 0xc0; LED1_OFF(); LED2_OFF(); LED3_OFF(); LED4_OFF(); #endif memset(&gen, 0, sizeof(gen)); memset(&mainboard, 0, sizeof(mainboard)); mainboard.flags = DO_ENCODERS | DO_CS | DO_RS | DO_POS | DO_POWER | DO_BD | DO_ERRBLOCKING; ballboard.lcob = I2C_COB_NONE; ballboard.rcob = I2C_COB_NONE; /* UART */ uart_init(); uart_register_rx_event(CMDLINE_UART, emergency); #ifndef HOST_VERSION #if CMDLINE_UART == 3 fdevopen(uart3_dev_send, uart3_dev_recv); #elif CMDLINE_UART == 1 fdevopen(uart1_dev_send, uart1_dev_recv); #endif /* check eeprom to avoid to run the bad program */ if (eeprom_read_byte(EEPROM_MAGIC_ADDRESS) != EEPROM_MAGIC_MAINBOARD) { int c; sei(); printf_P(PSTR("Bad eeprom value ('f' to force)\r\n")); c = uart_recv(CMDLINE_UART); if (c == 'f') eeprom_write_byte(EEPROM_MAGIC_ADDRESS, EEPROM_MAGIC_MAINBOARD); wait_ms(100); bootloader(); } #endif /* ! HOST_VERSION */ /* LOGS */ error_register_emerg(mylog); error_register_error(mylog); error_register_warning(mylog); error_register_notice(mylog); error_register_debug(mylog); #ifndef HOST_VERSION /* SPI + ENCODERS */ encoders_spi_init(); /* this will also init spi hardware */ /* I2C */ i2c_init(I2C_MODE_MASTER, I2C_MAINBOARD_ADDR); i2c_protocol_init(); i2c_register_recv_event(i2c_recvevent); i2c_register_send_event(i2c_sendevent); /* TIMER */ timer_init(); timer0_register_OV_intr(main_timer_interrupt); /* PWM */ PWM_NG_TIMER_16BITS_INIT(1, TIMER_16_MODE_PWM_10, TIMER1_PRESCALER_DIV_1); PWM_NG_TIMER_16BITS_INIT(4, TIMER_16_MODE_PWM_10, TIMER4_PRESCALER_DIV_1); PWM_NG_INIT16(&gen.pwm1_4A, 4, A, 10, PWM_NG_MODE_SIGNED, &PORTD, 4); PWM_NG_INIT16(&gen.pwm2_4B, 4, B, 10, PWM_NG_MODE_SIGNED | PWM_NG_MODE_SIGN_INVERTED, &PORTD, 5); PWM_NG_INIT16(&gen.pwm3_1A, 1, A, 10, PWM_NG_MODE_SIGNED, &PORTD, 6); PWM_NG_INIT16(&gen.pwm4_1B, 1, B, 10, PWM_NG_MODE_SIGNED, &PORTD, 7); /* servos */ PWM_NG_TIMER_16BITS_INIT(3, TIMER_16_MODE_PWM_10, TIMER1_PRESCALER_DIV_256); PWM_NG_INIT16(&gen.servo1, 3, C, 10, PWM_NG_MODE_NORMAL, NULL, 0); PWM_NG_TIMER_16BITS_INIT(5, TIMER_16_MODE_PWM_10, TIMER1_PRESCALER_DIV_256); PWM_NG_INIT16(&gen.servo2, 5, A, 10, PWM_NG_MODE_NORMAL, NULL, 0); PWM_NG_INIT16(&gen.servo3, 5, B, 10, PWM_NG_MODE_NORMAL, NULL, 0); PWM_NG_INIT16(&gen.servo4, 5, C, 10, PWM_NG_MODE_NORMAL, NULL, 0); support_balls_deploy(); /* init pwm for servos */ #endif /* !HOST_VERSION */ /* SCHEDULER */ scheduler_init(); #ifdef HOST_VERSION hostsim_init(); robotsim_init(); #endif #ifndef HOST_VERSION scheduler_add_periodical_event_priority(do_led_blink, NULL, 100000L / SCHEDULER_UNIT, LED_PRIO); #endif /* !HOST_VERSION */ /* all cs management */ microb_cs_init(); /* TIME */ time_init(TIME_PRIO); /* sensors, will also init hardware adc */ sensor_init(); #ifndef HOST_VERSION /* start i2c slave polling */ scheduler_add_periodical_event_priority(i2c_poll_slaves, NULL, 8000L / SCHEDULER_UNIT, I2C_POLL_PRIO); #endif /* !HOST_VERSION */ /* strat */ gen.logs[0] = E_USER_STRAT; gen.log_level = 5; /* strat-related event */ scheduler_add_periodical_event_priority(strat_event, NULL, 25000L / SCHEDULER_UNIT, STRAT_PRIO); #ifndef HOST_VERSION /* eeprom time monitor */ scheduler_add_periodical_event_priority(do_time_monitor, NULL, 1000000L / SCHEDULER_UNIT, EEPROM_TIME_PRIO); #endif /* !HOST_VERSION */ sei(); strat_db_init(); printf_P(PSTR("\r\n")); printf_P(PSTR("Respect et robustesse.\r\n")); #ifndef HOST_VERSION { uint16_t seconds; seconds = eeprom_read_word(EEPROM_TIME_ADDRESS); printf_P(PSTR("Running since %d mn %d\r\n"), seconds/60, seconds%60); } #endif #ifdef HOST_VERSION strat_reset_pos(400, COLOR_Y(400), COLOR_A(-90)); #endif cmdline_interact(); return 0; }
int main(void) { int16_t command = -1; uint16_t freemem; // set for 8 MHz clock because of 3.3v regulator CPU_PRESCALE(1); // set for 16 MHz clock //CPU_PRESCALE(0); //disable JTAG MCUCR = (1<<JTD) | (1<<IVCE) | (0<<PUD); MCUCR = (1<<JTD) | (0<<IVSEL) | (0<<IVCE) | (0<<PUD); // set all i/o lines to input releaseports(); //Init SPI SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_RISING | SPI_SAMPLE_LEADING | SPI_MODE_MASTER); hwspi_init(); // Initialize the USB, and then wait for the host to set configuration. // If the Teensy is powered without a PC connected to the USB port, // this will wait forever. usb_init(); while (!usb_configured()) /* wait */ ; // Wait an extra second for the PC's operating system to load drivers // and do whatever it does to actually be ready for input _delay_ms(1000); while (1) { // discard anything that was received prior. Sometimes the // operating system or other software will send a modem // "AT command", which can still be buffered. usb_serial_flush_input(); while (usb_configured()) { // is user still connected? command = usb_serial_getchar(); if (command == -1) continue; switch (command) { case CMD_PING1: usb_serial_putchar(VERSION_MAJOR); break; case CMD_PING2: freemem = freeRam(); usb_serial_putchar(VERSION_MINOR); usb_serial_putchar((freemem >> 8) & 0xFF); usb_serial_putchar(freemem & 0xFF); break; case CMD_BOOTLOADER: bootloader(); break; case CMD_IO_LOCK: break; case CMD_IO_RELEASE: break; case CMD_PULLUPS_DISABLE: IO_PULLUPS = 0; break; case CMD_PULLUPS_ENABLE: IO_PULLUPS = 0xFF; break; case CMD_SPI_ID: handle_read_id(); break; case CMD_SPI_READBLOCK: handle_read_block(); break; case CMD_SPI_WRITESECTOR: handle_write_block(); break; case CMD_SPI_ERASEBLOCK: handle_erase_block(); break; case CMD_SPI_ERASECHIP: handle_erase_chip(); break; case CMD_SPI_3BYTE_ADDRESS: SPI_ADDRESS_LENGTH = 3; break; case CMD_SPI_4BYTE_ADDRESS: SPI_ADDRESS_LENGTH = 4; break; case CMD_SPI_3BYTE_CMDS: SPI_USE_3B_CMDS = 1; break; case CMD_SPI_4BYTE_CMDS: SPI_USE_3B_CMDS = 0; break; default: break; } } } }
int main() { GPIO_InitTypeDef gpioInit = {0}; struct syslinkPacket slPacket; struct crtpPacket_s packet; unsigned int ledGreenTime=0; unsigned int ledRedTime = 0; unsigned int ledBlueTime = 0; /* Detecting if we need to boot firmware or DFU bootloader */ bootpinInit(); if (bootpinStartFirmware() == true) { if (*((uint32_t*)FIRMWARE_START) != 0xFFFFFFFFU) { void (*firmware)(void) __attribute__((noreturn)) = (void *)(*(uint32_t*)(FIRMWARE_START+4)); bootpinDeinit(); // Start firmware NVIC_SetVectorTable(FIRMWARE_START, 0); __set_MSP(*((uint32_t*)FIRMWARE_START)); firmware(); } } else if (bootpinNrfReset() == true) { void (*bootloader)(void) __attribute__((noreturn)) = (void *)(*(uint32_t*)(SYSTEM_BASE+4)); bootpinDeinit(); // Start bootloader NVIC_SetVectorTable(SYSTEM_BASE, 0); __set_MSP(*((uint32_t*)SYSTEM_BASE)); bootloader(); } bootpinDeinit(); /* Booting CRTP Bootloader! */ SystemInit(); uartInit(); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); gpioInit.GPIO_Pin = GPIO_Pin_2; gpioInit.GPIO_Mode = GPIO_Mode_OUT; GPIO_Init(GPIOD, &gpioInit); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); gpioInit.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_0; gpioInit.GPIO_Mode = GPIO_Mode_OUT; GPIO_Init(GPIOC, &gpioInit); GPIO_WriteBit(GPIOC, GPIO_Pin_0, 1); GPIO_WriteBit(GPIOC, GPIO_Pin_1, 1); SysTick->LOAD = (SystemCoreClock/8)/1000; // Set systick to overflow every 1ms SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk | SysTick_CTRL_TICKINT_Msk; NVIC_EnableIRQ(SysTick_IRQn); // Blue LED ON by default GPIO_WriteBit(GPIOD, GPIO_Pin_2, 1); while(1) { if (syslinkReceive(&slPacket)) { if (slPacket.type == SYSLINK_RADIO_RAW) { memcpy(packet.raw, slPacket.data, slPacket.length); packet.datalen = slPacket.length-1; ledGreenTime = tick; GPIO_WriteBit(GPIOC, GPIO_Pin_1, 0); if (bootloaderProcess(&packet)) { ledRedTime = tick; GPIO_WriteBit(GPIOC, GPIO_Pin_0, 0); memcpy(slPacket.data, packet.raw, packet.datalen+1); slPacket.length = packet.datalen+1; syslinkSend(&slPacket); } } } if (ledGreenTime!=0 && tick-ledGreenTime>10) { GPIO_WriteBit(GPIOC, GPIO_Pin_1, 1); ledGreenTime = 0; } if (ledRedTime!=0 && tick-ledRedTime>10) { GPIO_WriteBit(GPIOC, GPIO_Pin_0, 1); ledRedTime = 0; } if ((tick-ledBlueTime)>500) { if (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_2)) { GPIO_WriteBit(GPIOD, GPIO_Pin_2, 0); } else { GPIO_WriteBit(GPIOD, GPIO_Pin_2, 1); } ledBlueTime = tick; } } return 0; }
void firmware(void) { multiboot_info_t* mbi = (multiboot_info_t*)MULTIBOOT_ADDRESS; kaneton_mips_header* image_header = NULL; kaneton_mips_module* kmmod = NULL; module_t* mbi_mod = NULL; t_uint32 i = 0; t_vaddr mod_dest = 0; Elf64_Ehdr* kaneton_bootloader = NULL; void (*bootloader)(multiboot_info_t*); /* * 1) */ if(*flag_address != FIRMWARE_FLAG) firmware_error(); firmware_cons_msg('+', "Kaneton qemu-mips firmware starting\n\n\n"); /* * 2) */ image_header = (kaneton_mips_header*)(flag_address + 1); /* * 3) */ mbi->mods_count = image_header->nmod - 1; mbi->mods_addr = (unsigned long)mbi + sizeof(multiboot_info_t); mod_dest = IMAGE_BASE_ADDRESS; /* * 4) */ if (mbi->mods_count < 1) firmware_error(); /* * 5) */ kmmod = (kaneton_mips_module*)(image_header + 1); mbi_mod = (module_t*)mbi->mods_addr; /* * 6) */ firmware_cons_msg('+', "Kaneton image loading\n\n"); mbi_mod->string = 0; mbi_mod->mod_start = mod_dest; kaneton_bootloader = (Elf64_Ehdr*)mod_dest; mbi_mod->mod_end = mbi_mod->mod_start + kmmod->modsz; firmware_module_display(kmmod, mbi_mod, 0); memcpy((t_uint8*)(mbi_mod->mod_start), (t_uint8*)(kmmod->module), kmmod->modsz); mod_dest += kmmod->modsz; kmmod += 1; mbi_mod += 1; /* * 7) */ for (i = 0; i < mbi->mods_count; ++i) { mbi_mod->string = mod_dest; mod_dest += strlen(kmmod->name); mbi_mod->mod_start = mod_dest; mbi_mod->mod_end = mbi_mod->mod_start + kmmod->modsz; strcpy((t_uint8*)(mbi_mod->string), kmmod->name); memcpy((t_uint8*)(mbi_mod->mod_start), (t_uint8*)(kmmod->module), kmmod->modsz); firmware_module_display(kmmod, mbi_mod, i + 1); mod_dest += kmmod->modsz; kmmod += 1; mbi_mod += 1; } /* * 8) */ bootloader = (void (*)(multiboot_info_t*))kaneton_bootloader->e_entry; firmware_cons_print_char('\n'); firmware_cons_msg('+', "Jump to the bootloader function 0x%lx\n", bootloader); bootloader(mbi); firmware_error(); }
int main(void) { SetupHardware(); sei(); // Initialize the USB, and then wait for the host to set configuration. // If the Teensy is powered without a PC connected to the USB port, // this will wait forever. while (USB_DeviceState != DEVICE_STATE_Configured) /* wait */ USB_USBTask(); //configure all i/o lines (and set tristate=low) //nor_initports(); nand_initports(); // Wait an extra second for the PC's operating system to load drivers // and do whatever it does to actually be ready for input _delay_ms(1000); usbio_initbuffers(); int16_t command = -1; uint8_t nand_id = 0; uint16_t nand_block = 0; while (1) { USB_USBTask(); while (USB_DeviceState == DEVICE_STATE_Configured) { // is user still connected? command = usbio_get_byte(); if (command == -1) continue; switch (command) { case CMD_GETVERSION: usbio_set_byte(NANDORWAY_MAJOR_VERSION, 0); usbio_set_byte(NANDORWAY_MINOR_VERSION, 0); usbio_set_byte(NANDORWAY_BUILD_VERSION, 1); break; case CMD_PING: usbio_set_byte(0x42, 0); usbio_set_byte(0xbd, 1); break; case CMD_BOOTLOADER: bootloader(); break; case CMD_SPEEDTEST_READ: speedtest_send(); break; case CMD_SPEEDTEST_WRITE: speedtest_receive(); break; case CMD_IO_LOCK: nor_initports(); break; case CMD_IO_RELEASE: nor_releaseports(); break; case CMD_NOR_ID: nor_id(); break; case CMD_NOR_RESET: nor_reset(); break; case CMD_NOR_ERASE_SECTOR: nor_erase_sector(); break; case CMD_NOR_ERASE_CHIP: nor_erase_chip(); break; case CMD_NOR_ADDRESS_SET: nor_address_set(usbio_get_byte(), usbio_get_byte(), usbio_get_byte()); break; case CMD_NOR_ADDRESS_INCREMENT: nor_address_increment(1); break; case CMD_NOR_ADDRESS_INCREMENT_ENABLE: nor_address_increment_set(1); break; case CMD_NOR_ADDRESS_INCREMENT_DISABLE: nor_address_increment_set(0); break; case CMD_NOR_2ND_DIE_ENABLE: nor_2nd_die_offset(0x40); //A22=HIGH for Samsung K8Q2815 break; case CMD_NOR_2ND_DIE_DISABLE: nor_2nd_die_offset(0x00); //A22=LOW for Samsung K8Q2815 break; case CMD_NOR_READ_WORD: nor_read(NOR_BSS_WORD, 1); break; case CMD_NOR_READ_BLOCK_4KB: nor_read(NOR_BSS_4, 1); break; case CMD_NOR_READ_BLOCK_8KB: nor_read(NOR_BSS_8, 1); break; case CMD_NOR_READ_BLOCK_64KB: nor_read(NOR_BSS_64, 1); break; case CMD_NOR_READ_BLOCK_128KB: nor_read(NOR_BSS_128, 1); break; case CMD_NOR_WRITE_WORD: nor_write_word(); break; case CMD_NOR_WRITE_BLOCK_WORD: nor_write_block(NOR_PRG_MODE_WORD); break; case CMD_NOR_WRITE_BLOCK_UBM: nor_write_block(NOR_PRG_MODE_UBM); break; case CMD_NOR_WRITE_BLOCK_WBP: nor_write_block(NOR_PRG_MODE_WBP); break; case CMD_NAND_ID: switch (nand_id) { case 0: if (init_nand(&nand0) == 1) { //24 bytes usbio_set_byte(nand0.info.maker_code, 0); usbio_set_byte(nand0.info.device_code, 0); usbio_set_byte((nand0.info.page_size >> 24) & 0xFF, 0); usbio_set_byte((nand0.info.page_size >> 16) & 0xFF, 0); usbio_set_byte((nand0.info.page_size >> 8) & 0xFF, 0); usbio_set_byte(nand0.info.page_size & 0xFF, 0); usbio_set_byte(nand0.info.oob->size, 0); usbio_set_byte((nand0.info.block_size >> 24) & 0xFF, 0); usbio_set_byte((nand0.info.block_size >> 16) & 0xFF, 0); usbio_set_byte((nand0.info.block_size >> 8) & 0xFF, 0); usbio_set_byte(nand0.info.block_size & 0xFF, 0); usbio_set_byte((nand0.info.num_blocks >> 24) & 0xFF, 0); usbio_set_byte((nand0.info.num_blocks >> 16) & 0xFF, 0); usbio_set_byte((nand0.info.num_blocks >> 8) & 0xFF, 0); usbio_set_byte(nand0.info.num_blocks & 0xFF, 0); usbio_set_byte(nand0.info.num_planes, 0); usbio_set_byte((nand0.info.plane_size >> 56) & 0xFF, 0); usbio_set_byte((nand0.info.plane_size >> 48) & 0xFF, 0); usbio_set_byte((nand0.info.plane_size >> 40) & 0xFF, 0); usbio_set_byte((nand0.info.plane_size >> 32) & 0xFF, 0); usbio_set_byte((nand0.info.plane_size >> 24) & 0xFF, 0); usbio_set_byte((nand0.info.plane_size >> 16) & 0xFF, 0); usbio_set_byte((nand0.info.plane_size >> 8) & 0xFF, 0); usbio_set_byte(nand0.info.plane_size & 0xFF, 1); } else { for (uint8_t i = 0; i < 23; ++i) usbio_set_byte(0xFF, 0); usbio_set_byte(0xFF, 1); } break; case 1: if (init_nand(&nand1) == 1) { //24 bytes usbio_set_byte(nand1.info.maker_code, 0); usbio_set_byte(nand1.info.device_code, 0); usbio_set_byte((nand1.info.page_size >> 24) & 0xFF, 0); usbio_set_byte((nand1.info.page_size >> 16) & 0xFF, 0); usbio_set_byte((nand1.info.page_size >> 8) & 0xFF, 0); usbio_set_byte(nand1.info.page_size & 0xFF, 0); usbio_set_byte(nand1.info.oob->size, 0); usbio_set_byte((nand1.info.block_size >> 24) & 0xFF, 0); usbio_set_byte((nand1.info.block_size >> 16) & 0xFF, 0); usbio_set_byte((nand1.info.block_size >> 8) & 0xFF, 0); usbio_set_byte(nand1.info.block_size & 0xFF, 0); usbio_set_byte((nand1.info.num_blocks >> 24) & 0xFF, 0); usbio_set_byte((nand1.info.num_blocks >> 16) & 0xFF, 0); usbio_set_byte((nand1.info.num_blocks >> 8) & 0xFF, 0); usbio_set_byte(nand1.info.num_blocks & 0xFF, 0); usbio_set_byte(nand1.info.num_planes, 0); usbio_set_byte((nand1.info.plane_size >> 56) & 0xFF, 0); usbio_set_byte((nand1.info.plane_size >> 48) & 0xFF, 0); usbio_set_byte((nand1.info.plane_size >> 40) & 0xFF, 0); usbio_set_byte((nand1.info.plane_size >> 32) & 0xFF, 0); usbio_set_byte((nand1.info.plane_size >> 24) & 0xFF, 0); usbio_set_byte((nand1.info.plane_size >> 16) & 0xFF, 0); usbio_set_byte((nand1.info.plane_size >> 8) & 0xFF, 0); usbio_set_byte(nand1.info.plane_size & 0xFF, 1); } else { for (uint8_t i = 0; i < 23; ++i) usbio_set_byte(0xFF, 0); usbio_set_byte(0xFF, 1); } break; default: break; }