using CDC with usb stack v4.0.2 on FRDM board

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

using CDC with usb stack v4.0.2 on FRDM board

ソリューションへジャンプ
14,010件の閲覧回数
steve_fae
Senior Contributor I

I imported  USB_CDC_DEVICE_MKL25Z128_PEx into CW 10.3 and when I try to build the project I get a lot of path error issues.  What is the best way to fix this?

3282_3282.png

1 解決策
12,131件の閲覧回数
steve_fae
Senior Contributor I

Finally got the application to work with some additional help from Freescale TIC.  Last question is where do I hack so that its just not a loop back example, basically PC sends a simple string and Kinetis L responds with a different string [basically some data it gathered]. 

元の投稿で解決策を見る

0 件の賞賛
返信
65 返答(返信)
6,186件の閲覧回数
steve_fae
Senior Contributor I

Ok, working with CDC example that you posted Erich.  In trying to understand the code you provided for responding to a string sent from a Terminal program with a string and data:

I am not sure how not to let the input buffer overflow, but all I really want to do is in HyperTerminal or equiv. hit return and retrieve some logged data.  With my hack below I am basically returning the same string the entire length of input buffer to Hyperterminal.

Question:  How can I empty what Hyperterminal sends [clear the input buffer] and just return a string and the data such as Time_On_Hour?

Basically my program calls your routine periodically to see if data is requested from Hyperterminal then gets back to work. 

static void CDC_Run(void) {

  int i;

  extern uint32_t Time_On_Hours;

  unsigned char buf[16];

    while(CDC1_App_Task(cdc_buffer, sizeof(cdc_buffer))==ERR_BUSOFF) {

      /* device not enumerated */

      LEDR_Neg(); LEDG_Off();

      WAIT1_Waitms(10);

    }

    LEDR_Off(); LEDG_Neg();

    if (CDC1_GetCharsInRxBuf()!=0) {

    while(CDC1_GetChar(&in_buffer[i])==ERR_OK){i++;}

      }

      in_buffer[i] = '\0';

      (void)CDC1_SendString((unsigned char*)"Time on Hours: ");  //do I need this?

      (void)CDC1_SendString(in_buffer);

      UTIL1_strcpy(buf, sizeof(buf), (unsigned char*)"Time_On_Hours ");

      UTIL1_strcatNum32u(buf, sizeof(buf), Time_On_Hours);

      UTIL1_strcat(buf, sizeof(buf), (unsigned char*)"\r");

      (void)CDC1_SendString(buf);

    }

0 件の賞賛
返信
6,181件の閲覧回数
BlackNight
NXP Employee
NXP Employee

Hi Steven,

With CDCF1_GetChar() you are receiving the incoming characters from the RX ring buffer. In your case, you can simply read them and not using it.

So you do not need to do this:

>>(void)CDC1_SendString((unsigned char*)"Time on Hours: ");  //do I need this?

>>      (void)CDC1_SendString(in_buffer);

and instead of

>>while(CDC1_GetChar(&in_buffer[i])==ERR_OK){i++;}

>>       }

you could use a temp variable and do

while(CDC1_GetChar(&temp)==ERR_OK){}


But as I see now that someone might simply clear the input (or output) buffer, I have extended the implementation. For this I have added a Clear() method to the Ringbuffer component, plus ClearRxBuffer() and ClearTxBuffer() to the CDC component. See screenshot. That way it is much more convenient to clear the buffer, and you do not need to iterate through a loop to empty the buffer.


I hope this is what you wanted. I have not done extensive testing as the implementation was straight forward.

The updated components are available on GitHub (ErichStyger/mcuoneclipse · GitHub).

Let me know if this works for you.


0 件の賞賛
返信
6,181件の閲覧回数
steve_fae
Senior Contributor I

Erich, played around with this a bit.

I implemented the code like this below.  Problem is at best I get Time on Hours: show up in Hyperterminal

but then it hangs.  I honestly don't understand the USB stack very well, so what am I missing?


I also get a warning which doesn't make sense:

'CDC_Run' defined but not used [-Wunused-function]

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

static uint8_t cdc_buffer[USB1_DATA_BUFF_SIZE];

Main()

*

*

             //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
          if (CDC1_App_Task(cdc_buffer, sizeof(cdc_buffer))!=ERR_BUSOFF)  /* is device enumerated */
        

