C Code Simulation

From Flowcode Help
Revision as of 14:22, 30 May 2018 by DanielM (talk | contribs)
Jump to navigationJump to search

Unlike previous versions, Flowcode 8 can simulate C code inside C Code icons. Note, in order to use this feature you will need to have purchased the C Simulation feature pack.

This feature can be great for debugging custom C code within your project, prior to compilation to a microcontroller. It is also invaluable as a teaching tool, and allows you to teach students the effects of certain C language statements on variables. When simulating C code, you can step through individual statements within a block in the same way that you step through multiple icons in your Flowchart.

How to simulate C code

CSimButton.jpg

By default no C code will be simulated until the C Simulation button is enabled. C Code icons will simply be skipped by the simulator. The C Simulation button can be found on the main toolbar and via the DEBUG menu. C Simulation is enabled when the button icon is highlighted by a bounding square.


The Conversion Messages window

When simulating C code, a helpful Conversion Messages window will appear.

Errors

The Conversion Messages window will detail any problems that Flowcode has encountered when trying to parse your C code. Certain C language features such as pointers, and advanced modifiers such as const and typedef are not supported. If Flowcode encounters a statement within your C code that it cannot simulate, this information will be displayed in the Conversion Window, and Flowcode will simply skip that statement. Flowcode will still attempt to simulate the rest of the code within the C code icon.

Temporary variable renaming

The Conversion Messages window will also detail any information that is useful for debugging the values of variables. Flowcode may temporarily rename variables within your C code when simulating. This due to the fact that multiple variables in C may use the same name, as long as they exist at different scopes (e.g. a name can be used to define a variable at the top of a function, as well as a variable inside the curly braces of a while-loop). This renaming ability allows you to use the Simulation Debugger to track the values of different variables, even if they share a name in the C code. No variable renaming performed by Flowcode will alter the behavior of your C code. If a variable is renamed by Flowcode during simulation, then this information, including the name to use in the Simulation Debugger, will appear in the Conversion Messages window. For example, we might declare "int x" inside a while-loop, which Flowcode will rename to "x2" during simulation: this can then be tracked in the Simulation Debugger by writing ".x2" in the Expression field.


Interacting with Flowcode variables in C code

We recommend viewing your Flowchart using the C code view in order to get a better understanding of how the code relates to the Flowchart. When variables are created in the Project Explorer, you specify a given type (e.g. byte). The actual types as they appear in C code can be understood using the following mapping:


Calling Flowcode functions and your own Macros

Currently, there is a limited support for simulating Flowcode-generated function calls within your own C code.

Flowcode is able to simulate the effects of the following function calls:

  • FCI_DELAYBYTE_MS(x) where x is a byte and the function causes a delay of x milliseconds
  • FCI_DELAYBYTE_US(x) where x is a byte and the function causes a delay of x microseconds
  • FCI_DELAYBYTE_S(x) where x is a byte and the function causes a delay of x seconds
  • FCI_DELAYINT_MS(x) where x is an int up to 2000 and the function causes a delay of x milliseconds
  • FCI_DELAYINT_US(x) where x is an int up to 2000 and the function causes a delay of x microseconds
  • FCI_DELAYINT_S(x) where x is an int up to 2000 and the function causes a delay of x seconds
  • GET_PORT(x) where x is a port letter (A, B, C...) and the function returns the value of port x
  • GET_PORT_PIN(x, y) where x is a port letter (A, B, C...), y is a pin index (0, 1, 2 etc.) and the function returns the value of pin y of port x
  • GET_PORT_MASK(x, y) where x is a port letter (A, B, C...), y is a mask value (e.g. 0xFF00) and the function returns the value of the pins of port x given by the mask y
  • GET_PORT_SHIFT(x, y) where x is a port letter (A, B, C...), y is a shift value (e.g. 3) and the function returns the value of port x after right-shifting it by y positions
  • GET_PORT_MASK_SHIFT(x, y, z) where x is a port letter (A, B, C...), y is a mask value (e.g. 0xFF00), z is a shift value (e.g. 3) and the function returns the value of the pins of port x given by the mask y, after right-shifting it by z positions
  • SET_PORT(x, y) where x is a port letter (A, B, C...), y is a numerical value and the function sets the value of port x to y
  • SET_PORT_PIN(x, y, z) where x is a port letter (A, B, C...), y is a pin index (0, 1, 2 etc.), z is a numerical value and the function sets the value of pin y of port x to z
  • SET_PORT_MASK(x, y, z) where x is a port letter (A, B, C...), y is a mask value (e.g. 0xFF00), z is a numerical value and the function sets the value of the pins of port x given by the mask y, to z
  • SET_PORT_SHIFT(x, y, z) where x is a port letter (A, B, C...), y is a shift value (e.g. 3), z is a numerical value and the function sets the value of port x, to z, after left-shifting z by y positions
  • SET_PORT_MASK_SHIFT(w, x, y, z) where w is a port letter (A, B, C...), x is a mask value (e.g. 0xFF00), y is a shift value (e.g. 3), z is a numerical value and the function sets the value of the pins of port w given by the mask x, to z, after left-shifting z by y positions


Flowcode is also able to simulate the effects of calling your own user-defined macros. In C code, user-macros are referred to using the prefix "FCM_". For example, if we defined a user-macro called "F", which returned a byte, and required an integer as a parameter, we could write the following code, which calls the macro "F", passing the parameter "4" and then stores the return value in a byte variable called "x".


"MX_UINT8 x = FCM_F(4);"

Example

Here is an example program that counts up and down in binary using Port A and a Combo Board component to show the LED signals.

FC6 Icon.png C Simulation Example

CSim.jpg


The C Simulation feature is similar to the C Code to Flowchart Conversion feature that converts C code into Flowchart icons.