示例#1
0
文件: Main.c 项目: nuaaabb/nuaabb
void main(void)
{
	InitSysCtrl();
// Copy time critical code and Flash setup code to RAM
// This includes the following ISR functions: epwm1_timer_isr(), epwm2_timer_isr()
// epwm3_timer_isr and and InitFlash();
// The  RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart
// symbols are created by the linker. Refer to the F28335.cmd file.

// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
	DINT;
// Initialize the PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2833x_PieCtrl.c file.
	InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
	IER = 0x0000;
	IFR = 0x0000;
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example.  This is useful for debug purposes.
// The shell ISR routines are found in DSP2833x_DefaultIsr.c.
// This function is found in DSP2833x_PieVect.c.
	InitPieVectTable();
// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
	EALLOW;  // This is needed to write to EALLOW protected registers
	PieVectTable.SEQ1INT = &PWM_AD_isr;
	PieVectTable.ECAN0INTA = &Skiip4_CAN_isr;
	EDIS;     // This is needed to disable write to EALLOW protected registers 
	
	InitFlash();
	MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);
	MemCopy(&IQmathLoadStart, &IQmathLoadEnd, &IQmathRunStart);

	//ECan-A模块初始化
	InitECan();
	InitECan1();

	InitGpio();  // Skipped for this example 
	InitAdc();							// Initialize necessary ADC module, directly for SVPWM.
	InitEPwm();
  



	IER |= M_INT1; 
// Enable SEQ1_INT which is connected to PIE1.1:
    IER |= M_INT9;
// Enable eCAN0-A INT which is connected to PIE9.5:


	EINT;
} // end of main()
示例#2
0
void InitMicrocontroller(void)
{
	//copy InitFlash() to ram
	MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);
	InitFlash();
	InitSysCtrl();	//calibrate ADC, enable peripheral clocks, etc.
	DINT; // Disable CPU interrupts
   	// Initialize PIE control registers to all PIE interrupts disabled and flags clear
   	InitPieCtrl();
	// Disable CPU interrupts and clear all CPU interrupt flags:
   	IER = 0x0000;
   	IFR = 0x0000;
   	// Initialize the PIE vector table with pointers to the shell Interrupt
	// Service Routines (ISR).
   	InitPieVectTable();
}
示例#3
0
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	// Set current directory to module directory
	wchar_t imagePath[MAX_PATH];
	GetModuleFileName(NULL, imagePath, MAX_PATH);
	size_t pathLen = wcslen(imagePath);
	wchar_t* pathEnd = imagePath + pathLen - 1;
	while (pathEnd >= imagePath && (*pathEnd != L'\\' && *pathEnd != L'/'))
		--pathEnd;

	if (pathEnd >= imagePath)
		*pathEnd++ = 0;

	SetCurrentDirectory(imagePath);
	
	InitWindow(hInstance, nCmdShow);

	InitDevice();
	
	InitFlash();

	ShowWindow(hWnd, nCmdShow);


	while(true) {
		MSG msg;
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != 0) {
			if(msg.message == WM_QUIT)
				break;
			else {
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
		else 
		{
			DrawFrame();
		}
	}

	DeinitFlash();
	DeinitDevice();

// 	UnregisterClass(wc.lpszClassName, hInstance);

	return 0;
}
示例#4
0
文件: main.c 项目: 14Ohm/Energia
Uint32 main(void) {
	//GPIO and SCI are still setup from Sci_Boot()
	//Setup sysctl and pll
   DisableDog();
   IntOsc1Sel();
   InitPll(DSP28_PLLCR,DSP28_DIVSEL);
   InitFlash();

   DELAY_US(100);

//	ApplicationPtr = (void(*)(void))SCI_Boot();
//
//	if(ApplicationPtr)
//		ApplicationPtr();

	return SCI_Boot();

//	asm(" .ref _ExitBoot");
//    asm(" BF  _ExitBoot,UNC");

//   return 0;

}
示例#5
0
void main()
{
	int i;
	InitSysCtrl();
	InitGpio();
	DINT;					//禁止全局中断
	InitPieCtrl();			//将PIE控制寄存器初始化为默认状态
	IER=0x0000;				//禁止CPU中断(IER:CPU级中断使能寄存器)
	IFR=0x0000;				//清除CPU中断标志位(IFR:CPU级中断标志寄存器)
	InitPieVectTable(); 	//初始化中断向量表
	EALLOW;					//确认中断函数入口地址

	/***********车哥的是把MemoryCopy()放在这里************/

	PieVectTable.T1PINT = &renew_PWM_isr;
	PieVectTable.RXAINT = &scia_receive_isr;
	PieVectTable.TXAINT = &scia_send_isr;
	PieVectTable.RXBINT = &scib_receive_isr;
	PieVectTable.TXBINT = &scib_send_isr;
	//PieVectTable.CAPINT5= &measure_height_isr;
	PieVectTable.TINT0  = &cpu_timer0_isr;
//	PieVectTable.ADCINT = &adc_isr;

	EDIS;
	InitPeripherals();
	ConfigCpuTimer(&CpuTimer0, 150, 1000000);
	StartCpuTimer0();

	// Configure ADC
//	AdcRegs.ADCMAXCONV.all = 0x0001;       // Setup 2 conv's on SEQ1
//	AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 0x3; // Setup ADCINA3 as 1st SEQ1 conv.
//	AdcRegs.ADCCHSELSEQ1.bit.CONV01 = 0x2; // Setup ADCINA2 as 2nd SEQ1 conv.
//	AdcRegs.ADCTRL2.bit.EVA_SOC_SEQ1 = 1;  // Enable EVASOC to start SEQ1
//	AdcRegs.ADCTRL2.bit.INT_ENA_SEQ1 = 1;  // Enable SEQ1 interrupt (every EOS)

//	EvaRegs.GPTCONA.bit.T1TOADC = 1;       // Enable EVASOC in EVA
	for(i=0;i<11;i++)  	//初始化数据变量
	{
	    bufferA[i] = 0;
	    bufferB[i] = 0;
    }

	MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);
	InitFlash();

	PieCtrlRegs.PIEIER2.bit.INTx4=1;	//使能INT2 INTx4 T1PINT
	PieCtrlRegs.PIEIER9.bit.INTx1=1;  	//使能PIE模块中的SCIA接收中断
	PieCtrlRegs.PIEIER9.bit.INTx2=1;  	//使能PIE模块中的SCIA发送中断
	PieCtrlRegs.PIEIER9.bit.INTx3=1;	//使能PIE模块中的SCIB接收中断
	PieCtrlRegs.PIEIER9.bit.INTx4=1;	//使能PIE模块中的SCIB发送中断
//	PieCtrlRegs.PIEIER5.bit.INTx6=1;	//使能PIE模块中的CAP5捕获中断
	PieCtrlRegs.PIEIER1.bit.INTx7=1;	//使能PIE模块中的CPU定时器中断
	PieCtrlRegs.PIEIER1.bit.INTx6=1;	//使能PIE模块中的ADC中断
	IER|=M_INT9 | M_INT2  | M_INT1;		//开CPU中断

	EINT;  								//开全局中断
	ERTM;  								//开全局实时中断


	/*初始化pid_z模块	全局Q24
	pid_z.param.Kp  =_IQ(0.5);
	pid_z.param.Ki  =_IQ(0.0);
	pid_z.param.Kd  =_IQ(0);
	pid_z.param.Kr  =_IQ(1.0);
	pid_z.param.Km  =_IQ(1.0);
	pid_z.param.Umax=_IQ(1.0);
	pid_x.param.Umin=_IQ(1.0);

	//初始化pid_y模块	全局Q24
	pid_y.param.Kp  =_IQ(0.01);
	pid_y.param.Ki  =_IQ(0);
	pid_y.param.Kd  =_IQ(0);
	pid_y.param.Kr  =_IQ(1.0);
	pid_y.param.Km  =_IQ(1.0);
	pid_y.param.Umax=_IQ(10.0);
	pid_y.param.Umin=_IQ(10.0);

	//初始化pid_x模块	全局Q24
	pid_x.param.Kp  =_IQ(0.01);
	pid_x.param.Ki  =_IQ(0);
	pid_x.param.Kd  =_IQ(0);
	pid_x.param.Kr  =_IQ(1.0);
	pid_x.param.Km  =_IQ(1.0);
	pid_x.param.Umax=_IQ(10.0);
	pid_x.param.Umin=_IQ(10.0);
	*/
	/**************看门狗******************
	EALLOW;
	SysCtrlRegs.SCSR=1;
	EDIS;
	KickDog();
	EALLOW;
	SysCtrlRegs.WDCR =0x2F;				//使能看门狗
	EDIS;
	************************************/

	//DELAY_US(2000000);					//安全起见延时10s,现在延时2s做实验

	DELAY_US(5000000);				//delay 3s


//	EvaRegs.T1CMPR=0x5B8D;				//50%
//	EvaRegs.T2CMPR=0x5B8D;
//	EvbRegs.T3CMPR=0x5B8D;
//	EvbRegs.T4CMPR=0x5B8D;

//	EvaRegs.T1CMPR=soilgate;			//60%
//	EvaRegs.T2CMPR=soilgate;
//	EvbRegs.T3CMPR=soilgate;
//	EvbRegs.T4CMPR=soilgate;
//	DELAY_US(2000000);
/*
	EvaRegs.T1CMPR=0x7704;				//65%
	EvaRegs.T2CMPR=0x7704;
	EvbRegs.T3CMPR=0x7704;
	EvbRegs.T4CMPR=0x7704;
*/
	/**********************油门设置*************************
	EvaRegs.T1CMPR=0x927C;
	EvaRegs.T2CMPR=0x927C;
	EvbRegs.T3CMPR=0x927C;
	EvbRegs.T4CMPR=0x927C;
	DELAY_US(3000000);					//delay 2s
	EvaRegs.T1CMPR=0x493E;
	EvaRegs.T2CMPR=0x493E;
	EvbRegs.T3CMPR=0x493E;
	EvbRegs.T4CMPR=0x493E;
	****************************************************/



	//进入循环,等待中断
	for(;;)
	{
		DELAY_US(20000000);
		TakeoffSoilgate(23437);		//50%
		DELAY_US(2000000);
		TakeoffSoilgate(26906);		//起飞57.3%
		DELAY_US(10000000);
		TakeoffSoilgate(26250);		//软着陆
		DELAY_US(1000000);
		TakeoffSoilgate(25312);
		DELAY_US(1000000);
		TakeoffSoilgate(0x500E);
		DELAY_US(15000000);
		TakeoffSoilgate(26906);		//起飞
		b=-3.5;
		DELAY_US(6000000);
		TakeoffSoilgate(26250);		//软着陆
		DELAY_US(1000000);
		TakeoffSoilgate(25312);
		DELAY_US(1000000);
		TakeoffSoilgate(0x500E);
		DELAY_US(15000000);
//		TakeoffSoilgate(26906);		//起飞
		for(;;)
		{

		}
		/*
		switch(FlyFlag)
		{
			case 1:						//起飞

				 TakeoffSoilgate(809);
				 DELAY_US(2000000);
				 TakeoffSoilgate(23437);
				 break;
			case 2:						//开启定高PID,飞行20s
				 ;
				 break;
			case 3:						//降落
				 break;
			case 4:						//起飞
				 break;
			case 5:						//往回飞,改大点飞行角度
				 break;
			case 6:						//降落
				 break;
			case 7:						//起飞
				 break;
			case 8:						//飞行,此过程中有风扇
				 break;
			case 9:						//降落
				 break;
		}
		*/

	}//end for loop
}//end main
void main(void)
{

    DeviceInit(); // Device Life support & GPIO

// Only used if running from FLASH
// Note that the variable FLASH is defined by the compiler

#ifdef FLASH
    // Copy time critical code and Flash setup code to RAM
    // The  RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart
    // symbols are created by the linker. Refer to the linker files.
    MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);

    // Call Flash Initialization to setup flash waitstates
    // This function must reside in RAM
    InitFlash();  // Call the flash wrapper init function
#endif //(FLASH)

    // Waiting for enable flag set
    while (EnableFlag==FALSE)
    {
        BackTicker++;
    }

    // Timing sync for slow background tasks
    // Timer period definitions found in device specific PeripheralHeaderIncludes.h
    CpuTimer0Regs.PRD.all =  mSec1;    // A tasks
    CpuTimer1Regs.PRD.all =  mSec5;    // B tasks
    CpuTimer2Regs.PRD.all =  mSec50;   // C tasks

    // Tasks State-machine init
    Alpha_State_Ptr = &A0;
    A_Task_Ptr = &A1;
    B_Task_Ptr = &B1;
    C_Task_Ptr = &C1;

    // Initialize PWM module
    pwm1.PeriodMax = SYSTEM_FREQUENCY*1000000*T/2;  // Prescaler X1 (T1), ISR period = T x 1
    PWM_INIT_MACRO(pwm1)

    pwm2.PeriodMax = SYSTEM_FREQUENCY*1000000*T/2;  // Prescaler X1 (T1), ISR period = T x 1
    PWM_INIT_MACRO(pwm2)

    // Initialize PWMDAC module
    pwmdac1.PeriodMax = 500;   // @60Mhz: 1500->20kHz, 1000-> 30kHz, 500->60kHz
    pwmdac1.PwmDacInPointer0 = &PwmDacCh1;
    pwmdac1.PwmDacInPointer1 = &PwmDacCh2;

    PWMDAC_INIT_MACRO(pwmdac1)

    // Initialize DATALOG module
    dlog.iptr1 = &DlogCh1;
    dlog.iptr2 = &DlogCh2;
    dlog.iptr3 = &DlogCh3;
    dlog.iptr4 = &DlogCh4;
    dlog.trig_value = 0x1;
    dlog.size = 0x00c8;
    dlog.prescalar = 5;
    dlog.init(&dlog);

    // Initialize ADC module
    ADC_MACRO_INIT()

    // Initialize the PID_REG3 module for I
    pid1_i.Kp = _IQ(0.318);      //for 24V DC bus
    pid1_i.Ki = _IQ(T/0.0005);
    pid1_i.Kd = _IQ(0/T);
    pid1_i.Kc = _IQ(0.2);
    pid1_i.OutMax = _IQ(0.95);
    pid1_i.OutMin = _IQ(-0.95);

    pid2_i.Kp = _IQ(0.318);      //for 24V DC bus
    pid2_i.Ki = _IQ(T/0.0005);
    pid2_i.Kd = _IQ(0/T);
    pid2_i.Kc = _IQ(0.2);
    pid2_i.OutMax = _IQ(0.95);
    pid2_i.OutMin = _IQ(-0.95);

    // Reassign ISRs.

    EALLOW; // This is needed to write to EALLOW protected registers
    PieVectTable.EPWM1_INT = &MainISR;
    EDIS;

    // Enable PIE group 3 interrupt 1 for EPWM1_INT
    PieCtrlRegs.PIEIER3.bit.INTx1 = 1;

    // Enable CNT_zero interrupt using EPWM1 Time-base
    EPwm1Regs.ETSEL.bit.INTEN = 1;   // Enable EPWM1INT generation
    EPwm1Regs.ETSEL.bit.INTSEL = 1;  // Enable interrupt CNT_zero event
    EPwm1Regs.ETPS.bit.INTPRD = 1;   // Generate interrupt on the 1st event
    EPwm1Regs.ETCLR.bit.INT = 1;     // Enable more interrupts

    // Enable CPU INT3 for EPWM1_INT:
    IER |= M_INT3;
    // Enable global Interrupts and higher priority real-time debug events:
    EINT;   // Enable Global interrupt INTM
    ERTM;   // Enable Global realtime interrupt DBGM

    // IDLE loop. Just sit and loop forever:
    for(;;)  //infinite loop
    {
        // State machine entry & exit point
        //===========================================================
        (*Alpha_State_Ptr)(); // jump to an Alpha state (A0,B0,...)
        //===========================================================

        //Put the DRV chip in RESET if we want the power stage inactive
        if(DRV_RESET)
        {
            GpioDataRegs.GPACLEAR.bit.GPIO19 = 1;
            GpioDataRegs.GPBCLEAR.bit.GPIO32 = 1;
        }
        else
        {
            GpioDataRegs.GPASET.bit.GPIO19 = 1;
            GpioDataRegs.GPBSET.bit.GPIO32 = 1;
        }

    }
} //END MAIN CODE
//*****************************************************************************
// This is an example code demonstrating F021 Flash API usage.
// This code is in Flash
//*****************************************************************************
void main(void)
{
// Step 1. Initialize System Control:
// Enable Peripheral Clocks
// This example function is found in the F28M35x_SysCtrl.c file.
    InitSysCtrl();

//  Unlock CSM
//
//  If the API functions are going to run in unsecured RAM
//  then the CSM must be unlocked in order for the flash
//  API functions to access the flash.
//  If the flash API functions are executed from secure memory
//  then this step is not required.
    CsmUnlock();

// Step 2. Initalize GPIO:
// This example function is found in the F28M35x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio();  // Skipped for this example

// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
    DINT;

// Initialize the PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the F28M35x_PieCtrl.c file.
    InitPieCtrl();

// Disable CPU interrupts and clear all CPU interrupt flags:
    IER = 0x0000;
    IFR = 0x0000;

// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example.  This is useful for debug purposes.
// The shell ISR routines are found in DSP2802x_DefaultIsr.c.
// This function is found in F28M35x_PieVect.c.
    InitPieVectTable();

// Copy time critical code and Flash setup code to RAM
// This includes InitFlash(), Flash API functions and any functions that are
// assigned to ramfuncs section.
// The  RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart
// symbols are created by the linker. Refer to the device .cmd file.

   memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);

// Call Flash Initialization to setup flash waitstates
// This function must reside in RAM
    InitFlash();

//  Gain pump semaphore
    FlashGainPump();

