예제 #1
0
파일: init.c 프로젝트: wsj-zz/FamesOS
/*----------------------------------------------------------------------------------------------
 * 函数:    InitFamesOS()
 *
 * 说明:    初始化FamesOS
 *
 * 备注:    下面3个函数都由main()函数调用, 应用程序不可调用
**--------------------------------------------------------------------------------------------*/
void apical InitFamesOS(void)
{
    CALLED_ONLY_ONCE();
    FamesOSStarted=NO;
    InitializeOnExit(); /* should call this before others */
    InitMemService();
    InitIrqManager();
    InitTasks(); 
    InitSwitch();
    InitTimeTick();
    InitApplication();
}
예제 #2
0
파일: mcu.c 프로젝트: Inspirati/MatrixPilot
void mcu_init(void)
{
#if (MIPS == 64)
#warning Fast OSC selected
	PLLFBD = 62;                // M  = 64
#elif (MIPS == 32)
#warning Medium OSC selected
	PLLFBD = 30;                // M  = 32
#elif (MIPS == 16)
#warning Slow OSC selected
	PLLFBD = 14;                // M  = 16
#else
#error Invalid MIPS Configuration
#endif // MIPS
	CLKDIVbits.PLLPOST = 0;     // N1 = 2
	CLKDIVbits.PLLPRE = 0;      // N2 = 2
	OSCTUN = 0;

	__builtin_write_OSCCONH(0x03);  // Initiate Clock Switch to Primary Oscillator with PLL (NOSC= 0x3)
	__builtin_write_OSCCONL(0x01);  // Start clock switching
	while (OSCCONbits.COSC != 0x3);

	// Wait for Clock switch to occur and for PLL to lock
//	while (OSCCONbits.LOCK != 1);

	// new RobD - disable all analog inputs
	ANSELA = 0x0000;
	ANSELB = 0x0000;
	ANSELC = 0x0000;
	ANSELD = 0x0000;
	ANSELE = 0x0000;
	ANSELG = 0x0000;

	// Configuring the auxiliary PLL, since the primary
	// oscillator provides the source clock to the auxiliary
	// PLL, the auxiliary oscillator is disabled. Note that
	// the AUX PLL is enabled. The input 8MHz clock is divided
	// by 2, multiplied by 24 and then divided by 2. Wait till 
	// the AUX PLL locks.
	ACLKCON3 = 0x24C1;
	ACLKDIV3 = 0x7;
	ACLKCON3bits.ENAPLL = 1;
	while (ACLKCON3bits.APLLCK != 1);

	configurePPS();
	init_dataflash();
	configureDigitalIO();
	InitSwitch();

	_LATB2 = LED_OFF; _LATB3 = LED_OFF; _LATB4 = LED_OFF; _LATB5 = LED_OFF; 
	_TRISB2 = 0; _TRISB3 = 0; _TRISB4 = 0; _TRISB5 = 0;

	T1CONbits.TON = 0;      // ensure timer is off
	T1CONbits.TGATE = 0;
	T1CONbits.TCKPS = 3;    // set prescaler
	T1CONbits.TCS = 0;      // use the crystal to drive the clock
	PR1 = 0xFFFF;           // set period register
	T1CONbits.TON = 1;      // turn on timer 1
/*
	__C30_UART = CONSOLE_UART;

	U3BRG = BAUDRATEREG;
	U3MODE = 0;
	U3MODEbits.BRGH = BRGHX;
	U3STA = 0;
	U3MODEbits.UARTEN = 1;
	U3STAbits.UTXEN = 1;
	IFS5bits.U3RXIF = 0;

//	printf("\r\nAUAV3 " __TIME__ " " __DATE__ " @ %u mips\r\n", MIPS);
 */
}
/*--------------------------------------------------------------------------------------------------*/
int InitEmoro(void){
	int ret_value = 0;
	int ADC_read;
	long Vin, Vin_decimal;

	char lcd[20];
	unsigned char d[7], chk=0;
	
	
	GLAM_EEPROMReadBytes(121,7, d);					/* address 121 -> LCD, 122 -> switches			*/
													/* 123 -> Accelerometer, 124 -> Gyroscope		*/
													/* 125 -> Compas, 126 -> Bluetooth, 127 -> Chk	*/
	for(unsigned char n=0; n<7; n++)				/* calculate checksum							*/
		chk^=*(d+n);
	
	if(chk^0xAA == 0){
		if(d[0] == 0xC8){							/* check LCD									*/
			if(Lcd.init() == 0){					/* initialize lcd								*/
				Lcd.backlightOn();					/* turn on lcd backlight						*/
				ret_value |= (LCD_AVAILABLE);		/* set return value -> LCD Available			*/
				initialized_emoro_hw = ret_value;
			}
		}
		if(d[1] == 0xC8){							/* check switches								*/
			InitSwitch();							/* initialize switch (SW1 - SW4)				*/
			ret_value |= (SW_AVAILABLE);			/* set return value -> Switch Available			*/
		}
		if(Acc.testConnection() & d[2] == 0xC8){	/* test Acc and check 							*/
			Acc.init();								/* initialize acceleration sensor BMA150		*/
			ret_value |= (ACC_AVAILABLE);			/* set return value -> ACC Available			*/
		}
		if(Gyr.testConnection() & d[3] == 0xC8){	/* test Gyr and check 							*/
			Gyr.initBasic();						/* initialize gyroscope L3GD20					*/
			ret_value |= (GYR_AVAILABLE);			/* set return value -> GYR Available			*/
		}
		if(Mag.testConnection() & d[4] == 0xC8){	/* test magnetometer and check 					*/
			Mag.init();								/* initialize magnetometer						*/
			ret_value |= (MAG_AVAILABLE);			/* set return value -> MAG Available			*/
		}
		if(d[5] == 0xC8){
			if(Bluetooth.init() == 0){				/* initialize bluetooth communication			*/				
				ret_value |= (BLUETOOTH_AVAILABLE);	/* set return value -> Bluetooth Available		*/
			}
		}
		
		ADC_read=CheckVoltage();						/* read input voltage				*/
		Vin=(long)(ADC_read*15/1024);					/* calculate voltage; whole number	*/
		Vin_decimal=(long)ADC_read*1500/1024-Vin*100;	/* calculate decimal places			*/
		if( Vin >= 5 & (Vin == 6 & Vin_decimal<20) ){	/* if Vin>=5V & Vin < 6.20V batt is */
			sprintf(lcd, "Vin=%d.%d", (int)Vin, (int)Vin_decimal);
			Serial.begin(9600);				    /* initialize Serial monitor UART 9600 bps	        */
			Serial.println((char*)"Voltage low:");
			Serial.println(lcd);
			if(ret_value & LCD_AVAILABLE){
				Lcd.clear();								/* low, for Vin<5V:USB power supply */
				Lcd.printString((char*)"Voltage low:");
				Lcd.locate(1,0);
				Lcd.printString(lcd);
			}
			for (int rep=0; rep<2; rep++){
				tone(BUZ_BUILTIN, NOTE_C6, 50); _delay_ms(100); /* Mores code letter S(...) */
				tone(BUZ_BUILTIN, NOTE_C6, 50); _delay_ms(100);
				tone(BUZ_BUILTIN, NOTE_C6, 50); _delay_ms(200);
					
				tone(BUZ_BUILTIN, NOTE_C6, 150); _delay_ms(200);/* Mores code letter O(---) */
				tone(BUZ_BUILTIN, NOTE_C6, 150); _delay_ms(200);
				tone(BUZ_BUILTIN, NOTE_C6, 150); _delay_ms(300);
					
				tone(BUZ_BUILTIN, NOTE_C6, 50); _delay_ms(100);	/* Mores code letter S(...) */
				tone(BUZ_BUILTIN, NOTE_C6, 50); _delay_ms(100);
				tone(BUZ_BUILTIN, NOTE_C6, 50); _delay_ms(400);
			}
			noTone(BUZ_BUILTIN);
			
			Lcd.clear();
			
		}
	}

	if(ret_value & MAG_AVAILABLE){					
		
		if(ReadSwitch(SW_2) == 1){				/* magnetometer calibration request?				*/
			Serial.begin(9600);
			Serial.println((char*)"Compass");          
			Serial.println((char*)"Calibration.....\n"); 
			if(ret_value & LCD_AVAILABLE){
				Lcd.clear();
				Lcd.printString((char*)"Compass         ");
				Lcd.locate(1,0);
				Lcd.printString((char*)"Calibration.....");
			}
			while(ReadSwitch(SW_2)==1);			/* wait button release								*/
			_delay_ms(300);
			
			/* mag calibration step 1																*/
			Serial.println((char*)"Lay board flat");          
			Serial.println((char*)"Press button 1\n");          
			if(ret_value & LCD_AVAILABLE){
				Lcd.clear();
				Lcd.printString((char*)"Lay board flat");
				Lcd.locate(1,0);
				Lcd.printString((char*)"Press button 1");
			}
			WaitSwitch(SW_1);	
			Mag.calibrate(1);
			_delay_ms(500);
			
			/* mag calibration step 2																*/
			Serial.println((char*)"Rotate 180 degre");          
			Serial.println((char*)"Press button 1\n");          
			if(ret_value & LCD_AVAILABLE){
				Lcd.clear();
				Lcd.printString((char*)"Rotate 180 degre");
				Lcd.locate(1,0);
				Lcd.printString((char*)"Press button 1");						
			}
			WaitSwitch(SW_1);	
			Mag.calibrate(2);
			_delay_ms(300);
			
			/* mag calibration step 2																*/
			Serial.println((char*)"Flip board");          
			Serial.println((char*)"Press button 1\n");          
			if(ret_value & LCD_AVAILABLE){
				Lcd.clear();
				Lcd.printString((char*)"Flip board");
				Lcd.locate(1,0);
				Lcd.printString((char*)"Press button 1");						
			}
			WaitSwitch(SW_1);	
			Mag.calibrate(3);
			_delay_ms(300);
			
			Serial.println((char*)"Calibration");          
			Serial.println((char*)"Complete\n");          
			if(ret_value & LCD_AVAILABLE){
				Lcd.clear();
				Lcd.printString((char*)"Calibration     ");	
				Lcd.locate(1,0);
				Lcd.printString((char*)"Complete        ");	
			}
			_delay_ms(1500);	
			Serial.println((char*)"Press button 1\n");          
			if(ret_value & LCD_AVAILABLE){
				Lcd.clear();	
				Lcd.locate(1,0);
				Lcd.printString((char*)"Press button 1");
			}
			while(ReadSwitch(SW_1)==0){
				int dir, inc, str;
				Mag.read(&dir, &inc, &str);
				sprintf(lcd, "Direction=%3d", dir);
				Serial.println(lcd);  
				if(ret_value & LCD_AVAILABLE){
					Lcd.locate(0, 0);
					Lcd.printString(lcd);
				}				        
			}
			Lcd.clear();
		}
	}		
	initialized_emoro_hw = ret_value; 
	return initialized_emoro_hw;	
}