STM32F1 custom USB

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

User avatar
ADI-mistrzu
Posts: 12
Joined: Mon Jun 29, 2015 8:37 am

Re: STM32F1 custom USB

Post by ADI-mistrzu »

Oh, I wrong copy my code, it's look exacly that:

Code: Select all

static WORKING_AREA(waUSB, 128);
static msg_t USB_thread(void *arg)
{
   (void)arg;
   chRegSetThreadName("USB");

   EventListener el1;
   flagsmask_t flags=0;
   uint8_t cmd_res[2];
   uint8_t clear_buff[64], buff[64];

   sduObjectInit(&SDU1);
   sduStart(&SDU1, &serusbcfg);

   usbDisconnectBus(serusbcfg.usbp);
   chThdSleepMilliseconds(1500);
   usbStart(serusbcfg.usbp, &usbcfg);
   usbConnectBus(serusbcfg.usbp);

   chEvtRegisterMask(chnGetEventSource(&SDU1), &el1, ALL_EVENTS);
   while(SDU1.state != USB_READY)
      chThdSleepMilliseconds(10);

   while(GPIOA->IDR&GPIO_IDR_IDR9)
   {
      chEvtWaitAny(ALL_EVENTS);
       chSysLock();
       flags = chEvtGetAndClearFlagsI(&el1);
       chSysUnlock();

       if(flags & CHN_INPUT_AVAILABLE)
       {
          chnReadTimeout( &SDU1, cmd_res, 1, MS2ST(1500) );
          switch(cmd_res[0])
          {
          case cmd_GSM_nr:
             chnWrite(&SDU1, "000000000", 9);
          break;
          case cmd_IMEI:
             chnWrite(&SDU1, "0123456789ABCDE", 15);
          break;
          case cmd_Signal:
             chmWrite(&SDU1, "00", 2 );
          break;
          case cmd_FV_state:
             chnWrite(&SDU1, __DATE__, 12);
          break;
          case cmd_RLY_read:

          break;
          case Phone_list_long_read:

          break;
          case cmd_USART_cnt:
            LED_green_ON;
             cmd_res[0] = 2;
             cmd_res[1] = 3;
             chnWriteTimeout((BaseChannel *)&SDU1, cmd_res, 2, MS2ST(10) );
          break;
          case cmd_USART_buf:
            LED_green_ON;
             cmd_res[0] = 10;
             cmd_res[1] = 11;
             chnWriteTimeout((BaseChannel *)&SDU1, cmd_res, 2, MS2ST(10) );
          break;
          }
          if(SDU1.iqueue.q_counter)
             LED_green_tog;

       }
   }

   usbStop(serusbcfg.usbp);
   usbDisconnectBus(serusbcfg.usbp);
   chThdSleepMilliseconds(100);

   extChannelEnable(&EXTD1, 9);
}

At PIN9 I have USB supply connection detect. When I plugin USB, then EXI create USB thread, when plugout, then while loot is ending and thread is going down.

I modified code, but still it's the same problem.
PC program show me how much elements incomming.
Should be:
GSM number: 9
IMEI: 15
Signal: 2
Firmware: 12

When I first ask, then:
Image
All next:
Image
All value are shifted, I think it's something wrong with queue.

I trying found problem with debugind, but I nothing see.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32F1 custom USB

Post by Giovanni »

Try installing a SW USB analyzer and monitor the USB traffic. You will see what the board really receives and sends.

Google: usb analyzer for windows

There are a lot of them.

Giovanni
User avatar
ADI-mistrzu
Posts: 12
Joined: Mon Jun 29, 2015 8:37 am

Re: STM32F1 custom USB

Post by ADI-mistrzu »

Comunnication look that:
Image
At blue is first cmd do device.
As you can see, it's somethink wrong with device.

For test I do something that:

Code: Select all

uint8_t buff[64] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
if(flags & CHN_INPUT_AVAILABLE)
{
   chnReadTimeout( &SDU1, cmd_res, 1, MS2ST(1500) );
   chnWrite(&SDU1, buff, 9);
}

I read cmd from host and send buff[].

Image
Why system divide data?
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32F1 custom USB

Post by Giovanni »

The driver sends what it finds in the circular buffer, writing the buffer is not an atomic operation. The send can start while the write is only partially performed.

If you need to guarantee atomic transfers then you should not use the Serial-USB driver but use the USB API directly.

Alternatively you may design your protocol to use delimiters, for example lines terminated by a CRLF, the serial driver is character-oriented.

Giovanni
User avatar
ADI-mistrzu
Posts: 12
Joined: Mon Jun 29, 2015 8:37 am

Re: STM32F1 custom USB

Post by ADI-mistrzu »

I tried to modify system usb-serial, but it's realy dificult.

So I solved the problem by modyfication application on PC, I check how many elements was reads, and if it's not enough, then I again read, some like that:

Code: Select all

            send_buffor[0] = cmd_GSM_nr;
            usb_bulk_write( dev, EP_OUT, send_buffor, 1, 2000);
            i = usb_bulk_read( dev, EP_IN, buffor, 9, 2000);
            while(i<9)
                i += usb_bulk_read( dev, EP_IN, &buffor[i], 9-i, 2000);
Post Reply