/*
 * Copyright 2020-2025 Yuntu Microelectronics Co., Ltd.
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 * 
 * @file flash_ecc_fault.s
 * @brief 
 * 
 */
    .syntax unified
    .thumb
    .align 2
    .thumb_func
    .global FLASH_ECC_Fault_Handler
    .type   FLASH_ECC_Fault_Handler, %function
    .extern FLASH_ECC_Fault_Handler_C

FLASH_ECC_Fault_Handler:
    movs    r0, #4                          @ Load immediate 4 into R0 to test LR bit[2]
    mov     r1, lr                          @ Copy Link Register (LR) into R1
    tst     r1, r0                          @ Test if bit2 of LR (EXC_RETURN[2]) is set
    bne     get_psp                         @ If bit2 set, exception used PSP -> branch to get_psp
    mrs     r0, msp                         @ Otherwise, read Main Stack Pointer (MSP) into R0
    b       c_handler                       @ Branch to call the C-level fault handler
get_psp:
    mrs     r0, psp                         @ Read Process Stack Pointer (PSP) into R0
c_handler:
    ldr     r2, =FLASH_ECC_Fault_Handler_C  @ Load address of C handler into R2
    bx      r2                              @ Branch to address in R2 (call C handler)
