Skip to content

Add safety check to prevent system crash when calling Serial1.end() without Serial1.begin() on Arduino Uno R4 #489

@a10036gt

Description

@a10036gt

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

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():

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()

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: codeRelated to content of the project itselftype: enhancementProposed improvement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions