arduino / arduino/ArduinoCore-renesas
Add safety check to prevent system crash when calling Serial1.end() without Serial1.begin() on Arduino Uno R4
- Dominant language
- C
- Stars
- 193
- Forks
- 112
- PR merge metrics
- No merged PRs in 30d
Description
## Hardware
- **Boards**: Arduino Uno R4 WiFi
- **MCU**: Renesas RA4M1
- **Core Version**: renesas_uno 1.5.0 & 1.5.1
## Problem
Calling `Serial1.end()` without `Serial1.begin()` causes **Bus Fault** and system crash.
## Reproduce
```cpp
void setup() {
pinMode(13, OUTPUT);
// Serial1.begin(9600); // ← Comment this out
Serial1.end(); // ← Crashes here
}
void loop() {
digitalWrite(13, !digitalRead(13)); // Never executes
delay(500);
}
```
### System Error Information (via USB Serial debug output):
```
16:21:08.586 -> addr: 20007ee4 data: 00009d17
16:21:08.586 -> addr: 20007ee8 data: 000105d8
16:21:08.586 -> addr: 20007eec data: 0000415f
16:21:08.762 -> addr: 20007ef0 data: 000105d8
16:21:08.762 -> addr: 20007ef4 data: 000075d3
16:21:08.762 -> addr: 20007ef8 data: 000075c9
16:21:08.887 -> addr: 20007efc data: 00002599
16:21:08.887 -> ====================================
16:21:08.887 -> =================== Registers information ====================
16:21:09.011 -> R0 : 064770be R1 : e000e100 R2 : 000006e9 R3 : 056a1a10
16:21:09.011 -> R12: 00000002 LR : 00009cd1 PC : 000046fa PSR: 01000000
16:21:09.152 -> ==============================================================
16:21:09.152 -> Bus fault is caused by precise data access violation
16:21:09.293 -> The bus fault occurred address is 056a1a30
16:21:09.293 -> Show more call stack info by run: addr2line -e "C:\Users\Anthony\AppData\Local\arduino\sketches\67C6C2D23C7A2ED22A0DB176A1D0E7F9/DigitalReadSerial.ino".elf -a -f 000046fa 00009cd0 00009d16 0000415e 000075d2 000075c8
Call Stack (By addr2line):
r_sci_uart_transfer_close
/home/pennam/Arduino/hardware/arduino-git/renesas/extras/e2studioProjects/Santiago/Debug/../ra/fsp/src/r_sci_uart/r_sci_uart.c:1505
0x00009cd0
_Z12arduino_mainv
C:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.5.0\cores\arduino/main.cpp:118
0x00009d16
atexit
C:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.5.0\cores\arduino/main.cpp:143
0x0000415e
main
C:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.5.0\variants\UNOWIFIR4\tmp_gen_c_files/main.c:7
0x000075d2
Reset_Handler
/home/pennam/Arduino/hardware/arduino-git/renesas/extras/e2studioProjects/Santiago/Debug/../ra/fsp/src/bsp/cmsis/Device/RENESAS/Source/startup.c:69 (discriminator 1)
0x000075c8
Reset_Handler
/home/pennam/Arduino/hardware/arduino-git/renesas/extras/e2studioProjects/Santiago/Debug/../ra/fsp/src/bsp/cmsis/Device/RENESAS/Source/startup.c:62
```
## Root Cause
`Serial1.end()` calls `R_SCI_UART_Close(&uart_ctrl)` on uninitialized control structure, causing invalid memory access.
## Impact
- **Backward compatibility issue**: Arduino Uno R3 allows this pattern
- **Library compatibility**: Some libraries may call `end()` without `begin()`
## Solution (Tested ✅)
Add safety check in `UART::end()`:
```cpp
void UART::end() {
rxBuffer.clear();
txBuffer.clear();
if (!init_ok) {
return; // Safe exit
}
R_SCI_UART_Close(&uart_ctrl);
init_ok = false;
}
```
## Workaround
Always call `Serial1.begin()` before `Serial1.end()`
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by locating UART::end() and its init_ok state in the renesas_uno core, then inspect the R_SCI_UART_Close call and the r_sci_uart/r_sci_uart.c close path mentioned in the report. Reproduce the sketch with Serial1.begin() commented out and confirm that calling Serial1.end() no longer faults while normal begin/end behavior still works.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- embedded-iot
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100