0

I have just started playing with a low power arduino project, and have managed to get the power drain down to about 220uA using powerDown from LowPower.

What I would love to be able to do is start the USB controller and send some data every 8 seconds, but I cant seem to send anything...

Any idea would be greatly appreciated :)

Her is my code:

// **** INCLUDES *****
#include <LowPower.h>

#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>
#include <avr/io.h>

void setup()
{
  DDRD = B00100000;       // PORTD5 = TXLED
  DDRB = B00000001;        // PORTB0 = RXLed


  Serial.begin(9600);
  while (!Serial) ; // Needed for Leonardo only
  Serial.println("Starter...");
  delay(100);
  Serial.end();

  /*
  *  IKKE FJERN DENNE!!! Da greier vi ikke å omprogramere kortet...
  */
  delay(10000);
  Serial.begin(9600);
  while (!Serial) ; // Needed for Leonardo only
  Serial.println("low-power...");
  delay(100);
  Serial.end();
  flash();
}

void loop() 
{
  //LowPower.idle(SLEEP_8S, ADC_OFF, TIMER4_OFF, TIMER3_OFF, TIMER1_OFF, TIMER0_OFF, SPI_OFF, USART1_OFF, TWI_OFF, USB_OFF);//, USB_OFF);
  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  //USBCON &= B11011111; //Disable USB Freeze clock USBCON.FRZCLK
  //USBCON |= B10010001; //SET USB_MACRO_ENABLE_BIT USBCON.USBE

    PLLCSR |= B00000010;   //-Enable             PLL PLLCSR.PLLE = 1; 
    while (!(PLLCSR & 1)); //-Wait PLL LOCK      while (PLLCSR.PLOCK != 1);
    USBCON &= B11011111;   //-Unfreeze USB clock USBCON.FRZCLK = 0; (Clear to unfreeze)
    //-Clear Resume information ???

  flash();


  /*
  * Test USB kommunikasjon :)
  */
  Serial.begin(9600);
  while (!Serial);
  delay(5);
  Serial.println("Hello world!!!");
  delay(5);
  Serial.end();
   PORTD |= B00100000;
   PORTB |= B00000001;
}

void flash() {

   PORTD &= B11011111;
   PORTB &= B11111110;
   delay(100);
   PORTD |= B00100000;
   PORTB |= B00000001;
   delay(100);

   PORTD &= B11011111;
   PORTB &= B11111110;
   delay(100);
   PORTD |= B00100000;
   PORTB |= B00000001;
   delay(100);

   PORTD &= B11011111;
   PORTB &= B11111110;
   delay(100);
   PORTD |= B00100000;
   PORTB |= B00000001;
   delay(100);
}

Best regards, Richard

EDIT

The manual specifies this resume protocol: Resuming the USB interface (21.13 USB Software Operating Modes)

  • Enable PLL
  • Wait PLL lock
  • Unfreeze USB clock
  • Clear Resume Informtion

which I have translated into this:

PLLCSR |= B00000010;   //-Enable             PLL PLLCSR.PLLE = 1; 
while (!(PLLCSR & 1)); //-Wait PLL LOCK      while (PLLCSR.PLOCK != 1);
USBCON &= B11011111;   //-Unfreeze USB clock USBCON.FRZCLK = 0; (Clear to unfreeze)
//-Clear Resume information ???

It have no effect whatsoever, have no idea how to clear resume information though...

Also, for the record, the LEDs are flashing happily every 8 seconds.

  • You might have to put some delay after the serial.write. Once it comes back from sleep, it probably has to reestablish a USB connection (though your code seems to already account for that), and after that send the serial data. It can't do that when it's sleeping. – Gerben Apr 28 '15 at 19:51
  • Hi Gerben, I just tried that with 800ms, but no success, I will try a little more... Is there any low-level USB commands that needs to be issued again maybe? – Richard87 Apr 28 '15 at 19:56
  • Also, when running in Idle, 200ms is enough before sending data and receiving... edit even 50ms is enough in when idle(usb_off) / serial.end before sending again – Richard87 Apr 28 '15 at 19:59
  • Sorry, I never worked with any USB chip, so I can't really help you any further. – Gerben Apr 29 '15 at 20:00
1

I found an answer to a similar question here: https://forum.sparkfun.com/viewtopic.php?f=32&t=39214&start=15#p175098

Note: it requires you to close and open the serial monitor at proper timings.

// disable the USB prior going to sleep
USBCON |= _BV(FRZCLK);  //freeze USB clock
PLLCSR &= ~_BV(PLLE);   // turn off USB PLL
USBCON &= ~_BV(USBE);   // disable USB

// and wake it up after
//routine to properly wake up
sleep_disable();
delay(100);
USBDevice.attach(); // keep this
delay(100);
Serial.begin(9600);
delay(100);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.