ADuCM302x Device Drivers API Reference Manual  Release 3.1.2.0
adi_wdt.c
1 
45 #ifdef __ICCARM__
46 /*
47 * IAR MISRA C 2004 error suppressions.
48 *
49 *
50 * Pm011 (rule 6.3): the basic types of char, int, short, long, float, and double should not be used
51 * Necessary for stdbool.
52 *
53 * Pm073 (rule 14.7): a function should have a single point of exit
54 * Pm143 (rule 14.7): a function should have a single point of exit at the end of the function
55 * Multiple returns are used for error handling.
56 *
57 * Pm140 (Rule 11.4): a cast should not be performed between a pointer type and an integral type
58 * This violation appears when deferencing the pointer to the register typedef. No way around this.
59 */
60 #pragma diag_suppress=Pm011,Pm073,Pm140,Pm143
61 #endif /* __ICCARM__ */
62 
63 
75 #include <stdlib.h>
76 #include <adi_processor.h>
77 #include <rtos_map/adi_rtos_map.h>
78 #include <adi_wdt_config.h>
79 #include <drivers/wdt/adi_wdt.h>
80 
84 #define ADI_WDT_SYNC_BITS ((0x1u << BITP_WDT_STAT_COUNTING) | (0x1u << BITP_WDT_STAT_LOADING) | (0x1u << BITP_WDT_STAT_CLRIRQ))
85 
87 #define ADI_WDT_CLR_VALUE (0xCCCCu)
88 
90 #if (ADI_WDT_CONTROL_TIMEOUT_MODE == 1u)
91 static ADI_CALLBACK gAppCallback;
92 #endif
93 
96 /*********************************************************************************
97  API IMPLEMENTATIONS
98 *********************************************************************************/
99 
100 
115 ADI_WDT_RESULT adi_wdt_Enable(bool const bEnable, ADI_CALLBACK const pfCallback) {
116  /* IF(Device is enabled, application can't modify it) */
117  if ((pADI_WDT0->STAT & ((uint16_t) BITM_WDT_STAT_LOCKED)) != ((uint16_t) 0x0u)) {
118  return ADI_WDT_FAILURE_LOCKED;
119  } /* ENDIF */
120 
121  /* Setup interrupts if we are in interrupt mode */
122 #if (ADI_WDT_CONTROL_TIMEOUT_MODE == 1u)
123  gAppCallback = pfCallback;
124  /* IF(We are enabling the WDT) */
125  if (bEnable == true) {
126  NVIC_EnableIRQ (WDT_EXP_IRQn);
127  /* ELSE (We are disabling the WDT, this might not be necessary, depends on startup config) */
128  } else {
129  NVIC_DisableIRQ(WDT_EXP_IRQn);
130  } /* ENDIF */
131 #endif
132 
133  /* WHILE(Bus sync is underway) */
134  while((pADI_WDT0->STAT & ADI_WDT_SYNC_BITS) != 0u) {
135  ;
136  } /* ENDWHILE */
137 
138 
139  ADI_INT_STATUS_ALLOC();
140  ADI_ENTER_CRITICAL_REGION();
141 
142  pADI_WDT0->LOAD = ADI_WDT_LOAD_VALUE;
143 
144  /* IF(Turning the WDT on) */
145  if (bEnable == true) {
146  pADI_WDT0->CTL = (ADI_WDT_CONTROL_TIMER_MODE << BITP_WDT_CTL_MODE) |
147  (0x1u << BITP_WDT_CTL_EN ) |
148  (ADI_WDT_CONTROL_CLOCK_PRESCALER << BITP_WDT_CTL_PRE ) |
149  (ADI_WDT_CONTROL_TIMEOUT_MODE << BITP_WDT_CTL_IRQ ) |
151  /* ELSE(Turning the WDT off) */
152  } else {
153  pADI_WDT0->CTL = (ADI_WDT_CONTROL_TIMER_MODE << BITP_WDT_CTL_MODE) |
154  (0x0u << BITP_WDT_CTL_EN ) |
155  (ADI_WDT_CONTROL_CLOCK_PRESCALER << BITP_WDT_CTL_PRE ) |
156  (ADI_WDT_CONTROL_TIMEOUT_MODE << BITP_WDT_CTL_IRQ ) |
158  } /* ENDIF */
159 
160  ADI_EXIT_CRITICAL_REGION();
161 
162  return ADI_WDT_SUCCESS;
163 }
164 
172 void adi_wdt_Kick(void) {
173  /* WHILE(Bus sync is underway) */
174  while((pADI_WDT0->STAT & ADI_WDT_SYNC_BITS) != 0u) {
175  ;
176  } /* ENDWHILE */
177 
178  /* Kick the dog! */
179  pADI_WDT0->RESTART = ADI_WDT_CLR_VALUE;
180 }
181 
191 void adi_wdt_GetCount(uint16_t * const pCurCount) {
192  /* Read the count */
193  *pCurCount = pADI_WDT0->CCNT;
194 }
195 
208 #if (ADI_WDT_CONTROL_TIMEOUT_MODE == 1u)
209 extern void WDog_Tmr_Int_Handler(void);
210 void WDog_Tmr_Int_Handler(void) {
211  ISR_PROLOG()
212  /* Kick the dog */
213  adi_wdt_Kick();
214  /* IF(Application supplied a callback) */
215  if(gAppCallback != NULL) {
216  /* Call the callback */
217  gAppCallback(NULL, 0x0u, NULL);
218  } /* ENDIF */
219  ISR_EPILOG()
220 }
221 #endif /* (ADI_WDT_CONTROL_TIMEOUT_MODE == 1u) */
222 
#define ADI_WDT_CONTROL_CLOCK_PRESCALER
ADI_WDT_RESULT adi_wdt_Enable(bool const bEnable, ADI_CALLBACK const pfCallback)
WDT Enable.
Definition: adi_wdt.c:115
#define ADI_WDT_CONTROL_TIMER_MODE
#define ADI_WDT_CONTROL_TIMEOUT_MODE
void adi_wdt_Kick(void)
WDT Reset.
Definition: adi_wdt.c:172
#define ADI_WDT_LOAD_VALUE
void adi_wdt_GetCount(uint16_t *const pCurCount)
WDT Read Count.
Definition: adi_wdt.c:191
#define ADI_WDT_CONTROL_POWER_MODE
ADI_WDT_RESULT
Definition: adi_wdt.h:60