![]() |
AVR SD Safety Framework 1.0.0
|
Contains API prototypes for the Watchdog Manager. More...
Files | |
| file | midware_watchdog_manager.c |
| Implements APIs for the Watchdog Manager. | |
| file | midware_watchdog_manager.h |
| Contains API prototypes for the Watchdog Manager. | |
| file | midware_watchdog_manager_asm.h |
| Contains assembly API for the Watchdog Manager. | |
| file | midware_watchdog_manager_diag.c |
| Implements APIs for the Watchdog Manager diagnostics. | |
| file | midware_watchdog_manager_diag.h |
| Contains API prototypes for the Watchdog Manager diagnostics. | |
Macros | |
| #define | UINT24_MAX 0x00FFFFFFUL |
| Defines max value for 24-bit registers. | |
| #define | UINT24_MAX 0x00FFFFFFUL |
| Defines max value for 24-bit registers. | |
Enumerations | |
| enum | wdtTimeout_t |
| Defines available timeout configurations for the Watchdog Timer (WDT). More... | |
Functions | |
| errFlag_t | MW_GetSwdtPreclearError (void) |
| Reads the error flag indicating a non-preclear command received while expecting a preclear command in the Synchronous Watchdog Timer (SWDT). | |
| errFlag_t | MW_GetSwdtClearError (void) |
| Reads the error flag indicating a non-clear command received while expecting a clear command in the Synchronous Watchdog Timer (SWDT). | |
| errFlag_t | MW_GetSwdtUnexpectedCmdError (void) |
| Reads the error flag indicating a clear command received in the closed window after a preclear command in the Synchronous Watchdog Timer (SWDT). | |
| errFlag_t | MW_GetSwdtCounterExpiredError (void) |
| Reads the error flag indicating that the Synchronous Watchdog Timer (SWDT) counter expired. | |
| void | MW_ClearSwdtErrors (void) |
| Clears all the error interrupt flags in the Synchronous Watchdog Timer (SWDT). | |
| void | MW_PreClearSwdt (void) |
| Issues a PRECLEAR command to the Synchronous Watchdog Timer (SWDT). | |
| errFlag_t | MW_ClearSwdt (void) |
| Issues a CLEAR command to the Synchronous Watchdog Timer (SWDT). | |
| errFlag_t | MW_SetSwdtResetValue (uint32_t resetVal) |
| Sets the Synchronous Watchdog Timer (SWDT) countdown reset value. | |
| errFlag_t | MW_SetSwdtWindowValue (uint16_t window) |
| Sets the Synchronous Watchdog Timer (SWDT) open window value. | |
| void | MW_EnableSwdtInterrupts (void) |
| Enables all Synchronous Watchdog Timer (SWDT) error interrupts. | |
| errFlag_t | MW_EnableSwdt (bool useCcp, bool useLock, bool useInstrMode) |
| Enables the Synchronous Watchdog Timer (SWDT) with the provided configurations. | |
| void | MW_ClearWdt (void) |
| Clears the Watchdog Timer (WDT) by issuing a Watchdog Reset (WDR) instruction. | |
| errFlag_t | MW_SetWdtTimeout (wdtTimeout_t window, wdtTimeout_t period, bool useLock) |
| Configures and enables the Watchdog Timer (WDT) in either Normal or Window mode. | |
| void | ASM_IssueWdrInstruction (void) |
| Issues a Watchdog Reset instruction (WDR) to clear the WDT using assembly. | |
| errFlag_t | MW_DiagSwdtCount (void) |
| Performs diagnostic to detect faults in the Synchronous Watchdog Timer (SWDT) Counter mechanism. | |
| errFlag_t | MW_DiagSwdtExpire (uint32_t newReset) |
| Performs error injection diagnostic to detect faults in the Synchronous Watchdog Timer (SWDT) Expire mechanism. | |
| void | MW_DiagWdtCountBegin (void) |
| Begins diagnostic to detect faults in the Watchdog Timer (WDT) Counter mechanism. | |
| bool | MW_IsDiagWdtCountOngoing (void) |
| Checks whether the Watchdog Timer (WDT) Count diagnostic is ongoing. | |
| errFlag_t | MW_DiagWdtCountEnd (void) |
| Completes diagnostic to detect faults in the Watchdog Timer (WDT) Counter mechanism. | |
| errFlag_t | MW_DiagWdtExpire (void) |
| Performs error injection diagnostic to detect faults in the Watchdog Timer (WDT) Expire mechanism. | |
Contains API prototypes for the Watchdog Manager.
The Watchdog Manager provides services to Tasks to ensure correct interaction with features of the WDT and SWDT peripherals.
| #define UINT24_MAX 0x00FFFFFFUL |
Defines max value for 24-bit registers.
| #define UINT24_MAX 0x00FFFFFFUL |
Defines max value for 24-bit registers.
| enum wdtTimeout_t |
Defines available timeout configurations for the Watchdog Timer (WDT).
| void ASM_IssueWdrInstruction | ( | void | ) |
Issues a Watchdog Reset instruction (WDR) to clear the WDT using assembly.
| errFlag_t MW_ClearSwdt | ( | void | ) |
Issues a CLEAR command to the Synchronous Watchdog Timer (SWDT).
This function waits until the SWDT is ready to receive a CLEAR command using polling. A PRECLEAR command should be issued by calling MW_PreClearSwdt before this function is called to ensure the correct clear sequence.
The function busy waits until the CLEAR-ready bit in the INTFLAGS register is set. It is assumed that the PRECLEAR command was sent first. Otherwise, the CLEAR-ready bit will not be set and the SWDT will eventually expire. If the SWDT expires while waiting, or it has expired before calling this function, waiting is aborted and an error is returned.
| ERROR | SWDT already expired or expired while waiting for the CLEAR-ready bit. |
| NO_ERROR | CLEAR command issued after exiting the closed window. |
| void MW_ClearSwdtErrors | ( | void | ) |
Clears all the error interrupt flags in the Synchronous Watchdog Timer (SWDT).
This function clears all SWDT interrupt flags. Use MW_GetSwdtPreclearError, MW_GetSwdtClearError, MW_GetSwdtUnexpectedCmdError and MW_GetSwdtCounterExpiredError to read the error flags before they are cleared simultaneously in this function.
| void MW_ClearWdt | ( | void | ) |
Clears the Watchdog Timer (WDT) by issuing a Watchdog Reset (WDR) instruction.
| errFlag_t MW_DiagSwdtCount | ( | void | ) |
Performs diagnostic to detect faults in the Synchronous Watchdog Timer (SWDT) Counter mechanism.
This function reads and compares two samples of the SWDT counter. If the sampled counter values are different, the counter is counting and the diagnostic returns no error.
This diagnostic will not necessarily detect stuck bits in the register, just whether the least significant byte of the counter register is updated as expected.
| ERROR | Fault detected in SWDT count functionality. |
| NO_ERROR | No fault detected in SWDT count functionality. |
| errFlag_t MW_DiagSwdtExpire | ( | uint32_t | newReset | ) |
Performs error injection diagnostic to detect faults in the Synchronous Watchdog Timer (SWDT) Expire mechanism.
This function performs a diagnostic of the SWDT Expire mechanism by starting an error injection and observing the response. The error injection makes the countdown skip to zero, which normally triggers the expire error mechanism. If an expire error is reported, the diagnostic returns no error.
After completing the diagnostic, the SWDT will remain enabled even if it was previously disabled and begins a countdown from the specified 24-bit new reset value right after the injection. If the SWDT is already enabled, the new reset can be set to the current SWDT count when calling the diagnostic, minus a small delta that accounts for the time until the new reset is loaded, effectively restoring the count value and minimizing the impact of the error injection.
If the SWDT register lock feature is enabled, the new reset value cannot be loaded and the diagnostic is aborted.
Error injection will not be initiated if an invalid new reset value is used.
Error injection will not be initiated if the Error Controller is not in NORMAL state or the SWDT error channel is already set.
This function disables global interrupts during its execution to ensure atomic execution and that accidental interrupts are not triggered by initiating the error injection.
| newReset | The 24-bit reset value to be loaded after completing the error injection. The value needs to be larger than zero. |
| ERROR | Fault detected in the SWDT expire mechanism, provided reset value is invalid or SWDT is locked, or error injection did not start. |
| NO_ERROR | No fault detected in the SWDT expire mechanism. |
| void MW_DiagWdtCountBegin | ( | void | ) |
Begins diagnostic to detect faults in the Watchdog Timer (WDT) Counter mechanism.
This function reads and stores the current counter register value of the WDT for later comparison. If the counter register value has not changed when calling MW_DiagWdtCountEnd(), the diagnostic will report an error. After calling this function, MW_IsDiagWdtCountOngoing will return true until MW_DiagWdtCountEnd() is called.
The WDT runs on the internal OSC32K clock source, which is significantly slower than the typical main clock frequency. Since it counts asynchronously to the main clock, it takes up to 4 WDT clock cycles for the readable counter register to update to a new value. For this reason, begin and end diagnostic APIs are provided to avoid busy-waiting for the count to update.
| errFlag_t MW_DiagWdtCountEnd | ( | void | ) |
Completes diagnostic to detect faults in the Watchdog Timer (WDT) Counter mechanism.
This function reads and stores the current counter register value of the WDT and compares it to the previous sample stored when calling MW_DiagWdtCountBegin. If the two samples are identical, it indicates that the WDT counter is stuck and the diagnostic returns an error. If the diagnostic was not started by calling MW_DiagWdtCountBegin, the diagnostic will automatically fail. It is recommended to call MW_IsDiagWdtCountOngoing to check this before calling this function.
The WDT runs on the internal OSC32K clock source, which is significantly slower than the typical main clock frequency. Since it counts asynchronously to the main clock, it takes up to 4 WDT clock cycles for the readable counter register to update to a new value. For this reason, begin and end diagnostic APIs are provided to avoid busy-waiting for the count to update.
It is the responsibility of the calling function to ensure sufficient time separation of at least 4 WDT clock cycles between starting and completing the diagnostic. It is recommended to account for clock source frequency variations.
Global interrupts are disabled during the diagnostic, and restored before returning the result.
| ERROR | WDT Counter is stuck or diagnostic was not started. |
| NO_ERROR | WDT Counter is not stuck. |
| errFlag_t MW_DiagWdtExpire | ( | void | ) |
Performs error injection diagnostic to detect faults in the Watchdog Timer (WDT) Expire mechanism.
This function performs a diagnostic of the WDT Expire reset mechanism by setting the watchdog timeout period to the lowest possible value and waiting for it to expire and reset the device. If the WDT expire reset was not successful, an error is returned.
The diagnostics assumes that the WDT is not enabled (either through fuses or otherwise) when called, and will enable the WDT during execution. If already enabled, a false ERROR may be reported.
After a successful diagnosis, the device is reset. It is the responsibility of the calling function to determine the result of the diagnostic by reading the WDRF flag in the reset controller and the CAUSE bit in the WDT STATUS register.
This diagnostic reads the WDT counter register to determine if the count has passed the configured expire threshold of 8 WDT Clock cycles before returning. Should the counter register be stuck, the diagnostic will never return. For this reason, it is recommended that the Synchronous Watchdog Timer (SWDT) is enabled during execution, which should expire in this scenario and ultimately reset the device through the Error Controller (ERRCTRL).
| ERROR | Fault detected in the WDT expire mechanism. |
| errFlag_t MW_EnableSwdt | ( | bool | useCcp, |
| bool | useLock, | ||
| bool | useInstrMode ) |
Enables the Synchronous Watchdog Timer (SWDT) with the provided configurations.
MW_SetSwdtResetValue and MW_SetSwdtWindowValue must be called to set the Reset and Window values before enabling the SWDT to ensure correct operation.
Once the SWDT is enabled, it can only be disabled by a device reset.
| useCcp | Enable Configuration Change Protection for all SWDT registers |
| useLock | Enable locking of certain SWDT registers, preventing modification of the Reset and Window values, performing error injection and enabling/disabling error interrupts. |
| useInstrMode | Enable counting executed CPU instructions instead of number of main clock cycles. |
| ERROR | SWDT is already locked and cannot be configured properly, SWDT is not enabled. |
| NO_ERROR | SWDT enabled successfully. |
| void MW_EnableSwdtInterrupts | ( | void | ) |
Enables all Synchronous Watchdog Timer (SWDT) error interrupts.
| errFlag_t MW_GetSwdtClearError | ( | void | ) |
Reads the error flag indicating a non-clear command received while expecting a clear command in the Synchronous Watchdog Timer (SWDT).
The flag is cleared by calling MW_ClearSwdtErrors.
| ERROR | Bad clear error flag is set. |
| NO_ERROR | Bad clear error flag is not set. |
| errFlag_t MW_GetSwdtCounterExpiredError | ( | void | ) |
Reads the error flag indicating that the Synchronous Watchdog Timer (SWDT) counter expired.
The flag is cleared by calling MW_ClearSwdtErrors.
| ERROR | Counter expired error flag is set. |
| NO_ERROR | Counter expired error flag is not set. |
| errFlag_t MW_GetSwdtPreclearError | ( | void | ) |
Reads the error flag indicating a non-preclear command received while expecting a preclear command in the Synchronous Watchdog Timer (SWDT).
The flag is cleared by calling MW_ClearSwdtErrors.
| ERROR | Bad preclear error flag is set. |
| NO_ERROR | Bad preclear error flag is not set. |
| errFlag_t MW_GetSwdtUnexpectedCmdError | ( | void | ) |
Reads the error flag indicating a clear command received in the closed window after a preclear command in the Synchronous Watchdog Timer (SWDT).
The flag is cleared by calling MW_ClearSwdtErrors.
| ERROR | Unexpected command error flag is set. |
| NO_ERROR | Unexpected command flag is not set. |
| bool MW_IsDiagWdtCountOngoing | ( | void | ) |
Checks whether the Watchdog Timer (WDT) Count diagnostic is ongoing.
This function returns true if MW_DiagWdtCountBegin was called and MW_DiagWdtCountEnd has not yet been called, and false otherwise. It does not check whether sufficiently long time has passed to complete the diagnostic, just that it has been started.
| true | WDT Count diagnostic is ongoing. |
| false | WDT Count diagnostic is not ongoing. |
| void MW_PreClearSwdt | ( | void | ) |
Issues a PRECLEAR command to the Synchronous Watchdog Timer (SWDT).
A PRECLEAR command must be sent before a CLEAR command to avoid hardware reporting a bad preclear error.
| errFlag_t MW_SetSwdtResetValue | ( | uint32_t | resetVal | ) |
Sets the Synchronous Watchdog Timer (SWDT) countdown reset value.
This function sets the 24-bit reset value that is loaded into the countdown register after each SWDT clear.
The watchdog can only be cleared in the open window, see MW_SetSwdtWindowValue.
| resetVal | The 24-bit reset value to be set which has to be larger than zero. |
| ERROR | Provided reset value is invalid or SWDT is locked, reset value not set. |
| NO_ERROR | Provided reset value successfully set. |
| errFlag_t MW_SetSwdtWindowValue | ( | uint16_t | window | ) |
Sets the Synchronous Watchdog Timer (SWDT) open window value.
This function sets the 16-bit open window value for the SWDT. The SWDT counts down from the configured reset value (see MW_SetSwdtResetValue). While the countdown register value is above the configured window value, the SWDT is in the closed window and a CLEAR command is not accepted. Once the countdown register value reaches the configured window value, the SWDT is in the open window and a CLEAR command can be received after a PRECLEAR command (see MW_PreClearSwdt and MW_ClearSwdt).
| window | The 16-bit open window value to be set. |
| ERROR | Provided window value is set to 0 or SWDT is locked, window value not set. |
| NO_ERROR | Provided window value successfully set. |
| errFlag_t MW_SetWdtTimeout | ( | wdtTimeout_t | window, |
| wdtTimeout_t | period, | ||
| bool | useLock ) |
Configures and enables the Watchdog Timer (WDT) in either Normal or Window mode.
This function enables the WDT by writing the provided window and period values. If the WDT is already enabled, the new values are applied after the next WDT clear. The provided values select either Normal mode, where the WDT can be cleared at any time before it expires, or Window mode (WWDT), where the WDT can only be cleared within the open window. In Window mode, the sum of the window and period values defines the total timeout duration.
If enabled, Window mode is only activated after the first WDR instruction is issued, regardless of whether the WDT was initially enabled or not (see MW_ClearWdt).
If the WDT configuration registers are locked, this function aborts and returns an error. When useLock is enabled, the configuration registers are locked after the write. Setting the period and window values through user fuses automatically enables the register lock feature.
After writing the window and period values, the WDT synchronizes the configuration to the WDT clock domain. Calling this function while synchronization is in progress aborts and returns an error. The caller is expected to avoid requesting WDT reconfiguration more frequently than the hardware can synchronize configuration updates.
| window | Closed window duration in Window mode. Set to WDT_TIMEOUT_OFF to select Normal mode. |
| period | Open window duration in Window mode. Timeout duration in Normal mode. |
| useLock | Enable locking of the WDT configuration registers, preventing further modification of the WDT configuration except through a device reset. |
| ERROR | The WDT is locked, busy synchronizing, or the provided window or period value is invalid, configuration values not written. |
| NO_ERROR | Configuration values written successfully. |