uint8_t lis302dl_init(void) { power_twi_enable(); i2c_init(); //configure ports LIS302_INT1_DDR &= ~(LIS302_INT1_POS); LIS302_INT1_PORT &= ~(LIS302_INT1_POS); LIS302_INT2_DDR &= ~(LIS302_INT2_POS); LIS302_INT2_PORT &= ~(LIS302_INT2_POS); uint8_t who_am_i = 0; who_am_i = lis302_read_register(LIS302_REGISTER_WHO_AM_I); if (LIS302_WHO_AM_I_VALUE != who_am_i) { return -1; } //set active & enable axis uint8_t ctrl_reg_value = _BV(7) | _BV(6) | _BV(2) | _BV(1) | _BV(0); lis302_write_register(LIS302_REGISTER_CTRL_REG_1, ctrl_reg_value); uint8_t reg_value = lis302_read_register(LIS302_REGISTER_CTRL_REG_1); if (reg_value != ctrl_reg_value) { return -1; } else { return 0; } }
void i2c_start() { // Enable I2C power_twi_enable(); TWBR = TWI_BR; // Datasheet says to re-initialize after waking up // START TWCR = _BV(TWINT)|_BV(TWSTA)|_BV(TWEN); // Wait for START loop_until_bit_is_set(TWCR, TWINT); }
/* Configure the pins on the ATmega328p */ void InitDevice() { /* Disable all possible devices on the board */ power_all_disable(); /* LCD setup */ /* All PORTB pins are data pins for the LCD screen */ DDRB = (uint8_t)(-1); /* PDO, PD1, and PD3 are all used as control pins for the LCD screen. * PD0 is used as the R/S pin, which determines whether the LCD is getting a * command or a character * PD1 is the Enable pin, which signals the LCD to read the data pins * PD3 is the R/W Pin which is used to activate the Busy flag on the LCD and * is used for timing */ DDRD |= (_BV(PD0) | _BV(PD1) | _BV(PD3)); LcdInit(); /* Display title screen */ LcdWriteString(TITLE, LCD_LINE_ONE); LcdWriteString(NAME, LCD_LINE_TWO); _delay_ms(STARTUP_DELAY); LcdWriteString(BOOTUP, LCD_LINE_TWO); /* Calibration circuit */ /* PD4 is used to determine if the calibration circuit is active or not */ DDRD &= ~_BV(PD4); /* Configure PD4 as an input */ PORTD |= _BV(PD4); /* Pullup PD4 */ /* Configure the ADC */ //Currently not implemented //power_adc_enable(); /* Magnetometer set up */ /* INT0 is attached to the DataReady pin on the magnometer, and will be used * to signal that data is ready to be read (obviously). INT0 is set to go * off on a rising edge. */ EIMSK |= _BV(INT0); EICRA |= _BV(ISC01) | _BV(ISC00); /* Configure TWI */ power_twi_enable(); TWCR = _BV(TWEN); /* Enable TWI */ //May not be needed here, but definitely elsewhere //Check to see if the magnetometer needs initialization from the ATmega //will need init //Display "Waiting on data" on line 2 }
/********************************************************************************************************* ** Function name: wakeUp ** Descriptions: wakeUp *********************************************************************************************************/ void xadow::wakeUp() { #if defined(__AVR_ATmega32U4__) power_adc_enable(); power_usart0_enable(); power_spi_enable(); power_twi_enable(); power_timer1_enable(); power_timer2_enable(); power_timer3_enable(); power_usart1_enable(); power_usb_enable(); #endif }
void twiInit (uint8_t address, uint8_t mask) { power_twi_enable () ; delay (1) ; TWSR = 0 ; TWBR = 0 ; TWAR = address << 1 ; TWAMR = mask << 1 ; TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) ; twiStop () ; twiReleaseBus () ; #ifdef DEBUG_TWI_LED digitalWrite (DEBUG_TWI_LED, 0) ; #endif }
void Low_Power::idle(Period_t period, ADC_t adc, Timer4_t timer4, Timer3_t timer3, Timer1_t timer1, Timer0_t timer0, SPI_t spi, USART1_t usart1, TWI_t twi, usb_t usb) { if (adc == ADC_OFF) { ADCSRA &= ~(1 << ADEN); power_adc_disable(); } if (timer4 == TIMER4_OFF) power_timer4_disable(); if (timer3 == TIMER3_OFF) power_timer3_disable(); if (timer1 == TIMER1_OFF) power_timer1_disable(); if (timer0 == TIMER0_OFF) power_timer0_disable(); if (spi == SPI_OFF) power_spi_disable(); if (usart1 == USART1_OFF) power_usart1_disable(); if (twi == TWI_OFF) power_twi_disable(); if (usb == USB_OFF) power_usb_disable(); if (period != SLEEP_FOREVER) { wdt_enable(period); WDTCSR |= (1 << WDIE); } lowPowerBodOn(SLEEP_MODE_IDLE); if (adc == ADC_OFF) { power_adc_enable(); ADCSRA |= (1 << ADEN); } if (timer4 == TIMER4_OFF) power_timer4_enable(); if (timer3 == TIMER3_OFF) power_timer3_enable(); if (timer1 == TIMER1_OFF) power_timer1_enable(); if (timer0 == TIMER0_OFF) power_timer0_enable(); if (spi == SPI_OFF) power_spi_enable(); if (usart1 == USART1_OFF) power_usart1_enable(); if (twi == TWI_OFF) power_twi_enable(); if (usb == USB_OFF) power_usb_enable(); }
void Low_Power::idle(Period_t period, ADC_t adc, Timer5_t timer5, Timer4_t timer4, Timer3_t timer3, Timer2_t timer2, Timer1_t timer1, Timer0_t timer0, SPI_t spi, USART3_t usart3, USART2_t usart2, USART1_t usart1, USART0_t usart0, TWI_t twi) { // Temporary clock source variable unsigned char clockSource = 0; if (timer2 == TIMER2_OFF) { if (TCCR2B & CS22) clockSource |= (1 << CS22); if (TCCR2B & CS21) clockSource |= (1 << CS21); if (TCCR2B & CS20) clockSource |= (1 << CS20); // Remove the clock source to shutdown Timer2 TCCR2B &= ~(1 << CS22); TCCR2B &= ~(1 << CS21); TCCR2B &= ~(1 << CS20); power_timer2_disable(); } if (adc == ADC_OFF) { ADCSRA &= ~(1 << ADEN); power_adc_disable(); } if (timer5 == TIMER5_OFF) power_timer5_disable(); if (timer4 == TIMER4_OFF) power_timer4_disable(); if (timer3 == TIMER3_OFF) power_timer3_disable(); if (timer1 == TIMER1_OFF) power_timer1_disable(); if (timer0 == TIMER0_OFF) power_timer0_disable(); if (spi == SPI_OFF) power_spi_disable(); if (usart3 == USART3_OFF) power_usart3_disable(); if (usart2 == USART2_OFF) power_usart2_disable(); if (usart1 == USART1_OFF) power_usart1_disable(); if (usart0 == USART0_OFF) power_usart0_disable(); if (twi == TWI_OFF) power_twi_disable(); if (period != SLEEP_FOREVER) { wdt_enable(period); WDTCSR |= (1 << WDIE); } lowPowerBodOn(SLEEP_MODE_IDLE); if (adc == ADC_OFF) { power_adc_enable(); ADCSRA |= (1 << ADEN); } if (timer2 == TIMER2_OFF) { if (clockSource & CS22) TCCR2B |= (1 << CS22); if (clockSource & CS21) TCCR2B |= (1 << CS21); if (clockSource & CS20) TCCR2B |= (1 << CS20); power_timer2_enable(); } if (timer5 == TIMER5_OFF) power_timer5_enable(); if (timer4 == TIMER4_OFF) power_timer4_enable(); if (timer3 == TIMER3_OFF) power_timer3_enable(); if (timer1 == TIMER1_OFF) power_timer1_enable(); if (timer0 == TIMER0_OFF) power_timer0_enable(); if (spi == SPI_OFF) power_spi_enable(); if (usart3 == USART3_OFF) power_usart3_enable(); if (usart2 == USART2_OFF) power_usart2_enable(); if (usart1 == USART1_OFF) power_usart1_enable(); if (usart0 == USART0_OFF) power_usart0_enable(); if (twi == TWI_OFF) power_twi_enable(); }
/******************************************************************************* * Name: idle * Description: Putting microcontroller into idle state. Please make sure you * understand the implication and result of disabling module. * * Argument Description * ========= =========== * 1. period Duration of low power mode. Use SLEEP_FOREVER to use other wake * up resource: * (a) SLEEP_15MS - 15 ms sleep * (b) SLEEP_30MS - 30 ms sleep * (c) SLEEP_60MS - 60 ms sleep * (d) SLEEP_120MS - 120 ms sleep * (e) SLEEP_250MS - 250 ms sleep * (f) SLEEP_500MS - 500 ms sleep * (g) SLEEP_1S - 1 s sleep * (h) SLEEP_2S - 2 s sleep * (i) SLEEP_4S - 4 s sleep * (j) SLEEP_8S - 8 s sleep * (k) SLEEP_FOREVER - Sleep without waking up through WDT * * 2. adc ADC module disable control: * (a) ADC_OFF - Turn off ADC module * (b) ADC_ON - Leave ADC module in its default state * * 3. timer2 Timer 2 module disable control: * (a) TIMER2_OFF - Turn off Timer 2 module * (b) TIMER2_ON - Leave Timer 2 module in its default state * * 4. timer1 Timer 1 module disable control: * (a) TIMER1_OFF - Turn off Timer 1 module * (b) TIMER1_ON - Leave Timer 1 module in its default state * * 5. timer0 Timer 0 module disable control: * (a) TIMER0_OFF - Turn off Timer 0 module * (b) TIMER0_ON - Leave Timer 0 module in its default state * * 6. spi SPI module disable control: * (a) ADC_OFF - Turn off ADC module * (b) ADC_ON - Leave ADC module in its default state * * 7. usart0 USART0 module disable control: * (a) USART0_OFF - Turn off USART0 module * (b) USART0_ON - Leave USART0 module in its default state * * 8. twi TWI module disable control: * (a) TWI_OFF - Turn off TWI module * (b) TWI_ON - Leave TWI module in its default state * *******************************************************************************/ void LowPowerClass::idle(period_t period, adc_t adc, timer2_t timer2, timer1_t timer1, timer0_t timer0, spi_t spi, usart0_t usart0, twi_t twi) { // Temporary clock source variable unsigned char clockSource = 0; if (timer2 == TIMER2_OFF) { if (TCCR2B & CS22) clockSource |= (1 << CS22); if (TCCR2B & CS21) clockSource |= (1 << CS21); if (TCCR2B & CS20) clockSource |= (1 << CS20); // Remove the clock source to shutdown Timer2 TCCR2B &= ~(1 << CS22); TCCR2B &= ~(1 << CS21); TCCR2B &= ~(1 << CS20); power_timer2_disable(); } if (adc == ADC_OFF) { ADCSRA &= ~(1 << ADEN); power_adc_disable(); } if (timer1 == TIMER1_OFF) power_timer1_disable(); if (timer0 == TIMER0_OFF) power_timer0_disable(); if (spi == SPI_OFF) power_spi_disable(); if (usart0 == USART0_OFF) power_usart0_disable(); if (twi == TWI_OFF) power_twi_disable(); if (period != SLEEP_FOREVER) { wdt_enable(period); WDTCSR |= (1 << WDIE); } lowPowerBodOn(SLEEP_MODE_IDLE); if (adc == ADC_OFF) { power_adc_enable(); ADCSRA |= (1 << ADEN); } if (timer2 == TIMER2_OFF) { if (clockSource & CS22) TCCR2B |= (1 << CS22); if (clockSource & CS21) TCCR2B |= (1 << CS21); if (clockSource & CS20) TCCR2B |= (1 << CS20); power_timer2_enable(); } if (timer1 == TIMER1_OFF) power_timer1_enable(); if (timer0 == TIMER0_OFF) power_timer0_enable(); if (spi == SPI_OFF) power_spi_enable(); if (usart0 == USART0_OFF) power_usart0_enable(); if (twi == TWI_OFF) power_twi_enable(); }
// Shutdown void bg_pwr_down() { // execute sleep routine if (bg_pwr_exec_sleep_routine_flag) { if (bg_pwr_on_sleep != NULL) bg_pwr_on_sleep(); // the flag is needed because we want to execute sleep // routine only when we go from pwr up to pwr down bg_pwr_exec_sleep_routine_flag = 0; } //Shut off ADC, TWI, SPI, Timer0, Timer1, Timer2 ADCSRA &= ~(1<<ADEN); //Disable ADC ACSR |= (1<<ACD); //Disable the analog comparator DIDR0 = 0xFF; //Disable digital input buffers on all ADC0-ADC5 pins DIDR1 = (1<<AIN1D)|(1<<AIN0D); //Disable digital input buffer on AIN1/0 power_twi_disable(); power_spi_disable(); power_usart0_disable(); power_timer0_disable(); power_timer1_disable(); power_timer2_disable(); #if defined(__AVR_ATmega1284P__) power_timer3_disable(); #endif //Power down various bits of hardware to lower power usage set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_mode(); /*********/ /* SLEEP */ /*********/ // The processor wakes up back here after interrupt //Turn on ADC, TWI, SPI, Timer0, Timer1, Timer2 ADCSRA |= (1<<ADEN); // Enable ADC ACSR &= ~(1<<ACD); // Enable the analog comparator // this should be set to reflect real usage of analog pins DIDR0 = 0x00; // Enable digital input buffers on all ADC0-ADC5 pins DIDR1 &= ~(1<<AIN1D)|(1<<AIN0D); // Enable digital input buffer on AIN1/0 power_twi_enable(); power_spi_enable(); power_usart0_enable(); power_timer0_enable(); power_timer1_enable(); power_timer2_enable(); #if defined(__AVR_ATmega1284P__) power_timer3_enable(); #endif // check button press time and handle state unsigned long start = millis(); while ( (ellapsed_millis(start) < BG_PWR_BUTTON_TIME) && (digitalRead(bg_pwr_switch_pin) == HIGH) ); // if the button is pressed continuously for 2 seconds, swap to on state if (ellapsed_millis(start) >= BG_PWR_BUTTON_TIME) bg_pwr_state = BG_STATE_PWR_UP; // lower the button flag bg_pwr_button_pressed_flag = 0; // execute wake up routine only if we really woke up if (bg_pwr_state == BG_STATE_PWR_UP || sd_reader_interrupted) { // execute wake up routine if any is defined if (bg_pwr_on_wakeup != NULL) bg_pwr_on_wakeup(); // next time we sleep execute sleep routine bg_pwr_exec_sleep_routine_flag = 1; } }
void i2c_poweron(i2c_t dev) { assert(dev < I2C_NUMOF); (void) dev; power_twi_enable(); }