AnsweredAssumed Answered

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

Question asked by Andrea Bettati on May 5, 2019
Latest reply on May 16, 2019 by jeremyzhou

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.

Outcomes