Exemple #1
0
/* This example turns all 4 leds on and then off */
int main(void) {

    // Access 4 leds predefined as LED1, LED2, LED3, LED4
    Pin_t leds[] = {
        Pin_Init(LED1, 1, Output),
        Pin_Init(LED2, 1, Output),
        Pin_Init(LED3, 1, Output),
    };
    int nleds = 3;

    Pin_t button = Pin_Init(SW1, 1, Output);
    Pin_Input(button),
    Pin_Mode(button, PullUp);

    // Get the sensor enabler
    Pin_t enabler = Pin_Init(LED_EN, 1, Output);
    Pin_Output(enabler),
    Pin_On(enabler);

    // Get AnalogIns
    uint32_t sensors[] = {
        AnalogIn_Get(SENS0),
        AnalogIn_Get(SENS1),
        AnalogIn_Get(SENS2),
        AnalogIn_Get(SENS3),
        AnalogIn_Get(SENS4) };
    int nsensors = 5;

    while (1) {
        if (Pin_Read(button) == SW_ON) {
            Pin_Off(leds[2]);
        } else {
            Pin_On(leds[2]);
        }
        if (AnalogIn_Read(sensors[0]) > 0) {
            Pin_Off(leds[1]);
        } else {
            Pin_On(leds[1]);
        }


/*      for (i = 0; i < nleds; i++) {
            Pin_On(leds[i]);
            Delay(0.2);
        }
        for (i = 0; i < nleds; i++) {
            Pin_Off(leds[i]);
            Delay(0.2);
        }*/
    }
}
Exemple #2
0
int main(void)
{
    // Clock (80MHz)
    SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);


    // Init peripherals
    //  Init Red LED Pin
    Pin_Init(PF1);
    Pin_Set(PF1, LOW);

    // I2C0
    I2C_Init(I2C0);
    I2C_Enable(I2C0);
    

    // Init Objects
    PCA9557 pca9557 = PCA9557(I2C0);
    pca9557.set(P2, LOW);
    pca9557.set(P3, LOW);


    while(1)
    {
        Pin_Toggle(PF1);

        pca9557.toggle(P2);

        delay(0.2);
    }
}
Exemple #3
0
/**
 * Main entry point; start worker threads, setup signal handling, wait for threads to exit, exit
 * @param argc - Num args
 * @param argv - Args
 * @returns 0 on success, error code on failure
 * NOTE: NEVER USE exit(3)! Instead call Thread_QuitProgram
 */
