Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion process-node-stm32/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ LDFLAGS := \
-specs=nano.specs \
-specs=nosys.specs

APP_SRCS := main.c rl78emu.c rl78emu_port.c analog.c env.c i2cbus.c i2ctarget.c spi2.c uart3.c bench_cmds.c mcp2515.c syscalls.c sysmem.c system_stm32f4xx.c
APP_SRCS := main.c rl78emu.c rl78emu_port.c modbus.c rs485mock.c analog.c env.c i2cbus.c i2ctarget.c spi2.c uart3.c bench_cmds.c mcp2515.c syscalls.c sysmem.c system_stm32f4xx.c

HAL_SRCS := \
stm32f4xx_hal.c \
Expand Down Expand Up @@ -104,5 +104,7 @@ test-host: | $(BUILD_DIR)
$(BUILD_DIR)/test_proc
$(HOSTCC) -std=c11 -Wall -Wextra -Werror -I. tests/test_rl78emu.c rl78emu.c -o $(BUILD_DIR)/test_rl78emu
$(BUILD_DIR)/test_rl78emu
$(HOSTCC) -std=c11 -Wall -Wextra -Werror -I. tests/test_modbus.c modbus.c -o $(BUILD_DIR)/test_modbus
$(BUILD_DIR)/test_modbus

.PHONY: all clean flash test-host
43 changes: 43 additions & 0 deletions process-node-stm32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,49 @@ verify and checksum, data flash, parameter errors, every baud rate, security, an
afterwards. The protocol core (`rl78emu.c`) is also covered on the host by
`tests/test_rl78emu.c` (`make test-host`).

## RS-485 mock (Modbus RTU)

`modbus start <addr> <baud> [8E1|8N2|...] [delay <us>] [de on|off]` turns USART3 into an RS-485
bus with one Modbus RTU device on it, as a half-duplex transceiver presents it to the pod. It
uses the wires the UART sensors use, plus the LA6 data-ready wire as the transceiver's DE:

| Transceiver | Pod | Node |
| --- | --- | --- |
| DI (the pod sends) | LA8 | PC11, USART3 RX |
| RO (the pod listens) | LA7 | PC10, USART3 TX |
| DE + /RE | LA6 (GPIO) | PA8, through 1 kOhm (strobe off) |

- The device: holding registers 0-63 (0 at start), input registers 0-15 (1000H + index), coils
0-63; functions 01, 03, 04, 05, 06, 08 (return query data), 0F and 10; exceptions 01, 02 and
03; broadcasts (address 0) are carried out and not answered; a bad CRC or another address gets
no answer. `modbus reg <i> [<v>]` reads or sets a holding register from the console.
- Frames end after 3.5 characters of silence (1750 us above 19200 baud); a gap of more than 1.5
characters, or a framing or parity error, drops the frame. The device answers `delay` us after
the request (default 5000), never sooner than 3.5 characters.
- With `de on` the transceiver's rules apply: a byte the pod sends while DE is low never reaches
the device (`undriven`), and a reply byte while DE is still high never reaches the pod
(`collisions`), as the pod's receiver is off then. `de off` is a transceiver with automatic
direction control: no DE wire, only the protocol is tested.
- `modbus stat` reports the requests, replies, exceptions, CRC and frame errors, other addresses,
broadcasts, `undriven` bytes and `collisions`.

`tests/modbus_master.py` is a Modbus RTU master over the pod, and `tests/test_rs485.py` (marker
`rs485`, BENCH_NEW_WIRING=1 BENCH_LA6_RESISTOR=1) runs it: registers, coils, exceptions, the silent
cases, four frame formats, a parity mismatch, DE never raised, released too early and too late,
the docs page's example unchanged, and the turnaround a host-switched DE allows.

