1 /** @file sys_startup.c
2 * @brief Startup Source File
13 * which are relevant for the Startup.
16 /* (c) Texas Instruments 2009-2012, All rights reserved. */
18 /* USER CODE BEGIN (0) */
25 #include "sys/system.h"
26 #include "sys/sys_vim.h"
27 #include "sys/sys_core.h"
28 #include "sys/sys_selftest.h"
29 #include "sys/ti_drv_esm.h"
32 /* USER CODE BEGIN (1) */
36 /* Type Definitions */
38 typedef void (*handler_fptr)(const uint8_t *in, uint8_t *out);
40 /* USER CODE BEGIN (2) */
44 /* External Functions */
46 #pragma WEAK(__TI_Handler_Table_Base)
47 #pragma WEAK(__TI_Handler_Table_Limit)
48 #pragma WEAK(__TI_CINIT_Base)
49 #pragma WEAK(__TI_CINIT_Limit)
51 extern uint32_t __TI_Handler_Table_Base;
52 extern uint32_t __TI_Handler_Table_Limit;
53 extern uint32_t __TI_CINIT_Base;
54 extern uint32_t __TI_CINIT_Limit;
55 extern uint32_t __TI_PINIT_Base;
56 extern uint32_t __TI_PINIT_Limit;
57 extern uint32_t * __binit__;
59 extern void main(void);
60 extern void exit(void);
62 extern void muxInit(void);
64 /* USER CODE BEGIN (3) */
68 /* Vim Ram Definition */
70 * @brief Vim Ram Definition
72 * This type is used to access the Vim Ram.
75 * @brief Vim Ram Type Definition
77 * This type is used to access the Vim Ram.
79 typedef volatile struct vimRam
81 t_isrFuncPTR ISR[VIM_CHANNELS + 1];
84 #define vimRAM ((vimRAM_t *)0xFFF82000U)
86 static const t_isrFuncPTR s_vim_init[] =
89 &esmHighInterrupt, // 0
91 #if FREERTOS_VERSION_NUMBER_MAYOR == 7 && \
92 FREERTOS_VERSION_NUMBER_MINOR == 0 && \
93 FREERTOS_VERSION_NUMBER_REV == 2
94 &vPreemptiveTick, // FreeRTOS 7.0.2
96 &vPortPreemptiveTick, // FreeRTOS 7.4.0 and 7.4.2
100 &phantomInterrupt, // 5
105 &phantomInterrupt, // 10
107 &mibspi1HighLevelInterrupt,
108 &linHighLevelInterrupt,
110 &adc1Group1Interrupt, // 15
111 &can1HighLevelInterrupt,
112 &spi2HighLevelInterrupt,
115 &phantomInterrupt, // 20
120 &phantomInterrupt, // 25
121 &mibspi1LowLevelInterrupt,
125 &spi2LowLevelInterrupt, // 30
130 &can2HighLevelInterrupt, // 35
132 &mibspi3HighInterruptLevel,
133 &mibspi3LowLevelInterrupt,
135 &phantomInterrupt, // 40
140 &can3HighLevelInterrupt, // 45
144 &spi4HighLevelInterrupt,
145 &phantomInterrupt, // 50
146 &adc2Group1Interrupt,
149 &spi4LowLevelInterrupt,
150 &phantomInterrupt, // 55
155 &phantomInterrupt, // 60
159 &sciHighLevelInterrupt,
160 &phantomInterrupt, // 65
165 &phantomInterrupt, // 70
170 &phantomInterrupt, // 75
175 &phantomInterrupt, // 80
180 &phantomInterrupt, // 85
185 &phantomInterrupt, // 90
190 &phantomInterrupt, // 95
195 &phantomInterrupt, // 100
226 /* Startup Routine */
228 /* USER CODE BEGIN (4) */
231 #pragma INTERRUPT(_c_int00, RESET)
236 /* USER CODE BEGIN (5) */
239 /* Initialize Core Registers to avoid CCM Error */
240 _coreInitRegisters_();
242 /* USER CODE BEGIN (6) */
245 /* Initialize Stack Pointers */
246 _coreInitStackPointer_();
248 /* USER CODE BEGIN (7) */
251 /* Implement work-around for CCM-R4 issue on silicon revision A */
252 if (DEVICE_ID_REV == 0x802AAD05)
254 _esmCcmErrorsClear_();
257 /* USER CODE BEGIN (8) */
260 /* Enable response to ECC errors indicated by CPU for accesses to flash */
261 flashWREG->FEDACCTRL1 = 0x000A060A;
263 /* Enable CPU Event Export */
264 /* This allows the CPU to signal any single-bit or double-bit errors detected
265 * by its ECC logic for accesses to program flash or data RAM.
267 _coreEnableEventBusExport_();
269 /* Enable CPU ECC checking for ATCM (flash accesses) */
270 _coreEnableFlashEcc_();
272 /* USER CODE BEGIN (9) */
275 /* Reset handler: the following instructions read from the system exception status register
276 * to identify the cause of the CPU reset.
279 /* check for power-on reset condition */
280 if ((SYS_EXCEPTION & POWERON_RESET) != 0)
282 /* USER CODE BEGIN (10) */
285 /* clear all reset status flags */
286 SYS_EXCEPTION = 0xFFFF;
288 /* USER CODE BEGIN (11) */
291 /* continue with normal start-up sequence */
293 else if ((SYS_EXCEPTION & OSC_FAILURE_RESET) != 0)
295 /* Reset caused due to oscillator failure.
296 Add user code here to handle oscillator failure */
298 /* USER CODE BEGIN (12) */
301 else if ((SYS_EXCEPTION & WATCHDOG_RESET) !=0 )
304 * 1) windowed watchdog violation - Add user code here to handle watchdog violation.
305 * 2) ICEPICK Reset - After loading code via CCS / System Reset through CCS
307 /* Check the WatchDog Status register */
308 if(WATCHDOG_STATUS != 0U)
310 /* Add user code here to handle watchdog violation. */
311 /* USER CODE BEGIN (13) */
314 /* Clear the Watchdog reset flag in Exception Status register */
315 SYS_EXCEPTION = WATCHDOG_RESET;
317 /* USER CODE BEGIN (14) */
322 /* Clear the ICEPICK reset flag in Exception Status register */
323 SYS_EXCEPTION = ICEPICK_RESET;
324 /* USER CODE BEGIN (15) */
328 else if ((SYS_EXCEPTION & CPU_RESET) !=0 )
330 /* Reset caused due to CPU reset.
331 CPU reset can be caused by CPU self-test completion, or
332 by toggling the "CPU RESET" bit of the CPU Reset Control Register. */
334 /* USER CODE BEGIN (16) */
337 /* clear all reset status flags */
338 SYS_EXCEPTION = CPU_RESET;
340 /* USER CODE BEGIN (17) */
344 else if ((SYS_EXCEPTION & SW_RESET) != 0)
346 /* Reset caused due to software reset.
347 Add user code to handle software reset. */
349 /* USER CODE BEGIN (18) */
354 /* Reset caused by nRST being driven low externally.
355 Add user code to handle external reset. */
357 /* USER CODE BEGIN (19) */
361 /* Check if there were ESM group3 errors during power-up.
362 * These could occur during eFuse auto-load or during reads from flash OTP
363 * during power-up. Device operation is not reliable and not recommended
365 * An ESM group3 error only drives the nERROR pin low. An external circuit
366 * that monitors the nERROR pin must take the appropriate action to ensure that
367 * the system is placed in a safe state, as determined by the application.
369 if (esmREG->ESTATUS1[2])
371 /* USER CODE BEGIN (20) */
376 /* USER CODE BEGIN (21) */
379 /* Initialize System - Clock, Flash settings with Efuse self check */
383 /* USER CODE BEGIN (24) */
386 /* Run a diagnostic check on the memory self-test controller.
387 * This function chooses a RAM test algorithm and runs it on an on-chip ROM.
388 * The memory self-test is expected to fail. The function ensures that the PBIST controller
389 * is capable of detecting and indicating a memory self-test failure.
393 /* USER CODE BEGIN (26) */
397 /* Run PBIST on CPU RAM.
398 * The PBIST controller needs to be configured separately for single-port and dual-port SRAMs.
399 * The CPU RAM is a single-port memory. The actual "RAM Group" for all on-chip SRAMs is defined in the
402 pbistRun(0x08300020, /* ESRAM Single Port PBIST */
405 /* USER CODE BEGIN (27) */
408 /* Wait for PBIST for CPU RAM to be completed */
409 while(!pbistIsTestCompleted());
411 /* USER CODE BEGIN (28) */
414 /* Check if CPU RAM passed the self-test */
415 if( pbistIsTestPassed() != TRUE)
417 /* CPU RAM failed the self-test.
418 * Need custom handler to check the memory failure
419 * and to take the appropriate next step.
421 if(pbistPortTestStatus(PBIST_PORT0) != TRUE)
423 memoryPort0TestFailNotification(pbistREG->RGS, pbistREG->RDS, pbistREG->FSRA0, pbistREG->FSRDL0);
425 else if(pbistPortTestStatus(PBIST_PORT1) != TRUE)
427 memoryPort1TestFailNotification(pbistREG->RGS, pbistREG->RDS, pbistREG->FSRA1, pbistREG->FSRDL1);
435 /* USER CODE BEGIN (29) */
438 /* Disable PBIST clocks and disable memory self-test mode */
441 /* USER CODE BEGIN (30) */
444 /* Initialize CPU RAM.
445 * This function uses the system module's hardware for auto-initialization of memories and their
446 * associated protection schemes. The CPU RAM is initialized by setting bit 0 of the MSIENA register.
447 * Hence the value 0x1 passed to the function.
448 * This function will initialize the entire CPU RAM and the corresponding ECC locations.
452 /* USER CODE BEGIN (31) */
455 /* Enable ECC checking for TCRAM accesses.
456 * This function enables the CPU's ECC logic for accesses to B0TCM and B1TCM.
458 _coreEnableRamEcc_();
460 /* USER CODE BEGIN (32) */
463 /* Start PBIST on all dual-port memories */
464 pbistRun( 0x00000000 /* EMAC Dual Port PBIST */
465 | 0x00000000 /* USB Dual Port PBIST for RMx / Reserved for TMS570x */
466 | 0x00000800 /* DMA Dual Port PBIST */
467 | 0x00000200 /* VIM Dual Port PBIST */
468 | 0x00000040 /* MIBSPI1 Dual Port PBIST */
469 | 0x00000080 /* MIBSPI3 Dual Port PBIST */
470 | 0x00000100 /* MIBSPI5 Dual Port PBIST */
471 | 0x00000004 /* CAN1 Dual Port PBIST */
472 | 0x00000008 /* CAN2 Dual Port PBIST */
473 | 0x00000010 /* CAN3 Dual Port PBIST */
474 | 0x00000400 /* ADC1 Dual Port PBIST */
475 | 0x00020000 /* ADC2 Dual Port PBIST */
476 | 0x00001000 /* HET1 Dual Port PBIST */
477 | 0x00040000 /* HET2 Dual Port PBIST */
478 | 0x00002000 /* HTU1 Dual Port PBIST */
479 | 0x00080000 /* HTU2 Dual Port PBIST */
480 | 0x00004000 /* RTP Dual Port PBIST */
481 | 0x00000000 /* FTU Dual Port PBIST for TMS570x / Reserved for RMx */
482 | 0x00008000 /* FRAY Dual Port PBIST for TMS570x / Reserved for RMx */
483 , PBIST_March13N_DP);
485 /* USER CODE BEGIN (33) */
489 /* Test the CPU ECC mechanism for RAM accesses.
490 * The checkBxRAMECC functions cause deliberate single-bit and double-bit errors in TCRAM accesses
491 * by corrupting 1 or 2 bits in the ECC. Reading from the TCRAM location with a 2-bit error
492 * in the ECC causes a data abort exception. The data abort handler is written to look for
493 * deliberately caused exception and to return the code execution to the instruction
494 * following the one that caused the abort.
497 tcram1REG->RAMCTRL &= ~(0x00000100); /* disable writes to ECC RAM */
498 tcram2REG->RAMCTRL &= ~(0x00000100);
501 tcram1REG->RAMCTRL &= ~(0x00000100); /* disable writes to ECC RAM */
502 tcram2REG->RAMCTRL &= ~(0x00000100);
504 /* USER CODE BEGIN (34) */
508 /* Test the CPU ECC mechanism for Flash accesses.
509 * The checkFlashECC function uses the flash interface module's diagnostic mode 7
510 * to create single-bit and double-bit errors in CPU accesses to the flash. A double-bit
511 * error on reading from flash causes a data abort exception.
512 * The data abort handler is written to look for deliberately caused exception and
513 * to return the code execution to the instruction following the one that was aborted.
517 flashWREG->FDIAGCTRL = 0x000A0007; /* disable flash diagnostic mode */
519 /* USER CODE BEGIN (35) */
522 /* USER CODE BEGIN (36) */
525 /* Wait for PBIST for CPU RAM to be completed */
526 while(!pbistIsTestCompleted());
528 /* USER CODE BEGIN (37) */
531 /* Check if CPU RAM passed the self-test */
532 if( pbistIsTestPassed() != TRUE)
535 /* USER CODE BEGIN (38) */
538 /* CPU RAM failed the self-test.
539 * Need custom handler to check the memory failure
540 * and to take the appropriate next step.
542 if(pbistPortTestStatus(PBIST_PORT0) != TRUE)
544 memoryPort0TestFailNotification(pbistREG->RGS, pbistREG->RDS, pbistREG->FSRA0, pbistREG->FSRDL0);
546 else if(pbistPortTestStatus(PBIST_PORT1) != TRUE)
548 memoryPort1TestFailNotification(pbistREG->RGS, pbistREG->RDS, pbistREG->FSRA1, pbistREG->FSRDL1);
556 /* USER CODE BEGIN (39) */
559 /* Disable PBIST clocks and disable memory self-test mode */
563 /* USER CODE BEGIN (45) */
566 /* Release the MibSPI1 modules from local reset.
567 * This will cause the MibSPI1 RAMs to get initialized along with the parity memory.
569 mibspiREG1->GCR0 = 0x1;
571 /* Release the MibSPI3 modules from local reset.
572 * This will cause the MibSPI3 RAMs to get initialized along with the parity memory.
574 mibspiREG3->GCR0 = 0x1;
576 /* Release the MibSPI5 modules from local reset.
577 * This will cause the MibSPI5 RAMs to get initialized along with the parity memory.
579 mibspiREG5->GCR0 = 0x1;
581 /* USER CODE BEGIN (46) */
584 /* Initialize all on-chip SRAMs except for MibSPIx RAMs
585 * The MibSPIx modules have their own auto-initialization mechanism which is triggered
586 * as soon as the modules are brought out of local reset.
588 /* The system module auto-init will hang on the MibSPI RAM if the module is still in local reset.
590 _memoryInit_( 1 << 1 /* DMA Memory Init */
591 | 1 << 2 /* VIM Memory Init */
592 | 1 << 5 /* CAN1 Memory Init */
593 | 1 << 6 /* CAN2 Memory Init */
594 | 1 << 10 /* CAN3 Memory Init */
595 | 1 << 8 /* ADC1 Memory Init */
596 | 1 << 14 /* ADC2 Memory Init */
597 | 1 << 3 /* HET1 Memory Init */
598 | 1 << 4 /* HTU1 Memory Init */
599 | 1 << 15 /* HET2 Memory Init */
600 | 1 << 16 /* HTU2 Memory Init */
601 | 1 << 13); /* Reserved for RMx Family / FTU Memory Init for TMS570x Family */
603 /* Test the parity protection mechanism for peripheral RAMs
604 * The following memories have parity protection that needs to be checked:
605 * VIM, DMA, ADC1, ADC2, NHET1, NHET2, HTU1, HTU2, FlexRay, FTU,
606 * MibSPI1, MibSPI3, MibSPI5, DCAN1, DCAN2, DCAN3 based on user selection
609 /* USER CODE BEGIN (47) */
614 /* USER CODE BEGIN (48) */
619 /* USER CODE BEGIN (49) */
624 /* USER CODE BEGIN (50) */
629 /* USER CODE BEGIN (51) */
634 /* USER CODE BEGIN (52) */
639 /* USER CODE BEGIN (53) */
644 /* USER CODE BEGIN (54) */
649 /* USER CODE BEGIN (55) */
654 /* USER CODE BEGIN (56) */
659 /* USER CODE BEGIN (57) */
665 /* USER CODE BEGIN (58) */
668 while (mibspiREG1->BUFINIT); /* wait for MibSPI1 RAM to complete initialization */
669 while (mibspiREG3->BUFINIT); /* wait for MibSPI3 RAM to complete initialization */
670 while (mibspiREG5->BUFINIT); /* wait for MibSPI5 RAM to complete initialization */
672 /* USER CODE BEGIN (59) */
675 mibspi1ParityCheck();
677 /* USER CODE BEGIN (60) */
680 mibspi3ParityCheck();
682 /* USER CODE BEGIN (61) */
685 mibspi5ParityCheck();
688 /* USER CODE BEGIN (62) */
692 /* USER CODE BEGIN (63) */
696 /* Initialize VIM table */
700 for (i = 0; i < (VIM_CHANNELS + 1); i++)
702 vimRAM->ISR[i] = s_vim_init[i];
706 /* set IRQ/FIQ priorities */
707 vimREG->FIRQPR0 = SYS_FIQ
740 vimREG->FIRQPR1 = SYS_IRQ
774 vimREG->FIRQPR2 = SYS_IRQ
787 | (SYS_FIQ << 13U) // EMAC
789 | (SYS_FIQ << 15U) // EMAC
807 vimREG->FIRQPR3 = SYS_IRQ
841 /* enable interrupts */
842 vimREG->REQMASKSET0 = 1U
875 vimREG->REQMASKSET1 = 0U
908 vimREG->REQMASKSET2 = 1U
921 | (1U << 13U) // EMACCore0TxIsr
923 | (1U << 15U) // EMACCore0RxIsr
941 vimREG->REQMASKSET3 = 0U
974 /* USER CODE BEGIN (64) */
977 /* Configure system response to error conditions signaled to the ESM group1 */
978 /* This function can be configured from the ESM tab of HALCoGen */
981 /* initalise copy table */
982 if ((uint32_t *)&__binit__ != (uint32_t *)0xFFFFFFFFU)
984 extern void copy_in(void *binit);
985 copy_in((void *)&__binit__);
988 /* initalise the C global variables */
989 if (&__TI_Handler_Table_Base < &__TI_Handler_Table_Limit)
991 uint8_t **tablePtr = (uint8_t **)&__TI_CINIT_Base;
992 uint8_t **tableLimit = (uint8_t **)&__TI_CINIT_Limit;
994 while (tablePtr < tableLimit)
996 uint8_t *loadAdr = *tablePtr++;
997 uint8_t *runAdr = *tablePtr++;
998 uint8_t idx = *loadAdr++;
999 handler_fptr handler = (handler_fptr)(&__TI_Handler_Table_Base)[idx];
1001 (*handler)((const uint8_t *)loadAdr, runAdr);
1005 /* initalise contructors */
1006 if (__TI_PINIT_Base < __TI_PINIT_Limit)
1008 void (**p0)() = (void *)__TI_PINIT_Base;
1010 while ((uint32_t)p0 < __TI_PINIT_Limit)
1012 void (*p)() = *p0++;
1017 /* USER CODE BEGIN (65) */
1020 /* call the application */
1023 /* USER CODE BEGIN (66) */
1027 /* USER CODE BEGIN (67) */
1031 /* USER CODE BEGIN (68) */