How can i use to initialize for one-time execute code?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How can i use to initialize for one-time execute code?

Jump to solution
5,909 Views
byeongjinkim
Contributor II

I want to initialization code for one-time execute.

example)

main()

{

   --initialization---

   a = port input value (port input at power-on)

  ---------------------

   while(1)

   {

      b = current port input value

      if (a ~= b)

      {~~~~~~~

      }

   }

}

how can i use to one time execute code?

0 Kudos
1 Solution
4,350 Views
dumitru-daniel_
NXP Employee
NXP Employee

Hi byeongjinkim‌,

Simulink generated code has 3 main functions:

- step()

- initialize()

- terminate()

You need to inject the code into the initialize function because that is called only once in the beginning.

Capture.JPG

You can do this by using 2 standard (S-Function Builder and Initialize Function) Simulink blocks and one file from the Toolbox (mcd_s12zvm_user_copy_required_files.m)

The procedure is like this (an example is attached)

1: First you add the Initialize Function Block into your model

1.JPG

2: inside the Initialize function you add the S-Function Builder

2.JPG

3: inside the S-Function Builder you call your custom C-functions/C-files/C-headers

3.JPG

4.JPG

4: Use Toolbox Custom file copy to move all the custom files into the automatic generated folder before building the application

6.JPG

5: You build the application and check if the code was injected in the right place.

7.JPG

Please find attached an example - all you have to do is to:

- copy and replace the file: mcd_s12zvm_user_copy_required_files.m into c:\MCToolbox\mctbx_9s12zvm\mctbx_s12zvm\mctbx_s12zvm\mscripts\ folder

- add you own code into my_code function.

Hope this helps!

Daniel

View solution in original post

5 Replies
4,351 Views
dumitru-daniel_
NXP Employee
NXP Employee

Hi byeongjinkim‌,

Simulink generated code has 3 main functions:

- step()

- initialize()

- terminate()

You need to inject the code into the initialize function because that is called only once in the beginning.

Capture.JPG

You can do this by using 2 standard (S-Function Builder and Initialize Function) Simulink blocks and one file from the Toolbox (mcd_s12zvm_user_copy_required_files.m)

The procedure is like this (an example is attached)

1: First you add the Initialize Function Block into your model

1.JPG

2: inside the Initialize function you add the S-Function Builder

2.JPG

3: inside the S-Function Builder you call your custom C-functions/C-files/C-headers

3.JPG

4.JPG

4: Use Toolbox Custom file copy to move all the custom files into the automatic generated folder before building the application

6.JPG

5: You build the application and check if the code was injected in the right place.

7.JPG

Please find attached an example - all you have to do is to:

- copy and replace the file: mcd_s12zvm_user_copy_required_files.m into c:\MCToolbox\mctbx_9s12zvm\mctbx_s12zvm\mctbx_s12zvm\mscripts\ folder

- add you own code into my_code function.

Hope this helps!

Daniel

4,350 Views
byeongjinkim
Contributor II

A build error occurs.

pastedImage_2.png

pastedImage_1.png

matlab version : 2015a

you attached '*.slx' file is 2016 version. so, not open.

i used '.mdl' file.

what is the command "rp_copy"?

this is not find matlab command.

0 Kudos
4,350 Views
dumitru-daniel_
NXP Employee
NXP Employee

Hello,

The build error indicates that the custom files were not copied inside the *_mcd_rtw folder where those are needed to be.The  rp_copy is a toolbox function.Try the open the m file in the MATLAB editor and place a breakpoint and then build again. Does the breakpoint gets hit?If  not then the file was not copied in the location expected by the toolbox.

Idid not know your MATLAB version and I exported the mdl in 2014 version just to be covered. It should works.

Best regards, Daniel

0 Kudos
4,350 Views
byeongjinkim
Contributor II

initialize function block is not supported in simulink of matlab version 2015a.

pastedImage_2.png

matlab command "help rp_copy" --> not find rp_copy

rp_copy is wrong. The exact command is "copyfile".

pastedImage_3.png

after corrected command, compile is right operating.

pastedImage_1.png

but, initialize function is wrong position.

i think, initialize function block is not operation in 2015a version.

0 Kudos
4,350 Views
dumitru-daniel_
NXP Employee
NXP Employee

Hi byeongjinkim‌,

You're right - it look the Simulink introduced the Simulink Block Initialize Function after 2015.

Capture.JPG

I think there is still a hope even in the older Matlabs - even if you will not have the block in Simulink.

Can you try this approach Integrate Custom C/C++ Code for Simulation - MATLAB & Simulink ?

Hope this will work for you!

Daniel

0 Kudos