chipKIT® Development Platform

Inspired by Arduino™

Sleeping and Serial (USB)

Created Fri, 29 Nov 2013 14:52:47 +0000 by ChristopheDupriez


ChristopheDupriez

Fri, 29 Nov 2013 14:52:47 +0000

Hi! After making the PIC32 sleep a bit, everything works except USB. I spent the day trying many combinations but nothing allows me to regain a working USB.

In setup(), I have: ... U1CNFG1bits.UASUSPND = 1; // U1PWRCbits.USLPGRD = 1; // hardware will prevent the device from entering Sleep mode if USB is active Serial.begin(57600); ... In loop(), I have: ... if sleeping is a good thing: if (!U1PWRCbits.UACTPND ) { // USB is not active digitalWrite(statusLed, HIGH); // To know when I sleep // Turn off USB module: automatic? U1PWRCbits.USUSPEND = 1; // Unlock the OSCCON register SYSKEY = 0x12345678; //write invalid key to force lock SYSKEY = 0xAA996655; //write Key1 to SYSKEY SYSKEY = 0x556699AA; //write Key2 to SYSKEY // OSCCON is now unlocked

      OSCCONSET = 0x10; // set power save mode to Sleep
      // Alternate relock code in ‘C’
      SYSKEY = 0x0;
      // OSCCON is relocked

      asm volatile( "wait" );  

      // Wait for stable PLL clock
      while( !OSCCONbits.SLOCK ); 

      U1PWRCbits.USUSPEND = 0; // automatic?
      if (uBauds) Serial.begin(uBauds); //Restart USB: DO NOT CHANGE ANYTHING
      digitalWrite(statusLed, LOW);
    }

After executing this, USB is not working at all unless I reset.

I would be very grateful for a working example or any suggestion!

Christophe


caroper

Sun, 01 Dec 2013 15:22:41 +0000

I have not personally tried sleep mode but just looking at your code I would suggest trying a Serial.end() statement before you shout down the USB so that all the handles and buffers are released.

Cheers Chris


ChristopheDupriez

Sun, 01 Dec 2013 15:46:46 +0000

Sorry Chris but I see in HardwareSerial.cpp: ... void USBSerial::end() { } ...

Thinking again on this problem, I am telling myself that I do not need as many power saving efforts when the board is powered by USB.

The problem is then:

  • to know when you are USB powered. U1OTGSTATbits.SESVD is 1 when Vbus is present but only AFTER the USB interface has been initialized. Some people recommend to use an analog line to monitor Vbus: I think I will follow this idea. If EmbeddedMan reads this, I would deeply appreciate to know which small via, on FubarinoSD board, is located between external Vusb and the protection diode.

  • to be able to close completely the USB interface when the cable is removed (USBSerial::end above seems incomplete!)

Have a nice day!

Christophe


caroper

Mon, 02 Dec 2013 07:18:33 +0000

If it works after a reset, and you are happy for the app to reset after a sleep, you could try issuing a soft reset on waking up.

it is defined in Plib.h as void SoftReset(void)

I have also used:

void reset (void)
{
    ((void (far *) (void)) 0x000000) ();
}

but that was before I found the version in Plib.

Sleep with USB Power is still useful. I have powerd an MX250 from a Nexus 7 Tablet and a USB OTG adaptor in several experiments and it would be good if the MX250 went to sleep along with the Tablet.

Cheers Chris