Author Topic: ch32v307 Modbus + Web UI firmware  (Read 7508 times)

0 Members and 1 Guest are viewing this topic.

Online telluriumTopic starter

  • Regular Contributor
  • *
  • Posts: 229
  • Country: ua
ch32v307 Modbus + Web UI firmware
« on: November 27, 2023, 09:43:55 pm »
This thread is dedicated to the CH32V307 firmware that implements Modbus master and Web UI, built with Mongoose Libary (https://github.com/cesanta/mongoose/)

Steps to build & try (on Mac, Windows, Linux):
1. Setup build environment: https://mongoose.ws/documentation/tutorials/tools/ , most notably - symlink support for git & make
2. Install riscv toolchain, e.g. on Mac `brew install riscv-gnu-toolchain`
3. clone mongoose repo: git clone https://github.com/cesanta/mongoose/
4. Go to examples/wch/ch32v307-make-baremetal-builtin, start command prompt/terminal, run "make"
5. Attach serial console to USART1: TX pin A9, baud rate 115200. Use serial converter
6. Observe logs, look at the obtained IP address (see below for logs example)
7. Point your browser at that IP address

Example console output:

412    2 net_builtin.c:197:onstatechang READY, IP: 192.168.2.78
417    2 net_builtin.c:198:onstatechang        GW: 192.168.2.1
41d    2 net_builtin.c:199:onstatechang       MAC: 02:38:9a:a3:39:e3
423    2 main.c:107:main                Initialising application...
429    3 net.c:195:mg_listen            1 0 http://0.0.0.0
42e    2 main.c:110:main                Starting event loop
7dd    2 main.c:35:timer_fn             Ethernet: ready, IP: 192.168.2.78, rx:4, tx:3, dr:0, er:0
bc5    2 main.c:35:timer_fn             Ethernet: ready, IP: 192.168.2.78, rx:5, tx:3, dr:0, er:0
fad    2 main.c:35:timer_fn             Ethernet: ready, IP: 192.168.2.78, rx:5, tx:3, dr:0, er:0


Current firmware size: ~90k on flash.
Screenshot attached.

Note: this firmware can be also run on a Windows/Linux/Mac workstation: cd examples/modbus-dashboard && make

The MQTT functionality is not yet working.
Saving configuration persistently on a flash is not yet working either.

Feedback is welcome, please let me know what features you'd like to see.
« Last Edit: December 27, 2023, 10:59:02 pm by tellurium »
Open source embedded network library https://mongoose.ws
TCP/IP stack + TLS1.3 + HTTP/WebSocket/MQTT in a single file
 

Offline ali_asadzadeh

  • Super Contributor
  • ***
  • Posts: 1905
  • Country: ca
Re: ch32v307 Modbus + Web UI firmware
« Reply #1 on: November 28, 2023, 09:26:37 am »
thanks tellurium for sharing,
But I can not compile the code on win11, I will get these errors

Quote
riscv-none-elf-gcc main.c mongoose.c vendor/system_ch32v30x.c vendor/startup_ch32v30x_D8C.S -W -Wall -Wextra -Wno-error -Wundef -Wshadow -Wdouble-promotion -Wformat-truncation -fno-common -Wconversion -Wno-sign-conversion -ffunction-sections -fdata-sections  -march=rv32imafc -mabi=ilp32f  -DSYSCLK_FREQ_144MHz_HSE -I. -Ivendor -g3 -Os  -T vendor/link.ld -nostartfiles --specs=nano.specs  -lc -lgcc -Wl,--gc-sections  -o firmware.elf
Cannot create temporary file in C:\Windows\: Permission denied
make: *** [Makefile:25: firmware.elf] Error 127
ASiDesigner, Stands for Application specific intelligent devices
I'm a Digital Expert from 8-bits to 64-bits
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1988
  • Country: dk
Re: ch32v307 Modbus + Web UI firmware
« Reply #2 on: November 28, 2023, 09:52:45 am »
thanks tellurium for sharing,
But I can not compile the code on win11, I will get these errors

Quote
riscv-none-elf-gcc main.c mongoose.c vendor/system_ch32v30x.c vendor/startup_ch32v30x_D8C.S -W -Wall -Wextra -Wno-error -Wundef -Wshadow -Wdouble-promotion -Wformat-truncation -fno-common -Wconversion -Wno-sign-conversion -ffunction-sections -fdata-sections  -march=rv32imafc -mabi=ilp32f  -DSYSCLK_FREQ_144MHz_HSE -I. -Ivendor -g3 -Os  -T vendor/link.ld -nostartfiles --specs=nano.specs  -lc -lgcc -Wl,--gc-sections  -o firmware.elf
Cannot create temporary file in C:\Windows\: Permission denied
make: *** [Makefile:25: firmware.elf] Error 127

Didn't the answer in the other thread solve it ?
https://www.eevblog.com/forum/microcontrollers/ch32v307-risc-v-minicore-with-ethernet/msg5190546/#msg5190546

 

Online telluriumTopic starter

  • Regular Contributor
  • *
  • Posts: 229
  • Country: ua
Re: ch32v307 Modbus + Web UI firmware
« Reply #3 on: November 28, 2023, 10:32:56 am »
thanks tellurium for sharing,
But I can not compile the code on win11, I will get these errors

Yeah, as bingo600 mentioned, try a suggested fix: create a directory c:\temp and set a system-wide environment variable TMP=c:\temp

I am attaching .bin and .elf files of the latest firmware to the first post.
Open source embedded network library https://mongoose.ws
TCP/IP stack + TLS1.3 + HTTP/WebSocket/MQTT in a single file
 

Offline ali_asadzadeh

  • Super Contributor
  • ***
  • Posts: 1905
  • Country: ca
Re: ch32v307 Modbus + Web UI firmware
« Reply #4 on: November 28, 2023, 02:27:29 pm »
I have done it, Still no luck :'(


Quote
make
riscv-none-elf-gcc main.c mongoose.c vendor/system_ch32v30x.c vendor/startup_ch32v30x_D8C.S -W -Wall -Wextra -Wno-error -Wundef -Wshadow -Wdouble-promotion -Wformat-truncation -fno-common -Wconversion -Wno-sign-conversion -ffunction-sections -fdata-sections  -march=rv32imafc -mabi=ilp32f  -DSYSCLK_FREQ_144MHz_HSE -I. -Ivendor -g3 -Os  -T vendor/link.ld -nostartfiles --specs=nano.specs  -lc -lgcc -Wl,--gc-sections  -o firmware.elf
Cannot create temporary file in C:\Windows\: Permission denied
make: *** [Makefile:25: firmware.elf] Error 127

ASiDesigner, Stands for Application specific intelligent devices
I'm a Digital Expert from 8-bits to 64-bits
 

Online ejeffrey

  • Super Contributor
  • ***
  • Posts: 3719
  • Country: us
Re: ch32v307 Modbus + Web UI firmware
« Reply #5 on: November 28, 2023, 02:43:56 pm »
Try changing TEMP and TMPDIR as well.  Also check at the command line that the variables are set as you expect, windows has both system and user environment variables with the latter overriding the former.  Finally check that you can write files in the temp directory chosen.
 

Offline martinribelotta

  • Regular Contributor
  • *
  • Posts: 56
  • Country: ar
  • A camel is a horse designed by a committee
    • Martin Ribelotta design services
Re: ch32v307 Modbus + Web UI firmware
« Reply #6 on: November 28, 2023, 03:04:55 pm »
Quote
I have done it, Still no luck :'(

Ho! I see the problem, you don't have administrator permissions and the GCC are trying to write to C:\WINDOWS\TMP.

Change TEMP to directory that you can write as a normal user (I think that TMP and TEMP environment vars must be contains the same value)
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1988
  • Country: dk
Re: ch32v307 Modbus + Web UI firmware
« Reply #7 on: November 28, 2023, 03:19:25 pm »
According to the gcc docs
https://gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html

Setting env var : TMPDIR
Would be the one to set for GCC

As already suggested above

But i do agree that i would set all the "TMP" env vars to ie. C:\TMP or C:\TEMP
Not : C:\WINDOWS\TMP
« Last Edit: November 28, 2023, 03:22:34 pm by bingo600 »
 

Offline ali_asadzadeh

  • Super Contributor
  • ***
  • Posts: 1905
  • Country: ca
Re: ch32v307 Modbus + Web UI firmware
« Reply #8 on: November 28, 2023, 03:31:14 pm »
Still the same problem |O |O
ASiDesigner, Stands for Application specific intelligent devices
I'm a Digital Expert from 8-bits to 64-bits
 

Offline martinribelotta

  • Regular Contributor
  • *
  • Posts: 56
  • Country: ar
  • A camel is a horse designed by a committee
    • Martin Ribelotta design services
Re: ch32v307 Modbus + Web UI firmware
« Reply #9 on: November 28, 2023, 03:35:54 pm »
Quote
Still the same problem |O |O

Try to execute
Code: [Select]
echo $TMPDIR
echo $TEMP
echo $TMP

In MinGW terminal... maybe the /etc/profile has another configuration overwriting the system one.
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1988
  • Country: dk
Re: ch32v307 Modbus + Web UI firmware
« Reply #10 on: November 29, 2023, 07:52:33 pm »
@tellurium
Jyst FYI ...

I tried mongoose today, and was scratching my head a lot ... Using linux mint
It didn't work.

I have this board & programmer
https://www.aliexpress.com/item/1005005021450761.html
CH32V307 EVT (by SCDZ, close to official W.CH board)

I tried a "blinky" ... worked
And then i discovered that the programmer also registers as /dev/ttyACM0
I fired up putty @115200 on ttyACM0

When programmed i see this
Code: [Select]
0      2 main.c:88:main                 Starting, CPU freq 144 MHz
6      2 main.c:102:main                MAC: 02:38:32:38:39:e3. Waiting for IP...
3f5    2 main.c:35:timer_fn             Ethernet: down, IP: 0.0.0.0, rx:0, tx:0, dr:0, er:0
7dd    2 main.c:35:timer_fn             Ethernet: down, IP: 0.0.0.0, rx:0, tx:0, dr:0, er:0
bc5    2 main.c:35:timer_fn             Ethernet: down, IP: 0.0.0.0, rx:0, tx:0, dr:0, er:0
fad    2 main.c:35:timer_fn             Ethernet: down, IP: 0.0.0.0, rx:0, tx:0, dr:0, er:0
1395   2 main.c:35:timer_fn             Ethernet: down, IP: 0.0.0.0, rx:0, tx:0, dr:0, er:0
177d   2 main.c:35:timer_fn             Ethernet: down, IP: 0.0.0.0, rx:0, tx:0, dr:0, er:0
1b65   2 main.c:35:timer_fn             Ethernet: down, IP: 0.0.0.0, rx:0, tx:0, dr:0, er:0
1f4d   2 main.c:35:timer_fn             Ethernet: down, IP: 0.0.0.0, rx:0, tx:0, dr:0, er:0
2335   2 main.c:35:timer_fn             Ethernet: down, IP: 0.0.0.0, rx:0, tx:0, dr:0, er:0
271d   2 main.c:35:timer_fn             Ethernet: down, IP: 0.0.0.0, rx:0, tx:0, dr:0, er:0
2b05   2 main.c:35:timer_fn             Ethernet: down, IP: 0.0.0.0, rx:0, tx:0, dr:0, er:0
2eed   2 main.c:35:timer_fn             Ethernet: down, IP: 0.0.0.0, rx:0, tx:0, dr:0, er:0
32d5   2 main.c:35:timer_fn             Ethernet: down, IP: 0.0.0.0, rx:0, tx:0, dr:0, er:0
36bd   2 main.c:35:timer_fn             Ethernet: down, IP: 0.0.0.0, rx:0, tx:0, dr:0, er:0
3aa5   2 main.c:35:timer_fn             Ethernet: down, IP: 0.0.0.0, rx:0, tx:0, dr:0, er:0
3e8d   2 main.c:35:timer_fn             Ethernet: down, IP: 0.0.0.0, rx:0, tx:0, dr:0, er:0
4275   2 main.c:35:timer_fn             Ethernet: down, IP: 0.0.0.0, rx:0, tx:0, dr:0, er:0

Nothing happened besides the above ....

Then i disconnected the Ethernet Jack , and reconnected
And it saw a mac address , and after a few seconds it got an IP

That method works .... Sometimes


What always work , is to press reset when it "doesn't see (show) the ethernet MAC.

Code: [Select]
0      2 main.c:88:main                 Starting, CPU freq 144 MHz
6      2 main.c:102:main                MAC: 02:38:32:38:39:e3. Waiting for IP...
3ee    3 stm32.c:193:mg_tcpip_driver_st Link is 10M half-duplex
3f3    1 net_builtin.c:202:onstatechang Link up
3f7    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
3ff    2 main.c:35:timer_fn             Ethernet: up, IP: 0.0.0.0, rx:0, tx:1, dr:0, er:0
7d6    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
7dd    2 main.c:35:timer_fn             Ethernet: up, IP: 0.0.0.0, rx:0, tx:2, dr:0, er:0
bbe    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
bc5    2 main.c:35:timer_fn             Ethernet: up, IP: 0.0.0.0, rx:0, tx:3, dr:0, er:0
fa6    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
fad    2 main.c:35:timer_fn             Ethernet: up, IP: 0.0.0.0, rx:0, tx:4, dr:0, er:0
138e   3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
1395   2 main.c:35:timer_fn             Ethernet: up, IP: 0.0.0.0, rx:0, tx:5, dr:0, er:0
1776   3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
177d   2 main.c:35:timer_fn             Ethernet: up, IP: 0.0.0.0, rx:1, tx:6, dr:0, er:0
1b5e   3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
1b65   3 net_builtin.c:281:tx_dhcp_requ DHCP req sent
1b6a   2 net_builtin.c:407:rx_dhcp_clie Lease: 6210 sec (6217)
1b70   2 net_builtin.c:197:onstatechang READY, IP: 10.117.10.210
1b75   2 net_builtin.c:198:onstatechang        GW: 10.117.10.1
1b7b   2 net_builtin.c:199:onstatechang       MAC: 02:38:32:38:39:e3
1b81   2 main.c:35:timer_fn             Ethernet: ready, IP: 10.117.10.210, rx:4, tx:9, dr:0, er:0
1b8a   2 main.c:107:main                Initialising application...
1b90   3 net.c:195:mg_listen            1 0 http://0.0.0.0
1b95   2 main.c:110:main                Starting event loop
1f4d   2 main.c:35:timer_fn             Ethernet: ready, IP: 10.117.10.210, rx:4, tx:9, dr:0, er:0



Code: [Select]
1adbd  2 main.c:35:timer_fn             Ethernet: ready, IP: 10.117.10.210, rx:4, tx:8, dr:0, er:0
1b1a5  2 main.c:35:timer_fn             Ethernet: ready, IP: 10.117.10.210, rx:4, tx:8, dr:0, er:0
1b58d  2 main.c:35:timer_fn             Ethernet: ready, IP: 10.117.10.210, rx:4, tx:8, dr:0, er:0
1b975  2 main.c:35:timer_fn             Ethernet: ready, IP: 10.117.10.210, rx:4, tx:8, dr:0, er:0
1bd5d  2 main.c:35:timer_fn             Ethernet: ready, IP: 10.117.10.210, rx:4, tx:8, dr:0, er:0
1c145  2 main.c:35:timer_fn             Ethernet: ready, IP: 10.117.10.210, rx:4, tx:8, dr:0, er:0
0      2 main.c:88:main                 Starting, CPU freq 144 MHz
6      2 main.c:102:main                MAC: 02:38:32:38:39:e3. Waiting for IP...
3ee    3 stm32.c:193:mg_tcpip_driver_st Link is 10M half-duplex
3f3    1 net_builtin.c:202:onstatechang Link up
3f7    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
3ff    2 main.c:35:timer_fn             Ethernet: up, IP: 0.0.0.0, rx:0, tx:1, dr:0, er:0
7d6    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
7dd    2 main.c:35:timer_fn             Ethernet: up, IP: 0.0.0.0, rx:0, tx:2, dr:0, er:0
bbe    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
bc5    2 main.c:35:timer_fn             Ethernet: up, IP: 0.0.0.0, rx:0, tx:3, dr:0, er:0
fa6    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
fad    2 main.c:35:timer_fn             Ethernet: up, IP: 0.0.0.0, rx:0, tx:4, dr:0, er:0
138e   3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
1395   2 main.c:35:timer_fn             Ethernet: up, IP: 0.0.0.0, rx:0, tx:5, dr:0, er:0

I see same issue when doing a Power Reset.
It doesn't see the link-up (Ethernet link) , until i press Reset button.


Does the ethernet link check start too fast ??


Edit:
I have attached my Makefile (had to append .txt - to upload)

It uses the linux (wch clone) programmer : https://github.com/cnlohr/ch32v003fun/tree/master/minichlink
"clone" , build , and copy to "somewhere on path"


Note: There seems to be an issue w minichlink - It resets the chip to 288K/32K  Flash/Ram partitioning
But ATM that's fine for mongoose.

https://blog.dan.drown.org/ch32v307-dev-board-part-5/
Quote
I finally figured out that it was how minichlink was talking with the debugger. If I use wlink, the option bytes don't get overwritten. But wlink leaves my debugger hardware in some unknown state and I have to power cycle it to get the debugger working again. I've opened a github issue, and I want to try reverse engineering the WCH-LinkE protocol to see if I can figure out how to fix this.

There's a hack/fix here
https://www.eevblog.com/forum/microcontrollers/ch32v307-risc-v-minicore-with-ethernet/msg5195862/#msg5195862


Edit2:
ddrown's  lwip demo also needs an extra button reset to get an ip.
So it could be board related ... I'm using a HP 1820 switch, and have no issues w. that

/Bingo
« Last Edit: November 30, 2023, 04:48:00 am by bingo600 »
 

Offline ali_asadzadeh

  • Super Contributor
  • ***
  • Posts: 1905
  • Country: ca
Re: ch32v307 Modbus + Web UI firmware
« Reply #11 on: November 30, 2023, 08:44:35 am »
Quote
Try to execute
Code: [Select]
echo $TMPDIR
echo $TEMP
echo $TMP

I have tired, here is the results
ASiDesigner, Stands for Application specific intelligent devices
I'm a Digital Expert from 8-bits to 64-bits
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14471
  • Country: fr
Re: ch32v307 Modbus + Web UI firmware
« Reply #12 on: November 30, 2023, 09:00:24 am »
I guess you haven't tried adding the '-pipe' option to CFLAGS as I have suggested earlier, to circumvent this temp files altogether?
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1988
  • Country: dk
Re: ch32v307 Modbus + Web UI firmware
« Reply #13 on: November 30, 2023, 10:29:05 am »
Quote
Try to execute
Code: [Select]
echo $TMPDIR
echo $TEMP
echo $TMP

I have tired, here is the results

Why doesn't $TMPDIR point at the same as $TMP
Does c:\temp exist
 

Online telluriumTopic starter

  • Regular Contributor
  • *
  • Posts: 229
  • Country: ua
Re: ch32v307 Modbus + Web UI firmware
« Reply #14 on: November 30, 2023, 11:30:51 am »
I have this board & programmer
https://www.aliexpress.com/item/1005005021450761.html
CH32V307 EVT (by SCDZ, close to official W.CH board)

I don't have that board, ordered and expecting it to arrive in a couple of weeks.

I use this one:
https://www.aliexpress.com/item/1005004350410929.html

I don't know who makes those boards. They are set to 32k RAM / 288K flash by default.
To reprogram it, I press boot + reset buttons, then use "wchisp flash" command. The wchisp is this one: https://github.com/ch32-rs/wchisp

This board always picks up and IP from DHCP , I haven't seen an issue that you described. Will test on the WCH's board when it arrives.


Option byte notes:

I would like to set the board to the 96k RAM / 224k flash configuration, but unfortunately wchisp does not yet know how to set option bytes.
I can do in in the firmware code, but it needs a bootstrapping phase:
1. Add a code to the firmware that checks option byte to 96/224
2. Build a firmware with 32/288 linker script, flash & run it. This is necessary cause running a 96/224 linked firmware won't work if chip is configured to 32/288
3. Rebuild the firmware wirh 96/224 linker script, and flash it again: now chip should be 96/224

That works, I've tried that, but it is kinda cumbersome and confusing. I wish option bytes can be set by a utility before flashing. Maybe wchlink utility can do it, I don't have one yet, will test when it arrives.
Open source embedded network library https://mongoose.ws
TCP/IP stack + TLS1.3 + HTTP/WebSocket/MQTT in a single file
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1988
  • Country: dk
Re: ch32v307 Modbus + Web UI firmware
« Reply #15 on: November 30, 2023, 02:51:14 pm »
I wish option bytes can be set by a utility before flashing. Maybe wchlink utility can do it, I don't have one yet, will test when it arrives.

I just hacked Ram partitioning into minichlink

See
https://www.eevblog.com/forum/microcontrollers/ch32v307-risc-v-minicore-with-ethernet/msg5197074/#msg5197074


Code: [Select]
mongose/mongoose/examples/wch/ch32v307-make-baremetal-builtin$ make flash
minichlink -K 96 -a -w firmware.bin 0x08000000 -b
Found WCH Link
WCH Programmer is CH549 version 2.10
Chip Type: 307
CH32V30x or CH32V20x MCU detected. Using binary blob write for operation.
Setup success
Flash Storage: 288 kB
Part UUID    : 26-cb-d9-2c-38-32-38-3b
PFlags       : e3-39-e3-39
Part Type (B): 30-70-05-18
Read protection: disabled
Interface Setup
MCU Ram set to 96KB on next boot
Image written.


But my board still requires an extra reset ...

/Bingo
« Last Edit: November 30, 2023, 03:21:38 pm by bingo600 »
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1988
  • Country: dk
Re: ch32v307 Modbus + Web UI firmware
« Reply #16 on: November 30, 2023, 03:26:33 pm »
I don't have that board, ordered and expecting it to arrive in a couple of weeks.

Thanx for the support.

I'm just a "hobbyist"

I have only used the wchlink or wclink-e dongle to program my board.

I have no idea if the rust program supports my board or dongle.
I don't have that addon PCB with the USBC programmer/debugger , just the dongle(s).

I suppose there's no DFU loader on the CHIP USB's ?? ... Like on a STM32F4 ??


/Bingo
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1988
  • Country: dk
Re: ch32v307 Modbus + Web UI firmware
« Reply #17 on: November 30, 2023, 03:58:59 pm »
I just tried to connect USB , and the Boot0 key.
I could program via wchlink wchisp (rust binary) ....

But i still need to do a "button reset" , in order to get an IP.

So it's prob not the programmer.

/Bingo
« Last Edit: December 01, 2023, 04:44:24 am by bingo600 »
 

Online telluriumTopic starter

  • Regular Contributor
  • *
  • Posts: 229
  • Country: ua
Re: ch32v307 Modbus + Web UI firmware
« Reply #18 on: December 01, 2023, 12:20:00 am »
Quick update:

1. Still using 32k ram / 288k flash in the linker file. That should work also for devices configured for larger RAM, just not all RAM would be utilized. I'll try to use Bingo's whlink update to set device config (when I'll get a board with wchlink). If that works, I'll switch the build for larger RAM, perhaps 64/256.

2. Added ability to save configuration on flash permanently - so now it survives reboots. The code uses the sector that follows a mapped 320k area, no wear levelling is done. Attempted to use littlefs on the whole "unmapped" flash region - but littlefs eats too much RAM, so went for a simple sector write instead

3. Followed SiliconWizard's finding about bss. Hand-crafted linker script considering that, with pieces taken from the wch's SDK. Here's current linker script file for the project: https://github.com/cesanta/mongoose/blob/master/examples/wch/ch32v307-make-baremetal-builtin/link.ld

4. Attached an updated binary download to the first post (ch32v307.modbus.firmware.2023-12-01.zip)
Open source embedded network library https://mongoose.ws
TCP/IP stack + TLS1.3 + HTTP/WebSocket/MQTT in a single file
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1988
  • Country: dk
Re: ch32v307 Modbus + Web UI firmware
« Reply #19 on: December 02, 2023, 11:44:04 am »
I just noticed a much different 307 demo now.
And i haven't saved the old source ....


Well strange below

I programmed with the minichlink , that's supposed to set 96K Ram

On 1'st Boot (after being flashed) it shows correct
Code: [Select]
0      2 main.c:100:main                Starting, CPU freq 144 MHz
5      2 main.c:103:main                Heap size: 17440 bytes
b      2 main.c:108:main                RAM/FLASH configuration: 96/224      <-------------------



On Reset (Button) it changed to 32K Ram ....

Code: [Select]
12     2 main.c:100:main                Starting, CPU freq 144 MHz
17     2 main.c:103:main                Heap size: 17440 bytes
1d     2 main.c:108:main                RAM/FLASH configuration: 32/288  <-------------------
24     2 main.c:124:main                MAC: 02:38:32:38:39:e3. Waiting for IP...
40c    3 stm32.c:193:mg_tcpip_driver_st Link is 10M half-duplex
411    1 net_builtin.c:202:onstatechang Link up
415    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
7f4    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
bdc    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
fc4    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
13ac   3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
1794   3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
179b   3 net_builtin.c:281:tx_dhcp_requ DHCP req sent
17a0   2 net_builtin.c:407:rx_dhcp_clie Lease: 6884 sec (6890)
17a6   2 net_builtin.c:197:onstatechang READY, IP: 10.117.10.210
17ab   2 net_builtin.c:198:onstatechang        GW: 10.117.10.1
17b1   2 net_builtin.c:199:onstatechang       MAC: 02:38:32:38:39:e3
17b7   2 main.c:129:main                Initialising application...
17bd   3 net.c:195:mg_listen            1 0 http://0.0.0.0:8000
17c3   3 net.c:195:mg_listen            2 0 http://0.0.0.0/
17c8   3 net.c:195:mg_listen            3 0 https://0.0.0.0/
17cd   2 main.c:133:main                Starting event loop
17d2   3 net.c:171:mg_connect           4 -1 udp://time.google.com:123
17d9   3 net.c:171:mg_connect           5 -1 udp://8.8.8.8:53
17de   3 net_builtin.c:1002:mg_connect_ 5 10.117.10.210:32768 -> 8.8.8.8:53
2392   3 net.c:148:mg_close_conn        4 -1 closed


Wonder if that's the programmer ?
Can you set ram partitioning , so it's not written/saved to flash ???

Well if i comment out :
Code: [Select]
  //if (mode != 3) set_ram_size(2), mg_device_reset();
It will always show
Code: [Select]
b      2 main.c:108:main                RAM/FLASH configuration: 96/224      <-------------------

And i have issues getting an ip (DHCP) .... Also after a button Reset.
The button reset worked 100% on the old vers.

And so did a NVIC_Reset (instead of Button Reset) in the "Timer loop" after 10 loops of "down state" , on the old vers.

/Bingo
« Last Edit: December 02, 2023, 01:31:34 pm by bingo600 »
 

Online telluriumTopic starter

  • Regular Contributor
  • *
  • Posts: 229
  • Country: ua
Re: ch32v307 Modbus + Web UI firmware
« Reply #20 on: December 02, 2023, 02:49:39 pm »
Wonder if that's the programmer ?

Ah, that's just the firrmware code flips the option byte back to 32/388, you figured it out right.
Disable it by commenting out this line:

https://github.com/cesanta/mongoose/blob/7f8cdf1e79f609e834f59f247b69b51bc7444c9e/examples/wch/ch32v307-make-baremetal-builtin/main.c#L109

Then, reprogram the board again to 96/224
And, change the link.ld to 96/224 here:
https://github.com/cesanta/mongoose/blob/7f8cdf1e79f609e834f59f247b69b51bc7444c9e/examples/wch/ch32v307-make-baremetal-builtin/link.ld#L3-L4

Then your board should work on 96/224 configuration.
See what is the log message about heap size, it should be somewhat less than 96k

Without changling link.ld, you're still using little ram, see


17     2 main.c:103:main                Heap size: 17440 bytes


This heap size of ~17k is 32k minus static DMA Ethernet buffers.
« Last Edit: December 02, 2023, 02:55:07 pm by tellurium »
Open source embedded network library https://mongoose.ws
TCP/IP stack + TLS1.3 + HTTP/WebSocket/MQTT in a single file
 
The following users thanked this post: bingo600

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1988
  • Country: dk
Re: ch32v307 Modbus + Web UI firmware
« Reply #21 on: December 02, 2023, 06:10:01 pm »
Hmmm ... You moved the linkerfile (used) from vendor to main dir, But kept the vendor linkerfile ....  ;)
At first I did do the 224/96 adaptation at the beginning but in vendor linker file, and nothing happened ...
I have adapted makefile to vendor ld file again

Now i'm back to : It works after a button reset  :-+

Code: [Select]
▒0      2 main.c:100:main                Starting, CPU freq 144 MHz
5      2 main.c:103:main                Heap size: 82848 bytes
b      2 main.c:108:main                RAM/FLASH configuration: 96/224
12     2 main.c:124:main                MAC: 02:38:32:38:39:e3. Waiting for IP...
0      2 main.c:100:main                Starting, CPU freq 144 MHz
5      2 main.c:103:main                Heap size: 82848 bytes
b      2 main.c:108:main                RAM/FLASH configuration: 96/224
12     2 main.c:124:main                MAC: 02:38:32:38:39:e3. Waiting for IP...
3fa    3 stm32.c:193:mg_tcpip_driver_st Link is 10M half-duplex
3ff    1 net_builtin.c:202:onstatechang Link up
403    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
7e2    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
bca    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
fb2    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
139a   3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
1782   3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
1789   3 net_builtin.c:281:tx_dhcp_requ DHCP req sent
178e   2 net_builtin.c:407:rx_dhcp_clie Lease: 7044 sec (7050)
1794   2 net_builtin.c:197:onstatechang READY, IP: 10.117.10.210
1799   2 net_builtin.c:198:onstatechang        GW: 10.117.10.1
179f   2 net_builtin.c:199:onstatechang       MAC: 02:38:32:38:39:e3
17a5   2 main.c:129:main                Initialising application...
17ab   3 net.c:195:mg_listen            1 0 http://0.0.0.0:8000
17b1   3 net.c:195:mg_listen            2 0 http://0.0.0.0/
17b6   3 net.c:195:mg_listen            3 0 https://0.0.0.0/
17bb   2 main.c:133:main                Starting event loop
17c0   3 net.c:171:mg_connect           4 -1 udp://time.google.com:123
17c7   3 net.c:171:mg_connect           5 -1 udp://8.8.8.8:53
17cc   3 net_builtin.c:1002:mg_connect_ 5 10.117.10.210:32768 -> 8.8.8.8:53



And i did a NVIC_Reset if link was down ...
That got the board to get "link-up" , and a DHCP IP. Wo. any intevention

Code: [Select]
static void timer_fn(void *arg) {
  gpio_toggle(LED_PIN);                                  // Blink LED_PIN
  struct mg_tcpip_if *ifp = arg;                         // And show
  const char *names[] = {"down", "up", "req", "ready"};  // network stats

  if(0 == ifp->state)
  {
    NVIC_SystemReset();
  }
  return;
  MG_INFO(("Ethernet: %s, IP: %M, rx:%u, tx:%u, dr:%u, er:%u",
           names[ifp->state], mg_print_ip4, &ifp->ip, ifp->nrecv, ifp->nsent,
           ifp->ndrop, ifp->nerr));
}


Code: [Select]
0      2 main.c:105:main                Starting, CPU freq 144 MHz            <---------------- Programmer/Flash-done Boot
5      2 main.c:108:main                Heap size: 82848 bytes
b      2 main.c:113:main                RAM/FLASH configuration: 96/224
12     2 main.c:129:main                MAC: 02:38:32:38:39:e3. Waiting for IP...
0      2 main.c:105:main                Starting, CPU freq 144 MHz            <---------------- NVIC_Reset
5      2 main.c:108:main                Heap size: 82848 bytes
b      2 main.c:113:main                RAM/FLASH configuration: 96/224
12     2 main.c:129:main                MAC: 02:38:32:38:39:e3. Waiting for IP...
3fa    3 stm32.c:193:mg_tcpip_driver_st Link is 10M half-duplex
3ff    1 net_builtin.c:202:onstatechang Link up
403    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
7e2    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
bca    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
fb2    3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
139a   3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
1782   3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
1b6a   3 net_builtin.c:302:tx_dhcp_disc DHCP discover sent. Our MAC: 02:38:32:38:39:e3
1b71   3 net_builtin.c:281:tx_dhcp_requ DHCP req sent
1b76   2 net_builtin.c:407:rx_dhcp_clie Lease: 5629 sec (5636)
1b7c   2 net_builtin.c:197:onstatechang READY, IP: 10.117.10.210
1b81   2 net_builtin.c:198:onstatechang        GW: 10.117.10.1
1b87   2 net_builtin.c:199:onstatechang       MAC: 02:38:32:38:39:e3
1b8d   2 main.c:134:main                Initialising application...
1b93   3 net.c:195:mg_listen            1 0 http://0.0.0.0:8000
1b99   3 net.c:195:mg_listen            2 0 http://0.0.0.0/
1b9e   3 net.c:195:mg_listen            3 0 https://0.0.0.0/
1ba3   2 main.c:138:main                Starting event loop
1ba8   3 net.c:171:mg_connect           4 -1 udp://time.google.com:123
1baf   3 net.c:171:mg_connect           5 -1 udp://8.8.8.8:53
1bb4   3 net_builtin.c:1002:mg_connect_ 5 10.117.10.210:32768 -> 8.8.8.8:53
2768   3 net.c:148:mg_close_conn        4 -1 closed



Now it doesn't need the Button Reset

Btw:
Just an hunch.
The Board missing Ethernet link on POR "issue" ....
Maybe it could be power related  (I power via the wchlink dongle) ... If i disconnect the dongle--> board cable for a short while (1 sec or so) , and the reconnect.
It does make the link ... If i disconnect for 5 sec or more , it doesn't.

I'm not an EE , but does the MAC-Jack pull a lot of power to energize ?
Maybe the dongle can't supply on first boot.

I don't have a PSU beside me , but maybe i can solder ?? (isn't there a limit on how much capacitance you can put behind usb ?) ... 10uF or ?? on the board.
I see several of the boards have 10uF before and after the LDO , and other have 10uF before & 2.2uF after.
Judging from the size of my cap's , i think i have 10uF & 2.2uF on mine.





