Building an Industrial Gateway with CH32V307
Practical guide: 8 serial ports, dual CAN, Gigabit Ethernet, and Modbus TCP/RTU conversion on a $3 RISC-V chip.
Overview
The CH32V307 is WCH's industrial-grade connectivity MCU based on the QingKe V4F RISC-V core at 144MHz. With Gigabit Ethernet MAC, 8 serial ports, dual CAN, and rich analog peripherals, it's purpose-built for industrial gateway applications — all for about $3.
Key Features for Industrial Use
| Core | QingKe V4F (RISC-V) @144MHz with FPU |
| Flash / SRAM | 256KB / 64KB |
| Ethernet | Gigabit MAC (external PHY required) |
| Serial Ports | 3x USART + 5x UART = 8 total |
| CAN | 2x CAN 2.0B Active |
| ADC | 2 units, 16ch, 12-bit |
| DAC | 2 channels, 12-bit |
| USB | USB 2.0 FS OTG |
| Operating Temp | -40C to +85C (industrial) |
Gateway Architecture
A typical industrial gateway needs to:
- Collect data from serial devices (sensors, PLCs, meters)
- Bridge CAN bus networks
- Provide Ethernet uplink to SCADA/cloud
- Convert protocols (Modbus RTU <-> Modbus TCP)
- Monitor analog inputs
The CH32V307 can do ALL of this on a single chip:
- 8 UARTs connect to 8 serial devices simultaneously
- 2 CAN ports bridge two CAN networks
- Gigabit Ethernet provides high-speed uplink
- FreeRTOS + lwIP stack handle TCP/IP and Modbus
- 12-bit ADC monitors analog sensors (4-20mA, 0-10V)
Hardware Design
Ethernet Interface
CH32V307 has a Gigabit MAC but no built-in PHY. Use an external PHY like RTL8211F or use CH32V317 instead which has built-in 10/100M PHY. For Gigabit, connect via RGMII interface.
CAN Interface
Each CAN port needs an external transceiver (TJA1050 or SN65HVD230). Connect CAN_TX/CAN_RX pins to the transceiver, then to the CAN bus.
Serial Isolation
For industrial environments, use optocouplers (6N137) or digital isolators (ADuM1201) on serial and CAN lines for galvanic isolation.
Software Architecture
FreeRTOS Tasks:
Task 1: UART data collection (8 ports)
Task 2: CAN message routing
Task 3: Modbus TCP server (lwIP)
Task 4: Modbus RTU master/slave
Task 5: ADC monitoring
Task 6: Watchdog & diagnostics
Modbus TCP/RTU Bridge
The gateway receives Modbus TCP requests over Ethernet, converts them to Modbus RTU on serial ports, and returns the response. This is a common industrial use case.
// Simplified Modbus TCP -> RTU bridge
void modbus_bridge_task(void *pv) {
while(1) {
// Wait for TCP Modbus request
mbap_header_t *req = wait_tcp_request();
// Forward to appropriate UART (RTU)
uint8_t slave_id = req->unit_id;
uart_send(slave_uart[slave_id], req->data, req->len);
// Wait for RTU response
uint8_t resp[256];
int len = uart_recv(slave_uart[slave_id], resp, 256, 1000);
// Send back over TCP
send_tcp_response(resp, len);
}
}
Performance
| Serial throughput (per port) | Up to 3 Mbps |
| CAN throughput | Up to 1 Mbps (CAN 2.0B) |
| Ethernet throughput | Up to 100 Mbps (with external PHY) |
| Modbus transactions/s | ~1000+ (depending on slave response) |
| Power consumption | ~45uA/MHz (run mode) |
Why CH32V307 vs STM32?
| Feature | CH32V307 | STM32F407 |
|---|---|---|
| Price | ~$3 | ~$8 |
| Core | RISC-V V4F @144MHz | ARM Cortex-M4 @168MHz |
| Serial Ports | 8 (3 USART + 5 UART) | 6 (3 USART + 3 UART) |
| CAN | 2 | 2 |
| Ethernet MAC | Gigabit | 10/100M |
| FPU | Single precision | Single precision |
| Flash/SRAM | 256KB/64KB | 1MB/192KB |
| ISA License | Open (RISC-V) | Proprietary (ARM) |