Esempio n. 1
0
/** \brief Main function. */
int main(void)
{
	uint8_t i;
	
	/* Initialize the board hardware and system clocks. */
	board_init();
	sysclk_init();

	/* Detection of all RESET excepted WDT RESET. */
	if (reset_cause_get_causes() & ~CHIP_RESET_CAUSE_WDT) {
		/* Wait for 2 s. */
		delay_ms(2000);
		state_flag = START_OF_PROG;
		reset_cause_clear_causes(
				CHIP_RESET_CAUSE_POR |
				CHIP_RESET_CAUSE_EXTRST |
				CHIP_RESET_CAUSE_BOD_CPU |
				CHIP_RESET_CAUSE_OCD |
				CHIP_RESET_CAUSE_SOFT |
				CHIP_RESET_CAUSE_SPIKE);
	} else {
		reset_cause_clear_causes(CHIP_RESET_CAUSE_WDT);
	}

	while (true) {
		led_display(state_flag);

		switch (state_flag) {
		case START_OF_PROG:
			/* Writing test. */
			wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_8CLK);
			if (wdt_get_timeout_period() != WDT_TIMEOUT_PERIOD_8CLK) {
				state_flag = ERROR_STATE;
				break;
			}

			wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_250CLK);
			if (wdt_get_timeout_period() != WDT_TIMEOUT_PERIOD_250CLK) {
				state_flag = ERROR_STATE;
				break;
			}

			/* Wait for 2 s. */
			delay_ms(2000);
			state_flag = WDT_MCU_RESET;
			break;

		case WDT_MCU_RESET:
			/* Wait for 2 s. */
			delay_ms(2000);
			state_flag = REFRESH_NO_WIN;
			wdt_reset_mcu();
			break;

		case REFRESH_NO_WIN:
			/* Enable WDT 500 ms. */
			wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_500CLK);
			wdt_enable();
			for (i = 0; i < 8; i++) {
				/* Wait for 8x 250 ms = 2 s. */
				delay_ms(250);
				wdt_reset();
			}
			wdt_disable();
			state_flag = REFRESH_WINDOW;
			break;

		case REFRESH_WINDOW:
			/* Enable Window 250 ms & WDT 500 ms. */
			wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_500CLK);
			wdt_enable();
			if (!(wdt_set_window_period(WDT_WINDOW_PERIOD_250CLK)))
			{
				state_flag = ERROR_STATE;
				break;
			}

			if (!(wdt_enable_window_mode())) {
				state_flag = ERROR_STATE;
				break;
			}

			for (i = 0; i < 4; i++) {
				/* Wait for 500 ms. */
				delay_ms(500);
				wdt_reset();
			}
			wdt_disable();
			state_flag = WDT_RST_NO_WIN;
			break;

		case WDT_RST_NO_WIN:
			state_flag = WDT_RST_WINDOW;
			/* Enable WDT 2 s. */
			wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_2KCLK);
			wdt_enable();
			while (true) {
				/* Wait for Watchdog reset. */
			}
			break;

		case WDT_RST_WINDOW:
			state_flag = WDT_RST_RFSH_W;
			/* Enable Window 1 s & WDT 1 s. */
			wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_1KCLK);
			wdt_enable();
			if (!(wdt_set_window_period(WDT_WINDOW_PERIOD_1KCLK))) {
				state_flag = ERROR_STATE;
				break;
			}

			if (!(wdt_enable_window_mode())) {
				state_flag = ERROR_STATE;
				break;
			}

			while (true) {
				/* Wait for Watchdog reset. */
			}

			break;

		case WDT_RST_RFSH_W:
			state_flag = END_OF_PROG;
			/* Enable Window 4 s & WDT 250 ms. */
			wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_250CLK);
			wdt_enable();
			if (!(wdt_set_window_period(WDT_WINDOW_PERIOD_4KCLK))) {
				state_flag = ERROR_STATE;
				break;
			}

			if (!(wdt_enable_window_mode())) {
				state_flag = ERROR_STATE;
				break;
			}

			/* Wait for 2 s. */
			delay_ms(2000);
			wdt_reset();
			while (true) {
				/* Wait for Watchdog reset. */
			}

			break;

		case ERROR_STATE:
			while (true) {
				led_display(ERROR_STATE);
				/* Wait for 500 ms. */
				delay_ms(500);
				/* Blinking. */
				led_display(~ERROR_STATE);
				/* Wait for 500 ms. */
				delay_ms(500);
			}
		
			break;

		case END_OF_PROG:
		default:
			/* Wait for 2 s. */
			delay_ms(2000);
			reset_do_soft_reset();
			break;
		}
	}
}
Esempio n. 2
0
/**************************************************************************************************
* Handle received HID set feature reports
*/
void HID_set_feature_report_out(uint8_t *report)
{
	uint8_t		response[UDI_HID_REPORT_OUT_SIZE];
	response[0] = report[0] | 0x80;
	response[1] = report[1];
	response[2] = report[2];
	
	uint16_t	addr;
	addr = *(uint16_t *)(report+1);

	switch(report[0])
	{
		// no-op
		case CMD_NOP:
			break;
		
		// write to RAM page buffer
		case CMD_RESET_POINTER:
			page_ptr = 0;
			return;

		// read from RAM page buffer
		case CMD_READ_BUFFER:
			memcpy(response, &page_buffer[page_ptr], UDI_HID_REPORT_OUT_SIZE);
			page_ptr += UDI_HID_REPORT_OUT_SIZE;
			page_ptr &= APP_SECTION_PAGE_SIZE-1;
			break;

		// erase entire application section
		case CMD_ERASE_APP_SECTION:
			SP_WaitForSPM();
			SP_EraseApplicationSection();
			return;

		// calculate application and bootloader section CRCs
		case CMD_READ_FLASH_CRCS:
			SP_WaitForSPM();
			*(uint32_t *)&response[3] = SP_ApplicationCRC();
			*(uint32_t *)&response[7] = SP_BootCRC();
			break;

		// read MCU IDs
		case CMD_READ_MCU_IDS:
			response[3] = MCU.DEVID0;
			response[4] = MCU.DEVID1;
			response[5] = MCU.DEVID2;
			response[6] = MCU.REVID;
			break;
		
		// read fuses
		case CMD_READ_FUSES:
			response[3] = SP_ReadFuseByte(0);
			response[4] = SP_ReadFuseByte(1);
			response[5] = SP_ReadFuseByte(2);
			response[6] = 0xFF;
			response[7] = SP_ReadFuseByte(4);
			response[8] = SP_ReadFuseByte(5);
			break;
		
		// write RAM page buffer to application section page
		case CMD_WRITE_PAGE:
			if (addr > (APP_SECTION_SIZE / APP_SECTION_PAGE_SIZE))	// out of range
			{
				response[1] = 0xFF;
				response[2] = 0xFF;
				break;
			}
			SP_WaitForSPM();
			SP_LoadFlashPage(page_buffer);
			SP_WriteApplicationPage(APP_SECTION_START + ((uint32_t)addr * APP_SECTION_PAGE_SIZE));
			page_ptr = 0;
			break;

		// read application page to RAM buffer and return first 32 bytes
		case CMD_READ_PAGE:
			if (addr > (APP_SECTION_SIZE / APP_SECTION_PAGE_SIZE))	// out of range
			{
				response[1] = 0xFF;
				response[2] = 0xFF;
			}
			else
			{
				memcpy_P(page_buffer, (const void *)(APP_SECTION_START + (APP_SECTION_PAGE_SIZE * addr)), APP_SECTION_PAGE_SIZE);
				memcpy(&response[3], page_buffer, 32);
				page_ptr = 0;
			}
			break;
		
		// erase user signature row
		case CMD_ERASE_USER_SIG_ROW:
			SP_WaitForSPM();
			SP_EraseUserSignatureRow();
			break;
		
		// write RAM buffer to user signature row
		case CMD_WRITE_USER_SIG_ROW:
			SP_WaitForSPM();
			SP_LoadFlashPage(page_buffer);
			SP_WriteUserSignatureRow();
			break;

		// read user signature row to RAM buffer and return first 32 bytes
		case CMD_READ_USER_SIG_ROW:
			if (addr > (USER_SIGNATURES_PAGE_SIZE - 32))
			{
				response[1] = 0xFF;
				response[2] = 0xFF;
			}
			else
			{
				memcpy_P(page_buffer, (const void *)(USER_SIGNATURES_START + addr), USER_SIGNATURES_SIZE);
				memcpy(&response[3], page_buffer, 32);
				page_ptr = 0;
			}
			break;

		case CMD_READ_SERIAL:
			{
				uint8_t	i;
				uint8_t	j = 3;
				uint8_t b;
	
				for (i = 0; i < 6; i++)
				{
					b = SP_ReadCalibrationByte(offsetof(NVM_PROD_SIGNATURES_t, LOTNUM0) + i);
					response[j++] = hex_to_char(b >> 4);
					response[j++] = hex_to_char(b & 0x0F);
				}
				response[j++] = '-';
				b = SP_ReadCalibrationByte(offsetof(NVM_PROD_SIGNATURES_t, LOTNUM0) + 6);
				response[j++] = hex_to_char(b >> 4);
				response[j++] = hex_to_char(b & 0x0F);
				response[j++] = '-';

				for (i = 7; i < 11; i++)
				{
					b = SP_ReadCalibrationByte(offsetof(NVM_PROD_SIGNATURES_t, LOTNUM0) + i);
					response[j++] = hex_to_char(b >> 4);
					response[j++] = hex_to_char(b & 0x0F);
				}

				response[j] = '\0';
				break;
			}
		
		case CMD_READ_BOOTLOADER_VERSION:
			response[3] = BOOTLOADER_VERSION;
			break;
		
		case CMD_RESET_MCU:
			reset_do_soft_reset();
			response[1] = 0xFF;	// failed
			break;
		
		case CMD_READ_EEPROM:
			if (addr > (EEPROM_SIZE - 32))
			{
				response[1] = 0xFF;
				response[2] = 0xFF;
			}
			else
			{
				EEP_EnableMapping();
				memcpy_P(page_buffer, (const void *)(MAPPED_EEPROM_START + addr), APP_SECTION_PAGE_SIZE);
				EEP_DisableMapping();
				memcpy(&response[3], page_buffer, 32);
				page_ptr = 0;
			}
			break;

		case CMD_WRITE_EEPROM:
			if (addr > (EEPROM_SIZE / EEPROM_PAGE_SIZE))
			{
				response[1] = 0xFF;
				response[2] = 0xFF;
			}
			else
			{
				EEP_LoadPageBuffer(&report[3], EEPROM_PAGE_SIZE);
				EEP_AtomicWritePage(addr);
			}
			break;
		
		// unknown command
		default:
			response[0] = 0xFF;
			break;
	}

	udi_hid_generic_send_report_in(response);
}
/**
 * \brief Main function.
 */
