1 //*****************************************************************************
2 //
3 //  am_hal_wdt.c
4 //! @file
5 //!
6 //! @brief Hardware abstraction layer for the Watchdog Timer module.
7 //!
8 //! @addtogroup wdt2 Watchdog Timer (WDT)
9 //! @ingroup apollo2hal
10 //! @{
11 //
12 //*****************************************************************************
13 
14 //*****************************************************************************
15 //
16 // Copyright (c) 2017, Ambiq Micro
17 // All rights reserved.
18 //
19 // Redistribution and use in source and binary forms, with or without
20 // modification, are permitted provided that the following conditions are met:
21 //
22 // 1. Redistributions of source code must retain the above copyright notice,
23 // this list of conditions and the following disclaimer.
24 //
25 // 2. Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // 3. Neither the name of the copyright holder nor the names of its
30 // contributors may be used to endorse or promote products derived from this
31 // software without specific prior written permission.
32 //
33 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
34 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
37 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
38 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
39 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
40 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
41 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43 // POSSIBILITY OF SUCH DAMAGE.
44 //
45 // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
46 //
47 //*****************************************************************************
48 
49 #include <stdint.h>
50 #include <stdbool.h>
51 #include "am_mcu_apollo.h"
52 
53 //*****************************************************************************
54 //
55 // Adjacency check
56 //
57 // This is related to the timer read workaround. This macro checks to see if
58 // the two supplied count values are within one "tick" of eachother. It should
59 // still pass in the event of a timer rollover. The "B" read is assumed to
60 // follow the "A" read.  The macro returns "TRUE" when the adjacent timer reads
61 // can be used.
62 //
63 //*****************************************************************************
64 #define adjacent(A, B)      (((A) == (B)) || (((A) + 1) == (B)) || ((B) == 0))
65 
66 //*****************************************************************************
67 //
68 //! @brief Configure the watchdog timer.
69 //!
70 //! @param psConfig - pointer to a configuration structure containing the
71 //! desired watchdog settings.
72 //!
73 //! This function will set the watchdog configuration register based on the
74 //! user's desired settings listed in the structure referenced by psConfig. If
75 //! the structure indicates that watchdog interrupts are desired, this function
76 //! will also set the interrupt enable bit in the configuration register.
77 //!
78 //! @note In order to actually receive watchdog interrupt and/or watchdog reset
79 //! events, the caller will also need to make sure that the watchdog interrupt
80 //! vector is enabled in the ARM NVIC, and that watchdog resets are enabled in
81 //! the reset generator module. Otherwise, the watchdog-generated interrupt and
82 //! reset events will have no effect.
83 //!
84 //! @return None.
85 //
86 //*****************************************************************************
87 void
am_hal_wdt_init(const am_hal_wdt_config_t * psConfig)88 am_hal_wdt_init(const am_hal_wdt_config_t *psConfig)
89 {
90     uint32_t ui32ConfigVal;
91     uint16_t ui16IntCount, ui16ResetCount;
92     bool bResetEnabled = psConfig->ui32Config & AM_HAL_WDT_ENABLE_RESET;
93     bool bInterruptEnabled = psConfig->ui32Config & AM_HAL_WDT_ENABLE_INTERRUPT;
94 
95     //
96     // Read the desired settings from the psConfig structure.
97     //
98     ui16IntCount = psConfig->ui16InterruptCount;
99     ui16ResetCount = psConfig->ui16ResetCount;
100 
101     //
102     // Write the interrupt and reset count values to a temporary variable.
103     //
104     // Accept the passed Config value, but clear the Counts that we are about to set.
105     ui32ConfigVal = psConfig->ui32Config & ~(AM_REG_WDT_CFG_INTVAL_M | AM_REG_WDT_CFG_RESVAL_M);
106     ui32ConfigVal |= AM_WRITE_SM(AM_REG_WDT_CFG_INTVAL, ui16IntCount);
107     ui32ConfigVal |= AM_WRITE_SM(AM_REG_WDT_CFG_RESVAL, ui16ResetCount);
108 
109     //
110     // If interrupts should be enabled, set the appropriate bit in the
111     // temporary variable. Also, enable the interrupt in INTEN register in the
112     // watchdog module.
113     //
114     if ( bInterruptEnabled )
115     {
116         //
117         // Enable the watchdog interrupt if the configuration calls for them.
118         //
119         AM_REGn(WDT, 0, INTEN) |= AM_REG_WDT_INTEN_WDT_M;
120     }
121     else
122     {
123         //
124         // Disable the watchdog interrupt if the configuration doesn't call for
125         // watchdog interrupts.
126         //
127         AM_REGn(WDT, 0, INTEN) &= ~AM_REG_WDT_INTEN_WDT_M;
128     }
129 
130     //
131     // If resets should be enabled, set the appropriate bit in the temporary
132     // variable.
133     //
134     if ( bResetEnabled )
135     {
136         //
137         // Also enable watchdog resets in the reset module.
138         //
139         AM_REG(RSTGEN, CFG) |= AM_REG_RSTGEN_CFG_WDREN_M;
140     }
141     else
142     {
143         //
144         // Disable watchdog resets in the reset module.
145         //
146         AM_REG(RSTGEN, CFG) &= ~AM_REG_RSTGEN_CFG_WDREN_M;
147     }
148 
149     //
150     // Check for a user specified clock select. If none specified then
151     // set 128Hz.
152     //
153     if ( !(psConfig->ui32Config & AM_REG_WDT_CFG_CLKSEL_M) )
154     {
155         ui32ConfigVal |= AM_REG_WDT_CFG_CLKSEL_128HZ;
156     }
157 
158     //
159     // Write the saved value to the watchdog configuration register.
160     //
161     AM_REGn(WDT, 0, CFG) = ui32ConfigVal;
162 }
163 
164 //*****************************************************************************
165 //
166 //! @brief Starts the watchdog timer.
167 //!
168 //! Enables the watchdog timer tick using the 'enable' bit in the watchdog
169 //! configuration register.  This function does not perform any locking of the
170 //! watchdog timer, so it can be disabled or reconfigured later.
171 //!
172 //! @return None.
173 //
174 //*****************************************************************************
175 void
am_hal_wdt_start(void)176 am_hal_wdt_start(void)
177 {
178     //
179     // Make sure the watchdog timer is in the "reset" state, and then set the
180     // enable bit to start counting.
181     //
182     AM_REGn(WDT, 0, CFG) |= AM_REG_WDT_CFG_WDTEN_M;
183     AM_REGn(WDT, 0, RSTRT) |= AM_REG_WDT_RSTRT_RSTRT_KEYVALUE;
184 
185 }
186 
187 //*****************************************************************************
188 //
189 //! @brief Stops the watchdog timer.
190 //!
191 //! Disables the watchdog timer tick by clearing the 'enable' bit in the
192 //! watchdog configuration register.
193 //!
194 //! @return None.
195 //
196 //*****************************************************************************
197 void
am_hal_wdt_halt(void)198 am_hal_wdt_halt(void)
199 {
200 
201     //
202     // Clear the watchdog enable bit.
203     //
204     AM_REGn(WDT, 0, CFG) &= ~AM_REG_WDT_CFG_WDTEN_M;
205 }
206 
207 //*****************************************************************************
208 //
209 //! @brief Locks the watchdog configuration and starts the watchdog timer.
210 //!
211 //! This function sets the watchdog "lock" register, which prevents software
212 //! from re-configuring the watchdog. This action will also set the enable bit
213 //! for the watchdog timer, so it will start counting immediately.
214 //!
215 //! @return None.
216 //
217 //*****************************************************************************
218 void
am_hal_wdt_lock_and_start(void)219 am_hal_wdt_lock_and_start(void)
220 {
221     //
222     // Write the 'key' value to the watchdog lock register.
223     //
224     AM_REGn(WDT, 0, LOCK) = AM_REG_WDT_LOCK_LOCK_KEYVALUE;
225 }
226 
227 //*****************************************************************************
228 //
229 //! @brief Read the state of the wdt interrupt status.
230 //!
231 //! @param bEnabledOnly - return the status of only the enabled interrupts.
232 //!
233 //! This function extracts the interrupt status bits and returns the enabled or
234 //! raw based on bEnabledOnly.
235 //!
236 //! @return WDT interrupt status.
237 //
238 //*****************************************************************************
239 uint32_t
am_hal_wdt_int_status_get(bool bEnabledOnly)240 am_hal_wdt_int_status_get(bool bEnabledOnly)
241 {
242     if (bEnabledOnly)
243     {
244         uint32_t u32RetVal = AM_REG(WDT, INTSTAT);
245         return u32RetVal & AM_REG(WDT, INTEN);
246     }
247     else
248     {
249         return AM_REG(WDT, INTSTAT);
250     }
251 }
252 
253 //*****************************************************************************
254 //
255 //! @brief Set the state of the wdt interrupt status bit.
256 //!
257 //! This function sets the interrupt bit.
258 //!
259 //! @return None
260 //
261 //*****************************************************************************
262 void
am_hal_wdt_int_set(void)263 am_hal_wdt_int_set(void)
264 {
265     AM_REG(WDT, INTSET) = AM_REG_WDT_INTSET_WDT_M;
266 }
267 
268 //*****************************************************************************
269 //
270 //! @brief Clear the state of the wdt interrupt status bit.
271 //!
272 //! This function clear the interrupt bit.
273 //!
274 //! @return None
275 //
276 //*****************************************************************************
277 void
am_hal_wdt_int_clear(void)278 am_hal_wdt_int_clear(void)
279 {
280     AM_REGn(WDT, 0, INTCLR) = AM_REG_WDT_INTCLR_WDT_M;
281 }
282 
283 //*****************************************************************************
284 //
285 //! @brief Enable the wdt interrupt.
286 //!
287 //! This function enable the interrupt.
288 //!
289 //! @return None
290 //
291 //*****************************************************************************
292 void
am_hal_wdt_int_enable(void)293 am_hal_wdt_int_enable(void)
294 {
295     AM_REG(WDT, INTEN) |= AM_REG_WDT_INTSET_WDT_M;
296 }
297 
298 //*****************************************************************************
299 //
300 //! @brief Return the enabled WDT interrupts.
301 //!
302 //! This function returns the enabled WDT interrupts.
303 //!
304 //! @return enabled WDT interrupts.
305 //
306 //*****************************************************************************
307 uint32_t
am_hal_wdt_int_enable_get(void)308 am_hal_wdt_int_enable_get(void)
309 {
310     return AM_REG(WDT, INTEN);
311 }
312 
313 //*****************************************************************************
314 //
315 //! @brief Disable the wdt interrupt.
316 //!
317 //! This function disablee the interrupt.
318 //!
319 //! @return None
320 //
321 //*****************************************************************************
322 void
am_hal_wdt_int_disable(void)323 am_hal_wdt_int_disable(void)
324 {
325     AM_REG(WDT, INTEN) &= ~AM_REG_WDT_INTSET_WDT_M;
326 }
327 
328 //*****************************************************************************
329 //
330 // Static function for reading the WDT counter value.
331 //
332 //*****************************************************************************
333 #if defined(__GNUC_STDC_INLINE__)
334 __attribute__((naked))
335 static
336 void
back2back_read_asm(uint32_t * pui32Array,uint32_t * pui32Register)337 back2back_read_asm(uint32_t *pui32Array, uint32_t *pui32Register)
338 {
339     // pui32Array[] is a pointer to a 3 word data array provided by the caller.
340     // pui32Register = address of the timer to be read.
341     __asm
342     (
343         // Do 3 back-to-back reads of the register
344         "   ldr     r2, [r1, #0]\n"             // Get counter register value
345         "   ldr     r3, [r1, #0]\n"             // Get counter register value again
346         "   ldr     r1, [r1, #0]\n"             // Get counter register value for a third time
347         "   str     r2, [r0, #0]\n"             // Store register value to variable
348         "   str     r3, [r0, #4]\n"             // Store register value to variable
349         "   str     r1, [r0, #8]\n"             // Store register value to variable
350         "   bx      lr\n"
351     );
352 }
353 
354 #elif defined(__ARMCC_VERSION)
355 __asm static uint32_t
back2back_read_asm(uint32_t * pui32Array,uint32_t * pui32Register)356 back2back_read_asm(uint32_t *pui32Array, uint32_t *pui32Register)
357 {
358     ldr     r2, [r1, #0]             // Get TMRn register value
359     ldr     r3, [r1, #0]             // Get TMRn register value again
360     ldr     r1, [r1, #0]             // Get TMRn register value for a third time
361     str     r2, [r0, #0]             // Store register value to variable
362     str     r3, [r0, #4]             // Store register value to variable
363     str     r1, [r0, #8]             // Store register value to variable
364     bx      lr
365 }
366 
367 #elif defined(__IAR_SYSTEMS_ICC__)
368 #pragma diag_suppress = Pe940   // Suppress IAR compiler warning about missing
369                                 // return statement on a non-void function
370 __stackless static uint32_t
back2back_read_asm(uint32_t * pui32Array,uint32_t * pui32Register)371 back2back_read_asm(uint32_t *pui32Array, uint32_t *pui32Register)
372 {
373     __asm("    ldr     r2, [r1, #0]");  // Get TMRn register value
374     __asm("    ldr     r3, [r1, #0]");  // Get TMRn register value again
375     __asm("    ldr     r1, [r1, #0]");  // Get TMRn register value for a third time
376     __asm("    str     r2, [r0, #0]");  // Store register value to variable
377     __asm("    str     r3, [r0, #4]");  // Store register value to variable
378     __asm("    str     r1, [r0, #8]");  // Store register value to variable
379     __asm("    bx      lr");
380 }
381 #pragma diag_default = Pe940    // Restore IAR compiler warning
382 #endif
383 
384 
385 //*****************************************************************************
386 //
387 //! @brief Get the wdt counter value.
388 //!
389 //! This function reads the current value of watch dog timer counter register.
390 //!
391 //! WARNING caller is responsible for masking interrutps before calling this
392 //! function.
393 //!
394 //! @return None
395 //
396 //*****************************************************************************
397 uint32_t
am_hal_wdt_counter_get(void)398 am_hal_wdt_counter_get(void)
399 {
400   uint32_t ui32Values[3] = {0};
401     uint32_t ui32Value;
402 
403     //
404     // First, go read the value from the counter register 3 times
405     // back to back in assembly language.
406     //
407     back2back_read_asm(ui32Values, (uint32_t *)AM_REG_WDTn(0));
408 
409     //
410     // Now, we'll figure out which of the three values is the correct time.
411     //
412     if (ui32Values[0] == ui32Values[1])
413     {
414         //
415         // If the first two values match, then neither one was a bad read.
416         // We'll take this as the current time.
417         //
418         ui32Value = ui32Values[1];
419     }
420     else
421     {
422         //
423         // If the first two values didn't match, then one of them might be bad.
424         // If one of the first two values is bad, then the third one should
425         // always be correct. We'll take the third value as the correct count.
426         //
427         ui32Value = ui32Values[2];
428 
429         //
430         // If all of the statements about the architecture are true, the third
431         // value should be correct, and it should always be within one count of
432         // either the first or the second value.
433         //
434         // Just in case, we'll check against the previous two values to make
435         // sure that our final answer was reasonable. If it isn't, we will
436         // flag it as a "bad read", and fail this assert statement.
437         //
438         // This shouldn't ever happen, and it hasn't ever happened in any of
439         // our tests so far.
440         //
441         am_hal_debug_assert_msg((adjacent(ui32Values[1], ui32Values[2]) ||
442                                  adjacent(ui32Values[0], ui32Values[2])),
443                                 "Bad CDT read");
444     }
445 
446     return ui32Value;
447 }
448 
449 //*****************************************************************************
450 //
451 // End Doxygen group.
452 //! @}
453 //
454 //*****************************************************************************
455