Engee documentation
Notebook

Code generation for MIC32 (Binary Counter)

This demo presents the development of the Engee model for a two-bit binary counter, followed by code generation and execution on the MIK32 NUKE V0.3 debug board.

Introduction

In this example, a MIK32 NUKE V0.3 debug board based on the K1948VC018 MIK32 Amur microcontroller is used as the target device. A model of the final device - a two-bit binary counter - was developed in Engee and C code was generated. The code was compiled and loaded into the microcontroller from VS Code with PlatformIO extension.
The built-in user button K1 is used as the counter input, built-in LEDs VD3 and VD4 are used as outputs.

Model description

The model of this example is mik32_DI_DO.engee. Functionally, it can be divided into blocks for connecting the controller periphery, control logic and a block for simulating presses on the user button K1.

mik32_di_do.png

The User Button Simulation Block - Pulse Generator generates a rectangular signal with a frequency of 1 Hz and a fill factor of 0.5 during the simulation. Block C Function "PLATE_BUTTON" transmits this signal to the counter subsystem without any changes.

Connecting peripherals

To work with peripherals of the MIK32 controller, the following blocks are present in the model C Function:

  • GPIO_INIT - for switching on GPIO clocking;
  • PLATE_BUTTON - for initialisation of the built-in user button K1, reading its state state;
  • LED_1 - for initialisation of the built-in LED LD1, transferring its state to it state;
  • LED_2 - for initialisation of built-in LED LD2, transferring its state to it state.

Contacts seq of blocks GPIO_INIT and PLATE_BUTTON are used exclusively for defining the sequence of functions execution, which will be received from these blocks as a result of model code generation. Plug-in files for working with the controller periphery and successful compilation of the code obtained from the model in the development environment - mik32_memory_map.h, pad_config.h, power_manager.h and wakeup.h are located in the directory include of the current example. They are connected in the block GPIO_INIT, tab Build options. It is not necessary to download these files and connect them to the project in the VS Code environment, as they are already contained in the corresponding PlatformIO packages if the environment is set up correctly.

mik32_di_do_headers_2.png

A detailed description of the principles of the code embedded in C Function is given in the comments to the block code.

Binary counter

The binary counter implemented in the subsystem BINARY_COUNTER, works as follows. From the input Trigger of the subsystem to the block Chart comes the signal of the selected leading edge of the signal - pressing the button K1. In the block Chart the number of received signals is counted, the result of counting is output to the output out block. Counting is performed cyclically, in the range [0; 3].

mik32_di_do_counter_2.png

According to the counter output signal Multiport Switch blocks switch bit states to control LEDs VD3 and VD4, thus acting as encoders of decimal numbers into binary ones. The low bit of the number is formed on VD3 and the high bit is formed on VD4. The output data type is unsigned 32-bit integer (UInt32).

Modelling results

To simulate the operation of a two-bit binary counter, let's load and run the model mik32_DI_DO.engee:

In [ ]:
if "mik32_DI_DO" in [m.name for m in engee.get_all_models()]
    m = engee.open( "mik32_DI_DO" );
else
    m = engee.load( "$(@__DIR__)/mik32_DI_DO.engee" );
end

    data = engee.run(m);

From the obtained simulation data, plot the signals:

  • PLATE_BUTTON.1 - simulation of pressing the button,
  • BINARY_COUNTER.VD3 - states of the low bit of the output code,
  • BINARY_COUNTER.VD4 - state of the high bit of the output code. The plotted signals have a binary format, but they are scaled for clarity of display.
In [ ]:
using Plots
plotlyjs()

plot(data["PLATE_BUTTON.1"].time,
     data["PLATE_BUTTON.1"].value*0.5,
     label="Сигнал кнопки", lw=1)
plot!(data["BINARY_COUNTER.VD3"].time,
      data["BINARY_COUNTER.VD3"].value*0.99,
      label="Младший бит", size=(900,300),
      lw=3, legend=:topright)
plot!(data["BINARY_COUNTER.VD4"].time,
      data["BINARY_COUNTER.VD4"].value*1.01,
      label="Старший бит", lw=3)
Out[0]:

As we can see from the modelled graphs, the binary counter algorithm works correctly, now we can proceed to code generation and execution of the model code on the target device.

Code generation

Let's generate code from the model to load the control algorithm into the microcontroller:

In [ ]:
engee.generate_code( "$(@__DIR__)/mik32_DI_DO.engee",
                     "$(@__DIR__)/mik32_DI_DO_code")
[ Info: Generated code and artifacts: /user/start/examples/codegen/mik32_di_do/mik32_DI_DO_code

The files created in the folder mik32_DI_DO_code - header mik32_DI_DO.h and source mik32_DI_DO.c will be used further on in the project assembly. The generated file of the main program main.c will not be used in the project, for code execution in the development environment prepared by main.c, located in the root folder mik32_DI_DO of the example.

Preparing the project in the development environment

The development environment through which the project is built and loaded into the target device is VS Code with the PlatformIO add-on. The environment configurations and connections are not considered in this example, as they are described in detail at resources) of the controller developer.
From the example directory let's transfer the generated files, main.c and configuration file platformio.ini to the PlatformIO project.

mik32_di_do_ide.png

After that you can proceed to build the project and load the programme.

Code execution on MIK32

Let's connect the debug board MIK32 NUKE V0.3 to the USB-port of the computer, then in PlatformIO we can observe the connected device. To correctly identify the connection of this board requires a USB driver. The driver used in the example is libusbK.

mik32_di_do_target.png

After successful connection, let's proceed to building the project:
"PLATFORMIO -> PROJECT TASKS -> mik32v2 -> General -> Build ".
If there are no build errors, load the compiled code into the microcontroller:
"PLATFORMIO -> PROJECT TASKS -> mik32v2 -> General -> Upload ".

mik32_dido_1080.gif

As a result of executing the code generated from the Engee model, it is possible to observe the formation of binary code on the built-in LEDs VD3 and VD4 when the built-in button K1 is pressed. As can be observed, the loaded code reproduces the operation of a two-bit binary counter.

Conclusion

In this example we have considered the development of the Engee model for the control programme of built-in LEDs by the signal from the built-in button on the K1948VK018 MIK32 Amur microcontroller as a part of the MIK32 NUKE V0.3 debug board. The algorithm is implemented using the block Chart from the library of Engee finite automata and is suitable for code generation. The developed model is embedded with generated files into the PlatformIO environment project for VS Code with further assembly, loading and execution on the target device.