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 problems on PIC16LF1455

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



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

USB problems on PIC16LF1455
PostPosted: Wed Aug 03, 2016 8:20 pm     Reply with quote

Hi everyone.
I have a project using HID USB connection from circuit to PC.
In my project, I used PIC16LF1455 and used USB driver of CCSC (ver 5.058).
The first, circuit run normally, but after that about 1-2 hours, the circuit hangup (the PC not recognized USB), must power off and on again, the circuit must run.
I also add WDT, BROWNOUT to reset but result not change.
Pls help me to solve this problem.
Thanks all.

Code:

#include "ex_usb_common.h"
#define USE_USB_SERIAL_NUMBER
#define USB_CONFIG_HID_TX_SIZE   8
#define USB_CONFIG_HID_RX_SIZE   8

#include <pic16f_usb.h>       //Microchip PIC16Fxxxx hardware layer for usb.c
#include "usb_desc_hid.h"     //USB Configuration and Device descriptors for this UBS device
#include <usb.c>              //handles usb setup tokens and get descriptor reports

#serialize(id=USB_STRING_DESC, unicode=3, prompt="Serial Number")


#define led_dbg PIN_C3
int8 out_data[USB_CONFIG_HID_TX_SIZE];
int8 in_data[USB_CONFIG_HID_RX_SIZE];

#define  SPI_MISO       PIN_C1   //RC2,RP13; SPI(Usar por hardware)
#define  SPI_MOSI       PIN_C2   //RC7,RP18; SPI(Usar por hardware)
#define  SPI_CLK        PIN_C0   //RB0,RP3;  SPI(Usar por hardware)
#define  RF24_IRQ       PIN_A1   //RC5,RP16; interrupt  nRF24L01+
#define  RF24_CS        PIN_A5   //RB1,RP4;  chipselect nRF24L01+
#define  RF24_CE        PIN_A4   //RB2,RP5;  chipEnable nRF24L01+
#define  RF24_PERFORMANCE_MODE   //performance mode ON
#define  RF24_SPI_DISABLE_WARNING
#define  RF24_SPI       STREAM_SPI


int8 RX_RF_buffer[32];
int8 TX_RF_buffer[6];
int8 RF_Freq[4];
unsigned int32 rf_freq_demo=0;
unsigned int8 rf_freq_set=20;
unsigned int1 set_rf_cmd=0;

int RXdatasize, RXpipe;
unsigned int8 count_t0=0;
unsigned int1 timeout=0;
unsigned int1 retry=0;
unsigned int1 rf_finish=1;
unsigned int1 en_timer=0;
unsigned int8 time_retry=0;
unsigned int1 alert_pc=0;

unsigned int8 loop;

#include "nRF24L01P_HUB.c"

