10 years, 1 month ago.

MODSERIAL with support for KL25Z

Hi,

I am working on the KL25Z board. We are using mbed libraries but not the mbed compiler. We are using the gnu gcc compiler for our program.

The mbed library is providing functions for sending and receiving data byte by byte (putc and getc) using Serial library for KL25z board. the library does not have functions to send and receive buffer data.

I am trying to send 100 bytes of data and expecting to receive more bytes from host. If i use the putc and getc functions, i see the loss of data. I do not have mbed program to reproduce the issue.

I found MODSERIAL library (Created by Erik Olieman) works for the buffered data. But i do not find any good example programs where it explains how to send and receive bulk of data after creating MODSERIAL object (I mean which API function should i have to use ?)

I also tried to use the SimpleDMA library to resolve this issue. I am not succeed either. I am not so clear from the examples, on how to set the source and destination addresses for Serial receive and Transmit.

Can some one suggest me few solutions.

2 Answers

10 years, 1 month ago.

Using MODSERIAL and just putc 100 times for 100 bytes should work and not result in a loss of data. Alternatively, this one should work also on the KL25Z: http://mbed.org/users/sam_grove/code/BufferedSerial/

For SimpleDMA, did you check the HelloWorld: http://mbed.org/users/Sissors/code/SimpleDMA_HelloWorld/file/418889681f13/main.cpp? Only thing to take into account is that that is only for UART0, UART1 and UART2 have their DMA enable bits in C4 instead of C5 (besides that it should be equal, imo a bit weird they didn't make that also equal).

Jags K
poster
10 years, 1 month ago.

Hi Eric,

Thanks for your quick response.

If i use putc for 100 times, what is the difference between Serial and MODSERIAL objects ? I am looking for functions like Serial.write(char *buff,int size).

Actually i am integrating mbed libraries with MATLAB/Simulink code generation. As a part of this, i need to establish serial communication between MATLAB and target. It works fine as long as data is not big.

Here is my question. If the simulink sends 300bytes of data, how the target will handle with gect function ? My doubt is that target has one receive buffer of size 1 byte. So i am looking for the library where handles this situation without overrun.

My understanding is that DMA is right choice for my requiremenet. Whenever the data is available in the Rx buffer, the DMA immediately copies to internal buffers, later it should be available for software to read. Similarly, when software is ready to send, it should fill the DMA tx buffers and then DMA should initiate UART to transfer the data over serial. I have gone through the Hello world example. But the example basically explains the data tranfer across the buffers. The usage of DMA in UART is not so clear from the example.

As i got stuck with DMA, i found MODSERIAL library. I tried to use it but could not succeeded.

As you suggested i will try with Bufferedserial.

If you use putc with regular Serial, it will stop it in the KL25z hardware buffer, if there is no space it blocks until there is space. With MODSERIAL if there is no space it puts it in a software buffer, which will send it as soon as there is space.

If simulink sends 300 bytes of data, with regular serial they will be lost if getc isn't called fast enough. With MODSERIAL it will be placed in the software receive buffer (which needs to be large enough, I don't know if they default is 256 bytes or 512 bytes).

DMA looks indeed like a good choice for you. But what part of the example is unclear? One of the examples moves from buffer to UART TX. One from UART RX to UART TX. Okay there is none for from UART RX to a buffer, but that should be fairly straightforward from the others.

posted by Erik - 04 Feb 2014

Hi Erik,

I am using UART and below is the part of code that i am using to enable DMA. I see that serial communication is happening after using the below part of the code. But i see same issue of stuck. I have attached the sample code.Can you check the code once that am i using the triggers correcty? Any other code is required ?

/media/uploads/Jagadeesh/sample_code.txt

posted by Jags K 04 Feb 2014

That seems correct. Although I assume the waiting until it is finished is just for testing purposes? If you do it like that in general, you might as well just use regular Serial, since your code blocks anyway until it is finished. The advantage of both DMA and MODSERIAL is that you can do other things in your code, while it is sending that data.

When you say it gets stuck, how does it get stuck? Does it send part of your data? And is that part always the same size? Is the buffer you are receiving to/writing from large enough? Personally I haven't tested it for large buffers and the UART, but I did also use it for 512 byte writes/reads with SPI. So I don't see why it would stop after a certain number of bytes.

You can try to make a small sample program, which either works, in which case there is something in your other code interfering, or it doesn't work, in which case I can look at it.

posted by Erik - 04 Feb 2014

I have tested without the waiting loop, it did not work. I do not see any response from target.

We have a server/ client code running both on the simulink and target. There are couple of handshaking calls before actual data is analyzed. When i mean stuck, the host is waiting for acknowledgement and the target is not responding. My guess is that either target does not decode correctly as it did not received the complete command or the target send function is not working.

The buffer size may not be more than 256 bytes.

For dubugging if i use one more serial terminal, the serial communication stuck little more earlier. I see similar questions posted in forum about this but i did not find correct solution for two serial terminals issue.

posted by Jags K 04 Feb 2014

Without the waiting loop, do you then give it enough time before switching it to receive? If you want to transmit and receive at the same time, you need to make 2 simpledma objects on different channels (in the current version you don't need to give a channel number, it picks one automatically).

I wonder if the getting stuck you describe is related to the dma/modserial, or just to your code/protocol. To test sending is easy: store an array in your KL25Z (in your program), send it to your PC, look at it with teraterm, is it correct? Then that goes fine in principle.

To test receiving, send a block of data (probably easiest with matlab, but can also be done with teraterm), and once it is done receiving let it return it using printfs, check if it is the same.

But in general I can only really look at it with some code I can try myself to see where it is going wrong.

posted by Erik - 04 Feb 2014

Hi Eric,

The protocol code that we are using is standard and works for most of the targets. But as you said ther might be issue in that code also.

I am debugging from couple of days. And i found that overrun flag is set before the communication is stuck. As per the manual, it clearly mention that data will be lost whenever overflow occurs. When i increase the baud rate, sometime , i see that communication stuck later when compared to lower baud rate. With DMA, i expect that i should not see this issue. But i see same issue with DMA also.

Any ideas how to handle this overrun flag ?

posted by Jags K 08 Feb 2014