Measured on the bench (LAN): switching DE with a GPIO command from the host takes about 27 ms, so
DE drops about 25 ms after the request's last byte. A device answering after 30 ms or more works
every time, after 20 ms about 2 times in 10, after 5 ms never. A device that answers faster needs
the pod to switch DE itself: `open_uart(de=6)` (gateware v58, feature `uart_de`) drives DE from
before the first start bit to the end of the last stop bit, and `test_pod_de` gets every reply at
9600, 19200 and 115200 baud with the device answering 3.5 characters after the request. A
transceiver with automatic direction control (`de off` here) works too.

DE's falling edge is timed by EXTI line 8 on PA8: a byte counts as driven when DE fell within its
stop bit, as a master that drives DE well drops it at the end of the last stop bit, often before
the byte's receive interrupt has run.

## HIL tests

```bash
Expand Down
116 changes: 116 additions & 0 deletions process-node-stm32/bench_cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "i2ctarget.h"
#include "spi2.h"
#include "stm32f4xx_hal.h"
#include "rs485mock.h"
#include "uart3.h"
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -527,6 +528,7 @@ static void nmea_line_done(void)

void bench_service(void)
{
rs485_service();
if (!g_nmea_on)
{
return;
Expand Down Expand Up @@ -972,6 +974,116 @@ static void handle_env(char *args)

/* ---------------------------------------------------------------- entry points */

/* ---------------------------------------------------------------- modbus (RS-485 mock) */

static void print_modbus_stat(void)
{
const rs485_stats_t *st = rs485_stats();
const mb_slave_t *s = rs485_slave();
printf("MODBUS stat on=%u addr=%u baud=%lu format=%s delay_us=%lu de=%u requests=%lu replies=%lu "
"exceptions=%lu crc=%lu frame=%lu other=%lu broadcast=%lu undriven=%lu collisions=%lu "
"last_fn=%02X\r\n",
st->on, s->addr, (unsigned long)st->baud, st->on ? st->format : "-",
(unsigned long)st->delay_us, st->de, (unsigned long)s->requests, (unsigned long)s->replies,
(unsigned long)s->exceptions, (unsigned long)s->crc_errors,
(unsigned long)st->frame_errors, (unsigned long)s->other_addr,
(unsigned long)s->broadcasts, (unsigned long)st->undriven,
(unsigned long)st->collisions, s->last_fn);
}

/* modbus start <addr> <baud> [8N1|8E1|...] [delay <us>] [de on|off] | stop | stat | delay <us> | reg <i> [<v>] */
static void handle_modbus(char *args)
{
char *sub = strtok(args, " ");
if (sub == NULL || strcmp(sub, "stat") == 0)
{
print_modbus_stat();
return;
}
if (strcmp(sub, "start") == 0)
{
uint32_t addr, baud, delay = 5000U, de = 1U;
const char *fmt = "8E1";
int bad = parse_dec(strtok(NULL, " "), &addr, 247U) != 0 ||
parse_dec(strtok(NULL, " "), &baud, 1000000U) != 0;
for (char *t = strtok(NULL, " "); !bad && t != NULL; t = strtok(NULL, " "))
{
if (strcmp(t, "delay") == 0)
{
bad = parse_dec(strtok(NULL, " "), &delay, 100000U) != 0;
}
else if (strcmp(t, "de") == 0)
{
const char *v = strtok(NULL, " ");
bad = v == NULL || (strcmp(v, "on") != 0 && strcmp(v, "off") != 0);
de = (v != NULL && strcmp(v, "on") == 0) ? 1U : 0U;
}
else if (strlen(t) == 3U && t[0] == '8' && (t[2] == '1' || t[2] == '2'))
{
fmt = t;
}
else
{
bad = 1;
}
}
if (!bad && de && !strobe_is_off())
{
printf("MODBUS err PA8 is the strobe: strobe off first (PA8 reads DE)\r\n");
return;
}
if (bad || rs485_start((uint8_t)addr, baud, fmt[1], (uint8_t)(fmt[2] - '0'), delay, (uint8_t)de) != 0)
{
printf("MODBUS err usage: modbus start <1..247> <1200..1000000> [8N1|8E1|8O1|8N2|8E2|8O2] "
"[delay <0..100000 us>] [de on|off]\r\n");
return;
}
printf("MODBUS ok on addr=%lu baud=%lu format=%s delay_us=%lu de=%s\r\n", (unsigned long)addr,
(unsigned long)baud, fmt, (unsigned long)delay, de ? "on" : "off");
return;
}
if (strcmp(sub, "stop") == 0)
{
rs485_stop();
printf("MODBUS ok off\r\n");
return;
}
if (strcmp(sub, "delay") == 0)
{
uint32_t d;
if (parse_dec(strtok(NULL, " "), &d, 100000U) != 0)
{
printf("MODBUS err usage: modbus delay <0..100000 us>\r\n");
return;
}
rs485_set_delay(d);
printf("MODBUS ok delay_us=%lu\r\n", (unsigned long)d);
return;
}
if (strcmp(sub, "reg") == 0)
{
uint32_t i, v;
int bad = parse_dec(strtok(NULL, " "), &i, MB_HOLD_REGS - 1U) != 0;
const char *vs = bad ? NULL : strtok(NULL, " ");
if (!bad && vs != NULL)
{
bad = parse_dec(vs, &v, 0xFFFFU) != 0;
if (!bad)
{
rs485_slave()->hold[i] = (uint16_t)v;
}
}
if (bad)
{
printf("MODBUS err usage: modbus reg <0..63> [<0..65535>]\r\n");
return;
}
printf("MODBUS reg i=%lu v=%u\r\n", (unsigned long)i, rs485_slave()->hold[i]);
return;
}
printf("MODBUS err usage: modbus start|stop|stat|delay|reg\r\n");
}

void bench_init(void)
{
dwt_init();
Expand Down Expand Up @@ -1005,6 +1117,10 @@ int bench_command(char *cmd)
{
handle_uart3(args);
}
else if (is_cmd(cmd, "modbus", &args))
{
handle_modbus(args);
}
else if (is_cmd(cmd, "drdy", &args))
{
handle_drdy(args);
Expand Down
14 changes: 12 additions & 2 deletions process-node-stm32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/

#include "rl78emu.h"
#include "rs485mock.h"
#include "stm32f4xx_hal.h"
#include "alarm.h"
#include "analog.h"
Expand All @@ -44,7 +45,7 @@
#include <string.h>

#define FW_NAME "process-node"
#define FW_VERSION "0.3.1"
#define FW_VERSION "0.3.2"

#define CMD_BUF_SIZE 200

Expand Down Expand Up @@ -843,6 +844,9 @@ static void print_help(void)
printf(" uart3 read [t <ms>] [n <count>] [until <text>] bytes from PC11 (hex and text)\r\n");
printf(" uart3 nmea [on|off] print each NMEA sentence from PC11 with its checksum check\r\n");
printf(" drdy | drdy wait rise|fall|high|low [ms] | drdy count <ms> PA8 data ready (strobe off)\r\n");
printf(" modbus start <addr> <baud> [8E1..] [delay <us>] [de on|off] | stop | stat | delay <us> | reg <i> [<v>]\r\n");
printf(" RS-485 mock: a Modbus RTU device on USART3, DE on PA8 (strobe off;\r\n");
printf(" de off: an automatic-direction transceiver, no DE wire)\r\n");
printf(" status | can status counters, mode, error state\r\n");
printf(" can init [bitrate] reset + configure the MCP2515 (default 500000)\r\n");
printf(" can osc <MHz> module crystal (8 or 16), then re-init\r\n");
Expand Down Expand Up @@ -1419,9 +1423,15 @@ static void SPI1_Init(void)
SPI1->CR1 |= SPI_CR1_SPE;
}

/* Only here to wake the main loop; can_service() reads the INT level. */
/* Wakes the main loop for the MCP2515 (can_service() reads the INT level), and times the RS-485
mock's DE falling edge on PA8 (rs485mock.c). */
void EXTI9_5_IRQHandler(void)
{
if (EXTI->PR & GPIO_PIN_8)
{
EXTI->PR = GPIO_PIN_8;
rs485_de_edge();
}
EXTI->PR = INT_PIN;
}

Expand Down
Loading