Another, tidier, mixed mode simulation model. Dual phase IPT buck converter

Here’s a more complicated SMPS to show my latest improvements. It’s a dual phase Inter Phase Transformer (IPT) based buck converter. I contributed to a paper a couple of years ago (https://digital-library.theiet.org/doi/10.1049/icp.2022.1079) but only looked at the transformer so I thought that I would try designing a complete one.

Here’s the model.
2_phase_IPT.zip (1.5 MB)

There’s a switched resistor on the output that provides a load step. If you reduce the value below 1.5 ohms then you can test the current limit (see below).

The microcontroller model has been tidied up a lot compared to the last one. Where it takes more than one line to add something like an ADC or PWM output, I’ve made a sub circuit and shifted it to a separate file.

The c-code is all floating point so it’s easier to follow.

The PWM output is up down triangular mode (instead of up saw tooth only) with dead time generation and two compare points so there are four outputs to play with (anyone want a bidirectional dc motor controller or buck/boost converter instead - should be an easy modification).

There are now three files with models for ngspice.
* “models.lib” has the switches and transformer models. It’s a standard ngspice file.
* “microcontroller_internal_models.bus” has the models for the ADCs, delays and other internal functions that are called by “microcontroller.bus”. There should be no need to modify “microcontroller_internal_models.bus” unless you want to add more subcircuits.
* “microcontroller.bus” is the microcontroller model. It links the pwm and adcs and the inputs from kicad. This one can be modified to add or remove remove pwm channels and adcs or modify delays etc.

The two “.bus” files are non-standard ngspice files. The python script “generate_buses.py” interprets strings of the format “_BUS_NAME_XX” to add the required number of connections as ngspice doesn’t support busses and writing adc1_bus0, adc1_bus1… gets old fast. It outputs “.lib” files.

The controller has a number of files.
* “microcontroller_ngut.c” - no need to touch this one.
* “microcontroller.h” and “microcontroller.c” - these define the interface with ngspice (in_data_message and out_data_params) and set up the environment for the controller function. It reads parameters.p to get the controller parameters.
* “adc.h” has the gains and offsets for the ADCs
* “parameters.p” holds any user modifiable values that you might want to change between runs. Eg the controller gains and the set points. You don’t have to recompile it to make changes here.
* “buck_controller.h” and “buck_controller.c” - these should be exactly the same modules as your controller on the real microcontroller.
* “.compile_all.sh” is a bash script for Linux that will compile the controller and the verilog models (assuming that you have yosys and gcc installed).

As with the previous models (https://forum.kicad.info/t/easier-mixed-mode-simulation-and-real-world-comparison/ and Mixed mode simulation including C code and verilog), you need kicad 8+, ngspice 43+, gcc and yosys if you want to recompile the examples. Run “compile_all.sh” if you’re on a Linux machine. The verilog is already compiled so you don’t strictly need yosys if you don’t want to modify it.

If you modify the microcontroller.bus model then you will also need to run “generate_buses.py” using python to generate the .lib file.

Before you run the example provided, you still need to change the text on the schematic to set the directory that it’s run from. Kicad 8.07 still doesn’t appear to set the directory correctly.

I have still not run it on anything except Ubuntu 24.04. Some modifications were provided by @Briant for Windows but I haven’t tested them.

I also built one of these and it worked just like the simulation model again. I just copied and pasted the buck_converter.c and buck_converter.h and parameter files into the microcontroller code.

Any questions, feel free to ask.

Again, a big thanks to @uros, @holger and @Briant for their work making this possible.

3 Likes

Great use case, thanks for posting it. Works well on my Linux, just needed to recompile the bin due to the libc miss-match.

Regarding the “current directory” issue; for the moment I simply invoke the kicad from the project folder instead of clicking on the desktop icon; and then “cd” can be omitted in the sch to keep project clean from absolute paths.

The issue with the working directory should be solved in 9.0 (unless the devs change their mind). Kicad will then always use the project directory as the working directory.