« Last Edit: December 03, 2023, 04:36:36 am by bingo600 »
 

Offline ali_asadzadeh

  • Super Contributor
  • ***
  • Posts: 1905
  • Country: ca
Re: ch32v307 Modbus + Web UI firmware
« Reply #22 on: December 03, 2023, 10:07:52 am »
my ch32v307V-R1-1V0 board has WCH-Link Utility onboard, and Finlay I managed to compile the code, I think the msys2 was conflicting with this new installation

After compiling the code I used the  WCH-LinkUtility to load the bin file and program the board, the uart output is something like

288    2 main.c:100:main                Starting, CPU freq 144 MHz
28d    2 main.c:103:main                Heap size: 82976 bytes
293    2 main.c:108:main                RAM/FLASH configuration: 96/224

I think the board would crash and do not move forward, after plunging and unplugging the board from power, there is no UART output again :o :'( :'(, I should reprogram the board with some examples in mounriver studio to be able to see the UART output again,

what am I doing wrong? is there a way to debug the code?

ASiDesigner, Stands for Application specific intelligent devices
I'm a Digital Expert from 8-bits to 64-bits
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1988
  • Country: dk
Re: ch32v307 Modbus + Web UI firmware
« Reply #23 on: December 03, 2023, 04:37:27 pm »
my ch32v307V-R1-1V0 board has WCH-Link Utility onboard, and Finlay I managed to compile the code, I think the msys2 was conflicting with this new installation

After compiling the code I used the  WCH-LinkUtility to load the bin file and program the board, the uart output is something like

288    2 main.c:100:main                Starting, CPU freq 144 MHz
28d    2 main.c:103:main                Heap size: 82976 bytes
293    2 main.c:108:main                RAM/FLASH configuration: 96/224

I think the board would crash and do not move forward, after plunging and unplugging the board from power, there is no UART output again :o :'( :'(, I should reprogram the board with some examples in mounriver studio to be able to see the UART output again,

what am I doing wrong? is there a way to debug the code?

Your output looks correct to me.

But ....
Did you use the linker file from main-dir or the one from the vendor dir ?
The one from main dir prob. has to be adapted to 96K Ram.

Did you have the Bord LAN Jack connected to a working switchport ?
 
Have you connected the "Blinky led" ?
See LED2 (top of main.c) for port/pin  - That one should/would blink to show mongoose is running.


Could the Board Com-Port have switched to another number ??

« Last Edit: December 03, 2023, 04:45:14 pm by bingo600 »
 

Offline ali_asadzadeh

  • Super Contributor
  • ***
  • Posts: 1905
  • Country: ca
Re: ch32v307 Modbus + Web UI firmware
« Reply #24 on: December 04, 2023, 01:29:29 pm »
Quote
Your output looks correct to me.
It's like yours but it does not print more stuff to the console,

Quote
But ....
Did you use the linker file from main-dir or the one from the vendor dir ?
The one from main dir prob. has to be adapted to 96K Ram.
I have used the linker file with is beside the mian.c file

Quote
Have you connected the "Blinky led" ?
See LED2 (top of main.c) for port/pin  - That one should/would blink to show mongoose is running.
Yes, and it's do not do anything, I think the board would crash after doing some stuff, since I do not know how to debug the code, I can not say what's the buggy function.
ASiDesigner, Stands for Application specific intelligent devices
I'm a Digital Expert from 8-bits to 64-bits
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf