示例#1
0
/**
 ****************************************************************************************
 * @brief start measure temperature
 ****************************************************************************************
 */
void start_measure_temperature(void)
{
    
    // eanble temperature sensor
    temp_sensor_enable(MASK_ENABLE);
 
    // ADC initialize
    adc_init(ADC_DIFF_WITH_BUF_DRV, ADC_CLK_1000000, ADC_INT_REF, ADC_12BIT);
    
    // Read voltage. use interrupt
    adc_read_configuration read_cfg;
    read_cfg.trig_src = ADC_TRIG_SOFT;
    read_cfg.mode = CONTINUE_MOD;
    read_cfg.start_ch = TEMP;
    read_cfg.end_ch = TEMP;
    adc_read(&read_cfg, usr_env.htpt_reg_buf, HTPT_TEMP_SAMPLE_NUMBER, adc_sample_complete_callback);
 
}
示例#2
0
/**
 ****************************************************************************************
 * @brief ADC sample complete handler
 ****************************************************************************************
 */
void app_event_adc_sample_cmp_handler(void)
{
    ke_evt_clear(1UL << EVENT_ADC_SAMPLE_CMP_ID);

    // CLose ADC and temp sensor
    temp_sensor_enable(MASK_DISABLE);
    adc_clock_off();
    adc_power_off();
    
    if(usr_env.is_temp_meas_config && usr_env.is_should_indicate)
    {
        usr_env.is_should_indicate = false;
        app_temp_meas_indicate();
    }

    if (usr_env.is_temp_imeas_config)
    {
        app_interm_temp_notify();
    }
}
//***************************************************
// Temperature measurement operation (SNSv7)
//***************************************************
static void operation_temp_run(void){

	if (Tstack_state == TSTK_IDLE){
		#ifdef DEBUG_MBUS_MSG 
			mbus_write_message32(0xBB, 0xFBFB0000);
			delay(MBUS_DELAY*10);
		#endif
		Tstack_state = TSTK_LDO;

		temp_timeout_flag = 0;

		// Power on radio
		if (radio_tx_option || ((exec_count+1) < TEMP_CYCLE_INIT)){
			radio_power_on();
		}

		snsv7_r18.ADC_LDO_ADC_LDO_ENB = 0x0;
		mbus_remote_register_write(SNS_ADDR,18,snsv7_r18.as_int);

		// Put system to sleep
		set_wakeup_timer(WAKEUP_PERIOD_LDO, 0x1, 0x1);
		operation_sleep_noirqreset();

    }else if (Tstack_state == TSTK_LDO){
		#ifdef DEBUG_MBUS_MSG
			mbus_write_message32(0xBB, 0xFBFB1111);
			delay(MBUS_DELAY*10);
		#endif
		Tstack_state = TSTK_TEMP_RSTRL;
		snsv7_r18.ADC_LDO_ADC_LDO_DLY_ENB = 0x0;
		mbus_remote_register_write(SNS_ADDR,18,snsv7_r18.as_int);
		// Put system to sleep
		set_wakeup_timer(WAKEUP_PERIOD_LDO, 0x1, 0x1);
		operation_sleep_noirqreset();

	}else if (Tstack_state == TSTK_TEMP_RSTRL){
		#ifdef DEBUG_MBUS_MSG
			mbus_write_message32(0xBB, 0xFBFB2222);
			delay(MBUS_DELAY*10);
		#endif
		Tstack_state = TSTK_TEMP_READ;

		// Release Temp Sensor Reset
		temp_sensor_release_reset();
		delay(MBUS_DELAY);
			
		// Start Temp Sensor
		temp_sensor_enable();

		// Put system to sleep
		set_wakeup_timer(20, 0x1, 0x1); // FIXME timeout value should be set
		operation_sleep_noirqreset();

	}else if (Tstack_state == TSTK_TEMP_START){
	// Start temp measurement
	#ifdef DEBUG_MBUS_MSG
		mbus_write_message32(0xBB, 0xFBFB3333);
		delay(MBUS_DELAY*10);
	#endif

		mbus_msg_flag = 0;

		// Start Temp Sensor
		temp_sensor_enable();

		// Use Timer32 as timeout counter
		config_timer32(0x249F0, 1, 0, 0); // 1/10 of MBUS watchdog timer default

		// Wait for temp sensor output
		WFI();

		// Turn off Timer32
		*TIMER32_GO = 0;
		Tstack_state = TSTK_TEMP_READ;

	}else if (Tstack_state == TSTK_TEMP_READ){
		#ifdef DEBUG_MBUS_MSG
			mbus_write_message32(0xBB, 0xFBFB4444);
			delay(MBUS_DELAY*10);
		#endif

		// Grab Temp Sensor Data
		if (temp_timeout_flag){
			mbus_write_message32(0xFA, 0xFAFAFAFA);
		}else{
			read_data_reg11 = *((volatile uint32_t *) 0xA0000000);
		}
		meas_count++;

		// Last measurement from this wakeup
		if (meas_count == NUM_TEMP_MEAS){
			// No error; see if there was a timeout
			if (temp_timeout_flag){
				temp_storage_latest = 0x666;
				temp_timeout_flag = 0;
			}else{
				temp_storage_latest = read_data_reg11;

				// Record temp difference from last wakeup adjustment
				if (temp_storage_latest > temp_storage_last_wakeup_adjust){
					temp_storage_diff = temp_storage_latest - temp_storage_last_wakeup_adjust;
				}else{
					temp_storage_diff = temp_storage_last_wakeup_adjust - temp_storage_latest;
				}
				#ifdef DEBUG_MBUS_MSG_1
					mbus_write_message32(0xEA, temp_storage_diff);
					delay(MBUS_DELAY);
				#endif
				
				// FIXME: for now, do this every time					
				//measure_wakeup_period();
				
				if ((temp_storage_diff > 10) || (exec_count < 2)){
					measure_wakeup_period();
					temp_storage_last_wakeup_adjust = temp_storage_latest;
				}
				
			}
		}

		// Option to take multiple measurements per wakeup
		if (meas_count < NUM_TEMP_MEAS){	
			// Repeat measurement while awake
			temp_sensor_disable();
			Tstack_state = TSTK_TEMP_START;
				
		}else{
			meas_count = 0;

			// Assert temp sensor isolation & turn off temp sensor power
			temp_power_off();
			Tstack_state = TSTK_IDLE;

			#ifdef DEBUG_MBUS_MSG_1
				mbus_write_message32(0xCC, exec_count);
				delay(MBUS_DELAY);
				mbus_write_message32(0xC0, temp_storage_latest);
				delay(MBUS_DELAY);
			#endif

			exec_count++;

			// Grab latest PMU ADC readings
			// PMUv2 register read is handled differently
			mbus_remote_register_write(PMU_ADDR,0x00,0x03);
			delay(MBUS_DELAY);
			delay(MBUS_DELAY);
			read_data_batadc = *((volatile uint32_t *) REG0) & 0xFF;
			batadc_reset();
			delay(MBUS_DELAY);

			// Store results in memory; unless buffer is full
			if (temp_storage_count < TEMP_STORAGE_SIZE){
				temp_storage[temp_storage_count] = temp_storage_latest;
				radio_tx_count = temp_storage_count;
				temp_storage_count++;
			}

			// Optionally transmit the data
			if (radio_tx_option){
				send_radio_data_ppm(0, temp_storage_latest);
				delay(RADIO_PACKET_DELAY);
				send_radio_data_ppm(0, read_data_batadc);
			}

			// Enter long sleep
			if(exec_count < TEMP_CYCLE_INIT){
				// Send some signal
				delay(RADIO_PACKET_DELAY);
				send_radio_data_ppm(1, 0xFAF000);
				set_wakeup_timer(WAKEUP_PERIOD_CONT_INIT, 0x1, 0x1);

			}else{	
				set_wakeup_timer(WAKEUP_PERIOD_CONT, 0x1, 0x1);
			}
			
			// Release PMU ADC Reset for Battery Measurement
			batadc_resetrelease();

			// Make sure Radio is off
			if (radio_on){
				radio_ready = 0;
				radio_power_off();
			}

			if (temp_run_single){
				temp_run_single = 0;
				temp_running = 0;
				operation_sleep_notimer();
			}

			if ((set_temp_exec_count != 0) && (exec_count > (50<<set_temp_exec_count))){
				// No more measurement required
				// Make sure temp sensor is off
				temp_running = 0;
				operation_sleep_notimer();
			}else{
				operation_sleep_noirqreset();
			}

		}

    }else{
        //default:  // THIS SHOULD NOT HAPPEN
		// Reset Temp Sensor 
		temp_sensor_assert_reset();
		temp_power_off();
		operation_sleep_notimer();
    }

}
示例#4
0
//*****************************************************************************
// Temperature, battery voltage, light intensity measurement operation (SNSv7)
//*****************************************************************************
static void operation_run(void){

    	uint32_t count; 

	if (Tstack_state == TSTK_IDLE){
		#ifdef DEBUG_MBUS_MSG 
			mbus_write_message32(0xAA, 0x0);
			delay(MBUS_DELAY*10);
		#endif
		Tstack_state = TSTK_TEMP_LDO;

		mbus_remote_register_read(HRV_ADDR,0x02,1);
		delay(MBUS_DELAY);
		read_data_lux = *((volatile uint32_t *) REG1);
		delay(MBUS_DELAY);
		snail_cnt_reset();

		temp_reset_timeout_count = 0;

		// Power on radio
		if (radio_tx_option || ((exec_count+1) < SENSING_CYCLE_INIT)){
			radio_power_on();
		}

		snsv7_r18.ADC_LDO_ADC_LDO_ENB = 0x0;
		mbus_remote_register_write(SNS_ADDR,18,snsv7_r18.as_int);

		// Put system to sleep
		set_wakeup_timer(WAKEUP_PERIOD_LDO, 0x1, 0x1);
		operation_sleep_noirqreset();

    	}else if (Tstack_state == TSTK_TEMP_LDO){
		#ifdef DEBUG_MBUS_MSG
			mbus_write_message32(0xAA, 0x1);
			delay(MBUS_DELAY*10);
		#endif
		Tstack_state = TSTK_TEMP_RSTRL;
		snsv7_r18.ADC_LDO_ADC_LDO_DLY_ENB = 0x0;
		mbus_remote_register_write(SNS_ADDR,18,snsv7_r18.as_int);

		// Put system to sleep
		set_wakeup_timer(WAKEUP_PERIOD_LDO, 0x1, 0x1);
		operation_sleep_noirqreset();

	}else if (Tstack_state == TSTK_TEMP_RSTRL){
		#ifdef DEBUG_MBUS_MSG
			mbus_write_message32(0xAA, 0x2);
			delay(MBUS_DELAY*10);
		#endif
		Tstack_state = TSTK_TEMP_START;

		// Release Temp Sensor Reset
		temp_sensor_release_reset();


	}else if (Tstack_state == TSTK_TEMP_START){
	// Start temp measurement
	#ifdef DEBUG_MBUS_MSG
		mbus_write_message32(0xAA, 0x3);
		delay(MBUS_DELAY*10);
	#endif

		mbus_msg_flag = 0;
		temp_sensor_enable();

		for(count=0; count<TEMP_TIMEOUT_COUNT; count++){
			if( mbus_msg_flag ){
				mbus_msg_flag = 0;
				temp_reset_timeout_count = 0;
				Tstack_state = TSTK_DATA_READ;
				return;
			}else{
				delay(MBUS_DELAY);
			}
		}

		// Time out
		mbus_write_message32(0xAA, 0xFAFAFAFA);
		temp_sensor_disable();

		temp_reset_timeout_count++;
		Tstack_state = TSTK_DATA_READ;


	}else if (Tstack_state == TSTK_DATA_READ){
		#ifdef DEBUG_MBUS_MSG
			mbus_write_message32(0xAA, 0x4);
			delay(MBUS_DELAY*10);
		#endif


		//***************************************************
		// Grab Data
		//***************************************************

		uint32_t read_data_temp_done; // [0] Temp Sensor Done
		uint32_t read_data_temp_data; // [23:0] Temp Sensor D Out

		// PMUv2 register read is handled differently
		mbus_remote_register_write(PMU_ADDR,0x00,0x03);
		delay(MBUS_DELAY);
		read_data_batadc = *((volatile uint32_t *) REG0) & 0xFF;
		delay(MBUS_DELAY);
		batadc_reset();
		delay(MBUS_DELAY);
		
		// Set CPU Halt Option as Rx --> Use for register read e.g.
		set_halt_until_mbus_rx();

		mbus_remote_register_read(SNS_ADDR,0x10,1);
		read_data_temp_done = *((volatile uint32_t *) REG1);
		delay(MBUS_DELAY);
		if (temp_reset_timeout_count == 0){
			mbus_remote_register_read(SNS_ADDR,0x11,1);
			read_data_temp_data = *((volatile uint32_t *) REG1);
		}else{
			read_data_temp_data = 0;
			temp_reset_timeout_count = 0;
			temp_error_count++;
		}
		delay(MBUS_DELAY);

		// Set CPU Halt Option as TX --> Use for register write e.g.
		set_halt_until_mbus_tx();
		delay(MBUS_DELAY);
		batadc_resetrelease();

		//***************************************************
		// Finalize Sensing Operation 
		//***************************************************
		temp_sensor_disable();
		temp_sensor_assert_reset();	
		temp_power_off();
		Tstack_state = TSTK_IDLE;	


	#ifdef DEBUG_MBUS_MSG
		mbus_write_message32(0xCC, exec_count);
		delay(MBUS_DELAY);
		mbus_write_message32(0xCC, read_data_temp_done);
		delay(MBUS_DELAY);
		mbus_write_message32(0xCC, read_data_temp_data);
		delay(MBUS_DELAY);
		mbus_write_message32(0xCC, read_data_lux);
		delay(MBUS_DELAY);
		mbus_write_message32(0xCC, read_data_batadc);
		delay(MBUS_DELAY);
	#endif

	#ifdef DEBUG_MBUS_MSG_1
		mbus_write_message32(0xCC, exec_count);
		delay(MBUS_DELAY);
		mbus_write_message32(0xCC, read_data_temp_done);
		delay(MBUS_DELAY);
		mbus_write_message32(0xCC, read_data_temp_data);
		delay(MBUS_DELAY);
		mbus_write_message32(0xCC, read_data_lux);
		delay(MBUS_DELAY);
		mbus_write_message32(0xCC, read_data_batadc);
		delay(MBUS_DELAY);
	#endif

		exec_count++;
		// Store results in memory; unless buffer is full
		if (data_storage_count < DATA_STORAGE_SIZE){
			data_storage[data_storage_count] = (read_data_batadc<<16) | read_data_temp_data ;

	#ifdef DEBUG_MBUS_MSG_1
		mbus_write_message32(0xDD,data_storage_count);
		delay(MBUS_DELAY);
		mbus_write_message32(0xDD,data_storage[data_storage_count]);
		delay(MBUS_DELAY);
	#endif

		data_storage_count++;
		data_storage[data_storage_count] = read_data_lux;

	#ifdef DEBUG_MBUS_MSG_1
		mbus_write_message32(0xDD,data_storage_count);
		delay(MBUS_DELAY);
		mbus_write_message32(0xDD,data_storage[data_storage_count]);
		delay(MBUS_DELAY);
	#endif

		radio_tx_count = data_storage_count;
		data_storage_count++;
		}

		// Optionally transmit the data
		if (radio_tx_option){
			send_radio_data_ppm(0, read_data_temp_done);
			delay(RADIO_PACKET_DELAY);
			send_radio_data_ppm(0, read_data_temp_data);
			delay(RADIO_PACKET_DELAY);
			send_radio_data_ppm(0, read_data_lux);
			delay(RADIO_PACKET_DELAY);
			send_radio_data_ppm(0, read_data_batadc);
		}

		// Enter long sleep
		if(exec_count < SENSING_CYCLE_INIT){
			delay(RADIO_PACKET_DELAY);
			send_radio_data_ppm(0, 0xABC000+exec_count);
			set_wakeup_timer(WAKEUP_PERIOD_CONT_INIT, 0x1, 0x1);
			if(exec_count == (SENSING_CYCLE_INIT-1)){
				delay(RADIO_PACKET_DELAY);
				send_radio_data_ppm(1, 0xFAF000);
			}

		}else{
			set_wakeup_timer(WAKEUP_PERIOD_CONT, 0x1, 0x1);
		}
		
		// Make sure Radio is off
		if (radio_on){
			radio_ready = 0;
			radio_power_off();
		}

		// Restart HRV counter 
		snail_cnt_start();

		// Discharge battery if vbat is too high
		if ((read_data_batadc != 0) && (read_data_batadc < bat_discharge_threshold)){
    			snsv7_r18.ADC_LDO_ADC_LDO_DLY_ENB = 0;
    			snsv7_r18.ADC_LDO_ADC_LDO_ENB = 0;
    			snsv7_r18.CDC_LDO_CDC_LDO_DLY_ENB = 0;
    			snsv7_r18.CDC_LDO_CDC_LDO_ENB = 0;

	#ifdef DEBUG_MBUS_MSG_1
		mbus_write_message32(0xBB, read_data_batadc);
		delay(MBUS_DELAY);
		mbus_write_message32(0xBB, bat_discharge_threshold);
		delay(MBUS_DELAY);
	#endif

		}else{ 
    			snsv7_r18.ADC_LDO_ADC_LDO_DLY_ENB = 1;
    			snsv7_r18.ADC_LDO_ADC_LDO_ENB = 1;
    			snsv7_r18.CDC_LDO_CDC_LDO_DLY_ENB = 1;
    			snsv7_r18.CDC_LDO_CDC_LDO_ENB = 1;
		}

    		mbus_remote_register_write(SNS_ADDR,18,snsv7_r18.as_int);
		delay(MBUS_DELAY);
			

		if ((set_max_exec_count != 0) && (exec_count > (50<<set_max_exec_count))){
			// No more measurement required
			// Make sure temp sensor is off
			sensor_running = 0;
			temp_power_off();
			operation_sleep_notimer();
		}else{
			operation_sleep_noirqreset();
		}

    }else{
        // default: // THIS SHOULD NOT HAPPEN
		// Reset Temp Sensor 
		temp_sensor_assert_reset();
		temp_power_off();
		operation_sleep_notimer();
    }

}
示例#5
0
int main (void)
{

    SystemInit();

    adc_pin_enable(AIN0, MASK_ENABLE);
    adc_pin_enable(AIN1, MASK_ENABLE);
    
#if ADC_EXT_REF_EN==TRUE
    adc_pin_enable(AIN2, MASK_ENABLE);
    adc_pin_enable(AIN3, MASK_ENABLE);
#endif
    
#if (__AHB_CLK == 32000UL)
    uart_init(QN_UART0, __USART_CLK, UART_1200);
#else
    uart_init(QN_UART0, __USART_CLK, UART_115200);
#endif
    uart_tx_enable(QN_UART0, MASK_ENABLE);
    
    
    // ADC initialization    
#if ADC_WORKING_AT_32K==TRUE
    clk32k_enable(__32K_TYPE);
    adc_init(ADC_SINGLE_WITHOUT_BUF_DRV, ADC_CLK32K_16000, ADC_INT_REF, ADC_12BIT);
#else

#if ADC_EXT_REF_EN==TRUE
    adc_init(ADC_SINGLE_WITHOUT_BUF_DRV, ADC_CLK_1000000, ADC_EXT_REF2, ADC_12BIT);
    //adc_init(ADC_SINGLE_WITHOUT_BUF_DRV, ADC_CLK_1000000, ADC_EXT_REF1, ADC_12BIT);
#else    
    adc_init(ADC_SINGLE_WITHOUT_BUF_DRV, ADC_CLK_1000000, ADC_INT_REF, ADC_12BIT);
#endif
#endif
    
      
    
    // Read configuration
    adc_read_configuration read_cfg;

    
#if ADC_TRIG_BY_GPIO == TRUE

    read_cfg.trig_src = ADC_TRIG_GPIO;
    syscon_SetPMCR2WithMask(QN_SYSCON, SYSCON_MASK_ADCT_PIN_SEL, ADC_GPIO15_TRIG);

    // triger by gpio in single or single scan mode, connect PWM output to ADC trigger PIN
    pwm_init(PWM_CH0);
    pwm_config(PWM_CH0, PWM_PSCAL_DIV, PWM_COUNT_US(50, PWM_PSCAL_DIV), PWM_COUNT_US(25, PWM_PSCAL_DIV));
    pwm_enable(PWM_CH0, MASK_ENABLE);
      
#elif ADC_TRIG_BY_TOF == TRUE

    read_cfg.trig_src = ADC_TRIG_TOVF1;
    // triger by timer1 overflow
    timer_init(QN_TIMER1, NULL);
    timer_pwm_config(QN_TIMER1, TIMER_PSCAL_DIV, TIMER_COUNT_US(100, TIMER_PSCAL_DIV), TIMER_COUNT_US(50, TIMER_PSCAL_DIV));
    timer_enable(QN_TIMER1, MASK_ENABLE);

#elif ADC_TRIG_BY_SOFT == TRUE

    read_cfg.trig_src = ADC_TRIG_SOFT;

#endif


#if ADC_DECIMATION_EN == TRUE
    adc_decimation_enable(DECI_RATE_64, MASK_ENABLE);
#endif


#if ADC_COMPARATOR_EN == TRUE
    //adc_compare_init(DECI_DATA, 2500, -2000, adc_WCMP_cb);
    adc_compare_init(ADC_DATA, 600, -600, adc_WCMP_cb);
#endif


#if (ADC_TRIG_BY_GPIO==TRUE || ADC_TRIG_BY_TOF==TRUE || ADC_TRIG_BY_SOFT==TRUE)
    
    adc_done = 0;
    
    // modify here
    read_cfg.mode = SINGLE_MOD;
    read_cfg.start_ch = AIN0;
    read_cfg.end_ch = AIN0;
    adc_read(&read_cfg, buf, 512, adc_test_cb);
    while (adc_done == 0);
#endif


#if ADC_COMPARATOR_EN == TRUE
    int m = 0;
    int n = 0;
    for (int i = 0; i < 512; i++) {
        if (buf[i] > 600) {
            m++;
        }
        else if (buf[i] < -600) {
            n++;
        }
    }
    printf("m = %d\t n = %d\r\n", m, n);
#endif


    for (int i = 0; i < 512; i++) {
        printf("%d\t %d\r\n", buf[i], ADC_RESULT_mV(buf[i]));
    }
    int sum = 0;
    for (int i = 0; i < 10; i++) {
        sum += buf[511 - 2*i];
    }
    sum = sum / 10;
    printf("average: %d\t %d\r\n", sum, ADC_RESULT_mV(sum));
    


#if ADC_TEMP_SENSOR_EN==TRUE
    temp_sensor_enable(MASK_ENABLE);
    int16_t tempv;
    adc_init(ADC_DIFF_WITH_BUF_DRV, ADC_CLK_1000000, ADC_INT_REF, ADC_12BIT);
    adc_done = 0;
    read_cfg.trig_src = ADC_TRIG_SOFT;
    read_cfg.mode = SINGLE_MOD;
    read_cfg.start_ch = TEMP;
    read_cfg.end_ch = TEMP;
    adc_read(&read_cfg, &tempv, 1, adc_test_cb);
    while (adc_done == 0);
    printf("temperature: %0.1f\r\n", (float)(TEMPERATURE_X10(tempv)/10.0));
#endif


#if ADC_BATT_MONITOR_EN==TRUE
    battery_monitor_enable(MASK_ENABLE);
    int16_t battv;
    adc_init(ADC_SINGLE_WITH_BUF_DRV, ADC_CLK_1000000, ADC_INT_REF, ADC_12BIT);
    adc_done = 0;
    read_cfg.trig_src = ADC_TRIG_SOFT;
    read_cfg.mode = SINGLE_MOD;
    read_cfg.start_ch = BATT;
    read_cfg.end_ch = BATT;
    adc_read(&read_cfg, &battv, 1, adc_test_cb);
    while (adc_done == 0);
    printf("battery voltage: %d\r\n", 4*ADC_RESULT_mV(battv));
#endif
    
    
    while (1)                                /* Loop forever */
    {


    }
}
//***************************************************
// Temperature measurement operation (SNSv7)
//***************************************************
static void operation_temp_run(void){
    uint32_t count; 

	if (Tstack_state == TSTK_IDLE){
		#ifdef DEBUG_MBUS_MSG 
			mbus_write_message32(0xBB, 0xFBFB0000);
			delay(MBUS_DELAY*10);
		#endif
		Tstack_state = TSTK_LDO;

		temp_reset_timeout_count = 0;

		// Power on radio
		if (radio_tx_option || ((exec_count+1) < TEMP_CYCLE_INIT)){
			radio_power_on();
		}

		snsv7_r18.ADC_LDO_ADC_LDO_ENB = 0x0;
		mbus_remote_register_write(SNS_ADDR,18,snsv7_r18.as_int);

		// Put system to sleep
		set_wakeup_timer(WAKEUP_PERIOD_LDO, 0x1, 0x1);
		operation_sleep_noirqreset();

    }else if (Tstack_state == TSTK_LDO){
		#ifdef DEBUG_MBUS_MSG
			mbus_write_message32(0xBB, 0xFBFB1111);
			delay(MBUS_DELAY*10);
		#endif
		Tstack_state = TSTK_TEMP_RSTRL;
		snsv7_r18.ADC_LDO_ADC_LDO_DLY_ENB = 0x0;
		mbus_remote_register_write(SNS_ADDR,18,snsv7_r18.as_int);
		// Put system to sleep
		set_wakeup_timer(WAKEUP_PERIOD_LDO, 0x1, 0x1);
		operation_sleep_noirqreset();

	}else if (Tstack_state == TSTK_TEMP_RSTRL){
		#ifdef DEBUG_MBUS_MSG
			mbus_write_message32(0xBB, 0xFBFB2222);
			delay(MBUS_DELAY*10);
		#endif
		Tstack_state = TSTK_TEMP_START;

		// Release Temp Sensor Reset
		temp_sensor_release_reset();

	}else if (Tstack_state == TSTK_TEMP_START){
	// Start temp measurement
	#ifdef DEBUG_MBUS_MSG
		mbus_write_message32(0xBB, 0xFBFB3333);
		delay(MBUS_DELAY*10);
	#endif

		mbus_msg_flag = 0;
		temp_sensor_enable();

		if (temp_run_single){
			Tstack_state = TSTK_TEMP_READ;
			set_wakeup_timer(WAKEUP_PERIOD_RESET, 0x1, 0x1);
			operation_sleep_noirqreset();
		}

		for(count=0; count<TEMP_TIMEOUT_COUNT; count++){
			if( mbus_msg_flag ){
				mbus_msg_flag = 0;
				temp_reset_timeout_count = 0;
				Tstack_state = TSTK_TEMP_READ;
				return;
			}else{
				delay(MBUS_DELAY);
			}
		}

		// Time out
		mbus_write_message32(0xFA, 0xFAFAFAFA);
		temp_sensor_disable();

		if (temp_reset_timeout_count > 0){
			temp_reset_timeout_count++;
			temp_sensor_assert_reset();

			if (temp_reset_timeout_count > 5){
				// Temp sensor is not resetting for some reason. Go to sleep forever
				Tstack_state = TSTK_IDLE;
				temp_power_off();
				operation_sleep_notimer();
			}else{
				// Put system to sleep to reset the layer controller
				Tstack_state = TSTK_TEMP_RSTRL;
				set_wakeup_timer (WAKEUP_PERIOD_RESET, 0x1, 0x1);
				operation_sleep();
			}

	    }else{
		// Try one more time
	    	temp_reset_timeout_count++;
			temp_sensor_assert_reset();
	    }

	}else if (Tstack_state == TSTK_TEMP_READ){
		#ifdef DEBUG_MBUS_MSG
			mbus_write_message32(0xBB, 0xFBFB4444);
			delay(MBUS_DELAY*10);
		#endif

		// Grab Temp Sensor Data
		uint32_t read_data_reg10; // [0] Temp Sensor Done
		uint32_t read_data_reg11; // [23:0] Temp Sensor D Out

		// Set CPU Halt Option as RX --> Use for register read e.g.
		set_halt_until_mbus_rx();

		mbus_remote_register_read(SNS_ADDR,0x10,1);
		read_data_reg10 = *((volatile uint32_t *) 0xA0000004);
		delay(MBUS_DELAY);
		mbus_remote_register_read(SNS_ADDR,0x11,1);
		read_data_reg11 = *((volatile uint32_t *) 0xA0000004);
		delay(MBUS_DELAY);

		// Set CPU Halt Option as TX --> Use for register write e.g.
		set_halt_until_mbus_tx();

	#ifdef DEBUG_MBUS_MSG
		mbus_write_message32(0xCC, 0xCCCCCCCC);
		delay(MBUS_DELAY);
		mbus_write_message32(0xCC, exec_count);
		delay(MBUS_DELAY);
		mbus_write_message32(0xCC, read_data_reg10);
		delay(MBUS_DELAY);
		mbus_write_message32(0xCC, read_data_reg11);
		delay(MBUS_DELAY);
		mbus_write_message32(0xCC, exec_count);
		delay(MBUS_DELAY);
		mbus_write_message32(0xCC, 0xCCCCCCCC);
		delay(MBUS_DELAY);
	#endif

	#ifdef DEBUG_MBUS_MSG_1
		mbus_write_message32(0xCC, exec_count);
		delay(MBUS_DELAY);
		mbus_write_message32(0xCC, set_temp_exec_count);
		delay(MBUS_DELAY);
		mbus_write_message32(0xCC, read_data_reg11);
		delay(MBUS_DELAY);
		mbus_write_message32(0xCC, read_data_reg10);
		delay(MBUS_DELAY);
	#endif

		// Option to take multiple measurements per wakeup

		if (meas_count < 0){	
			meas_count++;

			// Repeat measurement while awake
			temp_sensor_disable();
			Tstack_state = TSTK_TEMP_START;
				
		}else{

			meas_count = 0;

			// Finalize temp sensor operation
			temp_sensor_disable();
			temp_sensor_assert_reset();
			Tstack_state = TSTK_IDLE;
			
			// Assert temp sensor isolation & turn off temp sensor power
			temp_power_off();


			if (temp_run_single){
				temp_run_single = 0;
				temp_storage_latest = read_data_reg11;
				return;
			}else{
				exec_count++;
				// Store results in memory; unless buffer is full
				if (temp_storage_count < TEMP_STORAGE_SIZE){
					temp_storage[temp_storage_count] = read_data_reg11;
					temp_storage_latest = read_data_reg11;
					radio_tx_count = temp_storage_count;
					temp_storage_count++;
				}

				// Optionally transmit the data
				if (radio_tx_option){
					send_radio_data_ppm(0, read_data_reg10);
					delay(RADIO_PACKET_DELAY);
					send_radio_data_ppm(0, read_data_reg11);
				}

				// Enter long sleep
				if(exec_count < TEMP_CYCLE_INIT){
					// Send some signal
					delay(RADIO_PACKET_DELAY);
					send_radio_data_ppm(1, 0xFAF000);
					set_wakeup_timer(WAKEUP_PERIOD_CONT_INIT, 0x1, 0x1);

				}else{
					set_wakeup_timer(WAKEUP_PERIOD_CONT, 0x1, 0x1);
				}

				// Make sure Radio is off
				if (radio_on){
					radio_ready = 0;
					radio_power_off();
				}

				if ((set_temp_exec_count != 0) && (exec_count > (50<<set_temp_exec_count))){
					// No more measurement required
					// Make sure temp sensor is off
					temp_running = 0;
					temp_power_off();
					operation_sleep_notimer();
				}else{
					operation_sleep_noirqreset();
					
				}

			}
		}

    }else{
        //default:  // THIS SHOULD NOT HAPPEN
		// Reset Temp Sensor 
		temp_sensor_assert_reset();
		temp_power_off();
		operation_sleep_notimer();
    }

}