//  Jump to RAM and call the Flash API functions
    Example_CallFlashAPI();

}
示例#8
0
void USB_Mode(void) 
{
	gb_cmd_timeout=0;
	set_clock_mode(CLOCK_MODE_USB); 
    PowerOn_Init_USB();
	Fill_EndpointDataTable_Into_ExRamBase();
	USBInitialize(); 
	Init_Flash_Reg();
	Init_Flash_Variable();
	gc_WriteProof = 0x00;
	InitFlash();
	
	USB_REG[0x27]    = 0x00; 	// 080304 jay change for new fpga
	GLOBAL_REG[0x17] = 0x01;	// UDC O/P	
    IE = 0x83;	   

	gdw_USB_CountDown = USB_DETECT_TIMEOUT;
	EA=1;	
	while(!gc_USBRST_ISR_Detected)                        // Bulk_In_Out  將跳出while 迴圈  //20090630 chiayen modify
	{
		USB_PlugDetect();
    	if((--gdw_USB_CountDown==0))  //080925chiayen add
		{
			gb_cmd_timeout=1;
    	  	break;
    	}
		if(!gbt_USB_Detected)
		{
			break;
		}
	}
	
	EA=0;
	if(gb_cmd_timeout==1)  //080925chiayen modify
	{
		USB_DisConnect();
		set_clock_mode(CLOCK_MODE_MP3); 
		InitFlash();
		return;
	}
	if(!gb_cmd_timeout)
	{
		LCM_clear();
		LCM_Disp_PC();
		LCM_Disp_PCConnect();
		LCM_Disp_MyMP3Logo();
	}
	   
	for(;;)
	{
		USB_PlugDetect();
		if(!gbt_USB_Detected)
		{//usb connected 
			USB_DisConnect();
			set_clock_mode(CLOCK_MODE_MP3);
			InitFlash();
			break;
		}
		else
		{
			if (gc_USBStage == CBWStage) 
			{			
				if(gbt_USB_Connect_Flag)
				{
				    //tdw_CMD_In_Count =0x02FFFFF;
				}
				else
				{
				    gdw_USB_CountDown =0x01FFFF;  //081208  detect charge first
				}
				EA=1;							
				while (!gbt_USB_CMD_IN)                        // Bulk_In_Out  將跳出while 迴圈
				{
					USB_PlugDetect();
					if(!gbt_USB_Detected)
					{
						break;
					}
					if(gbt_USB_Connect_Flag)
					{
					    if(gbt_USER_Remove_Flag)
						{
					        gb_cmd_timeout=1;
						    break;
						}
					} 
					else
					{
				    	if((--gdw_USB_CountDown==0)&&(!gb_USB_ISR_Detected)) 
						{
							gb_cmd_timeout=1;
				    	  	break;							
				    	}
					}
				}
				EA=0;                                       // 表示有 SCSI CMD 進入 關閉中斷避免常常進入			
				if((!gbt_USB_Detected)||(gb_cmd_timeout==1))  //080925chiayen modify
				{
					USB_DisConnect();
					set_clock_mode(CLOCK_MODE_MP3); 
					InitFlash();
					break;
				}
				gbt_USB_CMD_IN = 0;
				SD_Card_Detect();		
				gbt_USB_Connect_Flag =1; // 081208 jay add			
				Save_CBW_Packet();
				Decode_CBW_Command();                             // 解CBW和記錄相關手續		    
			}

			if(gw_USBDisplayTime==0)
			{
				USB_Mode_Disp();
			}

			if (gc_USBStage == BulkInReadSectorsStage) 
			{
				USB_REG[0xD2]      &= 0xDD;				//Disable Bulk-Out中斷
				ReadSectors();			    
				USB_REG[0xE9]       = 0x11;
				if (gc_ErrorCode == SET_ABRT) 
				{
					gc_RequestSenseCode = 0x06;        //ECC Error				
					gdw_dCBW_DataXferLength.LW = ((LWORD)TotalXferPageNum.WD) << 9;
					gc_bCSWStatus = 0x01;
				}
				else if (gc_ErrorCode == ID_NOT_FND) 
				{
					gc_RequestSenseCode = 0x07;        				//over range
					gdw_dCBW_DataXferLength.LW = ((LWORD)TotalXferPageNum.WD) << 9;
					gc_bCSWStatus = 0x01;
				}
				else 
				{
					gdw_dCBW_DataXferLength.LW = 0;
				}
				gc_USBStage = CSWStage;                                   // 進入Comand Status Wrapper
			}
			if (gc_USBStage == BulkOutWriteSectorsStage) 
			{
				USB_REG[0xD2]      &= 0xDD;				//Disable Bulk-Out中斷
				USB_REG[0x11]       = 0x01;   //HW auto ACK 
				WriteSectors();
				USB_REG[0x11]       = 0x00;
				USB_REG[0xE9]       = 0x22;

				if (gc_ErrorCode == SET_ABRT) 
				{
					gc_bCSWStatus = 0x01;
					gc_RequestSenseCode = 0x08;       					// write error 
				}
				else if (gc_ErrorCode == ID_NOT_FND) 
				{
					gc_RequestSenseCode = 0x07;      					//over range
					gc_bCSWStatus = 0x01;
				}
				else 
				{
					gc_bCSWStatus = 0x00;
					gc_RequestSenseCode = 0xFF;                        // normal
				}
				gdw_dCBW_DataXferLength.LW = 0;
				gc_USBStage = CSWStage;
			}
			if (gc_USBStage == BulkInResponseStage) 
			{                          // '4' BulkInProcess
				USB_REG[0xD2]      &= 0xDD;				//Disable Bulk-Out中斷
				if (gbt_XferNormalResponseData_or_ModeSense) 
				{                      // '1':Normal
			   		if (COMD_IN[0] == 0x23) 
					{
						gdw_dCBW_DataXferLength.WD[1] = 0x00;
						USB_REG[0x16]  = 0x00;
						USB_REG[0x15]  = 0x0C;
						USB_REG[0xA1] |= 0x01;
						while (USB_REG[0xA1] & 0x01)
						{
							USB_PlugDetect();  // 081014 Jay add for usb plug out
							if(!gbt_USB_Detected) break;
						}
						USB_REG[0xE9]  = 0x01;
			   		}
			   		else 
					{
						while (gdw_dCBW_DataXferLength.WD[1]) 
						{
							if (gdw_dCBW_DataXferLength.WD[1] > 64) 
							{
								gdw_dCBW_DataXferLength.WD[1] = gdw_dCBW_DataXferLength.WD[1] - 64;
					       		USB_REG[0x16] = 0x00;
								USB_REG[0x15] = 0x40;
							}
							else 
							{
					       		USB_REG[0x16] = gdw_dCBW_DataXferLength.BY[2] & 0x0F;
								USB_REG[0x15] = gdw_dCBW_DataXferLength.BY[3];
					       		gdw_dCBW_DataXferLength.WD[1] = 0x00;
					       	}
							USB_REG[0xA1] |= 0x01;
							while (USB_REG[0xA1] & 0x01)
							{  //081014 Jay add for usb plug out
								USB_PlugDetect();
								if(!gbt_USB_Detected) break;
							}
							USB_REG[0xE9]  = 0x01;
						}
		   			}
				}
				else 
				{
					if (COMD_IN[0] == 0x1A) 
					{
						gdw_dCBW_DataXferLength.WD[1] = 0x00;
						USB_REG[0x15]  = 0x04;
						USB_REG[0x16]  = 0x00;
						USB_REG[0xA1] |= 0x01;
						while (USB_REG[0xA1] & 0x01)
						{  //081014 Jay add for usb plug out
								USB_PlugDetect();
								if(!gbt_USB_Detected) break;
						}
						USB_REG[0xE9]  = 0x01;
					}
					else 
					{
						gdw_dCBW_DataXferLength.WD[1] = 0x00;
						USB_REG[0x15]  = 0x08;
						USB_REG[0x16]  = 0x00;
						USB_REG[0xA1] |= 0x01;
						while (USB_REG[0xA1] & 0x01)
						{  //081014 Jay add for usb plug out
								USB_PlugDetect();
								if(!gbt_USB_Detected) break;
						}
						USB_REG[0xE9]  = 0x01;
					}
					gbt_XferNormalResponseData_or_ModeSense = NormalResponseData;// 將預設設為一般型
				}
				gc_USBStage = CSWStage;
			}
			if (gc_USBStage == BulkStallStage) 
			{
				StallBulkRW();
				gc_USBStage = CSWStage;
			}
			if (gc_USBStage == BulkInReadBufferStage) 
			{
				USB_REG[0xD2]      &= 0xDD;				//Disable Bulk-Out中斷
				NoCard_ReadSectors();		    
				USB_REG[0xE9]       = 0x11;
				gdw_dCBW_DataXferLength.LW = 0;			
				gc_USBStage = CSWStage;                                   // 進入Comand Status Wrapper			                          
			}
			if (gc_USBStage == BulkOutWriteBufferStage) 
			{
				USB_REG[0xD2]      &= 0xDD;				//Disable Bulk-Out中斷
				USB_REG[0x11]       = 0x01;   //HW auto ACK 
				NoCard_WriteSectors();			
				USB_REG[0x11]       = 0x00;
				USB_REG[0xE9]       = 0x22;
				gdw_dCBW_DataXferLength.LW = 0;
				gc_USBStage = CSWStage;
			}				
			if (gc_USBStage == CSWStage) 
			{
				gc_USBStage = CBWStage;                                   // 重新準備收下一筆command
				USB_BUF_Bank0[ 0] = 'U';					// 以下由SetCSWData expand
				USB_BUF_Bank0[ 1] = 'S';
				USB_BUF_Bank0[ 2] = 'B';
				USB_BUF_Bank0[ 3] = 'S';
				USB_BUF_Bank0[ 4] = dCBW_Tag[0];
				USB_BUF_Bank0[ 5] = dCBW_Tag[1];
				USB_BUF_Bank0[ 6] = dCBW_Tag[2];
				USB_BUF_Bank0[ 7] = dCBW_Tag[3];
				USB_BUF_Bank0[ 8] = gdw_dCBW_DataXferLength.BY[3];                  // LSB 記錄剩下未被傳輸的資料
				USB_BUF_Bank0[ 9] = gdw_dCBW_DataXferLength.BY[2];                  //     記錄剩下未被傳輸的資料
				USB_BUF_Bank0[10] = gdw_dCBW_DataXferLength.BY[1];                  //     記錄剩下未被傳輸的資料
				USB_BUF_Bank0[11] = gdw_dCBW_DataXferLength.BY[0];                  // MSB 記錄剩下未被傳輸的資料
				USB_BUF_Bank0[12] = gc_bCSWStatus;                                 // Command Status 見BulkOnly P.15說明各種碼的情形
				USB_REG[0x2B]  = 0x00; 
				USB_REG[0x16]  = 0x00;
				USB_REG[0x15]  = 0x0D;
				USB_REG[0xA1] |= 0x01;
				while (USB_REG[0xA1] & 0x01)
				{  //081014 Jay add for usb plug out
					USB_PlugDetect();
					if(!gbt_USB_Detected) break;
				}
			}
			USB_REG[0xE9]  = 0x01;			
			USB_REG[0xD2]  |= 0x02;	//Enable Bulk-Out中斷
			USB_REG[0xA1]  |= 0x02;		//下一筆31Byte Bulk-Out
		}
	}
}
示例#9
0
void main(void)
{
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2803x_SysCtrl.c file.
   InitSysCtrl();

// Step 2. Initialize GPIO:
// This example function is found in the DSP2803x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio();  // Skipped for this example

// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
   DINT;

// Initialize the PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2803x_PieCtrl.c file.
   InitPieCtrl();

// Disable CPU interrupts and clear all CPU interrupt flags:
   IER = 0x0000;
   IFR = 0x0000;

// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example.  This is useful for debug purposes.
// The shell ISR routines are found in DSP2803x_DefaultIsr.c.
// This function is found in DSP2803x_PieVect.c.
   InitPieVectTable();

// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
   EALLOW;  // This is needed to write to EALLOW protected registers
   PieVectTable.EPWM1_INT = &epwm1_timer_isr;
   PieVectTable.EPWM2_INT = &epwm2_timer_isr;
   PieVectTable.EPWM3_INT = &epwm3_timer_isr;
   EDIS;    // This is needed to disable write to EALLOW protected registers

// Step 4. Initialize all the Device Peripherals:
   InitEPwmTimer();    // For this example, only initialize the ePWM Timers

// Step 5. User specific code, enable interrupts:

// Copy time critical code and Flash setup code to RAM
// This includes the following ISR functions: epwm1_timer_isr(), epwm2_timer_isr()
// epwm3_timer_isr and and InitFlash();
// The  RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart
// symbols are created by the linker.
   memcpy((uint16_t *)&RamfuncsRunStart,(uint16_t *)&RamfuncsLoadStart, (unsigned long)&RamfuncsLoadSize);

// Call Flash Initialization to setup flash waitstates
// This function must reside in RAM
   InitFlash();

// Initialize counters:
   EPwm1TimerIntCount = 0;
   EPwm2TimerIntCount = 0;
   EPwm3TimerIntCount = 0;
   LoopCount = 0;

// Enable CPU INT3 which is connected to EPWM1-3 INT:
   IER |= M_INT3;

// Enable EPWM INTn in the PIE: Group 3 interrupt 1-3
   PieCtrlRegs.PIEIER3.bit.INTx1 = PWM1_INT_ENABLE;
   PieCtrlRegs.PIEIER3.bit.INTx2 = PWM2_INT_ENABLE;
   PieCtrlRegs.PIEIER3.bit.INTx3 = PWM3_INT_ENABLE;

// Enable global Interrupts and higher priority real-time debug events:
   EINT;   // Enable Global interrupt INTM
   ERTM;   // Enable Global realtime interrupt DBGM

// Step 6. IDLE loop. Just sit and loop forever (optional):
   EALLOW;
   GpioCtrlRegs.GPBMUX1.bit.GPIO34 = 0;
   GpioCtrlRegs.GPBDIR.bit.GPIO34 = 1;
   EDIS;

   for(;;)
   {
       // This loop will be interrupted, so the overall
       // delay between pin toggles will be longer.
       DELAY_US(DELAY);
	   LoopCount++;
       GpioDataRegs.GPBTOGGLE.bit.GPIO34 = 1;
   }
}
示例#10
0
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// MAIN CODE - starts here
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void main(void)
{
//=================================================================================
//	INITIALISATION - General
//=================================================================================

//-------------------------------- FRAMEWORK --------------------------------------

	DeviceInit();	// Device Life support & GPIO
	SCIA_Init();  	// Initalize the Serial Comms A peripheral

// Only used if running from FLASH
// Note that the variable FLASH is defined by the compiler with -d FLASH
// (see TwoChannelBuck.pjt file)
#ifdef FLASH		
// Copy time critical code and Flash setup code to RAM
// The  RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart
// symbols are created by the linker. Refer to the linker files. 
	MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);

// Call Flash Initialization to setup flash waitstates
// This function must reside in RAM
	InitFlash();	// Call the flash wrapper init function
#endif //(FLASH)

// Timing sync for background loops
// Timer period definitions found in PeripheralHeaderIncludes.h
	CpuTimer0Regs.PRD.all =  mSec1;		// A tasks
	CpuTimer1Regs.PRD.all =  mSec50;	// B tasks
	CpuTimer2Regs.PRD.all =  mSec100;	// C tasks

// Tasks State-machine init
	Alpha_State_Ptr = &A0;
	A_Task_Ptr = &A1;
	B_Task_Ptr = &B1;
	C_Task_Ptr = &C1;

	BlinkStatePtr = 0;
	VTimer0[0] = 0;	
	VTimer1[0] = 0;
	VTimer2[0] = 0;
	CommsOKflg = 0;
	SerialCommsTimer = 0;
	HRmode = 1;		// Default to HR mode enabled
	LED_TaskPtr = 0;

// ---------------------------------- USER -----------------------------------------
//  put common initialization/variable definitions here

	Gui_LEDPrd = 1000;	//Default to 1 blink every second (Q0)
	temp_LEDDelay = 0;


//=================================================================================
//	INITIALISATION - GUI connections
//=================================================================================
// Use this section only if you plan to "Instrument" your application using the 
// Microsoft C# freeware GUI Template provided by TI

	//"Set" variables
	//---------------------------------------
	// assign GUI variable Textboxes to desired "setable" parameter addresses
	//varSetTxtList[0] = &Var1;

	// assign GUI Buttons to desired flag addresses
	//varSetBtnList[0] = &Var2;

	// assign GUI Sliders to desired "setable" parameter addresses
	varSetSldrList[0] = &Gui_LEDPrd;


	//"Get" variables
	//---------------------------------------
	// assign a GUI "getable" parameter address
	//varGetList[0] = &Var3;

	// assign a GUI "getable" parameter array address
	// 		only need to set initial position of array, program will run through it 
	//       based on the array length specified in the GUI
	//arrayGetList[0] = &Var4[0];



//==================================================================================
//	INITIALISATION - Peripherals used for support
//==================================================================================

// ---------------------------------- USER -----------------------------------------
//  Put peripheral initialisation here




//==================================================================================
//	INITIALISATION - BUILD OPTIONS - NOTE: select via ProjectSettings.h
//==================================================================================

// ---------------------------------- USER -----------------------------------------
//  Put build specific initialisation here




//=================================================================================
//	BACKGROUND (BG) LOOP
//=================================================================================

//--------------------------------- FRAMEWORK -------------------------------------
	for(;;)  //infinite loop
	{
		// State machine entry & exit point
		//===========================================================
		(*Alpha_State_Ptr)();	// jump to an Alpha state (A0,B0,...)
		//===========================================================

	}
} //END MAIN CODE
示例#11
0
void main(void)
{


// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the F2806x_SysCtrl.c file.
	char * rto1 = (char *)0x8000;
    for (rn1 = 0; rn1 < nn1; rn1++) *rto1++ = rfrom1;
	memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (Uint32)&RamfuncsLoadSize);
   InitSysCtrl();

// Step 2. Initalize GPIO:
// This example function is found in the F2806x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio();  // Skipped for this example

// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
   DINT;

// Initialize the PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the F2806x_PieCtrl.c file.
   InitPieCtrl();

// Disable CPU interrupts and clear all CPU interrupt flags:
   IER = 0x0000;
   IFR = 0x0000;

// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example.  This is useful for debug purposes.
// The shell ISR routines are found in F2806x_DefaultIsr.c.
// This function is found in F2806x_PieVect.c.
   InitPieVectTable();

// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
   EALLOW;  // This is needed to write to EALLOW protected registers
   PieVectTable.EPWM5_INT = &epwm5_timer_isr;
   EDIS;    // This is needed to disable write to EALLOW protected registers

	// Step 4. Initialize all the Device Peripherals:
	// This function is found in F2806x_InitPeripherals.c
	// InitPeripherals(); // Not required for this example
	InitAdc();  // For this example, init the ADC
	//InitAdcAio(); // Function that sets analog input pins
	ConfigADC();
	//AdcOffsetSelfCal();
	InitEPwm(); // Function initializes ePWM 1 - 5

// Step 5. User specific code, enable interrupts:

// Copy time critical code and Flash setup code to RAM
// This includes the following ISR functions: epwm1_timer_isr(), epwm2_timer_isr()
// epwm3_timer_isr and and InitFlash();
// The  RamfuncsLoadStart, RamfuncsLoadSize, and RamfuncsRunStart
// symbols are created by the linker. Refer to the F2808.cmd file.


   // Call Flash Initialization to setup flash waitstates
   // This function must reside in RAM
   InitFlash();

   IER |= M_INT3;		// Enable CPU INT3 which is connected to EPWM1-8 INT: page 175 in documentation
   IER |= M_INT10; 		// Enable CPU Interrupt 10

   // Enable EPWM INTn in the PIE: Group 3 interrupt 1-3
   PieCtrlRegs.PIEIER3.bit.INTx5 = PWM5_INT_ENABLE;

	// Enable ADCINT1 in PIE
	/*PieCtrlRegs.PIEIER10.bit.INTx1 = 1;	// Enable INT 10.1 in the PIE
	PieCtrlRegs.PIEIER10.bit.INTx2 = 1;	// Enable INT 10.2 in the PIE
	PieCtrlRegs.PIEIER10.bit.INTx3 = 1;	// Enable INT 10.3 in the PIE
	PieCtrlRegs.PIEIER10.bit.INTx4 = 1;	// Enable INT 10.4 in the PIE
	PieCtrlRegs.PIEIER10.bit.INTx5 = 1;	// Enable INT 10.5 in the PIE
	PieCtrlRegs.PIEIER10.bit.INTx6 = 1;	// Enable INT 10.6 in the PIE
	PieCtrlRegs.PIEIER10.bit.INTx7 = 1;	// Enable INT 10.7 in the PIE
	PieCtrlRegs.PIEIER10.bit.INTx8 = 1;	// Enable INT 10.8 in the PIE*/

	// Enable global Interrupts and higher priority real-time debug events:
	EINT;   // Enable Global interrupt INTM
	ERTM;   // Enable Global realtime interrupt DBGM

   // Step 6. IDLE loop. Just sit and loop forever (optional):
   EALLOW;
   GpioCtrlRegs.GPBMUX2.bit.GPIO54 = 0;
   GpioCtrlRegs.GPBDIR.bit.GPIO54 = 1;
   GpioDataRegs.GPBCLEAR.bit.GPIO54 = 1;
   EDIS;

   for(;;)
   {
       // This loop will be interrupted, so the overall
       // delay between pin toggles will be longer.
       DELAY_US(DELAY);
	   LoopCount++;
       //GpioDataRegs.GPBTOGGLE.bit.GPIO34 = 1;
   }

}
示例#12
0
void main(void)
{
//	XBYTE[0xB091]=0x01;	// 1.8v/3.3v regulator turn on
    XBYTE[0xB092]=0x78; // 3.2V & 2.0V
//	XBYTE[0xB093]=0x00;	// LVR=2.4V

    // DSP initial
    XBYTE[0xB010]&=0xFE;	// Disable DSP clock, Jimi 080729 for pop noise as 1st time play
    XBYTE[0xB002]&=0xFE;	// Release DSP Reset, Jimi 080729 for pop noise as 1st time play
    XBYTE[0xB08a]|=0x08; 	//(JC)MCU:audio DAC on
    XBYTE[0xB08a]|=0x10; 	//(JC)MCU:Headphone driver on
    XBYTE[0xB08a]|=0x20; 	//(JC)MCU:Headphone driver DD comm-mode bias o/p
    XBYTE[0xB0ED]|=0x02;	//(Jimi 091027)Audio DA zero cross reset bypass on
    XBYTE[0xB0C1]=0xB0;		//(Jimi 091027)Pre-mux , post-mux and internal XCK select
    XBYTE[0xB0C4]=2;		//(Jimi 091027)LCH speaker volume(i.e LCH OP Gain)
    XBYTE[0xB0C5]=2;		//(Jimi 091027)RCH speaker volume(i.e RCH OP Gain)

    //SAR ADC init
    XBYTE[0xB05E] = 0; // disable SAR FS clock
    XBYTE[0xB05F] = 0x5F; // SAR clock = 12MHz/16/6 = 125kHz = 8us

    //--------------------------------------------	//
    gbt_Force_USB_FullSpeed =FALSE;
    gbt_USB_Detected = 0;
    gc_CardDetect = 0x00 ;
    gc_CardExist  = 0x00 ;  			// bit 0  : Nand Flash  bit 1: SD_Card
    gc_CardType   = 0x01 ;				// default Flash Interface
    gc_CardChangeStage =0x00;           // bit 0 : Nand  bit 1 : SD_Card
    gw_FileSkipNumber=0;
    gb_TriggerFileSkip=0;
    gb_FindFile=0;
    gbt_Support_Dummy_Capacity =0;              // default disable dummy capacity function
    gc_bCBWLUN=0;
    gbt_enableCDROM =0;							//09/04/24,joyce
    gc_CDROM_Size=0 ;
    init_system();
    USB_PlugDetect();                               // Detect USB plug
    //----------------------------------------------// Initial Flash module
    Init_Flash_Reg();
    Init_Flash_Variable();
    //-------------- Need search Bank,Dsp ,hzk block info ,first---------//
    FDBP.cFDev = 0;
    XBYTE[0xB40F] = FDBP.cFDev;
    FlashReadID();
    Flash_State_Initial();
    Search_BankCode_Block();

    InitFlash();

    Timer0_init();
    XBYTE[0xB09F]|=0x06;//(JC)Vref Fast setup mode 'n Internal reference voltage power control
    gc_Vrefinit_Timer=60;
    XBYTE[0xB421]=0x13;
//	dbprintf("tftinit\n");
    TFT_init();
#ifdef CAR_48
    XBYTE[0xB102]|=0x3C;
#else
    XBYTE[0xB102] |= 0x01;
#endif
//	dbprintf("tftinit end\n");
    TFT_PowerOnlogo();
    if(gbt_USB_Detected)
    {
        USB_Task();
    }

    gb_Host_Exist=1;
    gb_SD_Exist=1;
    if(!Host_DetectDevice())
    {
        if(!Host_Initial())
        {
            gc_CurrentCard = CURRENT_MEDIA_HOST;
            gb_FindFlag = 0;
        }
        else
        {
            gc_CurrentCard=0;
        }
    }
    else
    {
        if(SD_Card_Detect())
        {
            if(SD_Identification_Flow())
            {
                gc_CardExist |=0x02;
                gc_CurrentCard=2;
            }
            else
            {
                gc_CurrentCard=0;
                gc_CardExist &=0xFD;
                gb_FindFlag = 0;
            }
        }
        else
        {
            gb_SD_Exist=0;
            gc_CurrentCard = 0;
        }
    }
    XBYTE[0xB400] =0x01;
    XBYTE[0xB010]&=0xFE;	// Disable DSP clock, Jimi 080729 for pop noise as 1st time play
    XBYTE[0xB002]&=0xFE;	// Release DSP Reset, Jimi 080729 for pop noise as 1st time play
    XBYTE[0xB08a]|=0x08;//(JC)MCU:audio DAC on
    XBYTE[0xB08a]|=0x10;//(JC)MCU:Headphone driver on
    XBYTE[0xB08a]|=0x20;//(JC)MCU:Headphone driver DD comm-mode bias o/p

    //SAR ADC init
    XBYTE[0xB05E] = 0; // disable SAR FS clock
    XBYTE[0xB05F] = 0x5f; // SAR clock = 12MHz/16/6 = 125kHz = 8us

    Get_LogData_PageIndex();  //chiayen0808
    USER_LogFile_ReadWrite(0);//use reserveblock for log
    if((gw_FM_frequency<875) || (gw_FM_frequency>1080))
    {
        gw_FM_frequency=875;
    }
    FM_initial();
    FM_drive();
    gc_SetNumber = gw_PagesPerBlock >> 2; //1set=4pages for dir management, Ching 080816  //20090107 chiayen add
    gc_SetIndex = 0; //Ching 080816  //20090107 chiayen add
    USER_GetUISetIndex();	//for DIR table	index  //20090107 chiayen add

    gb_FindFlag = 0;
    if (DOS_Initialize()) //20090803 chiayen modify
    {
        gdw_HOSTStartSectorRead=0xFFFFFFF0;
        if(gc_CurrentCard==CURRENT_MEDIA_HOST)
        {
            if(SD_Card_Detect())
            {
                gb_SD_Exist=1;
                if(SD_Identification_Flow())
                {
                    gc_CardExist |=0x02;
                    gc_CurrentCard=2;
                }
                else
                {
                    DEVICE_REG[0x00]= 0x01;
                    gc_CurrentCard=0;
                    gc_CardExist &=0xFD;
                    gb_FindFlag = 0;
                }
            }
            else
            {
                DEVICE_REG[0x00]= 0x01;
                gb_SD_Exist=0;
                gc_CurrentCard = 0;
            }
        }
        else
        {
            DEVICE_REG[0x00]= 0x01;  //20090730 chiayen add
            gc_CurrentCard=0;
            gc_CardExist &=0xFD;
            gb_FindFlag = 0;
            InitFlash();
        }
        DOS_Initialize();
    }
    gc_CurrentCard_backup=gc_CurrentCard; //20090803 chiayen move here
    gs_File_FCB[0].dw_FDB_StartCluster = gdw_DOS_RootDirClus;
    DOS_Search_File(C_File_All|C_Cnt_FileNo, C_MusicFileType, C_CmpExtName|C_Next);//(JC)count music file no. in root
    ir_init();
    EA  = 1;
    gc_Task_Current=C_Task_Play;
    gc_Task_Next=C_Task_Play;
    gc_PhaseInx=0;
    gw_init_needed=0xffff;
    Detect_USB();
    Polling_TaskEvents();
    if(gc_Task_Current!=gc_Task_Next)
    {
        gc_Task_Current=gc_Task_Next;
    }
    while(1)
    {
        switch(gc_Task_Current)
        {
        case C_Task_Play:
            Play_Task();
            break;

        case C_Task_USB:
            USB_Task();
            break;

        case C_Task_Dir:  //20090107 chiayen add
            Dir_Task();
            break;

        case C_Task_Jpeg:  //20090107 chiayen add
            Jpeg_Task();
            break;

        case C_Task_Mjpeg:  //20090107 chiayen add
            Mjpeg_Task();
            break;

        case C_Task_PlayMenu:  //20090107 chiayen add
//				PlayMenuProcess();
            break;

        case C_Task_PlayMenu_IR:  //20090107 chiayen add
//				PlayMenuProcess_IR();
            break;

        case C_Task_Setting:  //20090107 chiayen add
            SetupMenuProcess();
            break;

        case C_Task_Menu:
            MainMenuProcess();
            break;

        case C_Task_PowerOff:
            PowerOffProcess();
            break;
        }
    }
}
示例#13
0
文件: HwInit.c 项目: shuw/marconi
void HwInit(void)
{
    volatile WORD i;

    EALLOW;

    // Disable watchdog
    SysCtrlRegs.WDCR = 0x0068;

    // Place device in unsecure mode with dummy reads from PWL locations
    i = CsmPwl.PSWD0;
    i = CsmPwl.PSWD1;
    i = CsmPwl.PSWD2;
    i = CsmPwl.PSWD3;
    i = CsmPwl.PSWD4;
    i = CsmPwl.PSWD5;
    i = CsmPwl.PSWD6;
    i = CsmPwl.PSWD7;

    // Initalize PLL and wait for lock
    SysCtrlRegs.PLLCR.bit.DIV = HW_DSP_CLKIN_MULT << 1;
    for(i = 0; i < PLL_LOCK_DELAY; ++i);
       
    // Lo-speed, hi-speed peripheral clock prescale settings
    SysCtrlRegs.HISPCP.all = HW_DSP_HSPCLK_DIV >> 1;
    SysCtrlRegs.LOSPCP.all = HW_DSP_LSPCLK_DIV >> 1;
    
    // Copy RAM code into RAM from FLASH
    memmove(&RamCodeRunStart, &RamCodeLoadStart, &RamCodeLoadEnd - &RamCodeLoadStart);

    // Initialize FLASH (jump to code in RAM so we can change FLASH settings)
    InitFlash();

    // Enable all clocks (full blast)
    SysCtrlRegs.PCLKCR.bit.EVAENCLK = 1;
    SysCtrlRegs.PCLKCR.bit.EVBENCLK = 1;
    SysCtrlRegs.PCLKCR.bit.SCIAENCLK = 1;
    SysCtrlRegs.PCLKCR.bit.SCIBENCLK = 1;
    SysCtrlRegs.PCLKCR.bit.MCBSPENCLK = 1;
    SysCtrlRegs.PCLKCR.bit.SPIENCLK = 1;
    SysCtrlRegs.PCLKCR.bit.ECANENCLK = 1;
    SysCtrlRegs.PCLKCR.bit.ADCENCLK = 1;

    // Disable all maskable CPU interrupts and clear interrupt flags
    DINT;
    IER = 0x0000;
    IFR = 0x0000;

    // Clear interrupt enable and flag registers
    PieCtrlRegs.PIECRTL.bit.ENPIE = 0;
    PieCtrlRegs.PIEIER1.all = 0;
    PieCtrlRegs.PIEIER2.all = 0;
    PieCtrlRegs.PIEIER3.all = 0;	
    PieCtrlRegs.PIEIER4.all = 0;
    PieCtrlRegs.PIEIER5.all = 0;
    PieCtrlRegs.PIEIER6.all = 0;
    PieCtrlRegs.PIEIER7.all = 0;
    PieCtrlRegs.PIEIER8.all = 0;
    PieCtrlRegs.PIEIER9.all = 0;
    PieCtrlRegs.PIEIER10.all = 0;
    PieCtrlRegs.PIEIER11.all = 0;
    PieCtrlRegs.PIEIER12.all = 0;
    PieCtrlRegs.PIEIFR1.all = 0;
    PieCtrlRegs.PIEIFR2.all = 0;
    PieCtrlRegs.PIEIFR3.all = 0;	
    PieCtrlRegs.PIEIFR4.all = 0;
    PieCtrlRegs.PIEIFR5.all = 0;
    PieCtrlRegs.PIEIFR6.all = 0;
    PieCtrlRegs.PIEIFR7.all = 0;
    PieCtrlRegs.PIEIFR8.all = 0;
    PieCtrlRegs.PIEIFR9.all = 0;
    PieCtrlRegs.PIEIFR10.all = 0;
    PieCtrlRegs.PIEIFR11.all = 0;
    PieCtrlRegs.PIEIFR12.all = 0;

    // Enable PIE
    PieCtrlRegs.PIEACK.all = 0xFFFF;
    PieCtrlRegs.PIECRTL.bit.ENPIE = 1;

    EDIS;

    HwGpioInit();
    HwTimerInit();
    HwUartInit();
    HwCodecInit();

    // Enable maskable CPU interrupts
    EINT;
}
示例#14
0
void main(void) {
   memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
   InitSysCtrl();
   SysCtrlRegs.PCLKCR1.bit.EQEP2ENCLK = 0;    // eQEP2
   SysCtrlRegs.PCLKCR0.bit.SPIBENCLK = 0;     // SPI-B
   InitFlash();
// Step 2. Initalize GPIO: 
// InitGpio();  // Skipped; not needed
   InitEQep1Gpio();
   //InitEPwm1Gpio();
   //InitEPwm2Gpio();
   //InitEPwm3Gpio();
   InitECap1Gpio();
   InitECap2Gpio();
   InitECap3Gpio();
   EALLOW;
   GpioCtrlRegs.GPAMUX2.bit.GPIO16 = 1;
   GpioCtrlRegs.GPAMUX2.bit.GPIO17 = 1;
   GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 1;
   GpioCtrlRegs.GPAMUX2.bit.GPIO19 = 1;
   GpioCtrlRegs.GPBMUX2.bit.GPIO50 = 0;
   GpioCtrlRegs.GPBMUX2.bit.GPIO51 = 0;
   GpioCtrlRegs.GPBDIR.bit.GPIO50 = 1;
   setupDrv8301();
   setupSpiA();
   //DRV8301_setupSpi();
   EDIS;
   DINT;
   InitPieCtrl(); // The default state is all PIE interrupts disabled and flags are cleared.
   
// Disable CPU interrupts and clear all CPU interrupt flags:
   IER = 0x0000;
   IFR = 0x0000;

// Initialize the PIE vector table with pointers to the shell ISRs.
// This will populate the entire table, even if the interrupt
// is not used in this example.  This is useful for debug purposes.
// The shell ISR routines are found in F2806x_DefaultIsr.c.
   InitPieVectTable();

// Interrupts that are used in this example are re-mapped to our ISR functions
   EALLOW;
   PieVectTable.ECAP1_INT = &ecap1_isr;  // Group 4 PIE Peripheral Vectors
   PieVectTable.ECAP2_INT = &ecap2_isr;	 // ''
   PieVectTable.ECAP3_INT = &ecap3_isr;  // ''
   PieVectTable.ADCINT1 = &adc_isr; //
   //PieVectTable.SCIRXINTA = &scia_isr;
   EDIS;

// Step 4. Initialize all the Device Peripherals:
   InitECapRegs();
   scia_init();
   epwmInit(1,2,0); //10kHz, 50% duty, no chop
   InitAdc();
   AdcOffsetSelfCal();
   
// Step 5. Enable interrupts:
   IER |= M_INT1; // Enable CPU Interrupt 1 (connected to ADC)
   IER |= M_INT4; // Enable CPU INT4 which is connected to ECAP1-4 INT
   IER |= M_INT3; // Enable CPU INT1 which is connected to CPU-Timer 0:

   PieCtrlRegs.PIEIER1.bit.INTx1 = 1;	   // INT1.1 for ADC
   PieCtrlRegs.PIEIER4.bit.INTx1 = 1;      // INT4.1 for ecap1
   PieCtrlRegs.PIEIER4.bit.INTx2 = 1;      // INT4.2 for ecap2
   PieCtrlRegs.PIEIER4.bit.INTx3 = 1;      // INT4.3 for ecap3



// Enable global Interrupts and higher priority real-time debug events:
   EINT;   // Enable Global interrupt INTM
   ERTM;   // Enable Global realtime interrupt DBGM
   
   qep_data.init(&qep_data);
   int printData = 10001;
   readHallStateFlag = 1;
   char writeBuffer[80] = {0};
   int lastPhase = 0;
   gogo = 1;
   DRV8301_enable();
   DRV8301_setupSpi();
   i = 0;
	while(1) {
		qep_data.calc(&qep_data);
		if (readHallStateFlag)
			updateHallState();
		if ((lastPhase != Phase) && (printData)) {
			//\033[2J\033[0;0H\r
			sprintf(writeBuffer, "Hall State: %d\n\r", (int)Phase);
			scia_msg(writeBuffer);
		    sprintf(writeBuffer, "Velocity: %d rpm\n\r", qep_data.SpeedRpm_fr);
			scia_msg(writeBuffer);
			//sprintf(writeBuffer, "Mechanical Angle: %f degrees\n\r", qep_data.theta_mech*360);
			//scia_msg(writeBuffer);
			//sprintf(writeBuffer, "Electrical Angle: %f\n\r", qep_data.theta_elec);
			//scia_msg(writeBuffer);
			lastPhase = Phase;
		}
		//DRV8301_readData();
		//if (!Phase /*|| drv8301.fault || drv8301.OverTempShutdown || drv8301.OverTempWarning*/) {
			//while (!Phase || drv8301.fault || drv8301.OverTempShutdown || drv8301.OverTempWarning){
				//GpioDataRegs.GPBCLEAR.bit.GPIO50 = 1;
				///DELAY_US(32000);
				//DELAY_US(32000);
				//sprintf(writeBuffer, "\aERROR DECTECTED: \n\r Hall State: %d %d %d\n\r", CoilA, CoilB, CoilC);
				//scia_msg(writeBuffer);
				//sprintf(writeBuffer, "Fault Bit: %d\n\rOverTempShutdown: %d\n\rOverTempWarning%d\n\r", drv8301.fault, drv8301.OverTempShutdown, drv8301.OverTempWarning);
				//scia_msg(writeBuffer);
			//}
		//}
		//else {
		    //GpioDataRegs.GPBSET.bit.GPIO50 = 1;
		//}

	}

} 
示例#15
0
main()
{
	int16 i;
	int16 j;
	int16 test[100];
   

//一、 system initialize: pll clock:150M;hispcp=1 100m/2; lospcp=2 100m/4;eWM,ADC,cpu Timer clock enabled
	InitSysCtrl(); //修改了默认时钟到150MHz,关闭了无关外设时钟

//	DELAY_US(1000000);	
//二、initialize GPIO:set the GPIO to it's default state: 普通GPIO状态,均为输入,输入采样方式为第一种,均为上拉使能.
              //epwm disable pull up
	InitGpio();	//全部引脚初始化

//用户自定义IO初始化(主要包括:逻辑输入输出引脚)
	InitLogicIO();

//串口初始化
	InitScicGpio();
	InitSci();
//初始化触摸屏变量
	initvar();


//	MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);
//EPwm初始化	
 //      InitEPwmGpio();	//PWM IO初始化
	//InitEPwm1Gpio(); 
	//InitEPwm2Gpio(); //所有PWM模块都需要开启
	//InitEPwm3Gpio();
	//InitEPwm4Gpio();
	//InitEPwm5Gpio();
	//InitEPwm6Gpio();


//三、initialize interrupts:disable cpu interrupt; disabel all pie interrupts and clear all pie interrupt flags
	DINT;
	InitPieCtrl();
	IER=0x0000;
	IFR=0x0000;
//initialize Pie interrupts and enable pie
	InitPieVectTable();

//用户自定义的中断初始化
	EALLOW;
	PieVectTable.TINT0 = &cpu_timer0_isr;  //Cpu timer0 interrupt
    PieVectTable.XINT1 = &xint1_isr;
	EDIS;


//四、initialize peripherials and setup peripherials
    InitFlash();

    InitXintf();

//ADC initialize
	InitAdc();
//ADC setup
	SetupAdc();

//       Adc_Inquire();
//Epwm Setup
//	SetupEPwm();   //pwm开始产生

//CPU timer0 initialize
	InitCpuTimers();
// Configure CPU-Timer 0 to interrupt every msecond:
// 150MHz CPU Freq, 3m second Period (in uSeconds)
   	ConfigCpuTimer(&CpuTimer0, 150, 3000);
   	StartCpuTimer0();

//五、User specific code, enable interrupts:

//在开始中断之前先进行测试循环。此时进行单步运行

//1. I/O test
	//测试程序,GPIOA的低18位为通用I/O输入
	//测试程序,GPIOA的24,25,26,27为通用I/O输入,GPIOB的48-61为通用I/O输入
	//由于I/o口均有上拉,所以只要测试输入0时是否正确即可。这样最大限度保护芯片。
   	for(i=0;i<100;i++)
   		{
   		j=i;
   		}

//   EALLOW;
//   GpioCtrlRegs.GPADIR.all = 0x0003FFFF;     // GPIO functionality GPIO0-GPIO15
//   EDIS;
//   GpioDataRegs.GPACLEAR.all = 0x0003FFFF;
//   GpioDataRegs.GPASET.all = 0x0001AAAA;
//   GpioDataRegs.GPATOGGLE.all = 0x0003FFFF;


//2. Communication Test:  //具体通讯是否成功还需要更多程序

	//测试RS485通讯芯片
		GpioDataRegs.GPASET.bit.GPIO21=1;
		GpioDataRegs.GPATOGGLE.bit.GPIO22=1;

	//测试CAN通讯芯片
		GpioDataRegs.GPATOGGLE.bit.GPIO19=1;

   	for(i=0;i<100;i++)
   		{
   		j=i;
   		}
	//测试RS485通讯芯片
		GpioDataRegs.GPASET.bit.GPIO21=1;
		GpioDataRegs.GPATOGGLE.bit.GPIO22=1;

	//测试CAN通讯芯片
		GpioDataRegs.GPATOGGLE.bit.GPIO19=1;

   	for(i=0;i<100;i++)
   		{
   		j=i;
   		}

// 3. A/D Test
	//软件启动adc转换

	for(i=0;i<100;i++)
   		{
		AdcRegs.ADCTRL2.bit.SOC_SEQ1=1;
		Adc_Inquire();
   		}

// 4. PWM Test  //每次测一个单相,这样子发现错误及时下电

   	for(i=0;i<100;i++)
   		{
   		j=i;
   		}
	
		*FPGA_PWMA_Wait1=1000;
		*FPGA_PWMA_Duty1=1000;

		*FPGA_PWMA_Wait2=2000;
		*FPGA_PWMA_Duty2=2000;

		*FPGA_PWMA_Wait3=3000;
		*FPGA_PWMA_Duty3=3000;

		*FPGA_PWMA_Wait4=4000;
		*FPGA_PWMA_Duty4=4000;

		*FPGA_PWMA_Wait5=5000;
		*FPGA_PWMA_Duty5=5000;

		*FPGA_PWMA_Wait6=6000;
		*FPGA_PWMA_Duty6=6000;

		*FPGA_PWMA_Wait7=1000;
		*FPGA_PWMA_Duty7=1000;

		*FPGA_PWMA_Wait8=2000;
		*FPGA_PWMA_Duty8=2000;

		*FPGA_PWMA_Wait9=3000;
		*FPGA_PWMA_Duty9=3000;

		*FPGA_PWMA_Wait10=4000;
		*FPGA_PWMA_Duty10=4000;

		*FPGA_PWMA_Wait11=5000;
		*FPGA_PWMA_Duty11=5000;

		*FPGA_PWMA_Wait12=6000;
		*FPGA_PWMA_Duty12=6000;

   	for(i=0;i<100;i++)
   		{
   		j=i;
   		}

		*FPGA_PWMB_Wait1=1000;
		*FPGA_PWMB_Duty1=1000;

		*FPGA_PWMB_Wait2=2000;
		*FPGA_PWMB_Duty2=2000;

		*FPGA_PWMB_Wait3=3000;
		*FPGA_PWMB_Duty3=3000;

		*FPGA_PWMB_Wait4=4000;
		*FPGA_PWMB_Duty4=4000;

		*FPGA_PWMB_Wait5=5000;
		*FPGA_PWMB_Duty5=5000;

		*FPGA_PWMB_Wait6=6000;
		*FPGA_PWMB_Duty6=6000;

		*FPGA_PWMB_Wait7=1000;
		*FPGA_PWMB_Duty7=1000;

		*FPGA_PWMB_Wait8=2000;
		*FPGA_PWMB_Duty8=2000;

		*FPGA_PWMB_Wait9=3000;
		*FPGA_PWMB_Duty9=3000;

		*FPGA_PWMB_Wait10=4000;
		*FPGA_PWMB_Duty10=4000;

		*FPGA_PWMB_Wait11=5000;
		*FPGA_PWMB_Duty11=5000;

		*FPGA_PWMB_Wait12=6000;
		*FPGA_PWMB_Duty12=6000;

   	for(i=0;i<100;i++)
   		{
   		j=i;
   		}

		*FPGA_PWMC_Wait1=1000;
		*FPGA_PWMC_Duty1=1000;

		*FPGA_PWMC_Wait2=2000;
		*FPGA_PWMC_Duty2=2000;

		*FPGA_PWMC_Wait3=3000;
		*FPGA_PWMC_Duty3=3000;

		*FPGA_PWMC_Wait4=4000;
		*FPGA_PWMC_Duty4=4000;

		*FPGA_PWMC_Wait5=5000;
		*FPGA_PWMC_Duty5=5000;

		*FPGA_PWMC_Wait6=6000;
		*FPGA_PWMC_Duty6=6000;

		*FPGA_PWMC_Wait7=1000;
		*FPGA_PWMC_Duty7=1000;

		*FPGA_PWMC_Wait8=2000;
		*FPGA_PWMC_Duty8=2000;

		*FPGA_PWMC_Wait9=3000;
		*FPGA_PWMC_Duty9=3000;

		*FPGA_PWMC_Wait10=4000;
		*FPGA_PWMC_Duty10=4000;

		*FPGA_PWMC_Wait11=5000;
		*FPGA_PWMC_Duty11=5000;

		*FPGA_PWMC_Wait12=6000;
		*FPGA_PWMC_Duty12=6000;

   	for(i=0;i<100;i++)
   		{
   		j=i;
   		}

		*FPGA_PWMD_Wait1=1000;
		*FPGA_PWMD_Duty1=1000;

		*FPGA_PWMD_Wait2=2000;
		*FPGA_PWMD_Duty2=2000;

		*FPGA_PWMD_Wait3=3000;
		*FPGA_PWMD_Duty3=3000;

		*FPGA_PWMD_Wait4=4000;
		*FPGA_PWMD_Duty4=4000;

		*FPGA_PWMD_Wait5=5000;
		*FPGA_PWMD_Duty5=5000;

		*FPGA_PWMD_Wait6=6000;
		*FPGA_PWMD_Duty6=6000;

		*FPGA_PWMD_Wait7=1000;
		*FPGA_PWMD_Duty7=1000;

		*FPGA_PWMD_Wait8=2000;
		*FPGA_PWMD_Duty8=2000;

		*FPGA_PWMD_Wait9=3000;
		*FPGA_PWMD_Duty9=3000;

		*FPGA_PWMD_Wait10=4000;
		*FPGA_PWMD_Duty10=4000;

		*FPGA_PWMD_Wait11=5000;
		*FPGA_PWMD_Duty11=5000;

		*FPGA_PWMD_Wait12=6000;
		*FPGA_PWMD_Duty12=6000;

   	for(i=0;i<100;i++)
   		{
   		j=i;
   		}

		*FPGA_PWME_Wait1=1000;
		*FPGA_PWME_Duty1=1000;

		*FPGA_PWME_Wait2=2000;
		*FPGA_PWME_Duty2=2000;

		*FPGA_PWME_Wait3=3000;
		*FPGA_PWME_Duty3=3000;

		*FPGA_PWME_Wait4=4000;
		*FPGA_PWME_Duty4=4000;

		*FPGA_PWME_Wait5=5000;
		*FPGA_PWME_Duty5=5000;

		*FPGA_PWME_Wait6=6000;
		*FPGA_PWME_Duty6=6000;

		*FPGA_PWME_Wait7=1000;
		*FPGA_PWME_Duty7=1000;

		*FPGA_PWME_Wait8=2000;
		*FPGA_PWME_Duty8=2000;

		*FPGA_PWME_Wait9=3000;
		*FPGA_PWME_Duty9=3000;

		*FPGA_PWME_Wait10=4000;
		*FPGA_PWME_Duty10=4000;

		*FPGA_PWME_Wait11=5000;
		*FPGA_PWME_Duty11=5000;

		*FPGA_PWME_Wait12=6000;
		*FPGA_PWME_Duty12=6000;

		//PWM复位功能演示,复位以后,所有PWM为零。
		GpioDataRegs.GPACLEAR.bit.GPIO31=1;
		DELAY_US(1);
		GpioDataRegs.GPASET.bit.GPIO31=1;

// 5. D/A Test


		*DAC1=1024;
		*DAC2=2048;
		*DAC3=3072;
		*DAC4=4095;

// 6.测试FPGA的I/O
// 首先测试是否结果为1
for(j=0;j<10;j++)
{
	for(i=0; i<24; i++)
		test[i]=*(FPGA_IO1_DATA+i);
	i=0;
		}
//再测试是否结果为0
for(j=0;j<100;j++)
{
	for(i=0; i<24; i++)
		test[i]=*(FPGA_IO1_DATA+i);
	i=0;
		}

//输出功能演示,只有输出使能的IO pin才能输出0
		*FPGA_IODIR_LOW=0xFFFF;
		*FPGA_IODIR_HIGH=0x00FF;

		j=0;

	for(i=0; i<24; i++)
	{

		*(FPGA_IO1_DATA+i)=j;
		j=~j;
		}

		j=0;

	for(i=0; i<24; i++)
	{
		j=~j;
		*(FPGA_IO1_DATA+i)=j;

		}

//复位功能演示,复位以后,所有IO上拉。且输出不使能
		GpioDataRegs.GPACLEAR.bit.GPIO30=1;
		DELAY_US(1);
		GpioDataRegs.GPASET.bit.GPIO30=1;

		j=*FPGA_IODIR_LOW;
		j=*FPGA_IODIR_HIGH;

		j=0;
	for(i=0; i<24; i++)
		*(FPGA_IO1_DATA+i)=j;

//开用到的中断,最后开中断。测试SCI 通讯以及DSP 和FPGA的通讯

	EnableInterrupts();  //cpu timer0 中断

	i=0;
	
// 六. 主循环IDLE loop. Just sit and loop forever (optional):
  	for(;;)
   	{
	//	ScicRegs.SCITXBUF=i;
		DELAY_US(1000);

/*
		if(GpioDataRegs.GPADAT.bit.GPIO27==0)
			GpioDataRegs.GPBCLEAR.bit.GPIO50=1;
		else 
			GpioDataRegs.GPBSET.bit.GPIO50=1;
						
		if(GpioDataRegs.GPBDAT.bit.GPIO48==0)
			GpioDataRegs.GPBCLEAR.bit.GPIO51=1;	
		else 
			GpioDataRegs.GPBSET.bit.GPIO51=1;	
	//	ModebusRegsDataBuff[0]=i;
//		GpioDataRegs.GPBTOGGLE.bit.GPIO60=1;
//		GpioDataRegs.GPBTOGGLE.bit.GPIO61=1;		
//		GpioDataRegs.GPBTOGGLE.bit.GPIO58=1;
//		GpioDataRegs.GPBTOGGLE.bit.GPIO59=1;

     	GpioDataRegs.GPBCLEAR.bit.GPIO53= 1;
     	GpioDataRegs.GPBCLEAR.bit.GPIO52= 1;
						
		i++;
	//	if(i==2000)
		if(i==FPGA_DATA_Test_length)
			i=0;
	//	TestState=~TestState;

*/
	
		slavecomm();	

		
   	}

	
}
main() {
    InitSysCtrl(); // 初始化system
    DINT;
    // 初始化PIE
    InitPieCtrl();
    // 初始化ADC
    InitPieVectTable();
    MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);
    InitFlash();
    //AdcOffsetSelfCal();
    InitAdc();

    IER = 0x0000;
    IFR = 0x0000;
    // 初始化中斷向量表

    InitEPwm1Gpio();
    GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1;  // GPIO0 = PWM1A
    GpioCtrlRegs.GPADIR.bit.GPIO0 = 1;   //GPIO0 = output
    EALLOW;
    SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;
    InitEPwm1Example();

    InitCpuTimers();
    ConfigCpuTimer(&CpuTimer0, 1, 3906); //必須先配置  再致能 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    CpuTimer0Regs.TCR.all = 0x4000;   // 致能CPUTIMER0

    EDIS;

    // 設定中斷向量表位置
    EALLOW;
    PieVectTable.XINT1 = &xint1_isr;
    PieVectTable.XINT2 = &xint2_isr;
    PieVectTable.ADCINT1 = &adc_isr;
    PieVectTable.TINT0 = &cputimer_isr;
    EDIS;

    // 設置外部中斷腳位及燈號腳位
    EALLOW;
    GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 0;         // GPIO ADC開關
    GpioCtrlRegs.GPADIR.bit.GPIO1 = 0;          // input
    GpioCtrlRegs.GPAQSEL1.bit.GPIO1 = 0;        // XINT1 Synch to SYSCLKOUT only
    GpioIntRegs.GPIOXINT1SEL.bit.GPIOSEL = 1;   // XINT1 is GPIO1
    GpioCtrlRegs.GPAPUD.bit.GPIO1 = 0;

    GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 0;         // GPIO
    GpioCtrlRegs.GPADIR.bit.GPIO2 = 1;          // OUTput
    GpioDataRegs.GPACLEAR.bit.GPIO2 = 1; 		// 當ADC ON/OFF 標示燈

    GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 0;			// GPIO ePWM開關
    GpioCtrlRegs.GPADIR.bit.GPIO3 = 0;
    GpioCtrlRegs.GPAQSEL1.bit.GPIO3 = 0;        // XINT2 Synch to SYSCLKOUT only
    GpioIntRegs.GPIOXINT2SEL.bit.GPIOSEL = 3;   // XINT2 is GPIO3
    GpioCtrlRegs.GPAPUD.bit.GPIO3 = 0;  		//

    GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 0;         // GPIO
    GpioCtrlRegs.GPADIR.bit.GPIO4 = 1;          // OUTput
    GpioDataRegs.GPACLEAR.bit.GPIO4 = 1; 		// 當 ePWM ON/OFF 標示燈

    GpioCtrlRegs.GPAMUX1.bit.GPIO6 = 0;         // GPIO
    GpioCtrlRegs.GPADIR.bit.GPIO6 = 1;          // OUTput 閃爍用
    GpioDataRegs.GPACLEAR.bit.GPIO6 = 1;
    EDIS;

    IER |= M_INT1;  // 開啟GROUP1中斷
    EINT;
    ERTM;

    EALLOW;
    PieCtrlRegs.PIECTRL.bit.ENPIE = 1;  // 致能PIE(非必要)
    PieCtrlRegs.PIEIER1.bit.INTx1 = 0;
    PieCtrlRegs.PIEIER1.bit.INTx4 = 1;
    PieCtrlRegs.PIEIER1.bit.INTx5 = 1;
    //PieCtrlRegs.PIEIER1.bit.INTx7 = 1;  // !!!!!!!! 不能同時開啟  , 否則打架  !!!!!!!!!
    XIntruptRegs.XINT1CR.bit.POLARITY = 3; // interrupt occur on both falling and rising edge
    XIntruptRegs.XINT1CR.bit.ENABLE = 1; // Enable Xint1
    XIntruptRegs.XINT2CR.bit.POLARITY = 3; // interrupt occur on both falling and rising edge
    XIntruptRegs.XINT2CR.bit.ENABLE = 1; // Enable Xint2
    EDIS;

    LoopCount = 0;
    ConversionCount = 0;

