/*! Sleep function. * * Which IO line is in use is recorded in the progs struct. * * \note Incompatible with MONOSTABLE valve. */ void go_to_sleep(uint8_t valve, struct debug_t *debug) { if (valve == BISTABLE) { set_sleep_mode(SLEEP_MODE_PWR_SAVE); /* shut down everything */ i2c_shut(); io_shut(); led_shut(); /* start sleep procedure */ sleep_enable(); sleep_bod_disable(); sleep_cpu(); sleep_disable(); /* restart everything */ led_init(); io_init(); i2c_init(); } else { set_sleep_mode(SLEEP_MODE_IDLE); /* start sleep procedure */ sleep_enable(); sleep_cpu(); sleep_disable(); } }
void Pendant::sleepNow() { disableLEDs(); PCMSK0 |= _BV(PCINT0); // Watch pin PB1 GIFR |= _BV(PCIE0); // clear any outstanding interrupts set_sleep_mode(SLEEP_MODE_PWR_DOWN); power_all_disable(); // power off ADC, Timer 0 and 1, serial interface sleep_enable(); sleep_cpu(); // // ... SLEEPING ... // // resumes here on wake sleep_disable(); power_all_enable(); // power everything back on PCMSK0 &= ~_BV(PCINT0); // Turn off PB1 as interrupt pin sleepOnRelease = false; changeModeOnRelease = false; digitalWrite(PENDANT_LED_PWR_PIN, HIGH); }
int main(void) { // General Interrupt Mask Register // INT0: enable external interrupt 0 (on PB1 / pin 6) // Attiny datasheet p46 setBit(GIMSK, PCIE); setBit(PCMSK, PCINT1); // TODO: Power down modules // Set the sleep mode set_sleep_mode(SLEEP_MODE_PWR_DOWN); // setAsOutput(LED); // setBit(PORTB, LED); while(1){ // blinkTimes(state + 1); state %= 5; setState(state); sleep_enable(); // Set enable interrupts sei(); sleep_cpu(); sleep_disable(); } }
/*---------------------------------------------------------------------------*/ void check_bootloader_message(uint8_t timeout) { uint8_t getDataCounter = 0; /* listen to any bootloader messages! */ nrf24_powerUpRx(); while(getDataCounter++ < timeout) { _delay_ms(1); if(nrf24_dataReady()) { nrf24_getData(data_array); if((data_array[0] == 0) && (data_array[1] == 0xAA)) { /* prepare */ cli(); PORTA = 0x00; PORTB = 0x00; DDRA = 0x00; DDRB = 0x00; sleep_disable(); /* jump to bootloader! */ /* bootloader is located at: 0x1800 */ asm volatile("lds r31, 0x18"); // R31 = ZH asm volatile("lds r30, 0x00"); // R30 = ZL asm volatile("icall"); // Jump to the Z register value } } }
static uint16_t read_adc(uint8_t mux) { uint16_t adc_val; // read battery level ADMUX = mux & 0b11011111; // ensure right adjusted #if (F_CPU == 8000000) ADCSRA = 0b10001110; // Enable ADC with clock/64, interrupt enable #elif (F_CPU == 1000000) ADCSRA = 0b10001011; // Enable ADC with clock/8, interrupt enable #else #error Need 8Mhz or 1Mhz clock #endif adc_val = 0; set_sleep_mode(SLEEP_MODE_ADC); // take 8 samples... for (int i=0;i<8;i++) { adc_done = false; ADCSRA |= 0b01000000; // start the conversion cli(); while (!adc_done) { sleep_enable(); sei(); sleep_cpu(); sleep_disable(); } sei(); adc_val += ADCL; adc_val += (ADCH << 8); } ADCSRA &= 0b01111111; // turn off ADC // then return the average return (adc_val >> 3); }
void PowerManager::sleep() { if (!clk_IO.locks) { if (!clk_ADC.locks) { if (!clk_ASY.locks) { if (!clk_MAIN.locks) { set_sleep_mode(SLEEP_MODE_PWR_DOWN); } else { set_sleep_mode(SLEEP_MODE_STANDBY); } } else { if (!clk_MAIN.locks) { set_sleep_mode(SLEEP_MODE_PWR_SAVE); } else { set_sleep_mode(SLEEP_MODE_EXT_STANDBY); } } } else { set_sleep_mode(SLEEP_MODE_ADC); } } else { set_sleep_mode(SLEEP_MODE_IDLE); } sleep_enable(); sleep_cpu(); sleep_disable(); }
static void sleep(int mode) { set_sleep_mode(mode); sleep_enable(); sleep_mode(); sleep_disable(); }
void sleep_until_eswitch_pressed() { WDT_off(); ADC_off(); // make sure switch isn't currently pressed while (button_is_pressed()) {} empty_event_sequence(); // cancel pending input on suspend //PCINT_since_WDT = 0; // ensure PCINT won't ignore itself PCINT_on(); // wake on e-switch event // configure sleep mode set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); sleep_bod_disable(); sleep_cpu(); // wait here // something happened; wake up sleep_disable(); #ifdef USE_THERMAL_REGULATION // forget what the temperature was last time we were on reset_thermal_history = 1; #endif // go back to normal running mode //PCINT_on(); // should be on already // FIXME? if button is down, make sure a button press event is added to the current sequence ADC_on(); WDT_on(); }
/*********************************************************** * * uiPowerTimeOut * ***********************************************************/ void TTUI::uiPowerTimeOut() { if(state_UIPower == true) { #ifndef TT_SHIELD //if not TT shield if((millis() - previousMillis_UIPower) > UI_SLEEP_MS) { uiPowerOff(); set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); sei(); //make sure interrupts are on! detachInterrupt(0); attachInterrupt(0,sleepHandler, FALLING); sleep_mode(); //sleep now //----------------------------- ZZZZZZ sleeping here---------------------- sleep_disable(); //disable sleep, awake now attachInterrupt(0,startDownHandler,FALLING); //trigger ISR function on start button press. uiPowerOn(); } #endif //end TT_Shield } }
void adc_read(const uint8_t ch) { // Voltage reference for ACC is AVCC. // ch is for ADC channel selections. // Note: ch ={0x9, 0xA, 0xB, 0xC, 0xD} are reserved. // Note: ch ={0x4, 0x5} are used by TWI/I2c. ADMUX = (uint8_t)(_BV(REFS0) | (0x0F & ch)); // Enable the ADC. // Setup for ADC noise reduction mode. ADCSRA = (uint8_t)(_BV(ADEN) | _BV(ADIF) | _BV(ADIE) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0)); set_sleep_mode(SLEEP_MODE_ADC); sleep_enable(); // Start and wait for ADC conversion. // Enter sleep mode to start. sleep_cpu(); sleep_disable(); // Disable ADC to save power. ADCSRA = 0x00; // Restore the sleep mode. set_sleep_mode(SLEEP_MODE_IDLE); } // End of adc_read().
static void power_down(uint8_t wdto) { #ifdef PROTOCOL_LUFA if (USB_DeviceState == DEVICE_STATE_Configured) return; #endif wdt_timeout = wdto; // Watchdog Interrupt Mode wdt_intr_enable(wdto); // TODO: more power saving // See PicoPower application note // - I/O port input with pullup // - prescale clock // - BOD disable // - Power Reduction Register PRR set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); sei(); sleep_cpu(); sleep_disable(); // Disable watchdog after sleep wdt_disable(); }
void Sleep_Task (void) { if (!MustSleep) return; // Sleep if Current color is black => LED is faded down yet if ( (ELight.CurrentColor.Red == 0) && (ELight.CurrentColor.Green == 0) && (ELight.CurrentColor.Blue == 0) && (ELight.CurrentColor.Uf == 0) ) { // Enter sleep CC_ENTER_IDLE(); // } CC_POWERDOWN(); // } Shutdown CC //LED_PWR_OFF(); // Shutdown LED power // Enable IRQ to wake cli(); HANDLE_IRQ_SETUP_MASK(); HANDLE_IRQ_CLEAR(); HANDLE_IRQ_ENABLE(); sei(); // Enter sleep mode set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); wdt_disable(); sleep_cpu(); // Sleep now // Something happened, wake now wdt_enable(WDTO_2S); sleep_disable(); HANDLE_IRQ_DISABLE(); // ESens.HandleIsOn = true; // Event hanler will do the rest // EVENT_HandleTouched(); // Execute it from here to switch on immediately } }
void check_if_sleepy(){ bool readyforbed = true; for(int j = 0; j < NumberOfFlys; j++) { if ( flystatus[j] == 1 ) readyforbed=false; } if ( lightness_counter < 10 ) readyforbed=false; cli(); if ( readyforbed==true ) { DDRB = 0; // PORTB = 1<<anode[0]|0<<cathode[0]; // DDRB = 1<<anode[0]|1<<cathode[0]; set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_mode(); sleep_enable(); sei(); sleep_cpu(); sleep_disable(); } sei(); }
void m_rdy_line_handle(void) { hal_aci_data_t *p_aci_data; sleep_disable(); detachInterrupt(1); // Receive or transmit data p_aci_data = hal_aci_tl_poll_get(); // Check if we received data if (p_aci_data->buffer[0] > 0) { if (!m_aci_q_enqueue(&aci_rx_q, p_aci_data)) { /* Receive Buffer full. Should never happen. Spin in a while loop. */ while(1); } if (m_aci_q_is_full(&aci_rx_q)) { /* Disable RDY line interrupt. Will latch any pending RDY lines, so when enabled it again this routine should be taken again */ toggle_eimsk(false); } } }
int main(void) { unsigned char ret; init_io(); cli(); check_wdt(); setup_wdt(); sei(); // Enables interrupts // Enable Sleep Mode for Power Down set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Set Sleep Mode: Power Down sleep_enable(); // Enable Sleep Mode for(;;) { // Event Loop if(tick_flag) { tick_flag = 0; sleep_disable(); set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); sleep_mode(); } } }
static uint8_t _getAdcValue(uint8_t pin) { uint8_t ret = 0; ADMUX &= ~(_BV(MUX0) | _BV(MUX1)); ADMUX |= pin; adcBufferWritten = 0; ADCSRA |= _BV(ADIE); set_sleep_mode(SLEEP_MODE_IDLE); sleep_enable(); while (adcBufferWritten != ADC_BUFFER_SIZE) { while (TCNT0 != TIMER_TOP) {} sleep_cpu(); adcBufferWritten++; }; sleep_disable(); #if defined(ADC_MEDIAN_INSTEAD_OF_AVERAGE) ret = _getMedian(adcBuffer, adcBufferWritten); #else ret = _average(adcBuffer, adcBufferWritten); #endif return ret; }
int main(void) { char refreshed; editor_init(&refreshed); editor_refresh(); set_sleep_mode(SLEEP_MODE_STANDBY); sei(); for (;;) { cli(); if (refreshed) { sleep_enable(); sei(); sleep_cpu(); sleep_disable(); } else { refreshed = 1; sei(); editor_refresh(); } } };
/* * Put the processor into sleep mode, maybe */ void maybe_sleep(void) { // If we need to do the main loop again, don't bother going to sleep if (do_main_poll || TIFR0 & 0x1 << OCF0A) goto done; // Otherwise, we have some time to kill before our next poll. Time for bed. // Enable the poll interrupt. He is going to tell us to stop sleeping. TIMSK0 |= 0x1 << OCIE0A; do { sleep_enable(); // Enable SLEEP instruction sleep_cpu(); // Wake me when you need me... sleep_disable(); // Woken up. Disable SLEEP instruction // If the ISR we just serviced was not our poll ISR, go back to sleep } while (!do_main_poll); done: // Disable our poll interrupt during our main routine, because we don't // need someone to tell us to do the main poll; we are already doing it! TIMSK0 &= ~(0x1 << OCIE0A); do_main_poll = 0; }
/* * set the microcontroller to sleep. */ void Interrupt::sleep() { RX8025.getRtcTime(sleep_timestamp_ptr); Timer1.detachInterrupt(); set_sleep_mode(SLEEP_MODE_PWR_DOWN); if(WRITE_INTERRUPT_INFO) Serial.print("\nProcessor going to sleep.\n"); if(USE_LED) digitalWrite(LED_port,LOW); digitalWrite(latch_control_port,HIGH); RTC.refreshINTA(); sleep_enable(); attachInterrupt(interrupt0, clock_interupt_ISR, LOW); if(! ROUTER) attachInterrupt(interrupt1, sensor_interupt_ISR, RISING); // if the sensor interrupt goes off between attaching the interrupt and sleep mode finishing, that interrupt is locked out until the RTC interrupt occurs. // The same does not apply to RTC interrupt occuring between attaching the interrupt and sleep mode finishing. sleep_mode(); // Processor is sleeping // sleep_disable(); detachInterrupt(interrupt0); if(! ROUTER) detachInterrupt(interrupt1); if(WRITE_INTERRUPT_INFO) Serial.print("\nProcessor waking up.\n\n"); if(USE_LED) digitalWrite(LED_port,HIGH); sleep_SMC(); sleep_packet_handler(); sleep_packet_counter(); digitalWrite(latch_control_port,LOW); // read sensors }
void sleep_and_wake() { sleep_enable(); // set the sleep enable bit in the SMCR register sleep_cpu(); // use the sleep command sleep_disable(); // unset th esleep enable bit, for safety OCR2B = flag; }
/******************************************************************** * * sleepWDT * ********************************************************************/ int Sleep::sleepWDT(unsigned long remainTime, boolean &abortCycle) { #if defined(WDP3) byte WDTps = 9; // WDT Prescaler value, 9 = 8192ms #else byte WDTps = 7; // WDT Prescaler value, 7 = 2048ms #endif isrcalled = 0; sleep_enable(); while(remainTime > 0) { //work out next prescale unit to use while ((0x10<<WDTps) > remainTime && WDTps > 0) { WDTps--; } // send prescaler mask to WDT_On WDT_On((WDTps & 0x08 ? (1<<WDP3) : 0x00) | (WDTps & 0x07)); isrcalled=0; while (isrcalled==0 && abortCycle == false) { #if defined(__AVR_ATmega328P__) // turn bod off MCUCR |= (1<<BODS) | (1<<BODSE); MCUCR &= ~(1<<BODSE); // must be done right before sleep #endif sleep_cpu(); // sleep here } // calculate remaining time remainTime -= (0x10<<WDTps); if ((long) remainTime < 0 ) {remainTime = 0;} //check for unsigned underflow, by converting to signed } sleep_disable(); return remainTime; }
// // Go into idle or powerdown mode. // static void go2sleep( uint8_t sleepMode ) { set_sleep_mode( sleepMode ); sleep_enable(); sleep_cpu(); sleep_disable(); }
void LLAPSerial::sleep(byte pinToWakeOn, byte direction, byte bPullup) // full sleep wake on interrupt - pin is 2 or 3 { byte adcsraSave = ADCSRA; ADCSRA &= ~ bit(ADEN); // disable the ADC // switch off analog comparator - not in Jeelabs' code ACSR = ACSR & 0x7F; // note if using it then we need to switch this back on when we wake. set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); if (pinToWakeOn == 2) { pinMode(2,INPUT); if (bPullup) digitalWrite(2,HIGH); // enable pullup attachInterrupt(0, pin2_isr, direction); } else { pinMode(3,INPUT); if (bPullup) digitalWrite(3,HIGH); // enable pullup attachInterrupt(1, pin3_isr, direction); } cli(); // sleep_bod_disable(); // can't use this - not in my avr-libc version! #ifdef BODSE MCUCR = MCUCR | bit(BODSE) | bit(BODS); // timed sequence MCUCR = (MCUCR & ~ bit(BODSE)) | bit(BODS); #endif sei(); sleep_cpu(); // and wait until we are woken sleep_disable(); // re-enable what we disabled ADCSRA = adcsraSave; }
void sleep1ms(void) { /* Disable analog comparator to save power */ ACSR = 0b11000000; sleep_over = 0; set_sleep_mode(SLEEP_MODE_IDLE); /* Set compare register to interrupt in 1ms */ OCR0A = TCNT0 + 15; /* Enable OCR0A interrupt */ bit_set(TIMSK0, 1); /* Loop to continue sleeping if any other interrupt wakes CPU */ do { sleep_enable(); sleep_cpu(); } while(sleep_over == 0); /* Done, disable sleep */ sleep_disable(); /* Disable OCR0A interrupt */ bit_clear(TIMSK0, 1); /* Enable analog comparator */ ACSR = 0b01000000; return; }
static inline void sleepNow() { // All LEDs off for(int i = 0; i < LED_COUNT; i++) { *((unsigned char *)pgm_read_ptr(&(LED_PORT[i]))) &= ~_BV(pgm_read_byte(&(LED_PIN[i]))); } // We want to do this carefully. If someone pushes the button // while we're prepareing here, we do NOT want to 'eat' that // interrupt. Rather, we want to enable interrupts at the same // time we call sleep_cpu() (sei() is defferred for one instruction). // That way the button push will effectively cancel the sleep rather // than get 'eaten'. ATOMIC_BLOCK(ATOMIC_FORCEON) { power_timer0_disable(); PCMSK0 |= _BV(PCINT5); GIMSK |= _BV(PCIE0); sleep_enable(); } sleep_cpu(); // And now we wake up sleep_disable(); PCMSK0 &= ~_BV(PCINT5); GIMSK &= ~_BV(PCIE0); power_timer0_enable(); button_debounce_time = millis(); ignoring_button = 1; place_in_pattern = -1; milli_of_next_change = 0; }
void sleepabit(int howlong) { int i2 = 0; delay(100); while (i2 < (howlong / 8)) { cli(); delay(100); // disable ADC //ADCSRA = 0; //prepare interrupts WDTCSR |= (1 << WDCE) | (1 << WDE); // Set Watchdog settings: WDTCSR = (1 << WDIE) | (1 << WDE) | (1 << WDP3) | (0 << WDP2) | (0 << WDP1) | (1 << WDP0); sei(); //wdt_reset(); set_sleep_mode (SLEEP_MODE_PWR_DOWN); sleep_enable(); // turn off brown-out enable in software //MCUCR = bit (BODS) | bit (BODSE); //MCUCR = bit (BODS); sleep_cpu (); // cancel sleep as a precaution sleep_disable(); i2++; } wdt_disable(); }
void SleepClass::sleep(uint8_t modules,uint8_t sm){ // power_adc_disable(); cbi(ADCSRA,ADEN); // switch Analog to Digitalconverter OFF // ACSR = (1<<ACD); //Disable the analog comparator if(!(modules & SPI_ON)) power_spi_disable(); if(!(modules & TWI_ON)) power_twi_disable(); if(!(modules & USART0_ON)) power_usart0_disable(); if(!(modules & TIMER0_ON)) power_timer0_disable(); if(!(modules & TIMER1_ON)) power_timer1_disable(); if(!(modules & TIMER2_ON)) power_timer2_disable(); set_sleep_mode(sm); cli(); do{ sleep_enable(); #if defined __AVR_ATmega328P__ sleep_bod_disable(); #endif sei(); sleep_cpu(); // System sleeps here sleep_disable(); // System continues execution here when an interrupt woke up the divice } while(0); sei(); power_all_enable(); sbi(ADCSRA,ADEN); // switch Analog to Digitalconverter ON }
/** * @brief Main entry point. */ int main (void) { event_t evt; /* Disable watchdog timer since it doesn't always get reset on restart. */ wdt_disable(); /* Initialize the MCU. */ init(); /* Online. */ printf( "Exocore Apollo online...\n" ); /* Start fsm. */ fsm_start(); for (;;) { /* Atomic test to see if has anything to do. */ cli(); /* Handle events. */ while (event_poll(&evt)) { sei(); /* Reenable interrupts. */ fsm( &evt ); cli(); /* Disable for next check. */ } /* Atomic sleep as specified on the documentation. */ sleep_enable(); sei(); sleep_cpu(); sleep_disable(); } }
void suspend_power_down(void) { #ifdef BACKLIGHT_ENABLE backlight_set(0); #endif #ifndef NO_SUSPEND_POWER_DOWN // Enable watchdog to wake from MCU sleep cli(); wdt_reset(); // Watchdog Interrupt and System Reset Mode //wdt_enable(WDTO_1S); //WDTCSR |= _BV(WDIE); // Watchdog Interrupt Mode wdt_intr_enable(WDTO_120MS); // TODO: more power saving // See PicoPower application note // - I/O port input with pullup // - prescale clock // - BOD disable // - Power Reduction Register PRR // sleep in power down mode set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); sei(); sleep_cpu(); sleep_disable(); // Disable watchdog after sleep wdt_disable(); #endif }
int main(void) { init(); alarm_init(); clock_init(); display_init(); sei(); for(;;) { clock_update(); buttons_update(); handle_buttons(); handle_display(); handle_led(); buttons_age(); clock_ticked = false; // Check display timeout if (mode != mode_off || status != status_none || !clock_set) { // Need the main clock set_sleep_mode(SLEEP_MODE_IDLE); } else { set_sleep_mode(SLEEP_MODE_PWR_SAVE); } sleep_enable(); sleep_cpu(); sleep_disable(); } }