Pierpaolo Cira
Pierpaolo Cira

Reputation: 1477

Simulink generate code with unsupported istruction (ceil)

Generating code by Simulink (Matlab R2011A on MacOS 64bit) I got a problem: it uses ceil function inside the code, but it isn't supported on my target platform. I'm generating using ERT, for Arm Cortex processor (on a Cypress PSoC).

Is it possible to solve this problem?

I tried the solution without success. Also in Code Generation - Interface, I tried to disable floating-point or not-finite numbers... but in this way every signal of my project raises some errors (same behaviour also changing it data-type).

Really thanks to anyone suggests me what I can try to do

Upvotes: 1

Views: 563

Answers (2)

Pierpaolo Cira
Pierpaolo Cira

Reputation: 1477

Ok... I solved.

The problem was in the destination environment (PSoC Creator). As explained here http://www.cypress.com/?id=4&rID=42838 :

Go to Project -> Build Settings -> Linker -> General -> Additional Libraries. Type m in the Additional Libraries field. If you are not adding this Additional Library then you will get the following Build error "undefined reference to `sqrt'" where sqrt is a math function.

Nothing changes if the problem is with sqrt() or ceil(), because they are in the same library (math.h).

PS: thank you Engineero... your solution is very useful, and can be apprecied from other people with my problem (but in other environments).

Upvotes: 1

Engineero
Engineero

Reputation: 12928

You could write your own ceil function and include it in whatever your output code is for your target device. Assuming you are generating C-code, the function would look something like:

int ceil (double number) {
    if (number == 0)
        return 0;
    if (number > 0) {
        if (number - (int) number > 0)
            return (int) number + 1;
        else
            return (int) number;
    }
    else {
        if (number - (int) number < 0)
            return (int) number - 1;
        else
            return (int) number;
    }
}

With a prototype in your header file like:

int ceil (double);

Now your C-code can call integerValuedNumber = ceil(doubleValuedNumber) and it should work. You can also do this with macros in the C-file (see nintendo's answer).

EDIT: I corrected my code to use proper type casting syntax for C. Basically what you are doing with the (int) number syntax is taking the double-valued number variable and forcing it to be an integer. You can find more information about data types in C here, or Google "type casting C" or "data types C" for more information.

Also, some additional parenthesis might be needed, like return ((int) number) + 1; and similar. I am a little rusty on my C-programming, but hopefully this gets you going toward a viable solution.

EDIT 2: I corrected the return data type of our self-defined ceil function. You would want this to return an int, or maybe long. Again, check out documentation on data types in C if you are not sure what data type is appropriate for your application. If the values you are applying ceil to are not very large (less than +/- 2^15 for instance) then int is probably fine.

Upvotes: 2

Related Questions