bd2aee6f471dc39436a9846f159f64358516118d
1 /*
2 * ESM Example
3 *
4 * Error Signaling Module (ESM) Example Application
5 *
6 * Copyright (c) 2020 Texas Instruments Incorporated
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the
18 * distribution.
19 *
20 * Neither the name of Texas Instruments Incorporated nor the names of
21 * its contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 /**
37 * \file event_trig.c
38 *
39 * \brief This file contains functions that provide input event triggers
40 * for the Error Signaling Module (ESM) application.
41 */
43 /* ========================================================================== */
44 /* Include Files */
45 /* ========================================================================== */
47 /* For Timer functions */
48 #include <ti/osal/osal.h>
50 #include <ti/drv/sciclient/sciclient.h>
52 /* Diagnostic example utility functions, e.g. print outputs */
53 #include <diag_utils.h>
55 #include <ti/csl/soc.h>
56 #include <ti/csl/csl_vtm.h>
57 #include <ti/csl/csl_esm.h>
59 #include <sdr_esm.h>
60 #include "event_trig.h"
62 /* ========================================================================== */
63 /* Macros & Typedefs */
64 /* ========================================================================== */
66 #define PIN_TIMING_TIMER_ID (1)
67 #define HIGH_PRIO_TRIG_TIMER_ID (2)
68 #define LOW_PRIO_TRIG_TIMER_ID (3)
70 #define CSL_VTM_TEST_CFG1_BASE (CSL_WKUP_VTM0_MMR_VBUSP_CFG1_BASE)
71 #define CSL_VTM_TEST_CFG2_BASE (CSL_WKUP_VTM0_MMR_VBUSP_CFG2_BASE)
73 #define LT_THR0_DEFAULT (95000)
74 #define GT_THR1_DEFAULT (105000)
75 #define GT_THR2_DEFAULT (115000)
77 #define PIN_CLEAR_PERIOD_USEC (10)
79 /* ========================================================================== */
80 /* Structure Declarations */
81 /* ========================================================================== */
83 /* None */
85 /* ========================================================================== */
86 /* Function Declarations */
87 /* ========================================================================== */
89 static void timerExpTrigger(uintptr_t arg);
90 static int32_t vtmTriggerTh(int32_t lt_thr0_offset,
91 int32_t gt_thr1_offset,
92 int32_t gt_thr2_offset);
93 static int32_t vtmTriggerTh1(void);
94 static void setAllVTMTempThr(CSL_vtm_adc_code lt_thr0_adc_code,
95 CSL_vtm_adc_code gt_thr1_adc_code,
96 CSL_vtm_adc_code gt_thr2_adc_code);
97 static void timerExpPinDisable(uintptr_t arg);
99 /* ========================================================================== */
100 /* Global Variables */
101 /* ========================================================================== */
103 /* Completion of Use Case from Input trigger perspective updates these flags */
104 extern volatile uint32_t esmEventInputTrig[5];
105 /* Completion of Use Case from Output Pin clearing perspective updates these flags */
106 extern volatile uint32_t esmPinClearResult[5];
107 /* Current use case being run */
108 extern volatile uint8_t currUseCase;
110 static uint32_t esmPinMinIntervalCycles;
111 static uint32_t esmPinMinIntervalUsec;
112 static uint32_t pinClearTimeCycles;
113 static bool immediatePinClear;
115 static TimerP_Handle esmHiEventTrigTimerHandle;
116 static TimerP_Handle esmLoEventTrigTimerHandle;
117 static TimerP_Handle esmPinTimerHandle;
118 static SDR_ESM_InstanceType currEsmInstance;
120 /* ========================================================================== */
121 /* External Variables */
122 /* ========================================================================== */
124 /* None */
126 /* ========================================================================== */
127 /* Function Definitions */
128 /* ========================================================================== */
131 /*********************************************************************
132 * @fn esmPinTimeInit
133 *
134 * @brief This checks current timings for the minimum interval time
135 * on the MCU_SAFETY_ERRORn pin for eacl Error Signaling Module
136 *
137 * @param pinClearTime: Amount of time in microseconds that the Pin control
138 * timer waits before triggering clearing of the MCU_SAFETY_ERRORn pin
139 *
140 * @return 0 : Success; < 0 for failures
141 */
142 int32_t esmPinTimeInit(uint32_t pinClearTime)
143 {
144 int32_t retVal = 0;
145 uint32_t modId;
146 uint32_t clkId;
147 uint64_t esmInputClk;
149 ESMGetErrPinLowTimePreload(CSL_MCU_ESM0_CFG_BASE, &esmPinMinIntervalCycles);
150 #ifdef PRINT_DEBUG
151 DIAG_printf(" MCU ESM pin minimum interval is %d cycles\n",
152 esmPinMinIntervalCycles);
153 #endif
154 ESMGetErrPinLowTimePreload(CSL_WKUP_ESM0_CFG_BASE, &esmPinMinIntervalCycles);
155 #ifdef PRINT_DEBUG
156 DIAG_printf(" WKUP ESM pin minimum interval is %d cycles\n",
157 esmPinMinIntervalCycles);
158 #endif
160 /* WKUP ESM clock */
161 modId = TISCI_DEV_WKUP_ESM0;
162 clkId = TISCI_DEV_WKUP_ESM0_CLK;
164 retVal = Sciclient_pmGetModuleClkFreq(modId,
165 clkId,
166 &esmInputClk,
167 SCICLIENT_SERVICE_WAIT_FOREVER);
168 if (retVal == CSL_PASS) {
169 #ifdef PRINT_DEBUG
170 DIAG_printf(" WKUP ESM input clock is %d\n", esmInputClk);
171 #endif
172 /* MCU ESM clock */
173 modId = TISCI_DEV_MCU_ESM0;
174 clkId = TISCI_DEV_MCU_ESM0_CLK;
176 retVal = Sciclient_pmGetModuleClkFreq(modId,
177 clkId,
178 &esmInputClk,
179 SCICLIENT_SERVICE_WAIT_FOREVER);
180 if (retVal == CSL_PASS) {
181 #ifdef PRINT_DEBUG
182 DIAG_printf(" MCU ESM input clock is %d\n", esmInputClk);
183 #endif
184 }
185 }
187 if (retVal == CSL_PASS) {
188 /* Translate Pin Timer Time (microseconds) into number of ESM cycles */
189 pinClearTimeCycles = (uint32_t)((float)pinClearTime /
190 1000000 * (float)esmInputClk);
191 DIAG_printf("\n Any clear of MCU_SAFETY_ERRORn pin will first wait " \
192 "%d usecs", pinClearTime);
193 /* Translate Minimum Time Interval (cycles) into time (microseconds)*/
194 esmPinMinIntervalUsec = (uint32_t)((float)esmPinMinIntervalCycles /
195 (float)esmInputClk * 1000000);
196 DIAG_printf("\n Minimum Time Interval is %d usecs", esmPinMinIntervalUsec);
197 }
198 else {
199 retVal = -1;
200 }
202 /* If desired, ESMSetErrPinLowTimePreload can be used to change
203 * minimum interval time here */
205 return retVal;
206 }
208 /* TIMER FUNCTIONS */
210 /*********************************************************************
211 * @fn diag_esm_timerInit
212 *
213 * @brief This initializes all timers for the Error Signaling Module (ESM)
214 * example application.
215 *
216 * @param None
217 *
218 * @return 0 : Success; < 0 for failures
219 */
220 int32_t diag_esm_timerInit(void)
221 {
222 TimerP_Params timerParams;
224 /* Start Timer and register call back for periodic functions */
225 /* Initialize timer parameters */
226 TimerP_Params_init(&timerParams);
228 /* Configure periodic timer for 0.005 sec*/
229 timerParams.period = 5000; /* 0.005s */
230 timerParams.periodType = TimerP_PeriodType_MICROSECS;
231 timerParams.arg = (void *)HIGH_PRIO_TRIG_TIMER_ID;
232 timerParams.startMode = TimerP_StartMode_USER;
233 timerParams.runMode = TimerP_RunMode_ONESHOT;
235 /* Create High Priority Event Trigger Timer */
236 esmHiEventTrigTimerHandle = TimerP_create(HIGH_PRIO_TRIG_TIMER_ID,
237 (TimerP_Fxn)&timerExpTrigger,
238 &timerParams);
239 if (esmHiEventTrigTimerHandle == NULL) {
240 DIAG_printf("ERR: High Priority Trigger Timer create failed\n");
241 return -1;
242 }
244 /* Low Priority timer has same settings as High Priority timer
245 * but just different timer instance */
246 timerParams.arg = (void *)LOW_PRIO_TRIG_TIMER_ID;
248 /* Create Low Priority Event Trigger Timer */
249 esmLoEventTrigTimerHandle = TimerP_create(LOW_PRIO_TRIG_TIMER_ID,
250 (TimerP_Fxn)&timerExpTrigger,
251 &timerParams);
252 if (esmLoEventTrigTimerHandle == NULL) {
253 DIAG_printf("ERR: Low Priority Trigger Timer create failed\n");
254 return -1;
255 }
257 /* Configure periodic timer for 10 microseconds */
258 timerParams.period = PIN_CLEAR_PERIOD_USEC; /* 10 usec */
259 timerParams.periodType = TimerP_PeriodType_MICROSECS;
260 timerParams.arg = (void *)&currEsmInstance;
261 timerParams.startMode = TimerP_StartMode_USER;
262 timerParams.runMode = TimerP_RunMode_ONESHOT;
264 /* Create Timer for Pin Control */
265 esmPinTimerHandle = TimerP_create(PIN_TIMING_TIMER_ID,
266 (TimerP_Fxn)&timerExpPinDisable,
267 &timerParams);
268 if (esmPinTimerHandle == NULL) {
269 DIAG_printf("ERR: Pin Control and Measurment Timer create failed\n");
270 return -1;
271 }
273 if (esmPinTimeInit(PIN_CLEAR_PERIOD_USEC) != 0) {
274 DIAG_printf("ERR: Pin Time Init failed\n");
275 return -1;
276 }
278 DIAG_printf("\nESM example timer initialization complete\n");
280 return 0;
281 }
283 /*********************************************************************
284 * @fn diag_esm_timerDelete
285 *
286 * @brief This deletes all timers for the Error Signaling Module (ESM)
287 * example application.
288 *
289 * @param None
290 *
291 * @return 0 : Success; < 0 for failures
292 */
293 int32_t diag_esm_timersDelete(void)
294 {
295 int32_t retVal = 0;
297 if (esmHiEventTrigTimerHandle != NULL_PTR) {
298 if (TimerP_delete(esmHiEventTrigTimerHandle) != TimerP_OK) {
299 retVal = -1;
300 }
301 }
303 if (esmLoEventTrigTimerHandle != NULL_PTR) {
304 if (TimerP_delete(esmLoEventTrigTimerHandle) != TimerP_OK) {
305 retVal = -1;
306 }
307 }
309 if (esmPinTimerHandle != NULL_PTR) {
310 if (TimerP_delete(esmPinTimerHandle) != TimerP_OK) {
311 retVal = -1;
312 }
313 }
315 return (retVal);
316 }
318 /*********************************************************************
319 * This callback is triggered by expiration of the input event
320 * trigger timers.
321 */
322 static void timerExpTrigger(uintptr_t arg)
323 {
324 TimerP_Status timerStatus;
325 volatile uint32_t storeArg = arg;
327 if (storeArg == HIGH_PRIO_TRIG_TIMER_ID)
328 {
329 esmEventInputTrig[4] = USE_CASE_STATUS_COMPLETED_SUCCESS;
330 #ifdef PRINT_DEBUG
331 DIAG_printf("timerExpTrigger: High Priority interrupt timer expired\n");
332 #endif
333 /* Start Timer to control when external Pin is reset */
334 timerStatus = TimerP_start(esmPinTimerHandle);
335 if (timerStatus != TimerP_OK) {
336 DIAG_printf("ERR: Could not start the timer %d \n",
337 PIN_TIMING_TIMER_ID);
338 }
339 } else if (storeArg == LOW_PRIO_TRIG_TIMER_ID) {
340 esmEventInputTrig[3] = USE_CASE_STATUS_COMPLETED_SUCCESS;
341 #ifdef PRINT_DEBUG
342 DIAG_printf("timerExpTrigger: Low Priority interrupt timer expired\n");
343 #endif
344 }
345 }
347 /*
348 * This callback is triggered by expiration of the pin disable timer
349 */
350 static void timerExpPinDisable(uintptr_t arg)
351 {
352 int32_t retVal = 0;
353 bool pinStatus;
354 SDR_Result sdrStatus;
356 volatile SDR_ESM_InstanceType *pInstType = (SDR_ESM_InstanceType *)arg;
358 pinStatus = SDR_ESM_getNErrorStatus(*pInstType);
359 if (pinStatus != 0) {
360 DIAG_printf("\n timerExpPinDisable: Incorrect pin value before clearing\n");
361 retVal = -1;
362 } else {
363 #ifdef DEBUG_PRINT
364 DIAG_printf("\n timerExpPinDisable: before clear, ESM instance %d view of " \
365 "MCU_SAFETY_ERRORn pin is %d\n",
366 *pInstType, pinStatus);
367 #endif
368 }
370 sdrStatus = SDR_ESM_resetNError(*pInstType);
372 if (sdrStatus != SDR_PASS) {
373 /* If resetNError is called within minimum time interval, then the result of
374 * SDR_ESM_resetNError will be failure because pin does not change until
375 * minimum time interval has expired */
376 if ((pinClearTimeCycles >= esmPinMinIntervalCycles) ||
377 (immediatePinClear)) {
378 DIAG_printf("\n timerExpPinDisable: ERROR - Failed to clear the error " \
379 "pin from ESM Instance %d\n",
380 *pInstType);
381 retVal = -1;
382 } else {
383 /* Delay 1msec after minimum time interval is done and check the pin
384 * again */
385 Osal_delay((esmPinMinIntervalUsec / 1000) + 1);
386 }
387 } else {
388 #ifdef DEBUG_PRINT
389 DIAG_printf(" timerExpPinDisable: Cleared the error pin successfully from " \
390 "ESM Instance %d\n",
391 *pInstType);
392 #endif
393 }
395 pinStatus = SDR_ESM_getNErrorStatus(*pInstType);
396 if (pinStatus != 1) {
397 DIAG_printf("\n timerExpPinDisable: Incorrect pin value after clearing\n");
398 retVal = -1;
399 }
400 #ifdef DEBUG_PRINT
401 DIAG_printf(" timerExpPinDisable: after clear, ESM instance %d view of " \
402 "MCU_SAFETY_ERRORn pin is %d\n\n",
403 *pInstType, pinStatus);
404 #endif
406 if (retVal == 0) {
407 esmPinClearResult[currUseCase] = USE_CASE_STATUS_COMPLETED_SUCCESS;
408 } else {
409 esmPinClearResult[currUseCase] = USE_CASE_STATUS_COMPLETED_FAILURE;
410 }
411 }
413 /*
414 * This function clears timer interrupts for Timer input events to the ESM.
415 */
416 int32_t diag_esm_timerUpdateInt(uint32_t intEsmSrc)
417 {
418 int32_t retVal = -1;
419 TimerP_Status status;
421 /* Stop/Clear timer interrupt based on which timer triggered it */
422 if (intEsmSrc == CSLR_MCU_ESM0_ESM_LVL_EVENT_MCU_TIMER2_INTR_PEND_0)
423 {
424 status = TimerP_stop(esmHiEventTrigTimerHandle);
425 if (status == TimerP_OK) {
426 #ifdef PRINT_DEBUG
427 DIAG_printf("Cleared interrupt for Timer Triggering High " \
428 "Priority Event\n");
429 #endif
430 retVal = 0;
431 } else {
432 DIAG_printf("Failed to Clear interrupt for Timer Triggering High " \
433 "Priority Event\n");
434 }
435 } else if (intEsmSrc == CSLR_MCU_ESM0_ESM_LVL_EVENT_MCU_TIMER3_INTR_PEND_0)
436 {
437 status = TimerP_stop(esmLoEventTrigTimerHandle);
438 if (status == TimerP_OK) {
439 #ifdef PRINT_DEBUG
440 DIAG_printf("Cleared interrupt for Timer Triggering Low Priority Event\n");
441 #endif
442 retVal = 0;
443 } else {
444 DIAG_printf("Failed to Clear interrupt for Timer Triggering " \
445 "Low Priority Event\n");
446 }
447 }
449 return retVal;
450 }
452 /* VTM FUNCTIONS */
454 /*
455 * This function modifies all the VTM thresholds in a single function to ensure
456 * appropriate number of ESM events are triggered by the change in thresholds.
457 */
458 static void setAllVTMTempThr(CSL_vtm_adc_code lt_thr0_adc_code,
459 CSL_vtm_adc_code gt_thr1_adc_code,
460 CSL_vtm_adc_code gt_thr2_adc_code)
461 {
462 CSL_vtm_cfg1Regs *p_cfg = (CSL_vtm_cfg1Regs *) CSL_VTM_TEST_CFG1_BASE;
463 CSL_vtm_tsThrVal thr_val;
464 CSL_vtm_vdThr_interrupt_ctrl ctrl;
465 CSL_vtm_vdEvt_sel_set vd_temp_evts;
466 CSL_vtm_tmpSens_id ts_id = 0;
468 vd_temp_evts = CSL_VTM_VD_EVT_SELECT_TEMP_SENSOR_0;
470 thr_val.thrValidMap = CSL_VTM_GT_TH2_VALID | \
471 CSL_VTM_GT_TH1_VALID | \
472 CSL_VTM_LT_TH0_VALID;
473 ctrl = CSL_VTM_VD_GT_THR2_INTR_EN_SET | \
474 CSL_VTM_VD_GT_THR1_INTR_EN_SET | \
475 CSL_VTM_VD_LT_THR0_INTR_EN_CLR;
476 thr_val.gtTh2En = TRUE;
477 thr_val.gtTh2 = gt_thr2_adc_code;
478 thr_val.gtTh1En = TRUE;
479 thr_val.gtTh1 = gt_thr1_adc_code;
480 thr_val.ltTh0En = TRUE;
481 thr_val.ltTh0 = lt_thr0_adc_code;
483 /* Set the temperature thresholds */
484 CSL_vtmTsSetThresholds (p_cfg, ts_id, &thr_val, FALSE);
485 /* enable the threshold interrupts */
486 CSL_vtmVdThrIntrCtrl(p_cfg, CSL_VTM_VD_DOMAIN_1, ctrl, FALSE);
487 /* enable the tracking of temperature events on this VD */
488 CSL_vtmVdEvtSelSet (p_cfg, CSL_VTM_VD_DOMAIN_1, vd_temp_evts, FALSE);
489 }
491 /*
492 * This function reads the current temperature in the VTM module
493 * and then modifies the VTM thresholds to cause events to trigger.
494 * Note that in a typical system, the thresholds would be kept the static, and
495 * changes in temperature would cause the events to occur.
496 */
497 static int32_t vtmTriggerTh(int32_t lt_thr0_offset,
498 int32_t gt_thr1_offset,
499 int32_t gt_thr2_offset)
500 {
501 int32_t retVal = 0;
502 int32_t temp_milli_degrees_read, ts_id = 0;
503 CSL_vtm_adc_code adc_code_read;
504 CSL_vtm_adc_code adc_code_lt_thr0, adc_code_gt_thr1, adc_code_gt_thr2;
505 CSL_vtm_cfg1Regs *p_vtm_cfg1_regs;
506 int32_t gt_thr2_val, gt_thr1_val, lt_thr0_val;
507 CSL_vtm_tsStat_val ts_stat_val;
508 CSL_vtm_tsStat_read_ctrl tsStat_read_ctrl = CSL_VTM_TS_READ_DATA_OUT_VAL;
509 CSL_vtm_vdThr_interrupt_ctrl ctrl;
511 p_vtm_cfg1_regs = (CSL_vtm_cfg1Regs *) CSL_VTM_TEST_CFG1_BASE;
513 /* Get current temperature value */
514 CSL_vtmTsGetSensorStat (p_vtm_cfg1_regs, &tsStat_read_ctrl, \
515 ts_id, &ts_stat_val);
516 adc_code_read = ts_stat_val.data_out;
517 (void) CSL_vtmTsConvADCToTemp (adc_code_read,
518 (CSL_vtm_tmpSens_id) ts_id,
519 &temp_milli_degrees_read);
520 #ifdef DEBUG_PRINT
521 DIAG_printf ("sensor id : %d \n" \
522 "adc_code : %d \n" \
523 "temp in milli degree celcius : %d \n", \
524 ts_id, adc_code_read, temp_milli_degrees_read);
525 #endif
527 /* Disable interrupts while changing thresholds */
528 ctrl = (CSL_VTM_VD_GT_THR2_INTR_EN_CLR | \
529 CSL_VTM_VD_GT_THR2_INTR_RAW_CLR | \
530 CSL_VTM_VD_GT_THR1_INTR_EN_CLR | \
531 CSL_VTM_VD_GT_THR1_INTR_RAW_CLR | \
532 CSL_VTM_VD_LT_THR0_INTR_EN_CLR | \
533 CSL_VTM_VD_LT_THR0_INTR_RAW_CLR);
534 CSL_vtmVdThrIntrCtrl((CSL_vtm_cfg1Regs *)CSL_VTM_TEST_CFG1_BASE, \
535 CSL_VTM_VD_DOMAIN_1, ctrl, FALSE);
537 /* Change all 3 thresholds relative to the current temperature */
538 lt_thr0_val = temp_milli_degrees_read + lt_thr0_offset;
539 gt_thr1_val = temp_milli_degrees_read + gt_thr1_offset;
540 gt_thr2_val = temp_milli_degrees_read + gt_thr2_offset;
542 CSL_vtmTsConvTempToAdc(lt_thr0_val, ts_id, &adc_code_lt_thr0);
543 CSL_vtmTsConvTempToAdc(gt_thr1_val, ts_id, &adc_code_gt_thr1);
544 CSL_vtmTsConvTempToAdc(gt_thr2_val, ts_id, &adc_code_gt_thr2);
546 #ifdef DEBUG_PRINT
547 DIAG_printf ("vtmTriggerTh: Setting lt_thr0_val temp to " \
548 "%d millidegrees Celsius, and adc_code_lt_thr0 = %d\n",
549 lt_thr0_val,
550 adc_code_lt_thr0);
551 DIAG_printf ("vtmTriggerTh: Setting gt_thr1_val temp to " \
552 "%d millidegrees Celsius, and adc_code_gt_thr1 = %d\n",
553 gt_thr1_val,
554 adc_code_gt_thr1);
555 DIAG_printf ("vtmTriggerTh: Setting gt_thr2_val temp to " \
556 "%d millidegrees Celsius, and adc_code_gt_thr2 = %d\n",
557 gt_thr2_val,
558 adc_code_gt_thr2);
559 #endif
561 setAllVTMTempThr(adc_code_lt_thr0, adc_code_gt_thr1, adc_code_gt_thr2);
563 #ifdef DEBUG_PRINT
564 DIAG_printf("Finished VTM threshold setting\n");
565 #endif
566 return (retVal);
567 }
569 static int32_t vtmTriggerTh1(void)
570 {
571 int32_t retVal = 0;
572 #ifdef DEBUG_PRINT
573 DIAG_printf("vtmTriggerTh1: starting test with threshold change\n");
574 #endif
575 retVal = vtmTriggerTh(-4000, -2000, 5000);
577 return (retVal);
578 }
580 /*
581 * This function clears VTM THR0 interrupt (Less Than Temp event)
582 * for VTM input events to the ESM.
583 * It resets the VTM module to look for high temperature events again.
584 */
585 void diag_esm_vtmLtThr0CrossedUpdateInt(void)
586 {
587 CSL_vtm_vdThr_interrupt_ctrl ctrl;
589 /* Ack the interrupt, by clearing the pending bit */
590 ctrl = (CSL_VTM_VD_LT_THR0_INTR_EN_CLR | \
591 CSL_VTM_VD_GT_THR1_INTR_EN_SET | \
592 CSL_VTM_VD_GT_THR2_INTR_EN_SET | \
593 CSL_VTM_VD_LT_THR0_INTR_RAW_CLR | \
594 CSL_VTM_VD_GT_THR1_INTR_RAW_CLR | \
595 CSL_VTM_VD_GT_THR2_INTR_RAW_CLR);
597 CSL_vtmVdThrIntrCtrl((CSL_vtm_cfg1Regs *)CSL_VTM_TEST_CFG1_BASE, \
598 CSL_VTM_VD_DOMAIN_1, ctrl, FALSE);
600 /* Print output ESM event to the screen */
601 #ifdef PRINT_EVENTS
602 DIAG_printf ("\n Got ltThr0 interrupt through ESM module\n");
603 DIAG_printf ("\n System at a temperature below the threshold of lt_thr0 \n"
604 " System running at a safe temperature \n");
605 #endif
606 }
608 /*
609 * This function clears VTM THR1 interrupts for VTM input event to the ESM.
610 */
611 void diag_esm_vtmGtThr1CrossedUpdateInt(void)
612 {
613 CSL_vtm_vdThr_interrupt_ctrl ctrl;
614 /*
615 - disable the gt1 interrupt
616 - clear the gt1 interrupt
617 - clear the lt0 interrupt
618 - enable the lt0 intterupt
619 */
620 ctrl = (CSL_VTM_VD_GT_THR1_INTR_EN_CLR | \
621 CSL_VTM_VD_GT_THR1_INTR_RAW_CLR | \
622 CSL_VTM_VD_LT_THR0_INTR_EN_SET | \
623 CSL_VTM_VD_LT_THR0_INTR_RAW_CLR);
625 /* Ack and Re-enable the LT_THR0 interrupt to let system know if cooling
626 * took place */
627 CSL_vtmVdThrIntrCtrl((CSL_vtm_cfg1Regs *)CSL_VTM_TEST_CFG1_BASE, \
628 CSL_VTM_VD_DOMAIN_1, ctrl, FALSE);
630 /* Print output ESM event to the screen */
632 #ifdef PRINT_EVENTS
633 DIAG_printf ("\n Got gtThr1 interrupt through ESM module\n");
634 DIAG_printf ("\n Crossed early warning threshold of gt_thr1 \n"
635 " System should take action to implement system cooling \n");
636 #endif
637 }
639 /*
640 * This function clears VTM THR2 interrupts for VTM input event to the ESM.
641 */
642 void diag_esm_vtmGtThr2CrossedUpdateInt(void)
643 {
644 CSL_vtm_vdThr_interrupt_ctrl ctrl;
645 /* Ack the interrupt, by clearing the pending bit */
646 ctrl = (CSL_VTM_VD_GT_THR2_INTR_EN_CLR | \
647 CSL_VTM_VD_GT_THR2_INTR_RAW_CLR);
648 CSL_vtmVdThrIntrCtrl((CSL_vtm_cfg1Regs *)CSL_VTM_TEST_CFG1_BASE, \
649 CSL_VTM_VD_DOMAIN_1, ctrl, FALSE);
651 #ifdef PRINT_EVENTS
652 /* Print output ESM event to the screen */
653 DIAG_printf ("\n Got gtThr2 interrupt through ESM module\n");
654 DIAG_printf ("\n Crossed critical threshold of gt_thr2 \n"
655 " System should take critical action to implement system cooling \n");
656 #endif
657 }
659 /*
660 * This function starts timer for clearing the MCU_SAFETY_ERRORn pin.
661 * When this timer expires, the pin clearing function will be initiated.
662 */
663 void diag_esm_startPinClearTimer(bool forcePinClear)
664 {
665 /* Start timer to clear the pin at a certain time */
666 TimerP_Status timerStatus;
668 immediatePinClear = forcePinClear;
670 timerStatus = TimerP_start(esmPinTimerHandle);
671 if (timerStatus != TimerP_OK) {
672 DIAG_printf("ERR: Could not start the timer %d \n",
673 PIN_TIMING_TIMER_ID);
674 } else {
675 #ifdef PRINT_DEBUG
676 DIAG_printf("Started timer to simulate MCU addressing error\n");
677 #endif
678 }
680 }
682 /*
683 * This function prints the current VTM thresholds.
684 */
685 int32_t diag_esm_printCurrentThresholds(void)
686 {
687 int32_t retVal = 0;
689 CSL_vtm_cfg1Regs *p_vtm_cfg1_regs = (CSL_vtm_cfg1Regs *) CSL_VTM_TEST_CFG1_BASE;
690 CSL_vtm_tsThrVal tsThrVal;
692 tsThrVal.thrValidMap = CSL_VTM_GT_TH1_VALID | \
693 CSL_VTM_GT_TH2_VALID | \
694 CSL_VTM_LT_TH0_VALID;
696 retVal = CSL_vtmTsGetThresholds (p_vtm_cfg1_regs,
697 CSL_VTM_TS_ID_0,
698 &tsThrVal);
700 #ifdef PRINT_EVENTS
701 DIAG_printf("Current VTM Thresholds: lt_thr0 = %d, gt_thr1 = %d, gt_thr2 = %d\n",
702 tsThrVal.ltTh0, tsThrVal.gtTh1, tsThrVal.gtTh2);
703 #endif
704 return (retVal);
705 }
707 int32_t diag_esm_setThresholdsForCriticalTrigger(void)
708 {
709 int32_t retVal = 0;
710 #ifdef DEBUG_PRINT
711 DIAG_printf("diag_esm_setThresholdsForCriticalTrigger: setting thresholds " \
712 "to trigger high priority thermal event.\n");
713 #endif
714 retVal = vtmTriggerTh(-12000, -8000, -3000);
716 return (retVal);
717 }
719 /*
720 * This function resets the VTM thresholds back to some typical default
721 * values.
722 */
723 int32_t diag_esm_setNormalThresholds(void)
724 {
725 int32_t retVal = 0;
726 int32_t ts_id = 0;
727 CSL_vtm_adc_code adc_code_lt_thr0, adc_code_gt_thr1, adc_code_gt_thr2;
728 int32_t gt_thr2_val, gt_thr1_val, lt_thr0_val;
729 CSL_vtm_vdThr_interrupt_ctrl ctrl;
731 #ifdef DEBUG_PRINT
732 DIAG_printf("Start changing to normal VTM threshold setting\n");
733 #endif
735 /* Disable interrupts while changing thresholds */
736 ctrl = (CSL_VTM_VD_GT_THR2_INTR_EN_CLR | \
737 CSL_VTM_VD_GT_THR2_INTR_RAW_CLR | \
738 CSL_VTM_VD_GT_THR1_INTR_EN_CLR | \
739 CSL_VTM_VD_GT_THR1_INTR_RAW_CLR | \
740 CSL_VTM_VD_LT_THR0_INTR_EN_CLR | \
741 CSL_VTM_VD_LT_THR0_INTR_RAW_CLR);
742 CSL_vtmVdThrIntrCtrl((CSL_vtm_cfg1Regs *)CSL_VTM_TEST_CFG1_BASE, \
743 CSL_VTM_VD_DOMAIN_1, ctrl, FALSE);
745 /* Set to default values */
746 lt_thr0_val = LT_THR0_DEFAULT;
747 gt_thr1_val = GT_THR1_DEFAULT;
748 gt_thr2_val = GT_THR2_DEFAULT;
750 CSL_vtmTsConvTempToAdc(lt_thr0_val, ts_id, &adc_code_lt_thr0);
751 CSL_vtmTsConvTempToAdc(gt_thr1_val, ts_id, &adc_code_gt_thr1);
752 CSL_vtmTsConvTempToAdc(gt_thr2_val, ts_id, &adc_code_gt_thr2);
754 #ifdef DEBUG_PRINT
755 DIAG_printf ("diag_esm_setNormalThresholds: Setting lt_thr0_val temp to %d " \
756 "millidegrees Celsius, and adc_code_lt_thr0 = %d\n",
757 lt_thr0_val,
758 adc_code_lt_thr0);
759 DIAG_printf ("diag_esm_setNormalThresholds: Setting gt_thr1_val temp to %d " \
760 "millidegrees Celsius, and adc_code_gt_thr1 = %d\n",
761 gt_thr1_val,
762 adc_code_gt_thr1);
763 DIAG_printf ("diag_esm_setNormalThresholds: Setting gt_thr2_val temp to %d " \
764 "millidegrees Celsius, and adc_code_gt_thr2 = %d\n",
765 gt_thr2_val,
766 adc_code_gt_thr2);
767 #endif
769 setAllVTMTempThr(adc_code_lt_thr0, adc_code_gt_thr1, adc_code_gt_thr2);
771 #ifdef DEBUG_PRINT
772 DIAG_printf("Finished normal VTM threshold setting\n");
773 #endif
775 return (retVal);
776 }
778 /* USE CASE FUNCTIONS */
780 /*
781 * This function initiates the input trigger event for each use case
782 */
783 int32_t diag_esm_runUseCaseTrigger(uint8_t useCaseId)
784 {
785 TimerP_Status timerStatus;
786 int32_t retVal = 0;
788 DIAG_printf("\nStarting Use Case %d \n", useCaseId);
789 switch(useCaseId)
790 {
791 case 0:
792 /* UC-1: Low Priority interrupt on WKUP ESM -
793 * VTM greater than THR1 */
794 currEsmInstance = SDR_ESM_INSTANCE_WKUP;
795 retVal = vtmTriggerTh1();
796 if (retVal == 0) {
797 esmEventInputTrig[useCaseId] = USE_CASE_STATUS_COMPLETED_SUCCESS;
798 } else {
799 esmEventInputTrig[useCaseId] = USE_CASE_STATUS_COMPLETED_FAILURE;
800 }
801 /* This use case only has low priority interrupts not routed to
802 * the pin, so this flag can be set at beginning of the test */
803 esmPinClearResult[0] = USE_CASE_STATUS_COMPLETED_SUCCESS;
804 break;
806 case 1:
807 /* UC-2: High Priority interrupt on WKUP ESM -
808 * VTM greater than THR2, no clearing of
809 * MCU_SAFETY_ERRORn pin */
810 currEsmInstance = SDR_ESM_INSTANCE_WKUP;
811 retVal = vtmTriggerTh1();
812 if (retVal == 0) {
813 esmEventInputTrig[useCaseId] = USE_CASE_STATUS_COMPLETED_SUCCESS;
814 } else {
815 esmEventInputTrig[useCaseId] = USE_CASE_STATUS_COMPLETED_FAILURE;
816 }
817 break;
819 case 2:
820 /* UC-3: High Priority interrupt on WKUP ESM -
821 * VTM greater than THR2 with clearing
822 * of MCU_SAFETY_ERRORn pin */
824 /* Start the Pin Control and Measurement Timer */
825 currEsmInstance = SDR_ESM_INSTANCE_WKUP;
826 retVal = vtmTriggerTh1();
827 if (retVal == 0) {
828 esmEventInputTrig[useCaseId] = USE_CASE_STATUS_COMPLETED_SUCCESS;
829 } else {
830 esmEventInputTrig[useCaseId] = USE_CASE_STATUS_COMPLETED_FAILURE;
831 }
832 break;
834 case 3:
835 /* UC-4: Low Priority interrupt on MCU ESM -
836 * MCU TIMER 2 expiration */
837 currEsmInstance = SDR_ESM_INSTANCE_MCU;
838 /* Start the Low Priority Event Trigger Timer */
839 timerStatus = TimerP_start(esmLoEventTrigTimerHandle);
840 if (timerStatus != TimerP_OK) {
841 DIAG_printf("ERR: Could not start the timer %d \n",
842 LOW_PRIO_TRIG_TIMER_ID);
843 retVal = -1;
844 }
845 /* esmEventInputTrig flag is set in expiration of the timer */
846 /* This use case only has low priority interrupts not routed to
847 * the pin, so this flag can be set at beginning of the test */
848 esmPinClearResult[useCaseId] = USE_CASE_STATUS_COMPLETED_SUCCESS;
849 break;
851 case 4:
852 /* UC-5: High Priority interrupt on MCU ESM -
853 * MCU TIMER 1 expiration */
854 /* Start the High Priority Event Trigger Timer */
855 currEsmInstance = SDR_ESM_INSTANCE_MCU;
856 timerStatus = TimerP_start(esmHiEventTrigTimerHandle);
857 if (timerStatus != TimerP_OK) {
858 DIAG_printf("ERR: Could not start the timer %d \n",
859 HIGH_PRIO_TRIG_TIMER_ID);
860 esmEventInputTrig[useCaseId] = USE_CASE_STATUS_COMPLETED_FAILURE;
861 retVal = -1;
862 }
863 /* esmEventInputTrig flag is set in expiration of the Hi Event timer */
864 /* esmPinClearResult flag is set in expiration of the Pin timer */
865 break;
867 default:
868 DIAG_printf("ERR: Invalid Use Case ID %d \n", useCaseId);
869 retVal = -1;
870 break;
871 }
873 return (retVal);
874 }