/*This callback initializes the GPT1 timer, which controls the sensor st8-mc.*/ void rtc0Callback (void *pCBParam, uint32_t nEvent, void *EventArg) { bRtcInterrupt = true; if (ADI_RTC_ALARM_INT & nEvent) { DEBUG_MESSAGE("RTC interrupt"); /*Initialize st8-mc variables*/ curr_state = 0; cnt_samples = 0; cnt_fan_cycles = 0; cnt_co_heater_cycles = 0; /* Update RTC alarm */ rtc_UpdateAlarm(); /*Initialize debug pins to 0*/ adi_gpio_SetLow(DBG_ST8_PIN); adi_gpio_SetLow(DBG_ADC_PIN); /*kick-start sensor st8-mc*/ adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_500MSEC); adi_tmr_Enable(hDevice1, true); } }
void SetupGPTimer(unsigned int nTimerNum,void *const pMemory,ADI_CALLBACK pfCallback, ADI_TMR_HANDLE *const phTimer, uint32_t nPeriod,uint32_t nWidth) { ADI_TMR_RESULT eTmrResult; /* Open the timer */ if( (eTmrResult = adi_tmr_Open (nTimerNum, pMemory, ADI_TMR_MEMORY, pfCallback, NULL, phTimer)) != ADI_TMR_SUCCESS) { /* Failed to open the timer handle the error here */ } /* * Use the GP timer's API's to configure and enable the timer * */ eTmrResult = adi_tmr_EnableDataInt(*phTimer,false); eTmrResult = adi_tmr_EnableStatusInt(*phTimer,false); /* Set the mode to PWM OUT */ eTmrResult = adi_tmr_SetMode(*phTimer, ADI_TMR_MODE_CONTINUOUS_PWMOUT); /* Set the IRQ mode to get interrupt after counts till TMR_PERIOD register value */ eTmrResult = adi_tmr_SetIRQMode(*phTimer, ADI_TMR_IRQMODE_PERIOD); /* Set the Period */ eTmrResult = adi_tmr_SetPeriod(*phTimer,nPeriod); /* Set the timer width */ eTmrResult = adi_tmr_SetWidth(*phTimer, nWidth); /* Enable the timer */ eTmrResult = adi_tmr_Enable(*phTimer, true); }
void GP_Timer6_Init(void) { ADI_TMR_RESULT eTmrResult = ADI_TMR_SUCCESS; /* Open the timer */ if( (eTmrResult = adi_tmr_Open ( GP_TIMER6_NUM, TimerMemory6, ADI_TMR_MEMORY, Timer6_ISR, NULL, &ghTimer6)) != ADI_TMR_SUCCESS) { printf("Timer open failed 0x%08X \n", eTmrResult); } // Set the mode to PWM OUT if((eTmrResult = adi_tmr_SetMode( ghTimer6, ADI_TMR_MODE_CONTINUOUS_PWMOUT)) != ADI_TMR_SUCCESS) { printf("Failed to open timer in PWM out mode 0x%08X \n", eTmrResult); } //Set the IRQ mode to get interrupt after timer counts to Delay + Width if((eTmrResult = adi_tmr_SetIRQMode( ghTimer6, ADI_TMR_IRQMODE_PERIOD)) != ADI_TMR_SUCCESS) { printf("Failed to set the timer IRQ mode 0x%08X \n", eTmrResult); } //Set the Period if((eTmrResult = adi_tmr_SetPeriod( ghTimer6, TIMER6_PERIOD)) != ADI_TMR_SUCCESS) { printf("Failed to set the timer Period 0x%08X \n", eTmrResult); } //Set the timer width if((eTmrResult = adi_tmr_SetWidth( ghTimer6, TIMER6_WIDTH)) != ADI_TMR_SUCCESS) { printf("Failed to set the timer Width 0x%08X \n", eTmrResult); } // Set the timer Delay if((eTmrResult = adi_tmr_SetDelay( ghTimer6, TIMER6_DELAY)) != ADI_TMR_SUCCESS) { printf("Failed to set the timer Delay 0x%08X \n", eTmrResult); } //Enable the timer to stop gracefully,used in PWM mode,set the PWM wave more graceful. if((eTmrResult = adi_tmr_EnableGracefulStop( ghTimer6, true)) != ADI_TMR_SUCCESS) { printf("Failed to enable the timer to stop gracefully 0x%08X \n", eTmrResult); } adi_tmr_Enable(ghTimer6, true); }
/*GPT1 timer callback contains the sensor st8-mc*/ static void GPTimer1Callback(void *pCBParam, uint32_t Event, void *pArg) { ADI_ADC_RESULT adcResult = ADI_ADC_SUCCESS; ADI_ADC_BUFFER Buffer; adi_tmr_Enable(hDevice1,false); switch(Event) { case ADI_TMR_EVENT_TIMEOUT: switch (curr_state) { /* st8 0 : CO sensor - heater ON for 980 ms*/ case 0 : if (cnt_samples >= CNT_SAMPLES_AVG) { curr_state = 10;//go to PM2.5 sensor - begin by turning on the fan [st8 10] cnt_samples = 0; adi_gpio_SetHigh(LED3); adi_gpio_SetHigh(LED4); adi_gpio_SetLow(CO_HEATER); adi_gpio_SetLow(CO_SENSE); adi_gpio_SetHigh(PM25_FAN); adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_500MSEC); adi_tmr_Enable(hDevice1,true); } else { adi_gpio_SetHigh(CO_HEATER); adi_gpio_SetLow(LED3); adi_gpio_Toggle(DBG_ST8_PIN); /*Wait until 2 heater-cycles of 490 ms are done - i.e., wait for heater to be ON for 980 ms as per sensor spec. (limitation of GPT1 clock frequency - cannot count 980 ms in one go)*/ if (cnt_co_heater_cycles == 1) { curr_state = 1; } else { curr_state = 0; } cnt_co_heater_cycles++; adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_490MSEC); adi_tmr_Enable(hDevice1,true); } break; /* st8 1 : CO sensor - sense circuit ON for 2.5 ms*/ case 1 : adi_gpio_SetHigh(CO_SENSE); adi_gpio_Toggle(DBG_ST8_PIN); curr_state = 2; adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_2p5MSEC); adi_tmr_Enable(hDevice1,true); break; /* st8 2 : CO sensor - trigger ADC sampling on channel-2 */ case 2 : /* Populate the buffer structure */ Buffer.nBuffSize = sizeof(ADC_DataBuffer); Buffer.nChannels = ADI_ADC_CHANNEL_3; Buffer.nNumConversionPasses = ADC_NUM_SAMPLES; Buffer.pDataBuffer = ADC_DataBuffer; /* Submit the buffer to the driver */ adcResult = adi_adc_SubmitBuffer (hDevice, &Buffer); adi_gpio_Toggle(DBG_ST8_PIN); adi_gpio_SetHigh(DBG_ADC_PIN); adi_gpio_SetLow(LED4); curr_state = 3; adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_2p5MSEC); adi_tmr_Enable(hDevice1,true); break; /* st8 2 : CO sensor - heater and sense circuit OFF - wait for 14ms before taking next CO measurement */ case 3 : adi_gpio_SetLow(CO_HEATER); adi_gpio_SetLow(CO_SENSE); adi_gpio_SetHigh(LED3); adi_gpio_Toggle(DBG_ST8_PIN); cnt_co_heater_cycles = 0; curr_state = 0; adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_14MSEC); adi_tmr_Enable(hDevice1,true); break; /*st8 10: PM2.5 sensor FAN ON */ case 10 : /*Wait until 4 cycles of 500 ms are done - i.e., wait for fan to be ON for 2s [actually needs to be on for 10s as per sensor spec, * but trying to save power by reducing fan ON time]*/ if (cnt_fan_cycles == (NUM_FAN_500MS_CYCLES - 1)) { curr_state = 4; } else { curr_state = 10; } cnt_fan_cycles++; adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_500MSEC); adi_tmr_Enable(hDevice1,true); break; /*st8 4: PM2.5 sensor LED ON for 0.28ms */ case 4 : if (cnt_samples >= CNT_SAMPLES_AVG) { adi_tmr_Enable(hDevice1, false); adi_gpio_SetHigh(LED3); adi_gpio_SetHigh(LED4); adi_gpio_SetLow(PM25_LED); adi_gpio_SetLow(PM25_FAN); //cnt_samples = 0; //change } else { adi_gpio_SetHigh(PM25_LED); adi_gpio_SetLow(LED3); adi_gpio_Toggle(DBG_ST8_PIN); curr_state = 5; adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_0p28MSEC); adi_tmr_Enable(hDevice1,true); } break; /*st8 5: PM2.5 sensor - trigger ADC sampling on channel-3 */ case 5 : adi_gpio_SetLow(LED4); /* Populate the buffer structure */ Buffer.nBuffSize = sizeof(ADC_DataBuffer); Buffer.nChannels = ADI_ADC_CHANNEL_2; Buffer.nNumConversionPasses = ADC_NUM_SAMPLES; Buffer.pDataBuffer = ADC_DataBuffer; /* Submit the buffer to the driver */ adcResult = adi_adc_SubmitBuffer (hDevice, &Buffer); adi_gpio_Toggle(DBG_ST8_PIN); adi_gpio_SetHigh(DBG_ADC_PIN); curr_state = 6; adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_0p04MSEC); adi_tmr_Enable(hDevice1,true); break; /*st8 6: PM2.5 sensor - LED OFF, wait for 9.68 ms before next PM2.5 measurement */ case 6 : adi_gpio_SetLow(PM25_LED); adi_gpio_SetHigh(LED3); adi_gpio_Toggle(DBG_ST8_PIN); curr_state = 4; adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_9p68MSEC); //adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_50MSEC); adi_tmr_Enable(hDevice1,true); break; default : break; } break; case ADI_TMR_EVENT_CAPTURED: break; default: break; } }