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-HID resetting the pic18f4550

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



Joined: 26 Mar 2007
Posts: 11

View user's profile Send private message MSN Messenger

USB-HID resetting the pic18f4550
PostPosted: Fri Nov 26, 2010 1:13 pm     Reply with quote

Hi there!!!

I'm very confused and a bit bored, I'm trying to comunicate with my pic18f4550 in usb hid mode, but after each minute the micro reset and lost the connection, this takes place all time and I don't wanna give up of it, I need help with my source code. this is my code:

Code:


#include <18F4550.h>
//#DEVICE ADC=10

#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)

// Informa ao fmw do PIC para incluir codido de tratameto HID.
#DEFINE USB_HID_DEVICE  TRUE

// As seguintes definições são necessárias para o driver USB PIC abilitar o endpoint 1 para transmissão
// e separar um spaço para buffer no periférico.
#define USB_EP1_TX_ENABLE  USB_ENABLE_INTERRUPT   // EP1  para entrada de interrupt transfers
#define USB_EP1_TX_SIZE    8  //separa 8 bytes no hardware para transmissão.

// As seguintes definições são necessárias para o driver USB PIC abilitar o endpoint 1 para recepção
// e separar um spaço para buffer no periférico.
#define USB_EP1_RX_ENABLE  USB_ENABLE_INTERRUPT   //EP1 for Saída de interrupt transfers
#define USB_EP1_RX_SIZE    8  //Separa 8 bytes no hardware para receber

#include <pic18_usb.h>   //Microchip 18Fxx5x camada de hardware para usb.c
#include "mult_io_usb_desc_hid.h"  //Configurações USB e descritores do dispositivo
#include <usb.c>        //handles usb setup tokens and get descriptor reports

#include "globals.h"
#include "multi_lcd.h"
#include "texto.h"
#include "i2c/MCP23016Driver.h"
#include "multi_io_config.h"

void per_per(int slave);
void varre_gp(int offset, int gp);
void pin_out(int pin);

void usb_debug_task(void) {
   static int8 last_connected;
   static int8 last_enumerated;
   int8 new_connected;
   int8 new_enumerated;

   new_connected=usb_attached();
   new_enumerated=usb_enumerated();

   if (new_enumerated)
      LED_ON(LED_ENUM);
   else
      LED_OFF(LED_ENUM);

   if (new_connected && !last_connected)
      txt_conn_enum();//printf("\r\n\nUSB connected\nwait enumaration");
   if (!new_connected && last_connected)
      txt_disc_wait();//printf("\r\n\nUSB disconnected\nwait connection");
   if (new_enumerated && !last_enumerated)
      txt_enum_host();//printf("\r\n\nUSB enumerated\nby PC/HOST");
   if (!new_enumerated && last_enumerated)
      txt_unen_enum();//printf("\r\n\nUSB unen PC/HOST\nwait enumeration");

   last_connected=new_connected;
   last_enumerated=new_enumerated;
}
void main() {   
   int8 delay=0;
   int8 cont_timer = 0;
   setup();  //   configure port
   TotalPin.Gp = 1;
   conta_pin = 0;
   Pointer = 1;
   display=0;
   result = 1;
   usb_init();
   
   while (true) {
      //usb_task();
      usb_debug_task();
      if(usb_enumerated()){ 
         flag_enum = 1;
         delay_ms(1);
         delay++;
         if(delay >= 250){
          //  send_timer = 1;
          delay = 0;
            cont_timer++;
            if(cont_timer >= 0){
               conta_pin++;
               cont_timer = 0;
               out_data[0] = conta_pin;
               out_data[1] = 0;
              // num_xy(9,2,out_data[0]);
               while(!usb_put_packet(1,out_data,2,USB_DTS_TOGGLE));
               if(conta_pin >= 32)
                  conta_pin = 0;
            }
         }
      }
       if (usb_kbhit(1)) {
            usb_get_packet(1, in_data, 2);
            txt_dado_recebido(in_data[0], in_data[1]);
            pin_out(in_data[0]);
         }
   }
}
void per_per(int slave)
{
   int A, B;// Valores + a contagem dos pinos para mostrar a correta posicao
   if(slave == 70){
      A = 1;
      B = 9;
   }
   if(slave == 68){
      A = 17;
      B = 25;
   }
   if(slave == 66){
      A = 33;
      B = 41;
   }
   if(slave == 64){
      A = 49;
      B = 57;
   }
   LeMcp23016(slave, gp0);
   if(iVarGp0 || iVarGp1){
       result = 1;
      if(iVarGp0){
         varre_gp(A,iVarGp0);
         return;
      }
      if(iVarGp1){
         varre_gp(B,iVarGp1);
         return;
      }
   }else
       result = 0;
}

