|
|
View previous topic :: View next topic |
Author |
Message |
Cyberian
Joined: 06 May 2017 Posts: 7
|
MATLAB/SIMULINK Code Generation with CCS |
Posted: Sun May 07, 2017 8:16 pm |
|
|
Hi, I use matlab and simulink to generate code, both in diagrams and in stateflow. I use it perfectly with uVision for ARM, XC8 for pic18 and XC16 for dsPIC and HDL for my Spartan and Virtex. First of all I had a hard time generating a compilation with several compilation units. Once I did this, I wanted to use matlab-generated code, as I've been doing so far. The thing is, I do not hit the ball with CCS.
The issue is that with CCS, everything is more complicated. Is it possible to use the code generated by matlab ? In all the other compilers you can, and honestly you save thousands of complex lines of code, even implementing Kalman, LQG control etc., etc. is very simple with code generation.
I am currently using the dsPIC33EP512GM706.
CCS complies with the C99 standard ? Wth XC16, in order to use Simulink Coder, I must add to the compiler directives: -std = c99. In CCS is there any way to use the generated code?
Best Regards,
Sylvestre.
Last edited by Cyberian on Mon May 08, 2017 6:08 am; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Mon May 08, 2017 12:41 am |
|
|
Try it.
Just make sure you set the ANSI directive in CCS.
Generally the core code from Matlab, will work if you use the ANSI directive in CCS. A little depends on the compiler version, and it can have problems if you start using complex variable types. I found that static variable allocation always worked better than the dynamic forms.
The ANSI setting is vital though. Otherwise things like the CCS meaning of const, will 'bite you'.
I found it worked much better to just generate the code block as readable C code, and include this into a CCS project, rather than have the compilation done through Matlab.
It was a couple of years ago that I played with it, it may have improved since in this area. |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Mon May 08, 2017 4:55 am |
|
|
We are not CCS. This is a user self-help forum with no input, other than moderation, from CCS. As such passive-aggressive threats to use other compilers has little effect on us. My advice is to use whatever tools you feel are most appropriate to your applications and business model.
Personally I would never use any auto-generated code, no matter what source, blindly. I expect that it will always need and could generally do with some sort of optimisation to best suit the environment in which it is intended to run. in any case, wherever possible, its useful to understand the complexities of any code that you use. One issue that instantly springs to mind is that most PICs have very limited dynamic memory available, and that auto-generated code may well tend to make liberal use of dynamic memory, assuming a large, Gbytes, flat memory space, rather than the few kbytes that are available on most PICs. Also the way ANSI compilation mode is implemented means it generally uses more RAM than native mode would have done, putting further pressure on already limited meory space.
I do not know the full details of CCS C C99 compatibility. There is an ANSI compatibility mode. I don't know which ANSI C it most closely matches. I do know its not completely compatibile. There may be significant issues in some cases. I stress that's a may, and that you therefore exercise caution, and expect there may be a need to modify auto-generated code.
I suspect the best way to incorporate any such code is as a library or plug-in, depending on your frame of mind, and wrap it with whatever processor/target specific handwritten code that may be necessary. So, you set your code generation tool to generate your code as a library.
As Ttelmah says, try it and see. |
|
|
Cyberian
Joined: 06 May 2017 Posts: 7
|
|
Posted: Mon May 08, 2017 6:11 am |
|
|
Ttelmah wrote: | Try it.
Just make sure you set the ANSI directive in CCS.
Generally the core code from Matlab, will work if you use the ANSI directive in CCS. A little depends on the compiler version, and it can have problems if you start using complex variable types. I found that static variable allocation always worked better than the dynamic forms.
The ANSI setting is vital though. Otherwise things like the CCS meaning of const, will 'bite you'.
I found it worked much better to just generate the code block as readable C code, and include this into a CCS project, rather than have the compilation done through Matlab.
It was a couple of years ago that I played with it, it may have improved since in this area. |
Hi Ttelmah,
Thanks for answering. How do I add the ANSI directive to CCS? I am looking in the manual and still can not find anything. I will continue investigating, but if you can help me in that I will thank you.
Best regards,
Sylvestre. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Mon May 08, 2017 6:15 am |
|
|
I'm with RF-D and do NOT use ANY 'wizard' generated code. Frankly no 'high level' code genny can replace a good human code cutter. This site is filled with posts that the 'wizard' or 'simulator' created doesn't work in the Real World. Most programmers will have built their own libraries of drivers, functions, etc. over the years (decades to some of us) and have tweaked the code to be the best it can be.
The huge problem with the 'wizards' is that they are based on someone else's idea as to what it 'correct'. If 'tight', they won't work for you, if 'flexible', they chew up a LOT of code space and clock cycles....aka bad programming.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Mon May 08, 2017 7:30 am |
|
|
#device ANSI
At the top of the code (after the processor include). |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1909
|
|
Posted: Mon May 08, 2017 7:46 am |
|
|
RF_Developer wrote: | Personally I would never use any auto-generated code, no matter what source, blindly. |
My personal motto is "never trust anything you didn't write yourself."
Along those lines, I've been bitten by Linux (both drivers and the gnu compiler) so many times.... The ironic aspect is that I've barely used Linux - only when I was literally forced to by the circumstances - and I've discovered so many bugs. Just because something is open source doesn't mean that the people playing with it know what they're doing. |
|
|
Cyberian
Joined: 06 May 2017 Posts: 7
|
|
Posted: Mon May 08, 2017 7:50 am |
|
|
The code generated by matlab is very good, I always check it and never need to change anything, it is wonderful, since it is the same implementation of the diagrams in blocks. Besides the great advantage is STATEFLOW, you can make very long and complex state machines, that doing them manually would take a lot of time and it would be tedious to debug. Please stop attacking the automatically generated code, it is really efficient and it is the future in the big developments. Move forward with technology!
I entered because I need help finding the way to use the code generated by matlab with CCS. In the industry it is good to implement certain standards of good programming like MISRA 2012, the code generated by Matlab complies with these rules of good programming, in addition to generating security code for automotive implementation. We use code generation on all our development platforms, ARM, FPGA, Texas DSP, in particular Delfino and Piccolo, dsPIC33E and PICs 16 and 18. It really saves us a lot of code, we can make more complex developments in less time.
I keep trying, but without results, now I'll start verifying which part of the generated code is not CCS compliant.
I will comment on progress and if I make it work, I will upload a complete project with code generator.
Best regards,
Sylvestre. |
|
|
Cyberian
Joined: 06 May 2017 Posts: 7
|
|
Posted: Mon May 08, 2017 7:56 am |
|
|
Ttelmah wrote: | #device ANSI
At the top of the code (after the processor include). |
Hi Ttelmah,
Thanks for answering.
Yes, I tried with that directive and still nothing, I found it in the CCS manual, page 94, but still unable to compile. Something that also delayed me a little was the compilation of multiple units, but I think I have overcome it.
I'll keep trying.
Best regards,
Sylvestre. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Mon May 08, 2017 8:00 am |
|
|
I would not use multiple units.
This reduces the efficiency of CCS.
As I already said, and others are also saying, the way to do this is to have a CCS project, and compile this. Tell MATLAB to generate readable C code, and then include this code into the CCS project.
All you then have to do is have your CCS code setup the variables, and call the Matlab functions. |
|
|
Cyberian
Joined: 06 May 2017 Posts: 7
|
|
Posted: Mon May 08, 2017 8:08 am |
|
|
Ttelmah wrote: | I would not use multiple units.
This reduces the efficiency of CCS.
As I already said, and others are also saying, the way to do this is to have a CCS project, and compile this. Tell MATLAB to generate readable C code, and then include this code into the CCS project.
All you then have to do is have your CCS code setup the variables, and call the Matlab functions. |
Hi Ttelmah,
Thank you for your response, I will implement what you are telling me.
I will comment on the progress.
Best regards,
Sylvestre. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1358
|
|
Posted: Mon May 08, 2017 10:45 am |
|
|
I have a general question here:
If you are using a code generator to make the code, and you have it working for XC16, then why are you looking to have it compile in CCS? If you aren't going to mess with the generated code, then the compiler shouldn't matter as long as you can get the hex file at the end to program your PIC.
I'm not saying don't use CCS, but am curious as to the reasoning. |
|
|
Cyberian
Joined: 06 May 2017 Posts: 7
|
|
Posted: Mon May 08, 2017 12:07 pm |
|
|
jeremiah wrote: | I have a general question here:
If you are using a code generator to make the code, and you have it working for XC16, then why are you looking to have it compile in CCS? If you aren't going to mess with the generated code, then the compiler shouldn't matter as long as you can get the hex file at the end to program your PIC.
I'm not saying don't use CCS, but am curious as to the reasoning. |
I want to use CCS for what I said before, it is faster to configure the peripherals of the microcontroller, one line of code initializes the ADC module and another the PWM, another the UART, etc (using PCD Wizard). With Microchip you had the peripheral library, but it was never reliable, and no longer support. Also the dsPIC we use is not in MPLAB Code Configurator, which is also a powerful tool for configuring PICs.
Regards,
Sylvestre. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Mon May 08, 2017 12:20 pm |
|
|
Be careful when invoking the 'Wizard'. It is known(reported here..) that it doesn't set the correct bits for certain registers for certain chips. Just something to be aware of. So if your code doesn't run as expected, dump the listing and check to see if the bits are being properly coded.
Jay |
|
|
Cyberian
Joined: 06 May 2017 Posts: 7
|
|
Posted: Mon May 08, 2017 12:38 pm |
|
|
temtronic wrote: | Be careful when invoking the 'Wizard'. It is known(reported here..) that it doesn't set the correct bits for certain registers for certain chips. Just something to be aware of. So if your code doesn't run as expected, dump the listing and check to see if the bits are being properly coded.
Jay |
Hi temtronic,
Yes, I consider that, in fact, I print the registers by UART, to see how CCS configured them, and I use that information to learn faster about the dsPIC registers, and I can copy the configuration to XC16 or modify some Bit if it is not good.
I've had those kind of problems with some PIC18, which did not configure me well some registers the PCW Wizard.
Best regards,
Sylvestre |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|