1 /*
2 * Copyright (c) 2015-2017, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 /*
33 * ======== Watchdog.c ========
34 */
35
36 #include <stdbool.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39
40 #include <ti/drivers/Watchdog.h>
41 #include <ti/drivers/dpl/HwiP.h>
42
43 extern const Watchdog_Config Watchdog_config[];
44 extern const uint_least8_t Watchdog_count;
45
46 /* Default Watchdog parameters structure */
47 const Watchdog_Params Watchdog_defaultParams = {
48 NULL, /* callbackFxn */
49 Watchdog_RESET_ON, /* resetMode */
50 Watchdog_DEBUG_STALL_ON, /* debugStallMode */
51 NULL /* custom */
52 };
53
54 static bool isInitialized = false;
55
56 /*
57 * ======== Watchdog_clear ========
58 */
Watchdog_clear(Watchdog_Handle handle)59 void Watchdog_clear(Watchdog_Handle handle)
60 {
61 handle->fxnTablePtr->watchdogClear(handle);
62 }
63
64 /*
65 * ======== Watchdog_close ========
66 */
Watchdog_close(Watchdog_Handle handle)67 void Watchdog_close(Watchdog_Handle handle)
68 {
69 handle->fxnTablePtr->watchdogClose(handle);
70 }
71
72 /*
73 * ======== Watchdog_control ========
74 */
Watchdog_control(Watchdog_Handle handle,uint_fast16_t cmd,void * arg)75 int_fast16_t Watchdog_control(Watchdog_Handle handle, uint_fast16_t cmd,
76 void *arg)
77 {
78 return (handle->fxnTablePtr->watchdogControl(handle, cmd, arg));
79 }
80
81 /*
82 * ======== Watchdog_init ========
83 */
Watchdog_init(void)84 void Watchdog_init(void)
85 {
86 uint_least8_t i;
87 uint_fast32_t key;
88
89 key = HwiP_disable();
90
91 if (!isInitialized) {
92 isInitialized = (bool) true;
93
94 /* Call each driver's init function */
95 for (i = 0; i < Watchdog_count; i++) {
96 Watchdog_config[i].fxnTablePtr->watchdogInit((Watchdog_Handle)&(Watchdog_config[i]));
97 }
98 }
99
100 HwiP_restore(key);
101 }
102
103 /*
104 * ======== Watchdog_open ========
105 */
Watchdog_open(uint_least8_t index,Watchdog_Params * params)106 Watchdog_Handle Watchdog_open(uint_least8_t index, Watchdog_Params *params)
107 {
108 Watchdog_Handle handle = NULL;
109
110 /* Verify driver index and state */
111 if (isInitialized && (index < Watchdog_count)) {
112 /* If params are NULL use defaults */
113 if (params == NULL) {
114 params = (Watchdog_Params *) &Watchdog_defaultParams;
115 }
116
117 handle = (Watchdog_Handle)&(Watchdog_config[index]);
118 handle = handle->fxnTablePtr->watchdogOpen(handle, params);
119 }
120
121 return (handle);
122 }
123
124 /*
125 * ======== Watchdog_Params_init ========
126 */
Watchdog_Params_init(Watchdog_Params * params)127 void Watchdog_Params_init(Watchdog_Params *params)
128 {
129 *params = Watchdog_defaultParams;
130 }
131
132
133 /*
134 * ======== Watchdog_setReload ========
135 */
Watchdog_setReload(Watchdog_Handle handle,uint32_t ticks)136 int_fast16_t Watchdog_setReload(Watchdog_Handle handle, uint32_t ticks)
137 {
138 return (handle->fxnTablePtr->watchdogSetReload(handle, ticks));
139 }
140
141 /*
142 * ======== Watchdog_convertMsToTicks ========
143 */
Watchdog_convertMsToTicks(Watchdog_Handle handle,uint32_t milliseconds)144 uint32_t Watchdog_convertMsToTicks(Watchdog_Handle handle, uint32_t milliseconds)
145 {
146 return (handle->fxnTablePtr->watchdogConvertMsToTicks(handle, milliseconds));
147 }
148