scanf/printf not working when calling generated C code
Mostra commenti meno recenti
In Matlab I have generated C code (.c/.h files) from a Matlab function using codegen from Matlab Coder.
I call this function in a while loop on an ARM quad-core Cortex A53 using AMD Vitis.
Before calling this function I use scanf/printf to get user input and to print to the serial monitor. However, scanf/printf only work once in the 1st iteration (before calling the function) and not in the 2nd iteration or later (after calling the function).
Is this a common problem and has anyone encountered this problem before?
I think, but am not sure, that the problem is in the generated C code and not in Vitis itself.
Thank you in advance!
9 Commenti
dpb
il 23 Feb 2026 alle 14:59
What is the definition of failure to work here? What are the symptoms, specifically?
If you comment out the call to the function does the loop then run normally with whatever input/output being observed using scanf/printf? If so (and highly likely anyway) there's something wrong with the calling sequence or in the function itself. Without any other detail, it's not possible to say anything specific.
Victor Lam
il 23 Feb 2026 alle 15:10
Probably no one has ever done precisely the same thing with the combination of environment and hardware so it's extremely likely the answer to that question is "No".
Knowing nothing of what the function does nor anything about the system you're running, it's unlikely anybody here can help.
If not calling the function removes the symptoms, then it is conclusive that something in either the calling/return linkage is broken or the function itself did something damaging outside returning its expected output(s). One next debugging step would be to create an empty null function in its stead and test calling it. If that is successful, then add the argument list alone, then the output arguments until find what step causes something to break. You could also try replacing the function call with something simple like timestwo.c and make sure the code generator can handle the simple case first. It's always possible there is something unique in the development environment it doesn't know about -- maybe a default convention assumed isn't the same as expected.
"... I use scanf/printf to get user input and to print to the serial monitor"
I would suspect that the serial connection got broken would be a fairly likely possibility...
If that doesn't uncover the problem, you'll probably have to submit the whole enchilada to Mathworks suppor.
Victor Lam
il 25 Feb 2026 alle 16:41
dpb
il 25 Feb 2026 alle 19:55
Aha! At least do have symptoms to research.
It's certainly possible there's a type mismatch but to say anything definitive would need to see both the snippet of MATLAB code with enough context to be able to verify variables and their types and then the resultant C code. Or, are you saying the printf is MATLAB code, not converted to C and so isn't the C runtime i/o library routine? If the latter, it would look like perhaps your C function isn't returning the same variable type as you are presuming it is. Again, we've got to be able to see what we're talking about...
Victor Lam
circa 14 ore fa
Without the actual code, it's not possible to say anything definitive. The internal variable definitions and typing and possible conversions and how are retrieved/saved in memory and addressed in the calls are the critical points and cannot be determined explicitly without the code itself.
That a literal string does work and the floating point variable doesn't is pretty definite indication there's a mismatch in there somewhere between what is being passed and what the function prototype is expecting. It's possible there's a mismatch in the default floating point size type in the compiler flags other than inside the code itself, too, you need to confirm what the compiler is being told.
Again, "simplify, simplify"...can you write and successfully call a routine that does nothing but printf() a passed variable of varying type?
ADDENDUM
obtain the variables cycles_before and cycles_after which are of type uint64_t
define the variable clock_speed which is of type const double
compute double CPU_time = cycles / clock_speed * 1e6
may be an issue -- the integer divide will be integer -- need to promote those to doubles first I think.
Victor Lam
circa 8 ore fa
Being able to read the specific code would let folks have at least a chance of spotting an issue -- keeping it hidden does nothing to help advance diagnosing a problem. If I understand correctly, the printf() call is inside the C function. What if you were to return the additional variables to do the timing calculation to the calling routine -- can you get them back correctly and then compute and print the result in the calling MATLAB code instead? That doesn't solve the underlying problem but would perhaps confirm or deny that the internals of the function are doing what is expected.
In the above pseudo code cycles isn't defined -- one presumes it is the difference between the prior two - _after - before which is also going to be uiint64 so there is that issue of mixed mode arithmetic which otomh I don't recall exactly how C handles -- but
c=uint64(pi*1E4)
s=2E8;
t=c/s
t=c/s*1E6
with some just made up dummy numbers, MATLAB coerces to the integer type and the division is going to be integer -- here c<s so --> 0
Risposte (0)
Categorie
Scopri di più su ARM Cortex-A Processors in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!