FuSa 8-Bit Libraries Safety Framework
Loading...
Searching...
No Matches
midware_power_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_bodctrl.h>
30#include <driver_slpctrl.h>
32
33// Device-specific Includes
34#include <xc.h>
35
37{
38 const uint8_t regVal = BODCTRL_ReadIntFlags();
39 const uint8_t vlmMask = (uint8_t)BOD_VLMIF_bm;
40 const bool isVlmError = ((regVal & vlmMask) == vlmMask);
41
42 errFlag_t flag = NO_ERROR;
43 if (isVlmError)
44 {
45 flag = ERROR;
46 BODCTRL_WriteIntFlags(vlmMask); // The flag is cleared by writing the flag bitmask
47 }
48
49 return flag;
50}
51
53{
54 const uint8_t regVal = SLPCTRL_ReadIntFlags();
55 const uint8_t vovMask = (uint8_t)SLPCTRL_VOV_bm;
56 const bool isVovError = ((regVal & vovMask) == vovMask);
57
58 errFlag_t flag = NO_ERROR;
59 if (isVovError)
60 {
61 flag = ERROR;
62 SLPCTRL_WriteIntFlags(vovMask); // The flag is cleared by writing the flag bitmask
63 }
64
65 return flag;
66}
67
69{
70 const uint8_t regVal = SLPCTRL_ReadIntFlags();
71 const uint8_t vuvMask = (uint8_t)SLPCTRL_VUV_bm;
72 const bool isVuvError = ((regVal & vuvMask) == vuvMask);
73
74 errFlag_t flag = NO_ERROR;
75 if (isVuvError)
76 {
77 flag = ERROR;
78 SLPCTRL_WriteIntFlags(vuvMask); // The flag is cleared by writing the flag bitmask
79 }
80
81 return flag;
82}
83
85{
86 const uint8_t regVal = SLPCTRL_ReadIntFlags();
87 const uint8_t vdenterMask = (uint8_t)SLPCTRL_VDENTER_bm;
88 const bool isVdenterError = ((regVal & vdenterMask) == vdenterMask);
89
90 errFlag_t flag = NO_ERROR;
91 if (isVdenterError)
92 {
93 flag = ERROR;
94 SLPCTRL_WriteIntFlags(vdenterMask); // The flag is cleared by writing the flag bitmask
95 }
96
97 return flag;
98}
99
101{
102 const uint8_t regVal = SLPCTRL_ReadIntFlags();
103 const uint8_t vdexitMask = (uint8_t)SLPCTRL_VDEXIT_bm;
104 const bool isVdexitError = ((regVal & vdexitMask) == vdexitMask);
105
106 errFlag_t flag = NO_ERROR;
107 if (isVdexitError)
108 {
109 flag = ERROR;
110 SLPCTRL_WriteIntFlags(vdexitMask); // The flag is cleared by writing the flag bitmask
111 }
112
113 return flag;
114}
115
117{
118 const uint8_t regVal = SLPCTRL_ReadIntFlags();
119 const uint8_t verrMask = (uint8_t)SLPCTRL_VERR_bm;
120 const bool isVerrError = ((regVal & verrMask) == verrMask);
121
122 errFlag_t flag = NO_ERROR;
123 if (isVerrError)
124 {
125 flag = ERROR;
126 SLPCTRL_WriteIntFlags(verrMask); // The flag is cleared by writing the flag bitmask
127 }
128
129 return flag;
130}
131
133{
134 const uint8_t regVal = SLPCTRL_ReadIntFlags();
135 const uint8_t vslpMask = (uint8_t)SLPCTRL_VSLP_bm;
136 const bool isVslpError = ((regVal & vslpMask) == vslpMask);
137
138 errFlag_t flag = NO_ERROR;
139 if (isVslpError)
140 {
141 flag = ERROR;
142 SLPCTRL_WriteIntFlags(vslpMask); // The flag is cleared by writing the flag bitmask
143 }
144
145 return flag;
146}
147
149{
150 const uint8_t regVal = SLPCTRL_ReadIntFlags();
151 const uint8_t vdisMask = (uint8_t)SLPCTRL_VDIS_bm;
152 const bool isVdisError = ((regVal & vdisMask) == vdisMask);
153
154 errFlag_t flag = NO_ERROR;
155 if (isVdisError)
156 {
157 flag = ERROR;
158 SLPCTRL_WriteIntFlags(vdisMask); // The flag is cleared by writing the flag bitmask
159 }
160
161 return flag;
162}
163
165{
166 const uint8_t regVal = SLPCTRL_ReadIntFlags();
167 const uint8_t serrMask = (uint8_t)SLPCTRL_SERR_bm;
168 const bool isSerrError = ((regVal & serrMask) == serrMask);
169
170 errFlag_t flag = NO_ERROR;
171 if (isSerrError)
172 {
173 flag = ERROR;
174 SLPCTRL_WriteIntFlags(serrMask); // The flag is cleared by writing the flag bitmask
175 }
176
177 return flag;
178}
179
181{
182 const uint8_t bodctrlIntMask = (uint8_t)BOD_VLMIE_bm;
183 const uint8_t slpctrlIntConfig = (uint8_t)SLPCTRL_VOV_bm | (uint8_t)SLPCTRL_VUV_bm
184 | (uint8_t)SLPCTRL_VDENTER_bm | (uint8_t)SLPCTRL_VDEXIT_bm
185 | (uint8_t)SLPCTRL_VERR_bm | (uint8_t)SLPCTRL_VSLP_bm
186 | (uint8_t)SLPCTRL_VDIS_bm | (uint8_t)SLPCTRL_SERR_bm;
187
188 BODCTRL_SetIntControl(bodctrlIntMask);
189 SLPCTRL_WriteIntControl(slpctrlIntConfig);
190}
191
193{
194 const bool outOfRange = (threshold >= VLM_THRESHOLD_MAX) || (trigger >= VLM_TRIGGER_MAX);
195
196 if (outOfRange)
197 {
198 /* cppcheck-suppress misra-c2012-15.5 */
199 return ERROR;
200 }
201
202 const uint8_t regVal = (uint8_t)threshold << BOD_VLMLVL_gp;
204
205 const uint8_t groupConfig = (uint8_t)trigger << BOD_VLMCFG_gp;
206 BODCTRL_ModifyIntControl((uint8_t)BOD_VLMCFG_gm, groupConfig);
207
208 return NO_ERROR;
209}
210
212{
213 if (sleepMode >= SLP_MODE_MAX)
214 {
215 /* cppcheck-suppress misra-c2012-15.5 */
216 return ERROR;
217 }
218
219 const uint8_t groupConfig = (uint8_t)sleepMode << SLPCTRL_SMODE_gp;
220 SLPCTRL_ModifyControlA((uint8_t)SLPCTRL_SMODE_gm, groupConfig);
221
222 return NO_ERROR;
223}
224
225void MW_SetVmonSleep(bool enableInSleep, bool useFullPowerMode)
226{
227 uint8_t registerConfig = (uint8_t)SLPCTRL_PMODE_AUTO_gc;
228
229 if (useFullPowerMode)
230 {
231 registerConfig = (uint8_t)SLPCTRL_PMODE_FULL_gc;
232 }
233
234 if (enableInSleep)
235 {
236 registerConfig |= (uint8_t)SLPCTRL_VMONSEN_bm;
237 }
238
239 SLPCTRL_WriteVregControl(registerConfig);
240}
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
void BODCTRL_WriteVlmControlA(uint8_t value)
Overwrites the VLMCTRLA register value.
void BODCTRL_SetIntControl(uint8_t bitmask)
Sets specific bits in the INTCTRL register.
void BODCTRL_WriteIntFlags(uint8_t value)
Overwrites the INTFLAGS register value.
uint8_t BODCTRL_ReadIntFlags(void)
Reads the INTFLAGS register value.
void BODCTRL_ModifyIntControl(uint8_t groupMask, uint8_t groupConfig)
Modifies specific bit field(s) in the INTCTRL register.
errFlag_t MW_GetClearVmonOverError(void)
Reads and clears the flag indicating an overvoltage error detected by the Voltage Monitor.
vlmTrigger_t
Type defines for available Voltage Level Monitor (VLM) trigger configurations.
errFlag_t MW_GetClearVmonDiagEntryError(void)
Reads and clears the flag indicating a Diagnostic Mode entry error in the Voltage Monitor.
errFlag_t MW_GetClearVmonDiagExitError(void)
Reads and clears the flag indicating an Diagnostic Mode exit error in the Voltage Monitor.
errFlag_t MW_GetClearVmonUnderError(void)
Reads and clears the flag indicating an undervoltage error detected by the Voltage Monitor.
vlmThreshold_t
Type defines for available Voltage Level Monitor (VLM) threshold levels.
errFlag_t MW_GetClearVlmError(void)
Reads and clears the flag indicating a supply voltage trigger error detected by the Voltage Level Mon...
errFlag_t MW_GetClearSleepError(void)
Reads and clears the flag indicating a Sleep Aborted error in the Sleep Controller.
void MW_SetVmonSleep(bool enableInSleep, bool useFullPowerMode)
Configures Voltage Monitor (VMON) operation during Standby or Power-Down sleep.
errFlag_t MW_GetClearVmonSleepEntryError(void)
Reads and clears the flag indicating a sleep mode entry error in the Voltage Monitor.
errFlag_t MW_GetClearVmonInternalError(void)
Reads and clears the flag indicating internal error in the Voltage Monitor.
errFlag_t MW_ConfigVlm(vlmThreshold_t threshold, vlmTrigger_t trigger)
Configures the Voltage Level Monitor (VLM) with the provided voltage threshold and trigger condition.
sleepMode_t
Type defines for available sleep modes.
errFlag_t MW_SetSleepMode(sleepMode_t sleepMode)
Sets the sleep mode to be used when entering Sleep.
void MW_EnablePowerInterrupts(void)
Enables power related interrupts, including VMON, VLM and sleep error related interrupts.
errFlag_t MW_GetClearVmonDisabledError(void)
Reads and clears the flag indicating a disabled Voltage Monitor error.
@ VLM_TRIGGER_MAX
@ VLM_THRESHOLD_MAX
void SLPCTRL_ModifyControlA(uint8_t groupMask, uint8_t groupConfig)
Modifies specific bit field(s) in the CTRLA register.
void SLPCTRL_WriteVregControl(uint8_t value)
Overwrites the VREGCTRL register value.
void SLPCTRL_WriteIntFlags(uint8_t value)
Overwrites the INTFLAGS register value.
void SLPCTRL_WriteIntControl(uint8_t value)
Overwrites the INTCTRL register value.
uint8_t SLPCTRL_ReadIntFlags(void)
Reads the INTFLAGS register value.