int main(int argc, char ** argv)
{

	// Open log before calling ParseArguments (since ParseArguments may call the Log functions)
	openlog("mctxserv", LOG_PID | LOG_PERROR, LOG_USER);

	ParseArguments(argc, argv); // Setup the g_options structure from program arguments

	Log(LOGINFO, "Server started");


	
	#ifdef REALTIME_VERSION
	
	if (is_realtime())
	{
		Log(LOGDEBUG, "Running under realtime kernel");
	}
	else
	{
		Fatal("Not running under realtime kernel");
	}
	struct sched_param param;
	param.sched_priority = 49;
	if (sched_setscheduler(0, SCHED_FIFO, &param) < 0)
		Fatal("sched_setscheduler failed - %s", strerror(errno));
	if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
		Fatal("mlockall failed - %s", strerror(errno));
	stack_prefault();
	#endif //REALTIME_VERSION

	

	Pin_Init();
	
	// Try and start things
	
	//const char *ret;
	//if ((ret = Control_SetMode(CONTROL_START, "test")) != NULL)
	//	Fatal("Control_SetMode failed with '%s'", ret);
	

	// run request thread in the main thread
	FCGI_RequestLoop(NULL);

	
	Control_SetMode(CONTROL_STOP, NULL);
	//if ((ret = Control_SetMode(CONTROL_STOP, "test")) != NULL)
	//	Fatal("Control_SetMode failed with '%s'", ret);
	
	//Sensor_StopAll();
	//Actuator_StopAll();

	Pin_Close();

	Cleanup();
	return 0;
}
Exemple #4
0
Fichier : uart.c Projet : cmonr/PAL
void UART_Enable(tUART* uart)
{
    // Enable Pin Port
    Pin_Init(uart -> tx_pin);
    Pin_Init(uart -> rx_pin);
    
    // Set GPIO Pin Mux
    GPIOPinTypeUART(pins[uart -> tx_pin].port.base, pins[uart -> tx_pin].offset);
    GPIOPinTypeUART(pins[uart -> rx_pin].port.base, pins[uart -> rx_pin].offset);
    GPIOPinConfigure(uart -> tx_pin_mux);
    GPIOPinConfigure(uart -> rx_pin_mux);

    // Enable UART
    UARTEnable(uart -> base);
    
    // Disable internal FIFOs
    UARTFIFODisable(uart -> base);
}
/******************Ö÷³ÌÐò********************************/
void main(void)
{
   Pin_Init();
   T0_Init();
   Uart1_Init();
   Uart2_Init();
   Uart1_AppInit();
   Uart2_AppInit();
   MdTcbInit1();
   MdTcbInit2();
   Global_EI();
   while (1)
   {
      TimeDeal(&Ahu1);
      MdPoll01();
      MdPoll02();
      if (Ahu1.Times.Solo._1msFlag)
      {
         UartTimeOut(&UartAppData1);
         UartTimeOut(&UartAppData2);
	 KeyScanDeal(KeyGet());
	 KeyAllStateDeal(&Ahu1);
	 NumberDisplayScan(&Ahu1);
      }
      DisplayNumber(flag);
      if (Ahu1.Times.Solo._5msFlag)
      {
	 LedOutputScan(&Ahu1);
	 RhDisplayScan(&Ahu1);    
	 TmepDisplayScan(&Ahu1);  
      }
      if (Ahu1.Times.Solo._10msFlag)
      {


      }
      if (Ahu1.Times.Solo._1000msFlag)
      {
	 flag++;
      }
//      Ahu1.Humidity.Solo.SEG_B = 1;
//      Ahu1.Temperature.Solo.SEG_C = 1;
      Ahu1.Times.All[0] = 0;
   }
}
Exemple #6
0
Fichier : main.c Projet : cmonr/PAL
int main(void)
{
    // Clock (80MHz)
    SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);

    // Init LEDs
    Pin_Init(rLED);
    Pin_Set(rLED, LOW);

    // Init UART0
    UART_Init(UART0);
    UART_Enable(UART0);
    setbuf(stdout, NULL);   // Disable printf internal buffer

    // Init I2C0
    I2C_Init(I2C0);
    I2C_Enable(I2C0);



    // Wait until user presses enter
    UART_ReadChar(UART0);

    // Scan for I2C addresses
    for(i=0; i < (1 << 7); i++)
    {
        printf("x%02x:", i);
        if (I2C_Write(I2C0, i, 0) == true)
            printf("* ");
        else    
            printf("  ");

        Pin_Toggle(rLED);


        if (i % 8 == 7)
            printf("\r\n");
    }

    // Indicator LED off
    Pin_Set(rLED, LOW);
       
    while(1);      
}
Exemple #7
0
/**************************************************************************//**
 *
 * @brief	Initialize board
 *
 *****************************************************************************/