#int_timer1
void ngat_delay_check(void)
{
   set_timer1(5535);

   count_t0++;
   time_retry++;
   if(time_retry==4)
   {
      time_retry=0;
      retry=1;
   }
   if(count_t0>=40)
   {
      count_t0=0;
      timeout=1;
      retry=0;
   }

}
//================================================================================================================================
//-------------------------------------      MAIN PROGRAM      -------------------------------------------------------------------
//================================================================================================================================
void main(void)
{
   delay_ms(3000);
   output_high(PIN_C3);
   
   usb_init_cs();
   delay_ms(500);

   RF24_initPorts();
   RF24_default_config();
   delay_ms(50);
   RF24_check_config();
   RF24_RX_SET();       //Transmitter on   

   set_timer1(5535);                                                               // setup timer1: 120ms
   setup_timer_1 (T1_INTERNAL|T1_DIV_BY_8);   
   enable_interrupts(global);
   
   setup_wdt(WDT_ON|WDT_8s);   // khai bao sd WDT 500ms
   restart_wdt(); 
//!   
//!   for(loop=0;loop<30;loop++)
//!   {
//!      output_toggle(led_dbg);
//!      delay_ms(400);
//!   }
   
   
   for(;;)
   {
      restart_wdt();
      usb_task();
      if (usb_enumerated())
      { 
         if(alert_pc==0)
         {
            delay_ms(2000);
            alert_pc=1;
            switch (restart_cause() )
            {
         
               case NORMAL_POWER_UP:
               {
                  out_data="NORMALPU";
                  usb_put_packet(USB_HID_ENDPOINT, out_data, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE);           
               }
               break;
               
               case MCLR_FROM_POWER_UP:
               {
                  out_data="MCLR_PWU";
                  usb_put_packet(USB_HID_ENDPOINT, out_data, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE);
               }         
               break;
               
               case BROWNOUT_RESTART:
               {
                  out_data="BROWNOUT";
                  usb_put_packet(USB_HID_ENDPOINT, out_data, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE);                     
               }
               break;
               
               case MCLR_FROM_SLEEP:
               {
                  out_data="MCLR_SLE";
                  usb_put_packet(USB_HID_ENDPOINT, out_data, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE);
                           
               }
               break;
               
               case WDT_TIMEOUT:         
               {
                  out_data="WDT_OUT ";
                  usb_put_packet(USB_HID_ENDPOINT, out_data, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE);
                           
               }
               break;
               
               case WDT_FROM_SLEEP:
               {
                  out_data="WDT_SLE ";
                  usb_put_packet(USB_HID_ENDPOINT, out_data, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE);
                           
               }
               break;
               
               case INTERRUPT_FROM_SLEEP:
               {
                  out_data="INT_SLE ";
                  usb_put_packet(USB_HID_ENDPOINT, out_data, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE);
                           
               }       
               break;
               
               case MCLR_FROM_RUN:
               {
                  out_data="MCLR_RUN";
                  usb_put_packet(USB_HID_ENDPOINT, out_data, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE);
                           
               } 
               break;
               
               case RESET_INSTRUCTION:
               {
                  out_data="RST_CMD ";
                  usb_put_packet(USB_HID_ENDPOINT, out_data, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE);
                           
               }
               break;
               
               case STACK_OVERFLOW:
               {
                  out_data="STACK_OF";
                  usb_put_packet(USB_HID_ENDPOINT, out_data, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE);
                         
               }
               break; 
               
               case STACK_UNDERFLOW:
               {
                  out_data="STACK_UF";
                  usb_put_packet(USB_HID_ENDPOINT, out_data, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE);
                           
               }
               break;
               
               default:
               {
                  out_data="USBOK   ";
                  usb_put_packet(USB_HID_ENDPOINT, out_data, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE);         
               }
               break;
       
            }         
         }
         //-----------------------------------------------------------------------------------
         //-------------------------- SEND ACK TO PC  ----------------------------------------
         //-----------------------------------------------------------------------------------
         if(RF24_RX_getbuffer(&RXpipe, &RXdatasize, RX_RF_buffer)==true )
         {
            if((RXdatasize==6) && (RX_RF_buffer[0]=='A') )
            {
               retry=0;
               rf_finish=1;
               out_data[0] = '$';
               out_data[1] = RX_RF_buffer[0]; 
               out_data[2] = RX_RF_buffer[1];
               out_data[3] = RX_RF_buffer[2];
               out_data[4] = RX_RF_buffer[3];
               out_data[5] = RX_RF_buffer[4];
               out_data[6] = RX_RF_buffer[5];               
               out_data[7] = '#';
               
               usb_put_packet(USB_HID_ENDPOINT, out_data, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE);//!               output_low(led_dbg);
               delay_ms(10);
               
               timeout=0;
               count_t0=0;
               disable_interrupts(int_timer1);   
               clear_interrupt(int_timer1);
            }
            else if( (RXdatasize==8) && (RX_RF_buffer[0]=='C') && (RX_RF_buffer[7]=='R') )
            {
               retry=0;
               rf_finish=1;
               out_data[0] = '$';
               out_data[1] = RX_RF_buffer[1]; 
               out_data[2] = RX_RF_buffer[2];
               out_data[3] = RX_RF_buffer[3];
               out_data[4] = RX_RF_buffer[4];
               out_data[5] = RX_RF_buffer[5];
               out_data[6] = RX_RF_buffer[6];               
               out_data[7] = '#';
             
               usb_put_packet(USB_HID_ENDPOINT, out_data, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE); //!               output_high(led_dbg);
               delay_ms(10);
               
               timeout=0;
               count_t0=0;
               disable_interrupts(int_timer1);   
               clear_interrupt(int_timer1);               
            }
           
            else
            {
               out_data="Erros";
               usb_put_packet(USB_HID_ENDPOINT, out_data, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE);//!               output_low(led_dbg);
               delay_ms(10);  //!               output_high(led_dbg);
            }
           
            RF24_comm(FLUSH_RX);        //Delete RX buffer(already transfered to buffer)
            RF24_comm(FLUSH_TX);
            RF24_STATUS_clr_IRQs(IRQ_RX_dataready);      //clear RX IRQ     
            RF24_STATUS_clr_IRQs(IRQ_ALL);
           
         }
         //-----------------------------------------------------------------------------------
         //-------------------------- SEND RETRY            ----------------------------------
         //----------------------------------------------------------------------------------- 
         if(retry==1)
         {
               TX_RF_buffer[0]= in_data[1];
               TX_RF_buffer[1]= in_data[2];
               TX_RF_buffer[2]= in_data[3];
               TX_RF_buffer[3]= in_data[4];     
               TX_RF_buffer[4]= in_data[5];   
               TX_RF_buffer[5]= in_data[6];   
               
               RF24_comm(FLUSH_RX);        //Delete RX buffer(already transfered to buffer)
               RF24_comm(FLUSH_TX);
               
               RF24_TX_SET();
               delay_ms(10);
               RF24_TX_putbuffer(false,6,tx_rf_buffer);
               while(RF24_IRQ_state()==false);       
               RF24_STATUS_clr_IRQs(IRQ_ALL); 
               delay_ms(10);
               RF24_RX_SET();
               RF24_STATUS_clr_IRQs(IRQ_RX_dataready); 
               retry=0;
//!               output_toggle(led_dbg);
         }

         //-----------------------------------------------------------------------------------
         //-------------------------- SEND REQUEST TO NODE  ----------------------------------
         //-----------------------------------------------------------------------------------         
         if (usb_kbhit(USB_HID_ENDPOINT) && (rf_finish==1))
         {
            usb_get_packet(USB_HID_ENDPOINT, in_data, USB_CONFIG_HID_RX_SIZE);
            //--------------------------------------- CONTROL RELAY ------------------------------------------------
            if( (in_data[0]=='$') && (in_data[1]=='R') && (in_data[2]=='L') && ( in_data[7]=='#')  )
            {
               
               TX_RF_buffer[0]= in_data[1];
               TX_RF_buffer[1]= in_data[2];
               TX_RF_buffer[2]= in_data[3];
               TX_RF_buffer[3]= in_data[4];     
               TX_RF_buffer[4]= in_data[5];   
               TX_RF_buffer[5]= in_data[6];   
               
               RF24_comm(FLUSH_RX);        //Delete RX buffer(already transfered to buffer)
               RF24_comm(FLUSH_TX);
               
               RF24_TX_SET();
               delay_ms(50);
               RF24_TX_putbuffer(false,6,tx_rf_buffer);
               while(RF24_IRQ_state()==false);       
               RF24_STATUS_clr_IRQs(IRQ_ALL); 
               delay_ms(10);
               RF24_RX_SET();
               RF24_STATUS_clr_IRQs(IRQ_RX_dataready);   
               
               count_t0=0;
               timeout=0;
               
               enable_interrupts(int_timer1);
               clear_interrupt(int_timer1);
               rf_finish=0;
//!               output_toggle(led_dbg);
               
            }
           
            //--------------------------------------- RESET CRIMP   ------------------------------------------------
            if( (in_data[0]=='$') && (in_data[1]=='R') && (in_data[2]=='C') && ( in_data[7]=='#')  )
            {
               TX_RF_buffer[0]= in_data[1];
               TX_RF_buffer[1]= in_data[2];
               TX_RF_buffer[2]= in_data[3];
               TX_RF_buffer[3]= in_data[4];     
               TX_RF_buffer[4]= in_data[5];   
               TX_RF_buffer[5]= in_data[6];
               
               RF24_comm(FLUSH_RX);        //Delete RX buffer(already transfered to buffer)
               RF24_comm(FLUSH_TX);
               
               RF24_TX_SET();
               delay_ms(50);
               RF24_TX_putbuffer(false,6,tx_rf_buffer);
               while(RF24_IRQ_state()==false);       
               RF24_STATUS_clr_IRQs(IRQ_ALL); 
               delay_ms(10);
               RF24_RX_SET();
               RF24_STATUS_clr_IRQs(IRQ_RX_dataready);   
               
               count_t0=0;
               timeout=0;
               
               enable_interrupts(int_timer1);
               clear_interrupt(int_timer1);
               rf_finish=0;
//!               output_toggle(led_dbg);               
            }
            //--------------------------------------- REQUEST COUNT CRMIP   ------------------------------------------------
            if( (in_data[0]=='$') && (in_data[1]=='C') && (in_data[2]=='R') && ( in_data[7]=='#')  )
            {
               TX_RF_buffer[0]= in_data[1];
               TX_RF_buffer[1]= in_data[2];
               TX_RF_buffer[2]= in_data[3];
               TX_RF_buffer[3]= in_data[4];     
               TX_RF_buffer[4]= in_data[5];   
               TX_RF_buffer[5]= in_data[6];
               
               RF24_comm(FLUSH_RX);        //Delete RX buffer(already transfered to buffer)
               RF24_comm(FLUSH_TX);
               
               RF24_TX_SET();
               delay_ms(50);
               RF24_TX_putbuffer(false,6,tx_rf_buffer);
               while(RF24_IRQ_state()==false);       
               RF24_STATUS_clr_IRQs(IRQ_ALL); 
               delay_ms(10);
               RF24_RX_SET();
               RF24_STATUS_clr_IRQs(IRQ_RX_dataready);   
               
               count_t0=0;
               timeout=0;
               
               enable_interrupts(int_timer1);
               clear_interrupt(int_timer1);
               rf_finish=0;
//!               output_toggle(led_dbg);               
            }           
           
           
           
           
           
            if( in_data[0]=='$' && in_data[1]=='S' && in_data[2]=='F' && in_data[7]=='#')
            {
               RF_Freq[0] = in_data[3] - 0x30  ;
               RF_Freq[1] = in_data[4] - 0x30  ;
               RF_Freq[2] = in_data[5] - 0x30  ;
               RF_Freq[3] = in_data[6] - 0x30  ;
               
               rf_freq_demo = (RF_Freq[0]*1000) + (RF_Freq[1]*100) + (RF_Freq[2]*10) + RF_Freq[3];
               rf_freq_demo = rf_freq_demo- 2400;
               if( (rf_freq_demo>0))  rf_freq_set = rf_freq_demo;
//!               rf_freq_set = rf_freq_demo;
//!               else rf_freq_set =0;
               set_rf_cmd=1;
               
               RF24_initPorts();
               RF24_default_config();
               delay_ms(50);
               RF24_check_config();
               RF24_RX_SET();     
               delay_ms(50);
               
               
               out_data[0] = '$';
               out_data[1] = 'A'; 
               out_data[2] = 'C';
               out_data[3] = 'C';
               out_data[4] = 'E';
               out_data[5] = 'P';
               out_data[6] = 'T';               
               out_data[7] = '#';             
               usb_put_packet(USB_HID_ENDPOINT, out_data, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE);       //!               output_low(led_dbg);
               delay_ms(10);
//!               output_toggle(led_dbg);
            }
         
         }
         //-----------------------------------------------------------------------------------
         //-------------------------- SEND WARNING DISCONECT----------------------------------
         //-----------------------------------------------------------------------------------
         if(timeout==1)
         {
            out_data="DIS";
            usb_put_packet(USB_HID_ENDPOINT, out_data, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE);
           
            RF24_comm(FLUSH_RX);        //Delete RX buffer(already transfered to buffer)
            RF24_comm(FLUSH_TX);             
           
            timeout=0;
            count_t0=0;
            rf_finish=1;
            retry=0;
            disable_interrupts(int_timer1);
            clear_interrupt(int_timer1);
         }         
     
     
     
      }
   }
}