int main(void)
{
	uint8_t key;

	/* Initialize the SAM system. */
	sysclk_init();
	board_init();

	/* Initialize the UART console. */
	configure_console();

	/* Output example information. */
	puts(STRING_HEADER);
	puts("------ Please select which reset is to be generated ----------\r\n"
			"  c:  Generate core BOD reset\r\n"
			"  i:  Generate I/O BOD reset\r\n"
			"  w:  Generate watchdog reset\r\n"
			"  s:  Generate system reset \r");

	if (reset_cause_is_power_on_reset()) {
		LED_On(LED0);
	}
	if (reset_cause_is_cpu_brown_out_detected()) {
		puts("-- Reset Cause is Core browm out reset --\r");
	}
	if (reset_cause_is_io_brown_out_detected()) {
		puts("-- Reset Cause is I/O brown out reset --\r");
	}
	if (reset_cause_is_ocd()) {
		puts("-- Reset Cause is system reset request --\r");
	}
	if (reset_cause_is_external_reset()) {
		puts("-- Reset Cause is external reset --\r");
	}
	if (reset_cause_is_watchdog()) {
		puts("-- Reset Cause is watchdog reset --\r");
	}

	while(1) {
		scanf("%c", (char *)&key);
		switch (key) {
		case 'c':
			puts("-- Please press reset button --\r");
			BSCIF->BSCIF_BOD18CTRL = BSCIF_BOD18CTRL_EN | BSCIF_BOD18CTRL_ACTION(1) |
					BSCIF_BOD18CTRL_HYST | BSCIF_BOD18CTRL_FCD;
			BSCIF->BSCIF_BOD18LEVEL |= BSCIF_BOD18LEVEL_VAL(0x3F);
			break;
		case 'i':
			puts("-- Please press reset button --\r");
			BSCIF->BSCIF_BOD33CTRL = BSCIF_BOD33CTRL_EN | BSCIF_BOD33CTRL_ACTION(1) |
					BSCIF_BOD33CTRL_HYST | BSCIF_BOD33CTRL_FCD;
			BSCIF->BSCIF_BOD33LEVEL = BSCIF_BOD33LEVEL_VAL(0x3F);
			break;
		case 'w':
			wdt_reset_mcu();
			break;
		case 's':
			reset_do_soft_reset();
			break;
		}
	}
}
Esempio n. 4
0
void ManufactureTestButtonPress(void)
{
	startTimeout(&biosUnused, BIOS_UNUSED_MS);
	while(1)
	{
		uint8_t red = LED_OFF;
		uint8_t green = LED_OFF;
		uint8_t blue = LED_OFF;		
		
		WatchdogPet();
		
		if (buttonActivated(B_START) & buttonActivated(B_SELECT))
		{
			delay_ms(500);
			reset_do_soft_reset();
		}
		
		if(checkTimeout(&biosUnused))
		{
			cancelTimeout(&biosUnused);
			setLEDValue(0, 0, 0);
			powerOff(global_header.powerOffSoftware);
		}

		if( buttonActivated(B_UP) ||
			buttonActivated(B_DOWN) ||
			buttonActivated(B_LEFT) ||
			buttonActivated(B_RIGHT) ||
			buttonActivated(B_A) ||
			buttonActivated(B_B) ||
			buttonActivated(B_X) ||
			buttonActivated(B_Y) ||
			buttonActivated(B_START) ||
			buttonActivated(B_SELECT) ||
			buttonActivated(B_LB) ||
			buttonActivated(B_RB) ||
			buttonActivated(B_LT) ||
			buttonActivated(B_RT) ||
			buttonActivated(B_JL) ||
			buttonActivated(B_JR)
		)
		{
			green = LED_DIM;
		}
		
		for(uint8_t idx = 0 ; idx<NUM_JOYSTICKS; idx++) 
		{
			uint16_xy raw;
			raw.x = sampleAnalogChannel(joysticks[idx].adc_channel_x);
			raw.y = sampleAnalogChannel(joysticks[idx].adc_channel_y);
			
			int16_xy out;
			SimpleJoystickAdj( center[idx], &deadzone, &raw, &out );
			
			//Write raw and adjusted X,Y values to USB serial port
			PrintXY(raw.x,raw.y);
			sendUSBString("->", false);
			PrintXY(out.x,out.y);
			sendUSBLine("");
			
			if( out.y<0 )
			{
				//up is yellow
				red = LED_DIM;
				green = LED_DIM;
			}
			else if( out.y>0 )
			{
				//down is blue
				blue = LED_DIM;
			}
			else if( out.x>0 )
			{
				//right is Red
				red = LED_DIM;
			}
			else if( out.x<0 )
			{
				//left is green
				green = LED_DIM;
			}
		}
		
		setLEDValue(red, green, blue);
				
	} // end while(1)
}