void BoardInit(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;

	Pin_Init();
	
	LED_CLK();
	BSP_LedSet(0);
	GPIO_InitStructure.GPIO_Pin = LED_PIN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(LED_PORT, &GPIO_InitStructure);

	SystemCoreClockUpdate();
	TimingInit();

	PC_GPIO_CLK();
	GPIO_InitStructure.GPIO_Pin = PC_PIN_RX;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_Init(PC_PIN_PORT, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = PC_PIN_TX;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_Init(PC_PIN_PORT, &GPIO_InitStructure);

	PC_UART_CLK();
	USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(PC_UART, &USART_InitStructure);
	USART_Cmd(PC_UART, ENABLE);
}
Exemple #8
0
//-----------------------------------------------------------------------------
// Command_Decode
//-----------------------------------------------------------------------------
//
// Return Value : Error code
// Parameters   : Pointer to input string
//
// Parses the command string and calls the appropriate functions.
//
//-----------------------------------------------------------------------------
uint8_t CommandDecode(char * instr)
{
	char * cp;						// character pointer
	const DEVICE_FAMILY *dfptr;		// pointer to current device family
	const DERIVATIVE_ENTRY *deptr;	// pointer to current derivative
	uint8_t command_number = 99;	// number of command encoded in 'instr'
	uint8_t result = INVALID_COMMAND;

	printf("Command: %s\n", instr);
	command_number = 0xFF;
	if (instr[0] >= '0' && instr[0] <= '9')
	{
		// if first char is a digit, then interpret it as a command number
		command_number = atoi (instr);
	}

	{
		// interpret command as a string and find command number
		// or find command by command_number
		const COMMAND *ctptr = Commands;
		while (ctptr->name != NULL)
		{
			if (command_number != 0xFF)
			{
				if (ctptr->number == command_number)
					break;
			}
			else if (strncmp (instr, ctptr->name, ctptr->name_size) == 0)
			{	// we've found the command, so record its number and exit
				command_number = ctptr->number;
				break;
			}
			ctptr++;
		}
		if (ctptr->name != NULL)
		{
			if (ctptr->need_discover && !FamilyFound)
			{
				result = DEVICE_UNDISCOVERED;
				command_number = 0xFE;	// Unknown command
			}
		}
		else
			command_number = 0xFF;	// Unknown command
	}

	// Now we have a command number, so act on it.
	switch (command_number)
	{
		case 0:	// Device Autodetect
		{
			uint8_t deviceId = 0xFF;		// initialize device and derivative
			uint8_t revisionId = 0xFF;
			uint8_t derivativeId = 0xFF;	// id's to invalid selections

			printf("Device Autodetect\n");
			Start_Stopwatch();
			if (NO_ERROR == (result = C2_Halt ())
			&&	NO_ERROR == (result = C2_Discover (&deviceId, &revisionId, &derivativeId))
				)
			{
				CommandsList = KnownFamilies[FamilyNumber].InitStrings;
			}
			Stop_Stopwatch();
			printf("Device ID     : %02X\n", deviceId);
			printf("Revision ID   : %02X\n", revisionId);
			printf("Derivative ID : %02X\n", derivativeId);
			break;
		}
		case 1:	// Print Menu
		{
			printf("? stub\n");
			Start_Stopwatch();
			Display_Menu();
			Stop_Stopwatch();
			result = NO_ERROR;
			break;
		}
		case 2:	// Wait ms
		{
			uint16_t wait_time;
			cp = GetNextWord(instr);
			if (NO_ERROR == (result = CheckEmpty(cp)))
			{
				wait_time = atoi (cp);
				printf("Waiting %d ms\n", wait_time);
				Start_Stopwatch();
				Wait_ms (wait_time);
				Stop_Stopwatch();
				printf("Stopwatch_ms is %u\n", Stopwatch_ms);
				result = NO_ERROR;
			}
			break;
		}
		case 3:	// Wait us
		{
			uint16_t wait_time;
			cp = GetNextWord(instr);
			if (NO_ERROR == (result = CheckEmpty(cp)))
			{
				wait_time = atoi (cp);

				printf("Waiting %d us\n", wait_time);
				Start_Stopwatch();
				Wait_us (wait_time);
				Stop_Stopwatch();
				printf("Stopwatch_us is %u\n", Stopwatch_us);

				result = NO_ERROR;
			}
			break;
		}
		case 4:	// Start Stopwatch
		{
			printf("Start Stopwatch\n");
			result = Start_Stopwatch();
			break;
		}
		case 5:	// Stop Stopwatch
		{
			printf("Stop Stopwatch\n");
			result = Stop_Stopwatch();
			printf("Stopwatch_ms is %u\n", Stopwatch_ms);
			printf("Stopwatch_us is %u\n", Stopwatch_us);
			break;
		}
		case 6:	// Set Timeout ms
		{
			uint16_t wait_time;
			printf("Set Timeout ms:\n");
			cp = GetNextWord(instr);
			if (NO_ERROR == (result = CheckEmpty(cp)))
			{
				wait_time = atoi (cp);

				printf("Timing out for %d ms\n", wait_time);
				Start_Stopwatch();
				SetTimeout_ms (wait_time);
				SetTimeout_us (1);
				while (!IsDoneTimeout_ms())
					;
				Stop_Stopwatch();
				printf("Stopwatch_ms is %u\n", Stopwatch_ms);
				result = NO_ERROR;
			}
			break;
		}
		case 7:	// Set Timeout us
		{
			uint16_t wait_time;
			printf("Set Timeout us\n");
			cp = GetNextWord(instr);
			if (NO_ERROR == (result = CheckEmpty(cp)))
			{
				wait_time = atoi(cp);

				printf("Timing out for %d us\n", wait_time);
				Start_Stopwatch();
				SetTimeout_us(wait_time);
				while (!IsDoneTimeout_us())
					;
				Stop_Stopwatch();
				printf("Stopwatch_us is %u\n", Stopwatch_us);

				result = NO_ERROR;
			}
			break;
		}
		case 8:		// Pin init
		{
			printf("Pin Init\n");
			result = Pin_Init();
			break;
		}
		case 9:		// C2 Reset
		{
			printf("C2 Reset\n");
			result = C2_Reset();
			break;
		}
		case 10:	// C2 Write Address
		{
			uint16_t addr;
			cp = GetNextWord(instr);
			if (NO_ERROR == (result = CheckEmpty(cp)))
			{
				addr = atox (cp);

				printf("C2 Write Address: %02X\n", addr);
				Start_Stopwatch();
				result = C2_WriteAR(addr);
				Stop_Stopwatch();
			}
			break;
		}
		case 11:	// C2 Read Address
		{
			Start_Stopwatch();
			result = C2_ReadAR();
			Stop_Stopwatch();
			printf("C2 Read Address: %02X\n", (uint16_t) C2_AR);
			break;
		}
		case 12:	// C2 Write Data
		{
			uint8_t data;
			cp = GetNextWord(instr);
			if (NO_ERROR == (result = CheckEmpty(cp)))
			{
				data = atox (cp);

				printf("C2 Write Data: %02X\n", (uint16_t) data);
				Start_Stopwatch ();
				result = C2_WriteDR (data, C2_WRITE_DR_TIMEOUT_US);
				Stop_Stopwatch ();
			}
			break;
		}
		case 13:	// C2 Read Data
		{
			Start_Stopwatch ();
			result = C2_ReadDR (C2_READ_DR_TIMEOUT_US);
			Stop_Stopwatch ();
			printf("C2 Read Data: %02X\n", (uint16_t) C2_DR);
			break;
		}
		case 14:	// C2 Reset and Halt
		{
			printf("C2 Reset and Halt\n");
			result = C2_Halt ();
			break;
		}
		case 15:	// C2 Get Device ID
		{
			uint8_t devId;

			printf("C2 Get Device ID\n");
			Start_Stopwatch ();
			result = C2_GetDevID (&devId);
			Stop_Stopwatch ();
			printf("Device ID is %u, 0x%04X\n", devId, devId);
			break;
		}
		case 16:	// C2 Get Revision ID
		{
			uint8_t revid;
			printf("C2 Get Revision ID\n");
			Start_Stopwatch ();
			result = C2_GetRevID (&revid);
			Stop_Stopwatch ();
			printf("Revision ID is %u, 0x%04X\n", revid, revid);
			break;
		}
		case 17:	// C2 Read SFR
		{
			uint8_t sfr_value, sfr_address;
			cp = GetNextWord(instr);
			if (NO_ERROR == (result = CheckEmpty(cp)))
			{
				sfr_address = atox (cp);

				Start_Stopwatch ();
				result = C2_ReadSFR (sfr_address, &sfr_value);
				Stop_Stopwatch ();
				printf("C2 Read SFR(%02X) %02X\n", (uint16_t) sfr_address, (uint16_t) sfr_value);
			}
			break;
		}
		case 18:	// C2 Write SFR
		{
			uint8_t sfr_address, sfr_value;

			cp = GetNextWord(instr);
			if (NO_ERROR == (result = CheckEmpty(cp)))
			{
				sfr_address = atox (cp);
				cp = GetNextWord(cp);
				if (NO_ERROR == (result = CheckEmpty(cp)))
				{
					sfr_value = atox (cp);

					printf("C2 Write %02X to SFR(%02X)\n", (uint16_t) sfr_value, (uint16_t) sfr_address);
					Start_Stopwatch ();
					result = C2_WriteSFR (sfr_address, sfr_value);
					Stop_Stopwatch ();
				}
			}
			break;
		}
		case 19:	// C2 Read Direct
		{
			uint8_t sfr_value, sfr_address;
			cp = GetNextWord(instr);
			if (NO_ERROR == (result = CheckEmpty(cp)))
			{
				sfr_address = atox (cp);

				Start_Stopwatch ();
				result = C2_ReadDirect (sfr_address, &sfr_value, C2_DIRECT);
				Stop_Stopwatch ();
				printf("C2 Read Direct(%02X) %02X\n", (uint16_t) sfr_address, (uint16_t) sfr_value);
			}
			break;
		}
		case 20:	// C2 Write Direct <address> <value>
		{
			uint8_t sfr_address, sfr_value;

			cp = GetNextWord(instr);
			if (NO_ERROR == (result = CheckEmpty(cp)))
			{
				sfr_address = atox (cp);
				cp = GetNextWord(cp);
				if (NO_ERROR == (result = CheckEmpty(cp)))
				{
					sfr_value = atox (cp);

					printf("C2 Write %02x to Direct(%02X)\n", (uint16_t) sfr_value, (uint16_t) sfr_address);
					Start_Stopwatch ();
					result = C2_WriteDirect (sfr_address, sfr_value, C2_DIRECT);
					Stop_Stopwatch ();
				}
			}
			break;
		}
		case 21:	// C2 Read Indirect
		{
			uint8_t sfr_value, sfr_address;
			
			cp = GetNextWord(instr);
			if (NO_ERROR == (result = CheckEmpty(cp)))
			{
				sfr_address = atox (cp);

				Start_Stopwatch ();
				result = C2_ReadDirect (sfr_address, &sfr_value, C2_INDIRECT);
				Stop_Stopwatch ();
				printf("C2 Read Indirect(%02X) %02X\n", (uint16_t) sfr_address, (uint16_t) sfr_value);
			}
			break;
		}
		case 22:	// C2 Write Indirect
		{
			uint8_t sfr_address;
			uint8_t sfr_value;

			cp = GetNextWord(instr);
			if (NO_ERROR == (result = CheckEmpty(cp)))
			{
				sfr_address = atox (cp);
				cp = GetNextWord(cp);
				if (NO_ERROR == (result = CheckEmpty(cp)))
				{
					sfr_value = atox (cp);

					printf("C2 Write %02x to Indirect(%02X)\n", (uint16_t) sfr_value, (uint16_t) sfr_address);
					Start_Stopwatch ();
					result = C2_WriteDirect (sfr_address, sfr_value, C2_INDIRECT);
					Stop_Stopwatch ();
				}
			}
			break;
		}
		case 23:	// C2 Discover
		{
			uint8_t j, deviceId, revisionId, derivativeId;

			printf("C2 Discover\n");
			Start_Stopwatch ();
			result = C2_Discover (&deviceId, &revisionId, &derivativeId);
			Stop_Stopwatch ();

			if (result != NO_ERROR)
				break;

			dfptr = &(KnownFamilies[FamilyNumber]);
			deptr = &(KnownFamilies[FamilyNumber].DerivativeList[DerivativeNumber]);

			printf("Family Information:\n");
			printf("Device ID: 0x%04X\n", dfptr->DEVICE_ID);
			printf("Family string: %s\n", dfptr->FAMILY_STRING);
			printf("Mem Type: %u\n", (uint16_t) dfptr->MEM_TYPE);
			printf("Page Size: %u\n", dfptr->PAGE_SIZE);
			printf("Has SFLE: %u\n", (uint16_t) dfptr->HAS_SFLE);
			printf("Security Type: %u\n", (uint16_t) dfptr->SECURITY_TYPE);
			printf("FPDAT address: 0x%02X\n", (uint16_t) dfptr->FPDAT);
			printf("Device ID: 0x%04X\n", dfptr->DEVICE_ID);
			printf("Init strings:\n");
			for (j = 0; ; j++)
			{
				if (dfptr->InitStrings[j] == NULL)
					break;
				printf("%s\n", dfptr->InitStrings[j]);
			}
			printf("\n");
			printf("Derivative Information\n");
			printf("----------------------\n");
			printf("Derivative ID        : %02X\n", deptr->DERIVATIVE_ID);
			printf("Derivative String    : %s\n", deptr->DERIVATIVE_STRING);
			printf("Features String      : %s\n", deptr->FEATURES_STRING);
			printf("Package String       : %s \n", deptr->PACKAGE_STRING);
			printf("Code Start Address   : %05X\n", deptr->CODE_START);
			printf("Code Size            : %05X\n", deptr->CODE_SIZE);
			printf("Write Lock Byte Addr : %05X\n", deptr->WRITELOCKBYTEADDR);
			printf("Read Lock Byte Addr  : %05X\n", deptr->READLOCKBYTEADDR);
			printf("Code 2 Start Address : %05X\n", deptr->CODE2_START);
			printf("Code 2 Size          : %05X\n", deptr->CODE2_SIZE);
			printf("\n");

			break;
		}
		case 24:	// Run Init String
		{
			result = NO_ERROR;
			printf("Execute Device Init String:\n");
			if (FamilyFound == true)
				CommandsList = KnownFamilies[FamilyNumber].InitStrings;
			else
				printf("Device not connected.\n");
			break;
		}
		case 25:	// C2 Flash Read <start addr> <length>
		{
			uint32_t addr;
			uint16_t length;

			printf("C2 Flash Read\n");
			cp = GetNextWord(instr);
			if (NO_ERROR == (result = CheckEmpty(cp)))
			{
				addr = atolx (cp);
				cp = GetNextWord(cp);
				if (NO_ERROR == (result = CheckEmpty(cp)))
				{
					length = atoi (cp);
					if (length > sizeof (BinDest))
						length = sizeof (BinDest);

					printf(
						"Reading %u bytes starting at address 0x%05lX\n",
						length,
						(unsigned long)addr
					);
					Start_Stopwatch ();
					result = C2_FLASH_Read (BinDest, addr, length);
					Stop_Stopwatch ();

					BIN2HEXSTR (HexDest, BinDest, length);
					printf("Memory contents are %s\n", HexDest);
				}
			}
			break;
		}
		case 26:	// C2 OTP Read <start addr> <length>
		{
			uint32_t addr;
			uint16_t length;

			printf("C2 OTP Read\n");
			cp = GetNextWord(instr);
			if (NO_ERROR == (result = CheckEmpty(cp)))
			{
				addr = atolx (cp);
				cp = GetNextWord(cp);
				if (NO_ERROR == (result = CheckEmpty(cp)))
				{
					length = atoi (cp);

					if (length > sizeof (BinDest))
						length = sizeof (BinDest);

					printf("Reading %u bytes starting at address 0x%05lX\n",
						length,
						(unsigned long)addr
					);
					Start_Stopwatch ();
					result = C2_OTP_Read (BinDest, addr, length);
					Stop_Stopwatch ();

					BIN2HEXSTR (HexDest, BinDest, length);
					printf("Memory contents are %s\n", HexDest);
				}
			}
			break;
		}
		case 27:	// C2 Flash Write <start addr> <hex string>
		{
			uint32_t addr;
			uint8_t length;

			printf("C2 Flash Write\n");
			cp = GetNextWord(instr);
			if (NO_ERROR == (result = CheckEmpty(cp)))
			{
				addr = atolx (cp);
				cp = GetNextWord(cp);
				if (NO_ERROR == (result = CheckEmpty(cp)))
				{
					// warning! 'dest' could be overtaken by a long string
					if (NO_ERROR == (result = HEXSTR2BIN (BinDest, cp, &length, sizeof(BinDest))))
					{
						printf("Writing %u bytes starting at address 0x%05X\n", length, addr);
						// printf("Writing the following string: %s\n", cp);
						Start_Stopwatch ();
						result = C2_FLASH_Write (addr, BinDest, length);
						Stop_Stopwatch ();
					}
				}
			}
			break;
		}
		case 28:	// C2 OTP Write <start addr> <hex string>
		{
			uint32_t addr;
			uint8_t length;

			printf("C2 OTP Write\n");
			cp = GetNextWord(instr);
			if (NO_ERROR == (result = CheckEmpty(cp)))
			{
				addr = atolx (cp);
				cp = GetNextWord(cp);
				if (NO_ERROR == (result = CheckEmpty(cp)))
				{

					if (NO_ERROR != (result = HEXSTR2BIN (BinDest, cp, &length, sizeof(BinDest))))
					{
						printf("Hex string too long");
						break;
					}

					printf(
						"Writing %u bytes starting at address 0x%05lX\n",
						(uint16_t) length,
						(unsigned long)addr
					);
					printf("Writing the following string: %s\n", cp);
					Start_Stopwatch ();
					result = C2_OTP_Write (addr, BinDest, length);
					Stop_Stopwatch ();
				}
			}
			break;
		}
		case 29:	// C2 Page Erase <address in page to erase>
		{
			uint32_t addr;

			printf("C2 Flash Page Erase\n");
			cp = GetNextWord(instr);
			if (NO_ERROR == (result = CheckEmpty(cp)))
			{
				addr = atolx (cp);

				printf("Erasing page containing address 0x%05X\n", addr);
				Start_Stopwatch ();
				result = C2_FLASH_PageErase (addr);
				Stop_Stopwatch ();
			}
			break;
		}
		case 30:	// C2 Device Erase
		{
			printf("C2 Flash Device Erase\n");

			printf("Erasing device...\n");
			Start_Stopwatch ();
			result = C2_FLASH_DeviceErase ();
			Stop_Stopwatch ();

			break;
		}
		case 31:	// C2 Flash Blank Check
		{
			uint32_t addr;
			uint32_t length;

			printf("C2 Flash Blank Check\n");

			addr = KnownFamilies[FamilyNumber].DerivativeList[DerivativeNumber].CODE_START;
			length = KnownFamilies[FamilyNumber].DerivativeList[DerivativeNumber].CODE_SIZE;

			printf("Checking starting at address 0x%05X for 0x%05X bytes: ", addr, length);
			Start_Stopwatch ();
			result = C2_FLASH_BlankCheck (addr, length);
			Stop_Stopwatch ();

			printf((result == DEVICE_IS_BLANK) ? "OK\n" : "Fail\n");
			break;
		}
		case 32:	// C2 OTP Blank Check
		{
			uint32_t addr;
			uint32_t length;

			printf("C2 OTP Blank Check\n");

			addr = KnownFamilies[FamilyNumber].DerivativeList[DerivativeNumber].CODE_START;
			length = KnownFamilies[FamilyNumber].DerivativeList[DerivativeNumber].CODE_SIZE;

			printf("Checking starting at address 0x%05X for 0x%05X bytes: ", addr, length);

			Start_Stopwatch ();
			result = C2_OTP_BlankCheck (addr, length);
			Stop_Stopwatch ();

			printf((result == NO_ERROR) ? "OK\n" : "Fail\n");
			break;
		}
		case 33:	// C2 Get Lock Byte value
		{
			printf("C2 Get Lock Byte\n");
			break;
		}
		case 34:	// Write Target to HEX
		{
			printf("Write Target to HEX:\n");
			Start_Stopwatch ();
			result = OP_Write_TARGET2HEX();
			Stop_Stopwatch ();

			break;
		}
		case 36:	// Write HEX to Target
		{
			HEX_RECORD hex;

			printf("Write HEX to Target:\n");

			cp = GetNextWord(instr);
			if (NO_ERROR == (result = CheckEmpty(cp)))
			{
				hex.Buf = BinDest;
				result = HEX_Decode(&hex, cp, sizeof(BinDest));
				if (result == NO_ERROR && hex.RECLEN != 0)
				{
					printf("Writing %u bytes starting at address 0x%05X\n", hex.RECLEN, hex.OFFSET.U16);
					Start_Stopwatch ();
					result = C2_FLASH_Write (hex.OFFSET.U16, hex.Buf, hex.RECLEN);
					Stop_Stopwatch ();
				}
				else if (result == EOF_HEX_RECORD)
					result = NO_ERROR;
			}
			break;
		}
		case 35:	// Read SFRs and directs
		{
			uint8_t row;
			uint8_t col;
			uint8_t value;

			Start_Stopwatch ();
			for (row = 0xF8; row != 0x00; row = row - 8)
			{
				for (col = 0; col != 0x08; col++)
				{
					if (NO_ERROR != (result = C2_ReadDirect ((row+col), &value, C2_DIRECT)))
						break;

					if (col == 0)
						printf("\n0X%02X: %02X", (uint16_t) (row), (uint16_t) value);
					else
						printf(" %02X", (uint16_t) value);
				}
			}
			printf("\n\n");
			Stop_Stopwatch ();
			break;
		}
		case 0xFE:
			break;
		default:
		{
			result = INVALID_COMMAND;
		}
	}
	printf("Result: %02X %s\n", result, GetErrorName(result));
	return result;
}
/******************主程序********************************/
void main(void)
{
   Pin_Init();
   T0_Init();
   Uart_Init();
   Uart2_Init();
   Uart_AppInit();
   Uart2_AppInit();
   MdTcbInit();
   MdTcbInit2();
   AdcInit();
   PWMInit();
   FCInitOne();
   while (1)
   {
      TimeDeal(&FC1);
      MdPoll();
      MdMasterPoll();
      FCKeyScan(&FC1);
      ResetDeal(&FC1);
      FCURun(&FC1);
      FCRelayOutput(&FC1);
      if (FC1.Buff.Flag.solo._2msFlag)
      {
//        FC2IOScan(&FC1);
          swih ^= 1;
          PORTB_PORTB4 = swih;
         TestSpeed();
         UartTimeOut(&UartAppData1);
         UartTimeOut(&UartAppData2);
      }
      if (FC1.Buff.Flag.solo._4msFlag)
      {
         InputDeal();
      }
      if (FC1.Buff.Flag.solo._10msFlag)
      {
         FCWorkTime(&FC1);
      }
      if (FC1.Buff.Flag.solo._20msFlag)
      {
         AdcGetAll(&FC1);
         FCAnalogOutSet(&FC1);
         EePromDeal(&FC1.Run, sizeof(FC_RUNING_Def) - CRC_LEN, &EeSave1);
      }
      if (FC1.Buff.Flag.solo._10000msFlag)
      {
         uint16 tmpCrc = FC1.Run.RunCrc;
         uint16 tmpCrcNew = GetCRC16((uint08 *)&FC1.Run, sizeof(FC_RUNING_Def) - CRC_LEN);
         if (tmpCrc != tmpCrcNew)
         {
            EeWriteTrg(&EeSave1);
         }
      }
      if (FC1.Buff.Flag.solo._nmsFlag)
      {
          speedPerMin = speedCnt * 60 / 4 / (nTimeSpeed / 100);
          speedCnt = 0;
      }

      FC1.Buff.Flag.All = 0;
   }
}