void main()
{
    volatile int status = 0;
    uint16_t i;
    volatile FILE *fid;
    
    // If running from flash copy RAM only functions to RAM   
#ifdef _FLASH
    memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
#endif      

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



    // For this example, only init the pins for the SCI-A port.
    EALLOW;
    GpioCtrlRegs.GPCMUX2.bit.GPIO84 = 1;
    GpioCtrlRegs.GPCMUX2.bit.GPIO85 = 1;
    GpioCtrlRegs.GPCGMUX2.bit.GPIO84 = 1;
    GpioCtrlRegs.GPCGMUX2.bit.GPIO85 = 1;
    EDIS;


    //  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 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();


    // Initialize SCIA
    scia_init();

    //Initialize GPIOs for the LEDs and turn them off
    EALLOW;
    GpioCtrlRegs.GPADIR.bit.GPIO12 = 1;
    GpioCtrlRegs.GPADIR.bit.GPIO13 = 1;
    GpioDataRegs.GPADAT.bit.GPIO12 = 1;
    GpioDataRegs.GPADAT.bit.GPIO13 = 1;
    EDIS;


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

    
    // Configure the ADC:
    // Initialize the ADC
	EALLOW;

	//write configurations
	AdcaRegs.ADCCTL2.bit.PRESCALE = 6; //set ADCCLK divider to /4
	AdcbRegs.ADCCTL2.bit.PRESCALE = 6; //set ADCCLK divider to /4
    AdcSetMode(ADC_ADCA, ADC_RESOLUTION_12BIT, ADC_SIGNALMODE_SINGLE);
    AdcSetMode(ADC_ADCB, ADC_RESOLUTION_12BIT, ADC_SIGNALMODE_SINGLE);

	//Set pulse positions to late
	AdcaRegs.ADCCTL1.bit.INTPULSEPOS = 1;
	AdcbRegs.ADCCTL1.bit.INTPULSEPOS = 1;

	//power up the ADCs
	AdcaRegs.ADCCTL1.bit.ADCPWDNZ = 1;
	AdcbRegs.ADCCTL1.bit.ADCPWDNZ = 1;

	//delay for 1ms to allow ADC time to power up
	DELAY_US(1000);


   //ADCA
   EALLOW;
   AdcaRegs.ADCSOC0CTL.bit.CHSEL = 0x0E;  //SOC0 will convert pin ADCIN14
   AdcaRegs.ADCSOC0CTL.bit.ACQPS = 25; //sample window is acqps + 1 SYSCLK cycles
   AdcaRegs.ADCSOC1CTL.bit.CHSEL = 0x0E;  //SOC1 will convert pin ADCIN14
   AdcaRegs.ADCSOC1CTL.bit.ACQPS = 25; //sample window is acqps + 1 SYSCLK cycles
   AdcaRegs.ADCINTSEL1N2.bit.INT1SEL = 1; //end of SOC1 will set INT1 flag
   AdcaRegs.ADCINTSEL1N2.bit.INT1E = 1;   //enable INT1 flag
   AdcaRegs.ADCINTFLGCLR.bit.ADCINT1 = 1; //make sure INT1 flag is cleared



    
    //Redirect STDOUT to SCI
    status = add_device("scia", _SSA, SCI_open, SCI_close, SCI_read, SCI_write, SCI_lseek, SCI_unlink, SCI_rename);
    fid = fopen("scia","w");
    freopen("scia:", "w", stdout);
    setvbuf(stdout, NULL, _IONBF, 0);
    
    //Print a TI Logo to STDOUT
    drawTILogo();

    //Twiddle LEDs
    GpioDataRegs.GPADAT.bit.GPIO12 = 0;
    GpioDataRegs.GPADAT.bit.GPIO13 = 1;


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

        GpioDataRegs.GPATOGGLE.bit.GPIO12 = 1;
        GpioDataRegs.GPATOGGLE.bit.GPIO13 = 1;
        DELAY_US(50000);


    }

    //LEDs off
    GpioDataRegs.GPADAT.bit.GPIO12 = 1;
    GpioDataRegs.GPADAT.bit.GPIO13 = 1;
        
    
    //Clear out one of the text boxes so we can write more info to it
    clearTextBox();
    
    currentSample = sampleADC();
    

    //Main program loop - continually sample temperature
    for(;;) {
        

        //Sample ADCIN14
        currentSample = sampleADC();
        
        //Update the serial terminal output
        updateDisplay();
        
        //If the sample is above midscale light one LED
        if(currentSample > 2048){
    	    GpioDataRegs.GPADAT.all = 0x2000;
        }else{
            //Otherwise light the other
    	    GpioDataRegs.GPADAT.all = 0x1000;
        }

        DELAY_US(1000000);
        
    }
}
Пример #2
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;
		//}

	}

}