Beispiel #1
0
static bool _SYS_TMR_Setup(SYS_TMR_OBJECT* tmrObject)
{
    uint32_t    drvFreq, drvPeriod, errorFreq;
    uint32_t    tickFreq, tickUnitCount;

    // sanity check, protect against user error
    if(sSysTmrObject.sysTickFreq == 0 || SYS_TMR_UNIT_RESOLUTION < 1000 ||  SYS_TMR_UNIT_RESOLUTION / sSysTmrObject.sysTickFreq < 1 )
    {
        return false;
    }

    drvFreq = DRV_TMR_CounterFrequencyGet ( tmrObject->driverHandle );
    // sanity check that we can obtain the requested frequency
    // with the underlying 16 bit timer (period = [2, 0xffff]);
    drvPeriod = drvFreq / tmrObject->sysTickFreq - 1;

    if(drvPeriod < 1 || drvPeriod > 0xffff)
    {   // required freq not within obtainable range
        return false;
    }
    // calculate the actual tick frequency
    tickFreq = drvFreq / (drvPeriod + 1);
    // check we're within requested limits
    if(tickFreq >= tmrObject->sysTickFreq)
    {
        errorFreq = tickFreq - tmrObject->sysTickFreq;
    }
    else
    {
        errorFreq = tmrObject->sysTickFreq - tickFreq;
    }

    if((errorFreq * 100) / tmrObject->sysTickFreq > SYS_TMR_FREQUENCY_TOLERANCE)
    {   // too great an error
        return false;
    }

    // calculate the timer units
    tickUnitCount = SYS_TMR_UNIT_RESOLUTION / tickFreq;
    if(tickUnitCount < 1)
    {    // not enough resolution
        return false;
    }

    // success
    tmrObject->sysTickFreq = tickFreq;
    tmrObject->sysTickUnitCount = tickUnitCount;
    tmrObject->driverFreq = drvFreq;
    tmrObject->driverPeriod = drvPeriod;

 
    return true;
}
Beispiel #2
0
void APP_Initialize ( void )
{
    /* Place the App state machine in its initial state. */
    appData.state = APP_STATE_INIT;
    
    // set the direction of the pins and Open the Timer                 
   MyHandleIsr = DRV_TMR_Open( DRV_TMR_INDEX_0, DRV_IO_INTENT_EXCLUSIVE );
 
   // calculate the divider value and register the ISR
   uint32_t desiredFrequency = 10 ; // 10 hertz
   uint32_t actualFrequency = DRV_TMR_CounterFrequencyGet(MyHandleIsr) ;
   uint32_t divider = actualFrequency/desiredFrequency; // cacluate divider value
   DRV_TMR_AlarmRegister(MyHandleIsr, divider, true, 0 , MyISR);
 
   // Starting the Timer   
   DRV_TMR_Start(MyHandleIsr);
    
   TRISAbits.TRISA0 = 0;
   LATAbits.LATA0 = 0;
   
   /*
    TRISCbits.TRISC2 = 1;
    ANSELCbits.ANSC2 = 1;
    
    
    AD1CAL1 = DEVADC1;                  // Writing Calibration-Data to ADC-Registers
     AD1CAL2 = DEVADC2;
     AD1CAL3 = DEVADC3;
     AD1CAL4 = DEVADC4;
     AD1CAL5 = DEVADC5;
 
    AD1CON1 = 0;                        // First Clear AD1CON1-Register
    AD1CON1bits.STRGSRC = 1;            // Scan Trigger Src = Global Software Trigger (GSWTRG)
    AD1CON1bits.FILTRDLY = 0b11111 ;
    
    AD1CON2 = 0;                        // Clear AD1CON2-Register
     AD1CON2bits.ADCSEL = 1;             // ADC Clock-Src = SYSCLK (200 MHz)
     AD1CON2bits.ADCDIV = 4;             // Clock Divider 4 means (8*TQ) = 25 MHz
     AD1CON2bits.SAMC = 32;              // S&H-Sample Time = 32TAD
 
    AD1CON3 = 0;                        // Clear AD1CON3-Register
 
    AD1CON3bits.VREFSEL = 3;
    AD1IMOD = 0;                        // All SampleAndHold-Units set to Standard-values
                                         // Single-ended Inputs with unsigned integer-Format, Standard-Inputs
     //AD1IMODbits.SH3ALT = 1;             // Alternate Input for Sample And Hold 3 (uses normalla AN3, now AN48 - maybe this has blocked AN3??)
 
    AD1GIRQEN1 = 0;                     // No Interrupts used
     AD1GIRQEN2 = 0;                     // No Interrupts
 
    AD1CSS1 = 0;                        // First Clear all Channel-Scan-Select-Bits (Register 1)
     AD1CSS2 = 0;                        // Clear all CSS-Bits of Register 2
     //AD1CSS1bits.CSS21 = 1;               // Select CSS3 = AN3 for ChannelScan
 
    AD1CMPCON1 = 0;                     // No Comperator-Functions
     AD1CMPCON2 = 0;
     AD1CMPCON3 = 0;
     AD1CMPCON4 = 0;
     AD1CMPCON5 = 0;
     AD1CMPCON6 = 0;
 
    AD1FLTR1 = 0;                       // No Filter / Oversampling
     AD1FLTR2 = 0;
     AD1FLTR3 = 0;
     AD1FLTR4 = 0;
     AD1FLTR5 = 0;
     AD1FLTR6 = 0;

    AD1TRG1 = 0;                        // No Triggers
     AD1TRG2 = 0;                        // No Triggers
     AD1TRG3 = 0;                        // No Triggers

    AD1TRG2bits.TRGSRC7 = 1;            // Trigger for AN3 = STRIG (because AN3 is a Channel1-Input!)

    AD1CON1bits.ADCEN = 1;              // Enable ADC
 
    while (AD1CON2bits.ADCRDY == 0);    // Wait for end of Calibration
    */
    /* TODO: Initialize your application's state machine and other
     * parameters.
     */
}
Beispiel #3
0
void APP_Tasks ( void )
{
    switch ( appData.state )
    {
        case APP_STATE_INIT:
        {
            if(appData.status.ready)
            {
                appData.state = APP_STATE_RUN;
                break;
            }            
            if(appData.status.SPIReady==false)
            {
                appData.LED.SPIHandle = DRV_SPI_Open(DRV_SPI_INDEX_0,
                                                    DRV_IO_INTENT_EXCLUSIVE |
                                                    DRV_IO_INTENT_WRITE |
                                                    DRV_IO_INTENT_NONBLOCKING
                                               );     
                appData.status.SPIReady = ( DRV_HANDLE_INVALID != appData.LED.SPIHandle );
            }
            if(appData.status.TimerDriverReady==false)
            {
                appData.timer.driver.handle = DRV_TMR_Open(
                        DRV_TMR_INDEX_0,
                        DRV_IO_INTENT_READWRITE|DRV_IO_INTENT_EXCLUSIVE);
                appData.status.TimerDriverReady = 
                        (appData.timer.driver.handle != DRV_HANDLE_INVALID);
            }     
            if(appData.status.SPIReady && appData.status.TimerDriverReady)
            {
                appData.LED.start=0;                
                appData.state=APP_STATE_TIMER_START;
            }
            break;
        }        
        case APP_STATE_TIMER_START:
        {  
            if(DRV_TMR_AlarmRegister(appData.timer.driver.handle,
                    DRV_TMR_CounterFrequencyGet(appData.timer.driver.handle)/25,
                    true,
                    0,
                    &TimerCallback)
                )
            {                
                if(DRV_TMR_Start(appData.timer.driver.handle))
                {
                    appData.status.ready=true;
                    appData.state=APP_STATE_RUN;
                }
            }
            break;
        }        
        case APP_STATE_RUN:
        {
            if(!SendingToStrip())
            {                
                APP_TASKS_ACTIVITY_SET;
                PopulateStrip(&appData.LED);
                appData.state=APP_STATE_SEND_STRIP;
            }
            break;
        }
        case APP_STATE_SEND_STRIP:
        {
            if(appData.status.readyForNext)
            {
                appData.status.readyForNext=false;
                SendLEDStrip(&appData.LED);
                /* write each pixel out to the SPI buffer */            
                /* give the data over to the SPI system to send to the LED strip */
                appData.state=APP_STATE_WAIT;
                APP_TASKS_ACTIVITY_CLEAR;
            }
            break;
        }
        case APP_STATE_WAIT:
        {
            /* wait for the SPI transaction to finish. */
            if(SendingToStrip())
            {
                break;
            }
            appData.LED.start++;
            appData.state=APP_STATE_RUN;
            break;
        }        
        case APP_STATE_ERROR:
        {
            break;
        }
        default:
        {
            /* shouldn't get here. */
            APP_Initialize();
            break;
        }
    }
    if(appData.activityLED.blinkCount++>appData.activityLED.interval)
    {
        mActivityLEDInvert();
        appData.activityLED.blinkCount=0;
    }        
}