CDC_Run();  

else......................

/* Check for USB request for data */
        

   //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Then in CDC_Run......

static uint8_t in_buffer[USB1_DATA_BUFF_SIZE];

static void CDC_Run(void) {

  int i;

  extern uint32_t Time_On_Hours;

  unsigned char buf[16];

//    while(CDC1_App_Task(cdc_buffer, sizeof(cdc_buffer))==ERR_BUSOFF) {

//      /* device not enumerated */

//      LEDR_Neg(); LEDG_Off();

    LEDR_Off(); LEDG_Neg();

    if (CDC1_GetCharsInRxBuf()!=0)

        {

      Rx1_Clear();                    /* clear receive buffer */

      in_buffer[i] = '\0';

      (void)CDC1_SendString((unsigned char*)"Time on Hours: ");

      (void)CDC1_SendString(in_buffer);

      UTIL1_strcatNum32u(buf, sizeof(buf), Time_On_Hours);

      UTIL1_strcat(buf, sizeof(buf), (unsigned char*)"\r");

      (void)CDC1_SendString(buf);

        } // if the return character wasn't hit, don't send data

    }


0 件の賞賛
返信
6,180件の閲覧回数
BlackNight
NXP Employee
NXP Employee

Steve,

As Jim said: remove that write into the buffer which likely can corrupt your RAM. Additionally you can use CDC1_ClearRxBuffer() instead of RX1_Clear().

I have changed the demo to what I think you want to accomplish: if you enter a string (or just CR/LF), then the input buffer is discarded, and it writes the number of hours. See attached file. It still has the original code in it.

0 件の賞賛
返信
6,180件の閲覧回数
steve_fae
Senior Contributor I

Thank you.  I was staring at that but didn't lengthen it.  I will give it a go.

Thanks all, this works as needed and its rock solid.  Many thanks to Erich and Jim Donelson.

0 件の賞賛
返信
6,180件の閲覧回数
JimDon
Senior Contributor III

Make buf bigger. "time on hours: " is 15 plus the null is 16.

6,180件の閲覧回数
steve_fae
Senior Contributor I

Gentlemen, this is much more stable then what I had working, but there still is a glitch. See attached subroutine It uses just about all that you provided.  I didn't add the delays since there is other microcontroller code running in Main() that will add that time with other code execution. 

For test purposes I defined Time_On_Hours = 0xffff;

Problem:  each time I hit <return> in Hyperterminal, I see "time on hours:" but no value is printed for it AND

UTIL1_strcat(buf, sizeof(buf), (unsigned char*)"\r\n"); doesn't seem to be bringing the cursor back to the beginning of the line.

3415_3415.png

//=============================================================

/* User includes (#include below this line is not maintained by Processor Expert) */

static uint8_t cdc_buffer[USB1_DATA_BUFF_SIZE];

extern uint32_t Time_On_Hours;

static void CDC_Run(void) {

unsigned char buf[16];

//    while(CDC1_App_Task(cdc_buffer, sizeof(cdc_buffer))==ERR_BUSOFF) {

//      /* device not enumerated */

//      LEDR_Neg(); LEDG_Off();

    LEDR_Off(); LEDG_Neg();

    if (CDC1_GetCharsInRxBuf()!=0)

        {

       

        Time_On_Hours = 0xffff;

       

       

        CDC1_ClearRxBuffer(); /* discard incoming buffer */

        UTIL1_strcpy(buf, sizeof(buf), (unsigned char*)"time on hours: ");

        UTIL1_strcatNum32u(buf, sizeof(buf), Time_On_Hours);

        UTIL1_strcat(buf, sizeof(buf), (unsigned char*)"\r\n");

        (void)CDC1_SendString(buf);

        }

    }



0 件の賞賛
返信
6,180件の閲覧回数
JimDon
Senior Contributor III

Steve,

Well, I can give you some insight here, but cloud you attach the entire file?

It seems you are missing a closing brace, so that what was once a loop on the device not being ready is changed.

You discard the input data by clearing the buffer, then seem to want a return.

Remove this: in_buffer[i] = '\0'; since I dont see i getting set, this could write to unexpected places.

As I said, it would be easier to help you if you posted the file, and stated more clearly how it should work.


