Borrar filtros
Borrar filtros

USB CDC between Matlab and EMbedded C (microcontroller)

7 visualizaciones (últimos 30 días)
Andrea Bettati
Andrea Bettati el 2 de Ag. de 2019
Comentada: Urmila Rajpurohith el 6 de Dic. de 2020
Hi to you all,
I want to builld a reliable link over USB between my laptopt (ubuntu 18.04) running Matlab R2019a and a Cortex-M4 microcontroller (LPC4370 on LPC-Link2 evaluation board, I'll call it simply mcu).
It's been a while now I don't seem to be able to get to the point where the comunication is stable and realiable (other post I made on the topic here).
My requirements:
  • FSM like behaviour: I write a char (command) to the mcu and it responds in switch-case fashion. Then it goes back to idle, ready for next command.
  • Data transfer: I need to test HW performance on A LOT of data I generate in matlab, so I need to bulk-transfer vectors of integers, both to and from the mcu.
On the mcu side I'm using a USB API developed by NXP, called LPOCopen: note that this comes with no documentation other than some examples and the support on NXP forums is 0. (I will attach it to this post, since you need to login to download it). On matlab I use fwrite/fread as described here.
My problem is that I loose synchronization between matlab and mcu: most of the time either fread skips bytes either it hangs on until it reaches TimeOut and gives up.
I'll try to explain my software now.
MCU side:
I started off using an example called usbd_rom_cdc_vcom (The example shows how to us USBD ROM stack to creates a virtual comm port, and I will attach it as well). It initializes the USB in interrupt mode and uses 2 functions:
vcom_bread()
/**
* @brief Virtual com port buffered read routine
* @param pBuf : Pointer to buffer where read data should be copied
* @param buf_len : Length of the buffer passed
* @return Return number of bytes read.
*/
uint32_t vcom_bread (uint8_t *pBuf, uint32_t buf_len);
and
vcom_write()
/**
* @brief Virtual com port write routine
* @param pBuf : Pointer to buffer to be written
* @param buf_len : Length of the buffer passed
* @return Number of bytes written
*/
uint32_t vcom_write (uint8_t *pBuf, uint32_t buf_len);
So no need to specify:
  • Line temrinators
  • Baudrate
I just pass the array I want to send and the size of it (in bytes).
I'll attatch the matlab code I use to initialize the serial_object. Here's the status of the serial object right after initialization:
Serial Port Object : Serial-/dev/ttyACM2
Communication Settings
Port: /dev/ttyACM2
BaudRate: 115200
Terminator: 'LF'
Communication State
Status: open
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent: 0
As I mention in my post on stackexchange, my basic signaling is the following:
Matlab:
fwrite(serial_object, K_COEFF, 'int32');
if K_COEFF ~= fread(serial_object, 1, 'int32')
error('out of sync');
end
pause(0.0005)
fwrite(serial_object, L_COEFF, 'int32');
if L_COEFF ~= fread(serial_object, 1, 'int32')
error('out of sync');
end
pause(0.0005)
MCU:
/*SET K*/
while(!rdCnt)
{rdCnt = vcom_bread(&param_rxBuff, sizeof(int32_t));}
K_COEFF = param_rxBuff;
vcom_write((uint8_t*)&param_rxBuff, sizeof(int32_t));
param_rxBuff = 0;
rdCnt = 0;
/*SET L*/
while(!rdCnt)
{rdCnt = vcom_bread(&param_rxBuff, sizeof(int32_t));}
L_COEFF = param_rxBuff;
vcom_write((uint8_t*)&param_rxBuff, sizeof(int32_t));
param_rxBuff = 0;
rdCnt = 0;
But this is not reliable: I'm not sayng it's completely broken, but this thing is not acting in a repetable way.
Since I have 0 experience on USB and low-level matlab as well I wonder if there's something wrong in my whole approach. Especcially on matlab side.
I didn't even find a good document describing how to interface Matlab and an mcu this way.

Respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by