FuSa 8-Bit Libraries Safety Framework
Loading...
Searching...
No Matches
midware_shared_diag_private.c
Go to the documentation of this file.
1
22
23// Framework Includes
24#include <driver_cpuctrl.h>
25#include <driver_errctrl.h>
27
28// Device-specific Includes
29#include <xc.h>
30
31static volatile bool wasGlobalInterruptsEnabled = false;
32static volatile bool isAtomicSectionOngoing = false;
33
35{
36 // Skip execution if Atomic Section is ongoing to avoid overwriting backup
37 if (isAtomicSectionOngoing)
38 {
39 /* cppcheck-suppress misra-c2012-15.5 */
40 return;
41 }
42
43 const uint8_t gieMask = (uint8_t)CPU_I_bm; // Global Interrupt Enable status bit
44 const uint8_t cpuStatusRegVal = CPUCTRL_ReadStatusRegister();
45 wasGlobalInterruptsEnabled = ((cpuStatusRegVal & gieMask) == gieMask);
46
47 CPUCTRL_ClearStatusRegister((uint8_t)CPU_I_bm);
48
49 isAtomicSectionOngoing = true;
50}
51
53{
54 // Skip execution if Atomic Section is not ongoing to avoid restoring old backup
55 if (!isAtomicSectionOngoing)
56 {
57 /* cppcheck-suppress misra-c2012-15.5 */
58 return;
59 }
60
61 isAtomicSectionOngoing = false;
62
63 if (wasGlobalInterruptsEnabled)
64 {
65 CPUCTRL_SetStatusRegister((uint8_t)CPU_I_bm);
66 }
67}
68
69bool IsErrChConfigurable(uint32_t channelMask)
70{
71 // Entering CONFIG state when the error controller is not in NORMAL state results in Bus error
72 const uint8_t stateMask = (uint8_t)ERRCTRL_STATE_gm;
73 const uint8_t normalStateConfig = (uint8_t)ERRCTRL_STATE_NORMAL_gc;
74 const uint8_t regVal = ERRCTRL_ReadControlA();
75 const bool isStateNormal = ((regVal & stateMask) == normalStateConfig);
76
77 // Injecting error on already set channel might mask actual faults
78 const uint32_t chStatus = ERRCTRL_ReadChannelStatus();
79 const bool isErrorChSet = ((chStatus & channelMask) != 0UL);
80
81 return (!isErrorChSet && isStateNormal);
82}
uint8_t CPUCTRL_ReadStatusRegister(void)
Reads the SREG register value.
void CPUCTRL_ClearStatusRegister(uint8_t bitmask)
Clears specific bits in the SREG register.
void CPUCTRL_SetStatusRegister(uint8_t bitmask)
Sets specific bits in the SREG register.
uint8_t ERRCTRL_ReadControlA(void)
Reads the CTRLA register value.
uint32_t ERRCTRL_ReadChannelStatus(void)
Reads the ESF register value.
void AtomicSectionStart(void)
Backup and disable global interrupts.
void AtomicSectionEnd(void)
Restore global interrupts if previously enabled.
bool IsErrChConfigurable(uint32_t channelMask)
Check Error Controller state and error channels indicated by the channel mask input.