FuSa 8-Bit Libraries Safety Framework
Loading...
Searching...
No Matches
midware_watchdog_manager.c
Go to the documentation of this file.
1
22
23// Standard Library Includes
24#include <stdbool.h>
25#include <stdint.h>
26
27// Framework Includes
28#include <define_error_flags.h>
29#include <driver_swdt.h>
30#include <driver_wdt.h>
33
34// Device-specific Includes
35#include <xc.h>
36
42#define UINT24_MAX 0x00FFFFFFUL
43
45{
46 const uint8_t regVal = SWDT_ReadIntFlags();
47 const uint8_t badpcMask = (uint8_t)SWDT_BADPC_bm;
48 const bool isPreclearError = ((regVal & badpcMask) == badpcMask);
49
50 errFlag_t flag = NO_ERROR;
51 if (isPreclearError)
52 {
53 flag = ERROR;
54 }
55
56 return flag;
57}
58
60{
61 const uint8_t regVal = SWDT_ReadIntFlags();
62 const uint8_t badcMask = (uint8_t)SWDT_BADC_bm;
63 const bool isClearError = ((regVal & badcMask) == badcMask);
64
65 errFlag_t flag = NO_ERROR;
66 if (isClearError)
67 {
68 flag = ERROR;
69 }
70
71 return flag;
72}
73
75{
76 const uint8_t regVal = SWDT_ReadIntFlags();
77 const uint8_t unexpectedCmdMask = (uint8_t)SWDT_UC_bm;
78 const bool isUnexpectedCmdError = ((regVal & unexpectedCmdMask) == unexpectedCmdMask);
79
80 errFlag_t flag = NO_ERROR;
81 if (isUnexpectedCmdError)
82 {
83 flag = ERROR;
84 }
85
86 return flag;
87}
88
90{
91 const uint8_t regVal = SWDT_ReadIntFlags();
92 const uint8_t expiredMask = (uint8_t)SWDT_EXP_bm;
93 const bool isExpiredError = ((regVal & expiredMask) == expiredMask);
94
95 errFlag_t flag = NO_ERROR;
96 if (isExpiredError)
97 {
98 flag = ERROR;
99 }
100
101 return flag;
102}
103
105{
106 // All error flags are cleared by writing the ERROR bit
107 SWDT_WriteIntFlags((uint8_t)SWDT_ERROR_bm);
108}
109
111{
112 /*
113 * Assumes preclear can be received (PRECLEAR bit is set in INTFLAGS register).
114 * If not, an error is reported in hardware, which is the desired behavior.
115 */
116 SWDT_WriteCommand((uint8_t)SWDT_COMMAND_PRECLEAR_gc);
117}
118
119void MW_ClearSwdt(void)
120{
121 bool isClosedWindow = true;
122
123 while (isClosedWindow)
124 {
125 const uint8_t clearReadyMask = (uint8_t)(SWDT_CLEAR_bm);
126
127 uint8_t regVal = SWDT_ReadIntFlags();
128
129 isClosedWindow = ((regVal & clearReadyMask) != clearReadyMask);
130 }
131
132 SWDT_WriteCommand((uint8_t)SWDT_COMMAND_CLEAR_gc);
133}
134
136{
137 const uint32_t maxResetVal = UINT24_MAX;
138 if (resetVal > maxResetVal)
139 {
140 /* cppcheck-suppress misra-c2012-15.5 */
141 return ERROR;
142 }
143
144 if (resetVal == 0U)
145 {
146 /* cppcheck-suppress misra-c2012-15.5 */
147 return ERROR;
148 }
149
150 SWDT_WriteResetValue(resetVal);
151
152 return NO_ERROR;
153}
154
156{
157 if (window == 0U)
158 {
159 /* cppcheck-suppress misra-c2012-15.5 */
160 return ERROR;
161 }
162
163 SWDT_WriteWindowValue(window);
164
165 return NO_ERROR;
166}
167
169{
170 SWDT_WriteIntControl((uint8_t)SWDT_ERROR_bm);
171}
172
173void MW_EnableSwdt(bool useCcp, bool useLock, bool useInstrMode)
174{
175 uint8_t registerConfig = (uint8_t)SWDT_ENABLE_bm;
176
177 if (useCcp)
178 {
179 registerConfig |= (uint8_t)SWDT_CCP_bm;
180 }
181
182 if (useLock)
183 {
184 registerConfig |= (uint8_t)SWDT_LOCK_bm;
185 }
186
187 if (useInstrMode)
188 {
189 registerConfig |= (uint8_t)SWDT_MODE_bm;
190 }
191
192 SWDT_WriteControlA(registerConfig);
193}
194
195void MW_ClearWdt(void)
196{
197 // Watchdog cleared by issuing WDR instruction in assembly
199}
200
202{
203 if (window >= WDT_TIMEOUT_MAX)
204 {
205 /* cppcheck-suppress misra-c2012-15.5 */
206 return ERROR;
207 }
208
209 if (period >= WDT_TIMEOUT_MAX)
210 {
211 /* cppcheck-suppress misra-c2012-15.5 */
212 return ERROR;
213 }
214
215 const uint8_t windowConfig = (uint8_t)window << WDT_WINDOW_gp;
216 const uint8_t periodConfig = (uint8_t)period << WDT_PERIOD_gp;
217 WDT_WriteControlA(windowConfig | periodConfig);
218
219 return NO_ERROR;
220}
Defines error flag type for indicating detected errors in Middleware services.
errFlag_t
Defines the error flag used by Middleware services to indicate error detection.
@ NO_ERROR
uint8_t SWDT_ReadIntFlags(void)
Reads the INTFLAGS register value.
Definition driver_swdt.c:52
void SWDT_WriteIntFlags(uint8_t value)
Overwrites the INTFLAGS register value.
Definition driver_swdt.c:57
void SWDT_WriteIntControl(uint8_t value)
Overwrites the INTCTRL register value.
Definition driver_swdt.c:47
void SWDT_WriteResetValue(uint32_t value)
Overwrites the RESET register value.
Definition driver_swdt.c:67
void SWDT_WriteCommand(uint8_t value)
Overwrites the COMMAND register value.
Definition driver_swdt.c:77
void SWDT_WriteWindowValue(uint16_t value)
Overwrites the WINDOW register value.
Definition driver_swdt.c:72
void SWDT_WriteControlA(uint8_t value)
Overwrites the CTRLA register value.
Definition driver_swdt.c:30
void MW_PreClearSwdt(void)
Sends a PRECLEAR command to the Synchronous Watchdog.
errFlag_t MW_SetWdtTimeout(wdtTimeout_t window, wdtTimeout_t period)
Configures and enables the Watchdog Timer in either Normal or Window mode.
void MW_ClearSwdt(void)
Sends a CLEAR command to the Synchronous Watchdog.
#define UINT24_MAX
Defines max value for 24-bit registers.
errFlag_t MW_GetSwdtClearError(void)
Reads the error flag indicating a non-clear command received while expecting a clear command in the S...
void MW_EnableSwdt(bool useCcp, bool useLock, bool useInstrMode)
Enables the Synchronous Watchdog Timer with the provided configurations.
errFlag_t MW_GetSwdtCounterExpiredError(void)
Reads the error flag indicating that the Synchronous Watchdog counter expired.
wdtTimeout_t
Defines available timeout configurations for the Watchdog Timer.
errFlag_t MW_SetSwdtResetValue(uint32_t resetVal)
Sets the Synchronous Watchdog Timer countdown reset value.
void MW_EnableSwdtInterrupts(void)
Enables all Synchronous Watchdog error interrupts.
errFlag_t MW_GetSwdtPreclearError(void)
Reads the error flag indicating a non-preclear command received while expecting a preclear command in...
void MW_ClearWdt(void)
Clears the Watchdog Timer by issuing a Watchdog Reset (WDR) instruction.
void ASM_IssueWdrInstruction(void)
Issues a Watchdog Reset instruction (WDR) to clear the WDT using assembly.
errFlag_t MW_SetSwdtWindowValue(uint16_t window)
Sets the Synchronous Watchdog Timer open window value.
void MW_ClearSwdtErrors(void)
Clears all the error flags in the Synchronous Watchdog.
errFlag_t MW_GetSwdtUnexpectedCmdError(void)
Reads the error flag indicating a clear command received in the closed window after a preclear comman...
void WDT_WriteControlA(uint8_t value)
Overwrites the CTRLA register value.
Definition driver_wdt.c:33
Contains assembly API for the Watchdog Manager.