CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

USB and sleep problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
respected



Joined: 16 May 2006
Posts: 95

View user's profile Send private message

USB and sleep problem
PostPosted: Mon Sep 05, 2011 5:11 pm     Reply with quote

hi.
Pcm and Ttelmah helped me. made with Timer1 counter. works seamlessly.

USB does not work when I wrote the following code. I deleted sleep.
it's worked. But it's not work with sleep. I do wonder where is the error.
Thanks.. respects

Code:

#include <18F14K50.h>
#device ADC=10
#fuses HS,NOWDT,NOLVP,NODEBUG,CPUDIV4,NOMCLR,NOPUT,NOBROWNOUT,USBDIV1
#use delay(clock=12000000)
#define USB_CONFIG_HID_TX_SIZE   64
#define USB_CONFIG_HID_RX_SIZE   64
#include <pic18_usb.h>   
#include <usb_log_hid.h> 
#include <usb.c>

int8 out_data[64];
int8 in_data[64];
int16 ticks=0;
int8 a;

void usb_debug_task(void)
{
   if (usb_enumerated())
 
     output_high(pin_c3);

   else

     output_low(pin_c3);
}


void main(void)
{
   setup_timer_1(T1_EXTERNAL| T1_DIV_BY_1 | T1_CLK_OUT);
   set_timer1(0);
   enable_interrupts(INT_TIMER1);
   usb_init_cs();
   
   do{
      ticks=0;
      usb_task();
      usb_debug_task();     

      if (usb_enumerated())
      { 
               
         if (usb_kbhit(1))
         {       
       
    usb_get_packet(1, in_data, 8);

           }       
      }   
    a=in_data[0];   
       do {
               sleep();
               delay_cycles(1); //Instruction pre-fetched - NOP
               clear_interrupt(INT_TIMER1);
             
               } while(++ticks<2); 
   }while(true);
   
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Tue Sep 06, 2011 2:10 am     Reply with quote

You can't sleep like this with USB.....
When sleep is used, the master CPU clock is stopped. Without the clock, the USB peripheral is inactive. Ugh. :(

There are several things you can do though:
1) You can put the processor into 'idle' mode. This is like sleep, but with the master oscillator left running. Use 'sleep(SLEEP_IDLE);'. Typically perhaps 6uA consumption.
2) You can sleep fully, if the driver on the PC, implements remote wake-up. Basically here the PC controls when the device sleeps, and sends a request for the device to go to sleep. This triggers an IDLEIF interrupt on the PIC. Then when it wants the device to be awake, it sends a 'resume' command to the device, which generates a ACTVIF interrupt to the PIC. You would have to add support for this to the USB code, so that on the IDLEIF interrupt, the 'resume signalling' bit is set, and the processor then sleeps. However you must not sleep here, till the PC says 'go to sleep'.
3) Obviously you could potentially implement both, with the processor using IDLE sleep mode, when it wants, and going to full sleep mode when the PC requests this.

Simplest solution use SLEEP_IDLE. Most power efficient use option 3, _but_ this is complex to get working, and can give unreliable behaviour with some motherboard chipset versions....

Best Wishes
respected



Joined: 16 May 2006
Posts: 95

View user's profile Send private message

PostPosted: Tue Sep 06, 2011 2:35 am     Reply with quote

3. option is working with but, draws too much current.

sleep: ~10 uA
sleep idle ~2.5 mA
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Tue Sep 06, 2011 3:58 am     Reply with quote

Part of this, will depend what peripherals you have turned on, and what clock rate you are running the oscillator at.
With SLEEP_IDLE, any clocked peripheral is left running. So (for instance) the UART. This can be turned off separately. Similarly, if you use the PWM, this will stay running, while with full sleep it'll go off. So you have to set it to 0, or 1024, so the output stops. Again the ADC clock will stay running (turn the peripheral off). You should be able to get the power well below this, but it'll never go that low. 2.5mA, is 'in spec', with the USB peripheral running. As a comment, why not power the device off the USB, when USB power is available?. Then (depending on the port used on the motherboard), if you use the primary ports (usually the two closest to the keyboard connector), these remain powered when the PC goes off, or the others will turn off when the PC sleeps.
Also remember you can use the USB connection detect pin, combined with a feed from the USB power via a mosfet, to provide power control. If the USB bus is connected, and therefore extra power is available from this, you implement SLEEP_IDLE, while if no power is available from the USB (and connection detect is off), you implement the full sleep, since the USB peripheral is not needed. On each timer wakeup, you simply check the USB connection detect pin, and change the sleep mode according to which is needed. It'll delay the detection of the peripheral a tiny amount when it is first connected to the PC, but only a couple of seconds 'worst case'.

Best Wishes
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Sep 06, 2011 6:43 am     Reply with quote

gee..
3. option is working with but, draws too much current.

Guess I'm getting real old ! I don't think 2.5mA is a lot of current!!

I was really happy to get my stuff under 10 mA,25 years ago.Guess we had bigger batteries back then.

Ttelmah is right though, since it's connected via USB, you have LOTS of power available,so my question is why the concern about reducing power?
respected



Joined: 16 May 2006
Posts: 95

View user's profile Send private message

PostPosted: Tue Sep 06, 2011 8:50 am     Reply with quote

I told him I think is missing. My circuit 3V (2032) working with. After it is over and usb plugs onto the end of.
2032 battery is 240 mA. Hence the current drawn is very important for me.
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Tue Sep 06, 2011 2:51 pm     Reply with quote

The point is to use USB power when it is there.
If your unit is plugged onto a USB connector, then power the unit from this, and turn on the USB peripheral. If not, turn the USB peripheral off, and use the full sleep mode.
The USB peripheral on it's own draws over 2mA, clocked at your rate, so as soon as you turn it on, you _must_ be using a larger power source. However you only need to turn it on when the unit sees the USB connection.
You can reduce the power by about 50%, by switching to low power USB, and running at 6MHz, but you still need to turn off the peripheral when only battery power is available. Look at section 22.6.3 in the data sheet. Also look at Example 22-2, for how much current the USB transceiver alone may need to use. No way you can do this on the battery....
Also be aware of the limitations on Vusb. 3.3v required, which requires either an external regulator, or a minimum of 3.6v into the chip. Are you going to be able to supply this as the battery drops.....

Best Wishes
respected



Joined: 16 May 2006
Posts: 95

View user's profile Send private message

PostPosted: Tue Sep 06, 2011 3:51 pm     Reply with quote

Mr. Ttelmah
My circuit is link. http://img26.imageshack.us/img26/1309/14k50.jpg

Code:

#include <18F14K50.h>
#device ADC=10
#fuses HS,NOWDT,NOLVP,NODEBUG,CPUDIV4,NOMCLR,NOPUT,NOBROWNOUT,USBDIV1
#use delay(clock=12000000)
#define USB_CONFIG_HID_TX_SIZE   64
#define USB_CONFIG_HID_RX_SIZE   64
#include <pic18_usb.h>   
#include <usb_log_hid.h> 
#include <usb.c>

int8 out_data[64];
int8 in_data[64];
int16 ticks=0;
int8 a;

void usb_debug_task(void)
{
   if (usb_enumerated())
 
     ticks=100;

   else

     ticks=0;
}


void main(void)
{
   setup_timer_1(T1_EXTERNAL| T1_DIV_BY_1 | T1_CLK_OUT);
   set_timer1(0);
   enable_interrupts(INT_TIMER1);
   usb_init_cs();
   
   do{
      ticks=0;
      usb_task();
      usb_debug_task();     

      if (usb_enumerated())
      { 
               
         if (usb_kbhit(1))
         {       
       
    usb_get_packet(1, in_data, 8);

           }       
      }   
    a=in_data[0];   
       do {
               sleep();
               delay_cycles(1); //Instruction pre-fetched - NOP
               clear_interrupt(INT_TIMER1);
             
               } while(++ticks<2); 
   }while(true);
   
}

Why codes does not work that way when we change.
I wish
Let the enumerated control the PIC wakes up.
If you get in to sleep mode if it is not plugged into usb.
Usb is plugged in the USB commands wait.
When working with such very little current.
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Wed Sep 07, 2011 2:21 am     Reply with quote

It is a bit like the car driver, who can't understand that you have to turn the headlights off, when you switch off the engine, or the battery will quite quickly run down. The USB peripheral and transceiver draws too much power to be left switched on when you are running off the battery only. End of story. You _must__ setup the hardware/software to only enable this when the USB power is there.
Now there are several things here that look dubious:
1) LED with no current limiting resistor. Will overload the PIC output, and run the battery down very fast indeed.
2) The diode connection to the USB power, risks overloading the USB port, and damaging the battery. Are you trying to charge the battery?.
3) What is the point of the connection from this junction to MCLR?. It can't detect the presence of USB power, since the junction is connected to the battery, and you have NOMCLR selected....
4) You show 22pF load capacitors on both crystals. Is this right? (unlikely - most watch crystals require about 12pF for Cl, while 12MHz crystals will typically use something more like 18pF. Now Cl on the crystal, is Cl=C/2+Cs if you are using two identical capacitors, and Cs is the stray capacitance (typically perhaps 4pF for a reasonably laid out board). Changing this round to solve for C, we get C=(Cl-Cs)*2, which for a typical watch crystal gives C = (12.5-4)*2 = 17pF (use 18pF), while for the 12MHz crystal (need to check what the specified load capacitance is....), you get C = (18-4)*2 = 28pf (say 27pF). 22pF, would be for a crystal wanting something close to 15pF load.
5) Obviously we can't see how the actual power is routed, since you have used one of these schematic programs that doesn't show the Vdd connection. However there is no decoupling shown, and this is _necessary_ for reliability.

Best Wishes
respected



Joined: 16 May 2006
Posts: 95

View user's profile Send private message

PostPosted: Wed Sep 07, 2011 3:12 am     Reply with quote

1 - 22pF capacitance (12MHZ) and 27 pF (32768Hz)
2 - There are LED diode resistance. But this is not a representative circuit.

The circuit is designed to draw minimal current. ISIS program is simply drawn.

I want to wake up from sleep mode after 8 seconds to check usb is inserted. (8 sec. or different value)
If you plugged the USB to work without sleep

and USB is removed and sleep again
Is this possible?
thanks. best regards
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Wed Sep 07, 2011 4:26 am     Reply with quote

Yes.
I have already outlined what you need to do.
You need:
1) to implement the USB connection sense pin, so the code can tell if power is available from the USB connector.
2) To sort out the power supply connections, for automatic power switching. Because of the low voltage available from the battery, you will probably want to use a MOSFET switch to actually connect the battery power, rather than a diode.
3) Remember that with the code as shown, the board is actually waking for a few cycles every 2 seconds, not 8. Use this to detect the USB.
4) Query if you need the 12MHz crystal. You can operate low speed USB off the internal oscillator of the chip. This requires less power than the crystal oscillator, and also takes less time to wake up from sleep, reducing consumption further.

Best Wishes
respected



Joined: 16 May 2006
Posts: 95

View user's profile Send private message

PostPosted: Wed Sep 07, 2011 7:20 am     Reply with quote

I think I explained wrong. hardware is no problem.

1-sleep
2- USB plug and wake up (or not sleep)
3- USB unplug and sleep
that's all
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Wed Sep 07, 2011 7:56 am     Reply with quote

Hardware _is_ a problem.
What you have drawn, _won't_ allow what you are asking for. You need to rethink your hardware, and implement the features in this that are _needed_.

Critical first one is the USB connection sense pin. Without this you are _stuffed_. USB cannot enumerate, without the hardware turned on. It draws too much power (and enumeration takes too long) to turn the chipset on when on battery power, so you _must_ implement the connection sense, so the code can use this to know when USB is connected.

Also as shown the circuit risks destroying the battery, or blowing the self-resetting fuse in the USB power line.

Best Wishes
respected



Joined: 16 May 2006
Posts: 95

View user's profile Send private message

PostPosted: Wed Sep 07, 2011 8:05 am     Reply with quote

Critical first one is the USB connection sense pin ?
4 connection point VDD D+ D- VSS
Other than the connection of a pin 4 of this?


Yes. I understand now. USB connector vdd is vbus attach pin.
respected



Joined: 16 May 2006
Posts: 95

View user's profile Send private message

PostPosted: Wed Sep 07, 2011 1:03 pm     Reply with quote

Mr. Ttelmah
pin_c5 used as a sense pin.
How should I write a code in this case. How should I write a code in this case.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group