CORTEX-M LPC43XX USB-VCOM basics - proper way to build a simple FSM

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

CORTEX-M LPC43XX USB-VCOM basics - proper way to build a simple FSM

1,341 Views
a_bet
Contributor IV

I'm working on a project involving the LPC4370 mcu (LPC link 2 eval board) and Matlab. I need my embedded code to be user-reconfigurable from matlab: the user sends a command, the mcu replies.

I'm using LPCOpen VCOM example as base: it uses USB w/ interrupt.

My C code:

static uint8_t g_rxBuff[256]; [...] while (1) { /* Sleep until next IRQ happens */ __WFI(); /* Read cmd */ rdCnt = vcom_bread(&g_rxBuff, 1); state = (char)g_rxBuff;         if(rdCnt == 1) {             switch (state)             {                case 'a':                  /* cmd ackn */                   vcom_write((uint8_t*)&g_rxBuff, 1);               case 'b':                   /* cmd ackn */                   vcom_write((uint8_t*)&g_rxBuff, 1);                case 'c':                   /* cmd ackn */                   vcom_write((uint8_t*)&g_rxBuff, 1);             }         } }

Here I wait for a command and then I try to echo it back to host (i.e. ackn).

In Matlab

BaudRate=115200; serial_object=0; delete(instrfind('Type','serial')); serialInfo = instrhwinfo('serial'); serial_object=serial('/dev /ttyACM1','BaudRate',BaudRate,'InputBufferSize',4*4096,'OutputBufferSize',4*4096); fopen(serial_object);
sendCmd(serial_object,'a'); sendCmd(serial_object,'b'); sendCmd(serial_object,'c');

Where

function ret = sendCmd(serial_object, cmd_sent)    fwrite(serial_object, cmd_sent, 'char');    cmd_read = fread(serial_object, 1, 'char');    if (~isempty(cmd_read) && (cmd_read == cmd_sent))       disp(strcat('SENT: ', cmd_sent));    else       disp(strcat('ERROR: ', cmd_sent));    end end

So this basically never works. I always get errors like:

Warning: The specified amount of data was not returned within the Timeout period.'serial' unable to read any data. For more information on possible reasons, see Serial Read Warnings.

Since I've been working on this for a while now I can tell that port set-up is ok, I managed to get data out of the mcu with the same configuration.

So I think I'm missing some how-to-basics here. The only assumption I made is that the USB IRQ is triggered everytime Matlab writes to the mcu.

Labels (5)
0 Kudos
7 Replies

1,126 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Andrea Bettati,

Thank you for your interest in NXP Semiconductor products and
for the opportunity to serve you.
According to your statement, it seems that the host hasn't received any feedback from the LPC4370 even timeout period expires.
I'd like to know whether you try to check the LPC4370 receive the command when debugging the demo code, meanwhile, you also can capture the USB transmission by using the USB analyzer to locate the issue.
So please give a try.

Have a great day,
TIC

 

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos

1,126 Views
a_bet
Contributor IV

Hi jeremyzhou‌, thanks for the reply.
I checked what happens in the MCU using SWD.
Here's the IRQ calls timeline, after I issue this command from Matlab:

 fwrite(serial_object, 'a, 'char');

Selection_048.png

So a lot of calls are occurring, just for one byte. And you are right, my C code does not enter the swith case.
I.e. these break points are never hit:

Selection_049.png

Do you have any suggestions given this infos?

0 Kudos

1,126 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Andrea Bettati,

Thank you for your reply.
Now we can assure that the demo code doesn't succeed in receiving the data from the host, so you can capture the USB transmission by using the USB analyzer to confirm the cause: host hasn't transmitted the data actually or the code fails to handle the data from the host.
Please give a try.
Have a great day,
TIC

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos

1,126 Views
a_bet
Contributor IV

Hi jeremyzhou‌ sorry for the late reply. I found a bug in the previous code:

state = (char)g_rxBuff;

Was wrong since I was casting the start address instead of the first value.
Replacing with

state = g_rxBuff[0];

I see some activity, but the system is not behaving in a predictable way.
In Matlab I do this:

fwrite(serial_object, cmd_sent, 'char');
    pause(0.5)
cmd_read = fread(serial_object, 1, 'char');
    pause(0.5)

But the micro does not reply, or it doubles the otuput. For example if I send 'b' I got 'bb' (or even 'bbb') instead of 'b'.

0 Kudos

1,126 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Andrea Bettati ,

Thanks for your reply.
I was wondering if you can share the complete demo code for the LPC4370, meanwhile, whether you try to capture the USB transmission by using the USB analyzer, if yes, please upload the captured data too.

Please give a try.
Have a great day,
TIC

 

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos

1,126 Views
a_bet
Contributor IV

Hi jeremyzhou‌, I don't have a USB analyzer unfortunately.

0 Kudos

1,126 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Andrea Bettati ,

Thanks for your reply.

Maybe you can consider the tool:USBPcap to capture raw USB traffic

Have a great day,
TIC

 

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos