Page 1 of 1

usbFunctionWrite doesn't get called

Posted: Wed Dec 19, 2012 6:01 pm
by cocojack
Good evening,
at first i want to appologize, if my english is not the very best, its not my navie language.

I already posted my Problem on a german - speaking board, but it seems noone has a clue about the misake i made.Mikrocontroller.net

I followed the V-USB Tutorial on http://codeandlife.com and accomplished part 1-3 successfull. Now I wanted to try to send random char[16] to my attiny2313 and recive them back.

This is the code I've used:

Code: Select all

static uchar replyBuf[16]   = "Hello, USB!";
static uchar reciveBuf[16]  = {"TEST"};
static uchar dataReceived = 0, dataLength = 0; // for USB_DATA_IN


USB_PUBLIC uchar usbFunctionSetup(uchar data[8])
{
   
   
   usbRequest_t *rq = (void *)data; // cast data to correct type
       
    switch(rq->bRequest)
   {   
      // custom command is in the bRequest field
      case USB_LED_ON:
            PORTB |= (1<<PB0); // turn LED on
            return 0;
      case USB_LED_OFF:
            PORTB &= ~(1<<PB0); // turn LED off
            return 0;   
            
      case USB_DATA_OUT:   
            usbMsgPtr = replyBuf;
            return sizeof(replyBuf);
            
      case USB_DATA_WRITE:
                  
            dataLength = (uchar)rq->wLength.word;
            dataReceived = 0;
               
            if(dataLength > sizeof(reciveBuf))
            {dataLength = sizeof(reciveBuf);}   //setzte wieviele Bytes empfangen werden können
               
            return USB_NO_MSG;   // ruft usbFunctionWrite auf
            //return 0xFF;
            
      case    USB_DATA_ACT:
            
            for(uint8_t i = 0; i<16; i++)
            {
               replyBuf[i]=reciveBuf[i];
            }
            return 0;
}   

    return 0; // should not get here
}


///

USB_PUBLIC uchar usbFunctionWrite(uchar *data, uchar len) {
   uchar i;
   PORTB ^= (1<<PB1);  // to indicate the function got called
   for(i = 0; dataReceived < dataLength && i < len; i++, dataReceived++)
      reciveBuf[dataReceived] = data[i];
      
    return (dataReceived == dataLength); // 1 if we received it all, 0 if not
}




On the pc-side I've used following code

Code: Select all

// int main...
   using namespace std;
   int wtd;
   bool loop = true;


    usb_dev_handle *handle = NULL;
    int nBytes = 0;
    char buffer[256];
   char inputbuffer[16] = {"hl"}


///...
   while(loop)
   {
      printf("\n\n\n\n\n0 : LED an\n1 : LED aus\n2 : Daten Ausgeben\n3 : Daten Schreiben\n4 : AusgabePuffer aktualisieren\n5 : Programm Beenden\n\n");
      printf("Ihre Wahl?:   ");
      cin >> wtd;
      nBytes = -1;
      switch(wtd){
      case USB_LED_OFF   :   nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_LED_ON    , 0, 0, (char *)buffer, sizeof(buffer), 5000);         continue;
      case USB_LED_ON      :   nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_LED_OFF   , 0, 0, (char *)buffer, sizeof(buffer), 5000);         continue;
      case USB_DATA_OUT   :   nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_DATA_OUT  , 0, 0, (char *)buffer, sizeof(buffer), 5000);
                        printf("\nEmpfangene Bytes: %d \nInhalt: %s\n", nBytes, buffer);                                                                     continue;
      case USB_DATA_WRITE   :   cin >> inputbuffer;   
                        cout << endl << "Ihre Eingabe: " << inputbuffer << endl;
                        nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_DATA_WRITE, 0, 0, inputbuffer, strlen(inputbuffer)+1, 5000);      continue;
      case USB_DATA_ACT   :   nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_DATA_ACT  , 0, 0, (char *)buffer, sizeof(buffer), 5000);         continue;
      case 5            :   loop = false; nBytes = 0; break;
   
      }

      
      if(nBytes < 0)      // fehlerbehandlung
      {
         fprintf(stderr, "USB error: %sn", usb_strerror());
      }
   
      
   }



The problem is, the usbFunctionWrite doesnt get called...

Re: usbFunctionWrite doesn't get called

Posted: Wed Dec 19, 2012 7:05 pm
by cocojack
I think i found the mistake,
the tutorial said " argv[2], strlen(argv[2])+1,"
i wanted to reply a char[] and argv is a **char ..

so I changed strlen(argv[2])+1, to strlen(inputbuffer)+1, and it seemed to be wrong.
Now I changed this to sizeof(inputbuffer)+1 and for some reason this works now..

anyway thanks for readiny my post :)