// Configure ADC
// Note: Channel ADCINA4  will be double sampled to workaround the ADC 1st sample issue for rev0 silicon errata

    EALLOW;
    AdcRegs.ADCCTL1.bit.ADCENABLE = 0;  // 不致能ADC (在initadc中已經致能)
    AdcRegs.ADCCTL1.bit.INTPULSEPOS = 1; //ADCINT1 trips after AdcResults latch

    AdcRegs.INTSEL1N2.bit.INT1E = 0;	//disabled ADCINT1
    AdcRegs.INTSEL1N2.bit.INT1CONT = 0;	//Disable ADCINT1 Continuous mode
    AdcRegs.INTSEL1N2.bit.INT1SEL = 0;	//setup EOC0 to trigger ADCINT1 to fire

    AdcRegs.ADCSOC0CTL.bit.CHSEL = 1;	//set SOC0 channel select to ADCINA1
    AdcRegs.ADCSOC0CTL.bit.TRIGSEL = 1;	//set SOC0 start trigger on CPUtimer
    AdcRegs.ADCSOC0CTL.bit.ACQPS = 6;//set SOC0 S/H Window to 7 ADC Clock Cycles, (6 ACQPS plus 1)
    //AdcRegs.ADCINTFLGCLR.bit.ADCINT1 = 1;
    EDIS;

    //GpioCtrlRegs.AIOMUX1.bit. &= ~(0x08);  // ADCINA1
//
    EALLOW;
    SysCtrlRegs.XCLK.bit.XCLKOUTDIV = 2;
    EDIS;

    for (;;) {
        //volatile long double n;
        //for (n = 0; n < 5000; n++)
        //	;
        //EALLOW;
        //GpioDataRegs.GPATOGGLE.bit.GPIO6 = 1;
        //EDIS;

        if (GpioDataRegs.GPADAT.bit.GPIO1 == 1) {
            Voltage1[array] = AdcResult.ADCRESULT0;

            if (Voltage1[array] > 5000) {
                EALLOW;
                SysCtrlRegs.PCLKCR1.bit.EPWM1ENCLK = 0;
                SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;
                SysCtrlRegs.LPMCR0.bit.LPM = 3;  // 全部休眠
                EDIS;

                EALLOW;
                SysCtrlRegs.CLKCTL.bit.WDHALTI = 1;  // 從休眠中開啟 , 全部重設!!
                EDIS;
            }

            v1 = 3031;
            v2 = (int)Voltage1[array];
            k1 = 0.3;	//0.0001;
            k2 = 0.000055;
            verr = v1 - v2;
            vk2 = verr + z1;
            z1 = verr;
            vko2 =(int) vk2 * k2;
            if (vko2 >= 1400)
            {
                vko2 = 1400;
            }
            else if (vko2 <= -1300) 	///////////////////////
            {
                vko2 = -1300;
            }
            vou2 = vko2 + z2;
            z2 = vou2;
            vk1 = verr;
            vko1 =(int) vk1 * k1;
            vout =(int)( vou2 + vko1);
            if (vout >= 1400) {
                vout = 1400;
            } else if (vout <= 0) {
                vout = 0;

            }

            EALLOW;
            EPwm1Regs.CMPA.half.CMPA = 1500 - vout;     // Set compare A value
            EDIS;
            if (array < 255)
                array++;
            else
                array = 0;
        }
    }

}
示例#17
0
void InitSysCtrl(void)
{
    // Disable the watchdog
    DisableDog();

#ifdef _FLASH
// Copy time critical code and Flash setup code to RAM
// This includes the following functions:  InitFlash();
// The  RamfuncsLoadStart, RamfuncsLoadSize, and RamfuncsRunStart
// symbols are created by the linker. Refer to the device .cmd file.
    memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);

