Пример #1
0
int main() {
	// DeInit NVIC and SCBNVIC
	NVIC_DeInit();
	NVIC_SCBDeInit();

	/* Configure the NVIC Preemption Priority Bits:
	 * two (2) bits of preemption priority, six (6) bits of sub-priority.
	 * Since the Number of Bits used for Priority Levels is five (5), so the
	 * actual bit number of sub-priority is three (3)
	 */
	NVIC_SetPriorityGrouping(0x05);

	NVIC_SetVTOR(0x00000000);

	#ifdef	UARTDEBUG
		uart_init();
	#endif

	BlockDevInit();

#ifdef	UARTDEBUG
	if (1) {
		U32 size;
		BlockDevGetSize(&size);
		DBG("Found SD card of size %d", size);
		BlockDevGetBlockLength(&size);
		DBG("block length %d", size);
	}
#endif

	if (bootloader_button_pressed() || (user_code_present() == 0)) {
		DBG("entering bootloader");
		init_usb_msc_device();

		for (;usb_msc_not_ejected();)
			USBHwISR();

		DBG("usb ejected, rebooting");

		USBHwConnect(FALSE);
		spi_close();
	}
	else {
		if ((r = f_mount(0, &fatfs)) == FR_OK) {
			if ((r = f_open(&f, "/firmware.bin", FA_READ | FA_OPEN_EXISTING)) == FR_OK) {
				unsigned int fs = f_size(&f);
				DBG("found firmware.bin with %u bytes", fs);
				if ((fs > 0) && (fs <= USER_FLASH_SIZE)) {
					U8 buffer[FLASH_BUF_SIZE];
					for (unsigned int i = 0; i < fs; i += FLASH_BUF_SIZE) {
						unsigned int j = FLASH_BUF_SIZE;
						if (i + j > fs)
							j = fs - i;
						DBG("writing %d-%d", i, i+j);
						if ((r = f_read(&f, buffer, j, &j)) == FR_OK) {
							// pad last block to a full sector size
							while (j < FLASH_BUF_SIZE) {
								buffer[j++] = 0xFF;
							}
							write_flash((unsigned int *) (USER_FLASH_START + i), (char *) &buffer, j);
						}
						else {
							DBG("read failed: %d", r);
							i = fs;
						}
					}
					r = f_close(&f);
					r = f_unlink("/firmware.bck");
					r = f_rename("/firmware.bin", "/firmware.bck");
				}
			}
			else {
				DBG("open \"/firmware.bin\" failed: %d", r);
			}
			#ifdef	GENERATE_FIRMWARE_CUR
				if (f_open(&f, "/firmware.bck", FA_READ | FA_OPEN_EXISTING)) {
					f_close(&f);
				}
				else {
					// no firmware.bck, generate one!
					if (f_open(&f, "/firmware.bck", FA_WRITE | FA_CREATE_NEW) == FR_OK) {
						U8 *flash = (U8 *) USER_FLASH_START;

						f_close(&f);
					}
				}
			#endif
			// elm-chan's fatfs doesn't have an unmount function
			// f_umount(&fatfs);
		}
		else {
			DBG("mount failed: %d", r);
		}
		spi_close();

		if (user_code_present()) {
			DBG("starting user code...");
			execute_user_code();
		}
		else {
			DBG("user code invalid, rebooting");
		}
	}
	NVIC_SystemReset();
}
Пример #2
0
int main() {
	BOOT_INIT;

	#ifdef LED_PORT
		LED_PORT->FIODIR |= 1<<LED_PIN;
	#endif
		//init_printf(NULL, vcom_putc);
		//printf("a\n");
//	#ifdef LED_PORT
//		LED_PORT->FIOCLR |= 1<<LED_PIN;
//	#endif

	int rstsrc = LPC_SC->RSID & 0xF;
	uint8_t wdreset = (rstsrc & (1<<2)) /*|| rstsrc == 0*/;

	LPC_SC->RSID = 0xF;

#ifdef P2_10_RESET
	if(!(LPC_GPIO2->FIOPIN & 1<<10)) {
		LPC_SC->RSID = 0xF;
		NVIC_SystemReset();
	}
#endif

	if(!BOOT_ENTRY_CONDITION) {
		if(!wdreset && user_code_present()
#ifdef P2_10_RESET
		&& !(LPC_GPIO2->FIODIR & (1<<10))
#endif

		) {
			//delay_busy(1000);
			execute_user_code();
		}
	}

	SystemInit();
	_segs_init();

	//uart_init();
	//init_printf(NULL, vcom_putc);

	SYSTICK_InternalInit(1);
	SYSTICK_IntCmd(ENABLE);
	SYSTICK_Cmd(ENABLE);

#ifdef USB_CONNECT_PORT
	USB_CONNECT_PORT->FIODIR |= 1<<USB_CONNECT_PIN;
#ifdef USB_CONNECT_INVERT
	USB_CONNECT_PORT->FIOSET |= 1<<USB_CONNECT_PIN;
#endif
#endif

	USBInit();

	USBRegisterDescriptors(abDescriptors);
	//USBHwRegisterDevIntHandler(on_usb_device_status);

	usb_boot_init();

	usbConnect(TRUE);

	NVIC_EnableIRQ(USB_IRQn);

	//printf("labas\n");
	uint8_t timeout = 50;

	while(1) {

		//LED_PORT->FIOSET |= 1<<LED_PIN;


#ifdef LED_PORT
		delay_busy(80);
		LED_PORT->FIOSET |= 1<<LED_PIN;
		delay_busy(80);
		LED_PORT->FIOCLR |= 1<<LED_PIN;
#else
		delay_busy(160);
#endif
		timeout--;
		if(timeout == 0 && wdreset && dfu_state == DFU_STATE_dfuIDLE) {
			//LPC_SC->RSID |= 1<<2; //clear wd reset bit
			usbConnect(FALSE);
			NVIC_SystemReset();
		}

#ifdef P2_10_RESET
		// if P2.10 is low, reset the system to enter ISP
		if(!(LPC_GPIO2->FIOPIN & 1<<10)) {
			NVIC_SystemReset();
		}
#endif


	}
}