Skip to content

Commit 6b0bf75

Browse files
authored
Add tutorial on reading mbed crash logs
1 parent 646783b commit 6b0bf75

File tree

1 file changed

+173
-0
lines changed
  • content/hardware/07.opta/opta-family/opta/tutorials/24.reading-crash-logs-over-rs485

1 file changed

+173
-0
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
---
2+
title: 'Reading Mbed Crash Logs over RS485 on Arduino Opta'
3+
difficulty: advanced
4+
description: "This tutorial explains how to read Mbed crash logs over RS485 on the Arduino Opta. By redirecting the crash logs to the RS485 bus, you can capture and analyze them using another Opta or an RS485 dongle."
5+
tags:
6+
- Opta
7+
- RS485
8+
- Mbed OS
9+
- Debugging
10+
11+
author: 'Sebastian Romero'
12+
hardware:
13+
- hardware/07.opta/opta-family/opta
14+
---
15+
16+
## Introduction
17+
18+
This tutorial shows how to redirect the Mbed crash log output (STDOUT) to the RS485 bus on the **Arduino Opta**.
19+
This feature is available only when using the **Arduino Mbed Core** for Opta.
20+
21+
By default, crash logs and other Mbed debug messages are printed to the hardware serial port, which is not easily accessible on the Opta.
22+
Redirecting STDOUT to RS485 provides a simple and reliable way to capture these logs using another Opta, an RS485 dongle, or an MKR 485 Shield.
23+
24+
---
25+
26+
## Hardware Required
27+
28+
- **Arduino Opta** (Mbed core)
29+
- A second **Opta**, **RS485 USB dongle**, or **Arduino MKR 485 Shield** (for reading RS485 output)
30+
- RS485 wiring (A/B lines)
31+
32+
---
33+
34+
## Circuit
35+
36+
Connect the RS485 lines as follows:
37+
38+
| Opta (sender) | Receiver (Opta / RS485 dongle / MKR 485 Shield) |
39+
|----------------|-------------------------------------------------|
40+
| A | A |
41+
| B | B |
42+
| GND | GND (recommended) |
43+
44+
---
45+
46+
## Example Code
47+
48+
Upload the following example sketch to your **Opta (sender)**:
49+
50+
```cpp
51+
#include "RS485FileHandle.h"
52+
REDIRECT_STDOUT_TO(&RS485Console) // Redirect Mbed crash log output to RS485
53+
54+
void setup() {
55+
// Force a crash to demonstrate the crash log over RS485
56+
volatile int* p = nullptr;
57+
*p = 42; // Dereference null pointer to cause a crash
58+
}
59+
60+
void loop() {
61+
// Nothing to do here
62+
}
63+
```
64+
65+
Once uploaded, the Opta will crash on startup, and the crash log will be sent over the RS485 bus.
66+
67+
---
68+
69+
## Reading the RS485 Output
70+
71+
You can read the crash log output using:
72+
73+
- Another **Opta** running a simple RS485 receiver sketch
74+
- An **RS485 USB dongle** connected to your computer
75+
- An **Arduino MKR 485 Shield** attached to a compatible board
76+
77+
For example, with another Opta, you can run this minimal receiver sketch:
78+
79+
```cpp
80+
#include <ArduinoRS485.h>
81+
82+
void setup() {
83+
Serial.begin(115200);
84+
RS485.begin(115200);
85+
RS485.receive();
86+
Serial.println("Listening for RS485 data...");
87+
}
88+
89+
void loop() {
90+
while (RS485.available()) {
91+
Serial.write(RS485.read());
92+
}
93+
}
94+
```
95+
96+
You should see the raw crash log appear on the serial monitor.
97+
98+
---
99+
100+
## Parsing the Crash Log
101+
102+
The crash log generated by Mbed OS is not human-readable by default. A typical output on the receiver might look like this:
103+
104+
```
105+
+++ MbedOS Fault Handler +++
106+
FaultType: HardFault
107+
Context:
108+
R0 : 00000000
109+
R1 : 0000002A
110+
R2 : 00000000
111+
R3 : 00000000
112+
...
113+
PC : 08007E12
114+
LR : 08006B5B
115+
SP : 2001FFEC
116+
PSR : 61000000
117+
Fault: Null pointer dereference
118+
+++ End Fault Handler +++
119+
```
120+
121+
To interpret it, you can use the **Mbed crash log parser** available here:
122+
[mbed-os-example-fault-handler/crash_log_parser.py](https://github.com/ARMmbed/mbed-os-example-fault-handler/blob/master/crash_log_parser.py)
123+
124+
### 1. Generate a `.map` file
125+
126+
You need to provide the parser with a **map file** from your compiled firmware.
127+
You can generate it using the Arduino CLI:
128+
129+
```bash
130+
arduino-cli compile demo.ino \
131+
--fqbn arduino:mbed_opta:opta \
132+
--board-options "target_core=cm7" \
133+
--board-options "split=100_0" \
134+
--build-path build \
135+
--build-property compiler.cpp.extra_flags="-Og -Wl,-Map,output.map"
136+
```
137+
138+
This produces an `output.map` file in the `build` directory.
139+
140+
### 2. Run the crash log parser
141+
142+
Once you’ve captured the crash log over RS485 (for example, saved to a file named `crash.txt`), you can parse it as follows:
143+
144+
```bash
145+
python3 crash_log_parser.py -m output.map -c crash.txt
146+
```
147+
148+
You’ll get a human-readable report showing the stack trace, fault details, and memory addresses — for example:
149+
150+
```
151+
Crash Info:
152+
Crash location = demo.ino:7 (0x08001234)
153+
Current thread = Id: 0x20001C80
154+
Fault type = Hard Fault
155+
Registers:
156+
R0 = 0x00000000
157+
R1 = 0x0000002A
158+
R2 = 0x20001E40
159+
```
160+
161+
In this example, it indicates that the crash occurred at line 7 in `demo.ino`, which corresponds to the null pointer dereference we introduced.
162+
163+
---
164+
165+
## Summary
166+
167+
Redirecting STDOUT to RS485 on the Arduino Opta allows you to:
168+
169+
- Access otherwise hidden **crash logs** and **debug messages**
170+
- Use standard Mbed functionality for **fault analysis**
171+
- Capture logs easily via **RS485-compatible tools**
172+
173+
This feature is particularly useful for diagnosing issues in field-deployed Opta systems where the serial port is inaccessible.

0 commit comments

Comments
 (0)