void varre_gp(int offset, int gp)
{
   while(!resultado){
      resultado = Pointer & gp;
      if(resultado) {
         resultado=0;
         out_data[0] = (display+offset);
         out_data[1] = 0;
         num_xy(9,2,display+offset);
         if(flag_enum)
            usb_put_packet(1, out_data, 2, USB_DTS_TOGGLE);
         break;
      }
      Pointer <<= 1;
      display+=1;
   }
   Pointer = 1;
   display = 0;
   out_data[0] = 0;
   delay_ms(25);
}
void pin_out(int pin)
{
   int slave = slave_2;
   int mask_pin = 1;
  // Write_mcp_pin(slave_1,0xff, 0xff);
  // Write_mcp_pin(slave_2,0xff, 0xff);
   if(pin > 16){
      pin = (pin -16);
      slave = slave_1;
   }
   
   if(pin >0 && pin<=8){
      mask_pin <<= pin;
      Write_mcp_pin(slave,~mask_pin, 0xff);
   }
      Write_mcp_pin(slave,0xff, 0xff);

   if(pin >8 && pin<=16)
   {
      mask_pin <<= (pin-8);
      Write_mcp_pin(slave,0xff, ~mask_pin);
   }
}




What's wrong with my code, I'm reading the incoming data from usb, and sends data to outgoing data usb each 250ms, then after few minutes the picmicro freezes or reboot itself.
I did make a minimal source with sample ccs USBHID and nothing wrong occurs , then when I start to populating with more codes, like a lcd, i2c, and so on, the problems appears.
anybody could help me?.

thanks

Douglas Ricardo dos santos Pereira Confused
_________________
Breaking rocks in the hot sun!
Doglao
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Fri Nov 26, 2010 3:44 pm     Reply with quote

Given the demo works, rules out the likely hardware problems (inadequate smoothing on the Vusb line is a common problem).

I'd guess you have code using interrupts?.

The most likely thing is that you have some interrupt routine, that takes a long time, or uses another function that is also in the 'main' code, resulting in interrupts being disabled for a significant time somewhere. This _will_ kill USB.

Or you are overflowing the stack. Look at the .sym file, what is the 'worst case' stack useage?.

Best Wishes
Doglao



Joined: 26 Mar 2007
Posts: 11

View user's profile Send private message MSN Messenger

PostPosted: Fri Nov 26, 2010 8:56 pm     Reply with quote

Yes Laughing this is the problem, I disabled the interrupts in
both lcd and i2c source code with long times, thanks a
lot, now this is running at this moment and no reset, but
the lcd is corrupted, appearing wrong characters.
I think to use RTOS to separate in tasks the lcd, i2c and usb, I don't have experience with RTOS, is good time to start learn more about this.

thanks Ttelmah

Douglas Ricardo dos Santos Pereira
_________________
Breaking rocks in the hot sun!
Doglao
Doglao



Joined: 26 Mar 2007
Posts: 11

View user's profile Send private message MSN Messenger

PostPosted: Fri Nov 26, 2010 9:17 pm     Reply with quote

It's too early to say that this running now, return to resetting again.
_________________
Breaking rocks in the hot sun!
Doglao
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