0 件の賞賛
返信
6,180件の閲覧回数
steve_fae
Senior Contributor I

Thanks again Erich.  I can't say where, but your help has really helped me with my customer.

Now I need to experiment

0 件の賞賛
返信
6,180件の閲覧回数
JimDon
Senior Contributor III

I tired this.

It could be made easier (i.e. I feel your pain on doing this for each rev).If only you could do this from the command line.

It should be made clearer on how to copy these into the PE directory. After a nights sleep and a better understanding, it was pretty easy.

I work on it and get back to you...

0 件の賞賛
返信
6,180件の閲覧回数
steve_fae
Senior Contributor I

Erich, thank you so much. This is exactly what I need to move forward.  I have hacked Freescale projects for years using the basic peripherals like uart, spi, adc, dac, etc.  This was my first try at USB.  I never would have gotten it to work without your help.  Now I need to roll in the other PE components and hack your ProcessorExpert.C to make it all work together.

Is there an easy way to have the micro run at a much lower speed and when someone attaches the USB cable [to basically gather the data you helped me send] then bump the micro up to USB speed?

0 件の賞賛
返信
6,180件の閲覧回数
steve_fae
Senior Contributor I

Wow, works nicely right out of the box.  Thanks Erich. 

0 件の賞賛
返信
6,180件の閲覧回数
steve_fae
Senior Contributor I

Thanks Erich.  I will look your project.  I thought about what my goal is [basically attach usb to gather some data that was stored in the micro such as time at temperature] it seems that maybe I should instead use MSD.  Sadly, its like starting over with new problems.  I finally got the project to build but now I am landing up at PE_DEBUGHALT(); which means the clock tree isn't setup right or one of the components wasn't configured correctly.  Did anyone try the projects before they posted them in the USB stack?

0 件の賞賛
返信
6,180件の閲覧回数
BlackNight
NXP Employee
NXP Employee

I'm working (slowly) as well on the USB Host MSD port. I have seen as well HardFaults, and that's probably what you you see as well. On my end the problem was that for gcc the #pragma pack() was not properly implemented in the USB 4.0.2 stack. I have fixed that on my side, and the sources are on GitHub (mcuoneclipse/Drivers/FSL_USB_Stack at master · ErichStyger/mcuoneclipse · GitHub). Still working on an example project.

0 件の賞賛
返信
6,180件の閲覧回数
steve_fae
Senior Contributor I

I'm in California he is in Canada.

Sent from Steve Cohen's blackberry

(818)261-1033

0 件の賞賛
返信
12,132件の閲覧回数
steve_fae
Senior Contributor I

Finally got the application to work with some additional help from Freescale TIC.  Last question is where do I hack so that its just not a loop back example, basically PC sends a simple string and Kinetis L responds with a different string [basically some data it gathered]. 

0 件の賞賛
返信
6,180件の閲覧回数
steve_fae
Senior Contributor I

Getting closer, I imported usb device stack but now missing some include files such as usb_cdc.h and usb_framework.h

I added usb_cdc.h and now it can't find descriptor.h

pastedImage_0.png

0 件の賞賛
返信
6,180件の閲覧回数
steve_fae
Senior Contributor I

did you mean to say import usb_cdc_class instead of usb_device_stack?  thanks for your help btw

0 件の賞賛
返信
6,180件の閲覧回数
LuisCasado
NXP Employee
NXP Employee

HI

No, I mean the CW project. You only need the device stack PE component and you already have imported it. Try to remove the project from workspace or just switch to different workspace and import again the application project

Luis

6,180件の閲覧回数
steve_fae
Senior Contributor I

Luis, have a nice cold one on me.  I created a new workspace, re-imported the project and it compiles with only 27 warnings!  hehe

Goal is to help customer so that when they attach a PC to their product through USB it tells them a value over CDC.  Now I need to figure out how to implement this project in their working KL25 project.  Thanks again. 

0 件の賞賛
返信
6,180件の閲覧回数
steve_fae
Senior Contributor I

ok, now running project on FRDM board using Open SDA interface.  As you can see from my Device Manager

Open SDA enumerates to CDC on com2 but the other usb connection that connects directly to the KL25 doesn't enumerate properly.  Any ideas how to get it to enumerate to CDC?  Is the OpenSDA connection going to be a problem?

3290_3290.png

0 件の賞賛
返信