// Call Flash Initialization to setup flash waitstates
// This function must reside in RAM
    InitFlash();
#endif

    // *IMPORTANT*
    // The Device_cal function, which copies the ADC & oscillator calibration values
    // from TI reserved OTP into the appropriate trim registers, occurs automatically
    // in the Boot ROM. If the boot ROM code is bypassed during the debug process, the
    // following function MUST be called for the ADC and oscillators to function according
    // to specification. The clocks to the ADC MUST be enabled before calling this
    // function.
    // See the device data manual and/or the ADC Reference
    // Manual for more information.
#ifdef CPU1
    EALLOW;

    //enable pull-ups on unbonded IOs as soon as possible to reduce power consumption.
    GPIO_EnableUnbondedIOPullups();

	CpuSysRegs.PCLKCR13.bit.ADC_A = 1;
	CpuSysRegs.PCLKCR13.bit.ADC_B = 1;
	CpuSysRegs.PCLKCR13.bit.ADC_C = 1;
	CpuSysRegs.PCLKCR13.bit.ADC_D = 1;

    //check if device is trimmed
    if(*((Uint16 *)0x5D1B6) == 0x0000){
        //device is not trimmed, apply static calibration values
        AnalogSubsysRegs.ANAREFTRIMA.all = 31709;
        AnalogSubsysRegs.ANAREFTRIMB.all = 31709;
        AnalogSubsysRegs.ANAREFTRIMC.all = 31709;
        AnalogSubsysRegs.ANAREFTRIMD.all = 31709;
    }

	CpuSysRegs.PCLKCR13.bit.ADC_A = 0;
	CpuSysRegs.PCLKCR13.bit.ADC_B = 0;
	CpuSysRegs.PCLKCR13.bit.ADC_C = 0;
	CpuSysRegs.PCLKCR13.bit.ADC_D = 0;
    EDIS;

    // Initialize the PLL control: PLLCR and CLKINDIV
    // F28_PLLCR and F28_CLKINDIV are defined in F2837xD_Examples.h
    // Note: The internal oscillator CANNOT be used as the PLL source if the
    // PLLSYSCLK is configured to frequencies above 194 MHz.
    InitSysPll(XTAL_OSC,IMULT_20,FMULT_1,PLLCLK_BY_2); 		//PLLSYSCLK = 20MHz(XTAL_OSC) * 20 (IMULT) * 1 (FMULT) /  2 (PLLCLK_BY_2)

    //Turn on all peripherals
	InitPeripheralClocks();
#endif
}
示例#18
0
void main(void)
{
	char s1[32];
	unsigned char flag = 0;

// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2802x_SysCtrl.c file.
   InitSysCtrl();

// Step 2. Initalize GPIO:
// This example function is found in the DSP2802x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio();
// Setup only the GP I/O only for I2C functionality
   InitI2CGpio();

// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
   DINT;

// Initialize PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2802x_PieCtrl.c file.
   InitPieCtrl();

// Disable CPU interrupts and clear all CPU interrupt flags:
   IER = 0x0000;
   IFR = 0x0000;

// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example.  This is useful for debug purposes.
// The shell ISR routines are found in DSP2802x_DefaultIsr.c.
// This function is found in DSP2802x_PieVect.c.
   InitPieVectTable();
#ifndef _DEBUG
   // Copy time critical code and Flash setup code to RAM
   // This includes the following ISR functions: EPwm1_timer_isr(), EPwm2_timer_isr()
   // EPwm3_timer_isr and and InitFlash();
   // The  RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart
   // symbols are created by the linker. Refer to the F2808.cmd file.
      MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);

   // Call Flash Initialization to setup flash waitstates
   // This function must reside in RAM
      InitFlash();
#endif
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP2802x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
   I2C_Init();		//use polling

// Step 5. Com port initial
   scia_echoback_init();
   scia_msg("-- Network Analyzer V0.03--\r\n");
   scia_msg("-- Build On: "__DATE__" "__TIME__"--\r\n");
   scia_msg("-- Start: 36.125KHz--\r\n");
   scia_msg("-- End:   100kHz   --\r\n");
   scia_msg("-- Step : 125Hz    --\r\n");
   scia_msg("-- Point: 511      --\r\n");

// Step 6. BSP init, LEDs & Button
   led_init();
   button_init();
   GPIOx_Init();
   //blink
   led_on(0x007f);
   led_off(0x007f);
   //clear GPIO0~GPIO3
   GPIOx_Clear(0x000f);

