/* This function initializes the Hardware Abstraction Layer. * */ void radio_hal_init(void) { /*IO Specific Initialization.*/ DDR_SLP_TR |= (1 << SLP_TR); //Enable SLP_TR as output. DDR_RST |= (1 << RST); //Enable RST as output. radio_spi_init(); radiol_enable_trx_interrupt(); //Enable interrupts from the radio transceiver. }
void do_sampling(void) { /* ADC LR Clock on P0[2], rising edge is trigger */ LPC_IOCON->PIO0_2 &= ~0x07; /* GPIO */ LPC_GPIO0->MASKED_ACCESS[1<<2] = (0<<2); /* Low to start */ LPC_GPIO0->DIR |= (1<<2); /* Output */ /* Setup SPI for the ADC transfer */ wm8737_spi_init(); wm8737_spi_on(); /** * Disable all interrupts - Be careful that the watchdog * calibration timer doesn't occur during this period */ __disable_irq(); /* We need to use a function pointer to jump our execution to RAM */ sampling_func sampling_ptr = sampling; /* Prepare for the sampling loop */ sampling_index = 0; /* Let RAM accesses settle down before we jump */ __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); /* Actually take the sample */ sampling_ptr(); LPC_GPIO0->MASKED_ACCESS[1<<2] = (1<<2); /* P0[2] = ADCLRCLK */ /* Tidy up from the sampling run */ spi_flush(); /* Re-enable all interrupts */ __enable_irq(); /* Return the SPI to how it was before */ wm8737_spi_off(); /** * Revert the SPI bus to working for the radio. */ radio_spi_init(); }
void nodeSleep(u16 tenthSeconds) { // ************** Power down the other board peripherals Leds_off(); // ************** Power down the radio // wait for radio to be done u8 state = BUSY_TX_ARET; while (state == BUSY_TX_ARET || state == BUSY_RX_AACK) state = radioGetTrxState(); // Now put radio to sleep radioEnterSleepMode(); // TODO: figure out what needs to be put to sleep to minimize current consumption // ************** Power down the on-chip modules // PRR = 0xbf; ??? // Disable ADC // ADCSRA &= ~(1 << ADEN); // Turn off comparator // ACSR |= (1 << // turn off ports etc // Turn off BOD // This should only be done once -- No need to do it over again /* AVR_ENTER_CRITICAL_REGION(); #define BODS 6 #define BODSE 5 MCUCR |= (1 << BODSE) | (1<< BODS); MCUCR &= ~(1 << BODSE); AVR_LEAVE_CRITICAL_REGION(); */ // ************** Set the timer to wake up // Set TIMER2 Asyncronous Mode. ASSR |= (1 << AS2); // Set TIMER2 Prescaler to 1024. TCCR2B |= ((1 << CS22)|(1 << CS21)|(1 << CS20)); // Wait for TCNT2 write to finish. while(ASSR & (1 << TCR2BUB)) ; // Sleep as many times as needed to sleep for the full time while (tenthSeconds) { // This is to get the node manually out of sleeping mode -- // Might take up to 7.5Sec to detect button press // if (BUTTON_PRESSED() ) { Led1_on(); macConfig.sleeping = false; while (BUTTON_PRESSED()) ; Led1_off(); break; // exit the Sleeping loop } // Set TIMER2 output compare register from user. if (tenthSeconds > 75) { // Just decrement by the max timeout OCR2A = 240; // 7.5 seconds, max timeout tenthSeconds -= 75; } else { // Can measure the remaining time in one timer cycle tenthSeconds = tenthSeconds * 16 / 5; if (!tenthSeconds) tenthSeconds++; OCR2A = tenthSeconds; tenthSeconds = 0; } // Wait for OCR2 write to finish. while(ASSR & (1 << OCR2AUB)) ; // Reset TIMER2 timer counter value. TCNT2 = 0; // Wait for TCNT2 write to finish before entering sleep. while(ASSR & (1 << TCN2UB)) ; // Clear interrupt flag TIFR2 |= (1 << OCF2A); // Enable TIMER2 output compare interrupt. TIMSK2 |= (1 << OCIE2A); // ************** Go to sleep AVR_ENTER_CRITICAL_REGION(); set_sleep_mode( SLEEP_MODE_PWR_SAVE); sleep_enable(); sei(); sleep_cpu(); // sleeping right here sleep_disable(); AVR_LEAVE_CRITICAL_REGION(); wdt_disable(); } // ************** Awake now, wake up everything // PRR = 0x03; ?? if (SERIAL) serial_init(NULL); debugMsgStr("\r\nNode slept"); if ( macConfig.associated) HAL_ADC_INIT(); // Bring SPI port back up (must re-init after sleep) radio_spi_init(); // Wake up radio. radioLeaveSleepMode(); // Set RF212 to 250KB mode. // TODO: do I need to call this?? //radioSetup900(); radioSetTrxState(PLL_ON); }