_________________
Begin Begin Begin !!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19551

View user's profile Send private message

PostPosted: Thu Aug 04, 2016 2:31 am     Reply with quote

Look at the power settings in your PC.

It is probably going to a low power state, and trying to put the USB to such a state. The standard code does not support this.

<http://helpdeskgeek.com/windows-xp-tips/prevent-windows-from-powering-off-usb-device/>

The same applies to W7.
tienchuan



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

PostPosted: Thu Aug 11, 2016 2:06 am     Reply with quote

Thanks for your helping Smile
I tried but it not change, the PC is turned off sleep mode or hirbernate. :((
Maybe problems with WDT or anythings else in my program?
_________________
Begin Begin Begin !!!
temtronic



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

View user's profile Send private message

PostPosted: Thu Aug 11, 2016 4:52 am     Reply with quote

I have to 2nd Mr' T's suggestion. Just because the PC is not in 'sleep' or 'hibernate' doesn't mean that the PC USB drivers are not automatically going to 'sleep'. You need to find and alter the PC USB 'options'. It can also be in the BIOS setup ! new PCs (after 1999) have a LOT of setup/config options in the BIOS and Windows has several 'layers' ANY of which WILL cause the USB ports to 'sleep'.
If possible try another PC to see what happens.

Jay
tienchuan



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

PostPosted: Wed Aug 17, 2016 2:28 am     Reply with quote

temtronic wrote:
I have to 2nd Mr' T's suggestion. Just because the PC is not in 'sleep' or 'hibernate' doesn't mean that the PC USB drivers are not automatically going to 'sleep'. You need to find and alter the PC USB 'options'. It can also be in the BIOS setup ! new PCs (after 1999) have a LOT of setup/config options in the BIOS and Windows has several 'layers' ANY of which WILL cause the USB ports to 'sleep'.
If possible try another PC to see what happens.

Jay

:(( i tried it and the result dont change.
Anybody have another idea to fix it?
Thanks you
_________________
Begin Begin Begin !!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19551

View user's profile Send private message

PostPosted: Wed Aug 17, 2016 4:04 am     Reply with quote

First, have you disabled the selective suspend feature. This really is the most likely cause of the problem:
<http://www.sevenforums.com/tutorials/147369-usb-selective-suspend-turn-off.html>
It causes grief for a huge number of devices. It really must be off.

It is on by default in Windows, which is why there are so many threads telling you how to turn it off....

Then, do you have the connection sense wire on the USB?.
(This is _required_ by the USB specs, unless your device is powered directly from USB...), then your USB initialisation code should change to:

Code:

   //at the top of main
   int1 usb_old_connected = FALSE;
   //your other initialisation

   usb_init_cs();

   //Then in your loop
   for(;;)
   {
      if (usb_attached())
      {
         if (usb_old_connected==FALSE)
         {
             usb_token_reset();
         }
         usb_task();
         if (usb_enumerated())
         {
             usb_old_connnected=TRUE;           
             //your usb and other code


        }
        else
            usb_old_connected=FALSE;
     }
   }

This forces the USB stack to re-initialise if the cable is disconnected/re-connected, or enumeration is lost.
'But', I hear you say - I'm not disconnecting the cable....

The Windows USB driver will electrically disconnect and re-connect in certain circumstances. These are caused by certain types of stack overflow at the Windows end, and also on multi-processor machines in particular. If your device can't handle it, it will become hung.....
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