Elliot
Hello
I'm a beginner in the area of PLC programming and embeded system, I want to know if it's possible to run OpenPLC on my STM32f407-st-discovery board.
I've read a lot in the forum but still confused. How can I get it work?
1. Should I run OpenPLC runtime on Windows PC and use my STM32 board as a slave device ?(and how?)
2. Or should I upload the OpenPLC to the board and run as the microcontroller way? (still how?)
I'm really new to this and hope you give me some advice and I'll appreciate it.
Thanks a lot!
Elliot
Reply 0 0
Elliot
kinsa wrote:

There was a problem with the platform.ini file.

Delete the openplc dir and run openplc.bat again. Make sure you save first whatever programs you have stored in that folder.


Uploader failed to start again.😢
I'm wondering do I follow the right steps? openplc.bat -> write a simple program -> click the upload button -> uploader start failed
when should I modify platform.ini?
Reply 0 0
kinsa
Delete everything and start all over again.

Test first if the uploader works for you by downloading this installer:

https://github.com/kinsamanka/openplc-uploader/raw/master/scripts/start.bat

Make sure you have selected a board before closing the program.

If that works, then download the complete set again:

https://github.com/kinsamanka/OpenPLC_Editor/raw/main/scripts/openplc.bat

Make sure you run it on a different folder.

When the uploader first runs, make sure to select a board. Otherwise you will need to delete the ".cfg" file (This bug will be fixed later).
Reply 0 0
Elliot
@kinsa Thank you for your reply !
I've followed your guidance and the uploader runs successfully.
However, I've met some new problems.
1. I change platform.ini for my board and the uploader can not build successfully unless I delete "board_build.core = maple" (and only after this the button below change from "compile" to "upload"
2. Since my board supports stlink-v2, I want to use stlink protocol to upload, however, it does not work.
3. Then I try to use serial port to upload but the uploader shows an error "Fail to init device"
Reply 0 0
kinsa
You have two options:
  1. Port the platformio.ini settings from your VSCode project
  2. Port my platformio.ini settings to your project.
This is the part where you will be on your own as I don't have the hardware that you have.

If you are comfortable with cmd line, the command to upload would be "pio run -e <your_env> -t upload".
Reply 0 0
kinsa
BTW, the GUI uploader app will not work with your board as it assumes that you will be uploading using the serial port. Since you are using stlink-v2, you will have to modify the GUI to allow uploading without a selected serial port.

Otherwise invoking platformio using the command line will be your only option.
Reply 0 0
kinsa
I've updated the uploader to use the default upload protocol defined in the the platformio.ini file. Just select "Default" for the Port.

This is an example for the blue pill board when using stlink-v2:

[env:bluepill]
board_hw = stm32
board_name = STM32-F103CB (Bluepill)
platform = ststm32
board = bluepill_f103c8
upload_protocol = stlink
build_flags =
    ${env.build_flags}
    -D BOARD_STM32
Reply 0 0
Elliot
@kinsa thank you soooooo much for your help and your patience!
I can finally run my OpenPLC demo on my STM32 board!😭😭😭
Reply 0 0
Elliot
@kinsa Hi kinsa your work help me a lot in the past months.
But now I've met some new problems.
I wonder how can I use my STM32 as a modbus slave device, I notice that you've added the modbus settings in your uploader but it doesn't work when I enable it and select a EN_pin, the uploader just report an error of undefined code.
I'll appreciate it if you can give me some help.😉
Reply 0 0
kinsa
If you are using RS485, you will need to define a MACRO that returns true if the TX has completed sending the data.
The following is the code for the AVR boards:
#if defined(__AVR_ATmega32U4__)
#define UART_TX_COMPLETE            (UCSR1A & (1 << TXC1))
#elif defined(__AVR_ATmega328P__)
#define UART_TX_COMPLETE            (UCSR0A & (1 << TXC0))
#endif


Try adding the following line on your build_flags:
-D UART_TX_COMPLETE="(Serialn.flush(), 1)"

where "n" is the serial port number (i.e., Serial2 for port 2).
Note that this is not the optimal solution as the test should be non-blocking
Reply 0 0
Elliot
@kinsa Thank you soooooooo much for your guidance, I've successfully get my board work as modbus slave device.
Actually I just add the line in my build_flags as you said.
But I still wonder what the "optimal solution" is, since I don't understand what "non-blocking" means and how to define a MACRO for my board.
Could you please give me some more explanation? Thank you again! 😉
Reply 0 0
kinsa
Try the following (I've not tested though):

Add the following line on serial.cpp:
#include <libmaple/usart.h>


and change the build_flags to:
-D UART_TX_COMPLETE="(Serialn.c_dev()->regs->SR & (1<<USART_SR_TC_BIT))"
Reply 0 0
Reply