// Step 7. Initial AD5933
   ad5933_init();

   // Application loop
   for(;;)
   {
	   /*
	    * GPIO12 pressed routine
	    */
	   while( (0 == GpioDataRegs.GPADAT.bit.GPIO12)
			   && ( 0 == GpioDataRegs.GPADAT.bit.GPIO19 ) ){};
	   //GPIO12 pressed
	   if(1 == GpioDataRegs.GPADAT.bit.GPIO12)
	   {
		  //clear GPIO0~GPIO3
		  GPIOx_Clear(0x000f);
		  DELAY_US(100000);    // Delay 100ms , wait
		  sprintf(s1,"Temperature=%d\r\n", ad5993_GetTemperature());
		  scia_msg(s1);
		  ad5933_sweep( 0x0001 << 0, mag_ref);
		  //led indicator
		  led_off(0x007f);
		  if( diff_variance < AD5933_STANDARD_VARIANCE )
		  {
			  led_off(0x01 << 0);	//led0 off
			  led_off(0x01 << 6);	//led6 off
			  sprintf(s1,"Ref larger than standard.\r\n" );
			  scia_msg(s1);
		  }
		  else
		  {
			  led_on(0x01 << 0);	//led0 on
			  led_on(0x01 << 6);	//led6 on
			  sprintf(s1,"Ref less than standard.\r\n" );
			  scia_msg(s1);
		  }
		  scia_PrintLF();
	   }

	   /*
	    * GPIO19 pressed routine
	    */
	   if( 1 == GpioDataRegs.GPADAT.bit.GPIO19)
	   {
		   //clear flag
		   flag = 0;

		   //GROUP1--0b1010
		   //clear GPIO0~GPIO3
		   GPIOx_Clear(0x000f);
		   //set GPIO1 GPIO3
		   GPIOx_Set(0x000A);
		   // Delay 100ms , wait
		   DELAY_US(100000);
		   sprintf(s1,"Temperature=%d\r\n", ad5993_GetTemperature());
		   scia_msg(s1);
		   ad5933_sweep(0x0001 << 0, mag_ref0);;
		   //led0 indicator
		   led_off(0x007f);
		   if( diff_variance < AD5933_STANDARD_VARIANCE )
		   {
		   	  led_off(0x01 << 0);	//led0 off
			  sprintf(s1,"Group0 Ref larger than standard.\r\n" );
			  scia_msg(s1);
		   }
		   else
		   {
		   	  led_on(0x01 << 0);	//led0 on
		   	  flag++;
              sprintf(s1,"Group0 Ref less than standard.\r\n" );
			  scia_msg(s1);
		   }
		   scia_PrintLF();

		   //GROUP2--0b0101
		   //clear GPIO0~GPIO3
		   GPIOx_Clear(0x000f);
		   //set GPIO0 GPIO2
		   GPIOx_Set(0x0005);
		   // Delay 100ms , wait
		   DELAY_US(100000);
		   sprintf(s1,"Temperature=%d\r\n", ad5993_GetTemperature());
		   scia_msg(s1);
		   ad5933_sweep(0x0001 << 1, mag_ref1);
		   //led1 indicator
		   led_off(0x007f);
		   if( diff_variance < AD5933_STANDARD_VARIANCE )
		   {
		   	  led_off(0x01 << 1);	//led1 off
			  sprintf(s1,"Group1 Ref larger than standard.\r\n" );
			  scia_msg(s1);
		   }
		   else
		   {
		   	  led_on(0x01 << 1);	//led1 on
		   	  flag++;
			  sprintf(s1,"Group1 Ref less than standard.\r\n" );
			  scia_msg(s1);
		   }
		   scia_PrintLF();

		   //GROUP3--0b1111
		   //clear GPIO0~GPIO3
		   GPIOx_Clear(0x000f);
		   //set GPIO0~GPIO3
		   GPIOx_Set(0x000F);
		   // Delay 100ms , wait
		   DELAY_US(100000);
		   sprintf(s1,"Temperature=%d\r\n", ad5993_GetTemperature());
		   scia_msg(s1);
		   ad5933_sweep(0x0001 << 2, mag_ref2);
		   //led2 indicator
		   led_off(0x007f);
		   if( diff_variance < AD5933_STANDARD_VARIANCE )
		   {
		   	  led_off(0x01 << 2);	//led2 off
              sprintf(s1,"Group2 Ref larger than standard.\r\n" );
			  scia_msg(s1);
		   }
		   else
		   {
		   	  led_on(0x01 << 2);	//led2 on
		   	  flag++;
			  sprintf(s1,"Group2 Ref less than standard.\r\n" );
			  scia_msg(s1);
		   }
		   scia_PrintLF();

		   //GROUP4--0b1110
		   //clear GPIO0~GPIO3
		   GPIOx_Clear(0x000f);
		   //set GPIO1~GPIO3
		   GPIOx_Set(0x000E);
		   // Delay 100ms , wait
		   DELAY_US(100000);
		   sprintf(s1,"Temperature=%d\r\n", ad5993_GetTemperature());
		   scia_msg(s1);
		   ad5933_sweep(0x0001 << 3, mag_ref3);
		   //led3 indicator
		   led_off(0x007f);
		   if( diff_variance < AD5933_STANDARD_VARIANCE )
		   {
		   	  led_off(0x01 << 3);	//led3 off
  			  sprintf(s1,"Group3 Ref larger than standard.\r\n" );
			  scia_msg(s1);
		   }
		   else
		   {
		   	  led_on(0x01 << 3);	//led3 on
		   	  flag++;
			  sprintf(s1,"Group3 Ref less than standard.\r\n" );
			  scia_msg(s1);
		   }
		   scia_PrintLF();

		   //GROUP5--0b0110
		   //clear GPIO0~GPIO3
		   GPIOx_Clear(0x000f);
		   //set GPIO2~GPIO3
		   GPIOx_Set(0x0006);
		   // Delay 100ms , wait
		   DELAY_US(100000);
		   sprintf(s1,"Temperature=%d\r\n", ad5993_GetTemperature());
		   scia_msg(s1);
		   ad5933_sweep(0x0001 << 4, mag_ref4);
		   //led4 indicator
		   led_off(0x007f);
		   if( diff_variance < AD5933_STANDARD_VARIANCE )
		   {
		   	  led_off(0x01 << 4);	//led4 off
			  sprintf(s1,"Group4 Ref larger than standard.\r\n" );
			  scia_msg(s1);
		   }
		   else
		   {
		   	  led_on(0x01 << 4);	//led4 on
		   	  flag++;
			  sprintf(s1,"Group4 Ref less than standard.\r\n" );
			  scia_msg(s1);
		   }
		   scia_PrintLF();

		   //GROUP6--0b1101
		   //clear GPIO0~GPIO3
		   GPIOx_Clear(0x000f);
		   //set GPIO0 GPIO2 GPIO3
		   GPIOx_Set(0x000D);
		   // Delay 100ms , wait
		   DELAY_US(100000);
		   sprintf(s1,"Temperature=%d\r\n", ad5993_GetTemperature());
		   scia_msg(s1);
		   ad5933_sweep(0x0001 << 5, mag_ref5);
		   //led5 indicator
		   led_off(0x007f);
		   if( diff_variance < AD5933_STANDARD_VARIANCE )
		   {
		   	  led_off(0x01 << 5);	//led5 off
			  sprintf(s1,"Group5 Ref larger than standard.\r\n" );
			  scia_msg(s1);
		   }
		   else
		   {
		   	  led_on(0x01 << 5);	//led5 on
		   	  flag++;
			  sprintf(s1,"Group5 Ref less than standard.\r\n" );
			  scia_msg(s1);
		   }
		   scia_PrintLF();

		   if(6 == flag)
		   {
			   led_on(0x01 << 6);	//led6 on
 			   sprintf(s1,"total less than standard.\r\n" );
			   scia_msg(s1);
		   }
		   else
		   {
			   led_off(0x01 << 6);	//led6 off
			   sprintf(s1,"any larger than standard.\r\n" );
			   scia_msg(s1);
		   }

		   //the end
		   //clear GPIO0~GPIO3
		   GPIOx_Clear(0x000f);
	   }

   }   // end of for(;;)
}   // end of main
示例#19
0
/*********************************************************************
 * Function:        INT8U MRF24J40Init(void)
 *
 * PreCondition:    BoardInit (or other initialzation code is required)
 *
 * Input:           None
 *
 * Output:          Success/error
 *
 * Side Effects:    MRF24J40 is initialized
 *
 * Overview:        This function initializes the MRF24J40
 ********************************************************************/
INT8U MRF24J40Init(void)
{  
    volatile INT8U i;
    volatile INT16U j;
    
#if PROCESSOR == COLDFIRE_V1
    iNesting++;       
#endif
      
    PHY_RESETn_LOW;
    for(j=0;j<7000;j++)
    {
    };

    PHY_RESETn_HIGH;
    for(j=0;j<7000;j++)
    {
    };

    /* do a soft reset */
    PHYSetShortRAMAddr(WRITE_SOFTRST,0x07); 
    for(j=0;j<1000;j++){};
    
    PHYSetShortRAMAddr(WRITE_RFCTL,0x04);
    PHYSetShortRAMAddr(WRITE_RFCTL,0x00);
    for(j=0;j<1000;j++){};

    
    // Enable FIFO and TX Enable
    PHYSetShortRAMAddr(WRITE_FFOEN, 0x98);
    PHYSetShortRAMAddr(WRITE_TXPEMISP, 0x95);    


    /* program the RF and Baseband Register */
    //PHYSetLongRAMAddr(RFCTRL4,0x02);
    
    /* Enable the RX */
    //PHYSetLongRAMAddr(RFRXCTRL,0x01);
    
    /* setup */  
    PHYSetLongRAMAddr(RFCTRL0,0x03);
          
    PHYSetLongRAMAddr(RFCTRL1,RFCTRL1_VAL);
    
    /* enable the RF-PLL */
    PHYSetLongRAMAddr(RFCTRL2,0x80);
    
    /* set TX output power */
    PHYSetLongRAMAddr(RFCTRL3,TX_POWER_LEVEL);
    
    /* program RSSI ADC with 2.5 MHz clock */
    //PHYSetLongRAMAddr(RFCTRL6,0x04);
    PHYSetLongRAMAddr(RFCTRL6,0x90);

    //PHYSetLongRAMAddr(RFCTRL7,0b00000000);
    //PHYSetLongRAMAddr(RFCTRL7,0b10011000);
    PHYSetLongRAMAddr(RFCTRL7,0x80);
    
    PHYSetLongRAMAddr(RFCTRL8,0x10);     
    
    // Disable clockout
    PHYSetLongRAMAddr(SCLKDIV,0x21);


    #if (DEVICE_TYPE != PAN_COORDINATOR)
    // Join network as router
    // and Automatic Acknowledgment enabled
      PHYSetShortRAMAddr(WRITE_RXMCR, 0x04);
    #else
    // Join network as PAN Coordinator
    // and Automatic Acknowledgment enabled
      PHYSetShortRAMAddr(WRITE_RXMCR, 0x0C);
    #endif
    
    /* flush the RX fifo */
    PHYSetShortRAMAddr(WRITE_RXFLUSH,0x01);    
    
    // Configure Unslotted CSMA-CA Mode
    i = PHYGetShortRAMAddr(READ_TXMCR);
    i = i & 0xDF;
    PHYSetShortRAMAddr(WRITE_TXMCR,i);
    
    // Define Nonbeacon-Enabled Network
    PHYSetShortRAMAddr(WRITE_ORDER,0xFF); 


    /* Program CCA mode using RSSI */
    // 0x80 é o valor default, 0xB8 é o recomendado
    PHYSetShortRAMAddr(WRITE_BBREG2,0xB8);
    //PHYSetShortRAMAddr(WRITE_BBREG2,0x80);
    
    /* Program CCA, RSSI threshold values */
    // Valor Default é 0x00, recomendado é 0x60
    // Determina o níve de sinal que indica o canal como ocupado
    // 0x60 = -69 dBm
    PHYSetShortRAMAddr(WRITE_RSSITHCCA,0x60);
    //PHYSetShortRAMAddr(WRITE_RSSITHCCA,0x00);
    
    /* Enable the packet RSSI */
    PHYSetShortRAMAddr(WRITE_BBREG6,0x40);
    
    /* Program the short MAC Address, 0xffff */
    #if (DEVICE_TYPE != PAN_COORDINATOR)
      PHYSetShortRAMAddr(WRITE_SADRL,0xFF);
      PHYSetShortRAMAddr(WRITE_SADRH,0xFF);
    #else
      PHYSetShortRAMAddr(WRITE_SADRL,0x00);
      PHYSetShortRAMAddr(WRITE_SADRH,0x00);    
    #endif
    
    /* and the short PanId Identifier, 0xffff */
    #if (DEVICE_TYPE != PAN_COORDINATOR)    
      PHYSetShortRAMAddr(WRITE_PANIDL,0xFF);
      PHYSetShortRAMAddr(WRITE_PANIDH,0xFF);
    #else
      PHYSetShortRAMAddr(WRITE_PANIDL,(PANID_INIT_VALUE & 0xFF));
      PHYSetShortRAMAddr(WRITE_PANIDH,((PANID_INIT_VALUE>>8) & 0xFF));      
    #endif
    
    /* Test radio */
    do
    {
        PHYSetShortRAMAddr(WRITE_EADR0,0xCA);
        i = PHYGetShortRAMAddr(READ_EADR0);
        if(i!= 0xCA) goto EXIT_ERROR; 
        PHYSetShortRAMAddr(WRITE_EADR1,0xF1);
        i = PHYGetShortRAMAddr(READ_EADR1);
        if(i!= 0xF1) goto EXIT_ERROR; 
        PHYSetShortRAMAddr(WRITE_EADR2,0x05);
        i = PHYGetShortRAMAddr(READ_EADR2);
        if(i!= 0x05) goto EXIT_ERROR; 
        PHYSetShortRAMAddr(WRITE_EADR3,0xBA);
        i = PHYGetShortRAMAddr(READ_EADR3);
        if(i!= 0xBA) goto EXIT_ERROR; 
        PHYSetShortRAMAddr(WRITE_EADR4,0xFF);
        i = PHYGetShortRAMAddr(READ_EADR4);
        if(i!= 0xFF) goto EXIT_ERROR; 
        PHYSetShortRAMAddr(WRITE_EADR5,0x10);
        i = PHYGetShortRAMAddr(READ_EADR5);
        if(i!= 0x10) goto EXIT_ERROR; 
        PHYSetShortRAMAddr(WRITE_EADR6,0x4E);
        i = PHYGetShortRAMAddr(READ_EADR6);
        if(i!= 0x4E) goto EXIT_ERROR; 
        PHYSetShortRAMAddr(WRITE_EADR7,0x11);
        i = PHYGetShortRAMAddr(READ_EADR7);
        if(i!= 0x11) goto EXIT_ERROR;
        
        break;
        
        EXIT_ERROR:
#if PROCESSOR == COLDFIRE_V1
            iNesting--; 
#endif
            return RADIO_ERROR;  
        
    }while(0);

	// Inicializa o módulo de memória FLASH
	InitFlash();
    
#if (PROCESSOR == ARM_Cortex_M0)
    if ((mac64Address[0] == 0xFF) && (mac64Address[1] == 0xFF) && (mac64Address[2] == 0xFF) && (mac64Address[3] == 0xFF))
    {
    	INT8U tmp_mac64[8] = {EUI_0,EUI_1,EUI_2,EUI_3,EUI_4,EUI_5,EUI_6,EUI_7};

    	EraseFlash(0x0001F000, 1024);

    	WriteToFlash((INT8U*)&tmp_mac64, MAC64_MEM_ADDRESS, 8);
    }
#endif
    
    /* Program Long MAC Address*/
    for(i=0;i<8;i++)
    {
        PHYSetShortRAMAddr(WRITE_EADR0+i*2,mac64Address[7-i]);
    }
     
    
    SetChannel(CHANNEL_INIT_VALUE);
    for(j=0;j<1000;j++){};
    
    //i = PHYGetShortRAMAddr(READ_ISRSTS);
    PHYSetShortRAMAddr(WRITE_FFOEN, 0x98);
    
    // Habilita interrupções
    PHYSetShortRAMAddr(WRITE_INTMSK,0xF6);
    
    // bypass the errata to make RF communication stable
    PHYSetLongRAMAddr(RFCTRL1, RFCTRL1_VAL);
    
#if PROCESSOR == COLDFIRE_V1
    iNesting--; 
#endif
    return RADIO_OK;
    
}