cstart.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  1. /**************************************************************************
  2. ** *
  3. ** FILE : cstart.c *
  4. ** *
  5. ** DESCRIPTION : *
  6. ** The system startup code initializes the processor's registers *
  7. ** and the application C variables. *
  8. ** *
  9. ** Copyright 1996-2015 Altium BV *
  10. ** *
  11. **************************************************************************/
  12. #include "cstart.h" /* include configuration */
  13. #include <stdlib.h>
  14. #include <stdbool.h>
  15. #include "IfxAsclin_regdef.h"
  16. #include "IfxAsclin_reg.h"
  17. #include "IfxGtm_reg.h"
  18. #include "IfxGtm_regdef.h"
  19. #ifdef __CPU__
  20. //#include __SFRFILE__(__CPU__)
  21. #include "_Reg/IfxScu_reg.h"
  22. #include "special_startup_sfr.h"
  23. #endif
  24. #define _REGTC23X_H
  25. #pragma weak exit
  26. #pragma extern _Exit
  27. #pragma profiling off /* prevent profiling information on cstart */
  28. #pragma tradeoff 4 /* preset tradeoff level (for size) */
  29. #pragma runtime BCM /* disable runtime error checking for cstart */
  30. #pragma nomisrac /* disable MISRA-C checking */
  31. #pragma noclear /* bss clearing not before cinit */
  32. #pragma immediate_in_code /* no external ROM access before bus configuration */
  33. #if __USE_ARGC_ARGV
  34. static char argcv[__ARGCV_BUFSIZE] __align(4);
  35. extern int _argcv( const char *, size_t );
  36. #endif
  37. /* linker definitions */
  38. extern __far void _lc_ue_ustack[]; /* user stack end */
  39. extern __far void _lc_ue_istack[]; /* interrupt stack end */
  40. extern __far void _lc_u_int_tab[]; /* interrupt table */
  41. extern __far void _lc_u_trap_tab[]; /* trap table */
  42. extern __far void _SMALL_DATA_[]; /* centre of A0 addressable area */
  43. extern __far void _LITERAL_DATA_[]; /* centre of A1 addressable area */
  44. extern __far void _A8_DATA_[]; /* centre of A8 addressable area */
  45. extern __far void _A9_DATA_[]; /* centre of A9 addressable area */
  46. /* external functions */
  47. #if __C_INIT
  48. extern void _c_init(void); /* C initialization function */
  49. #endif
  50. #ifdef __CALL_INIT
  51. extern void _call_init(void); /* call a user function before main() */
  52. #endif
  53. #ifdef _CALL_ENDINIT
  54. extern void _endinit(void); /* call a user function with protection switched off */
  55. #endif
  56. /* local functions */
  57. static void __noinline__ __noreturn__ __jump__ __init_sp( void );
  58. static void __noinline__ __noreturn__ __jump__ _start( void );
  59. #if ( defined _REGTC26X_H || defined _REGTC26XB_H )
  60. #if __XC800_INIT
  61. static void _xc800_init ( void );
  62. #endif
  63. #endif
  64. /* library references */
  65. #pragma extern main
  66. extern int main( int argc, char *argv[] );
  67. /* endinit types */
  68. typedef enum
  69. {
  70. _ENDINIT_DISABLE,
  71. _ENDINIT_ENABLE
  72. } _endinit_t;
  73. /*
  74. * inlining is required for endinit_set and safety_endinit_set,
  75. * because the CSA list is not ready to support function calls.
  76. */
  77. #ifdef inline
  78. #undef inline
  79. #endif
  80. static void inline endinit_set(_endinit_t endinit_value);
  81. /* external interface to endinit_set */
  82. extern void _endinit_clear(void) { endinit_set(_ENDINIT_DISABLE); }
  83. extern void _endinit_set (void) { endinit_set(_ENDINIT_ENABLE); }
  84. #if __PROF_ENABLE__
  85. extern void __prof_init( void );
  86. #endif
  87. #if __CLOCKS_PER_SEC_INIT
  88. static void setclockpersec( void );
  89. extern unsigned long long setfoschz( unsigned long long );
  90. #endif
  91. #if ( defined _REGUSERDEF16X_H || defined _REGUSERDEF162_H || defined _REGTC21X_H || defined _REGTC22X_H || defined _REGTC23X_ADAS_H || defined _REGTC23X_H || defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H || defined _REGTC26X_H || defined _REGTC26XB_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  92. #define WDT_CON0 SCU_WDTCPU0CON0
  93. #define WDT_CON1 SCU_WDTCPU0CON1
  94. static void inline safety_endinit_set(_endinit_t endinit_value);
  95. /* external interface to endinit_set */
  96. extern void _safety_endinit_clear(void) { safety_endinit_set(_ENDINIT_DISABLE); }
  97. extern void _safety_endinit_set (void) { safety_endinit_set(_ENDINIT_ENABLE); }
  98. #endif
  99. /* Configure start TC1 and TC2 */
  100. #if __START_INIT_TC1
  101. #define __STADD_INIT_TC1 1 /* Initialize start address TC1 */
  102. #define __HALT_INIT_TC1 1 /* Initialize HALT state of TC1 */
  103. #define __HALT_VALUE_TC1 2 /* Reset HALT TC1 */
  104. #else
  105. #define __STADD_INIT_TC1 0
  106. #define __HALT_INIT_TC1 0
  107. #define __HALT_VALUE_TC1 0
  108. #endif
  109. #if __START_INIT_TC2
  110. #define __STADD_INIT_TC2 1 /* Initialize start address TC2 */
  111. #define __HALT_INIT_TC2 1 /* Initialize HALT state of TC2 */
  112. #define __HALT_VALUE_TC2 2 /* Reset HALT TC2 */
  113. #else
  114. #define __STADD_INIT_TC2 0
  115. #define __HALT_INIT_TC2 0
  116. #define __HALT_VALUE_TC2 0
  117. #endif
  118. #if ( defined _REGUSERDEF16X_H || defined _REGUSERDEF162_H || defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H || defined _REGTC26X_H || defined _REGTC26XB_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  119. #if (__HALT_INIT_TC1 && (__HALT_VALUE_TC1 & 0x2)) || (__HALT_INIT_TC2 && (__HALT_VALUE_TC2 & 0x2))
  120. #if __C_INIT || __CLOCKS_PER_SEC_INIT || __PROF_ENABLE__ || _CALL_ENDINIT || (defined __CALL_INIT)
  121. /* c init semaphores */
  122. #if (defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  123. #define __AT_NOT_CACHED_MEMORY__ __at(0x70000000)
  124. #else
  125. #define __AT_NOT_CACHED_MEMORY__
  126. #endif
  127. unsigned int volatile __far __align(4) _tcx_end_c_init __AT_NOT_CACHED_MEMORY__; /* _tcx_end_c_init := 1 << core_id */
  128. #endif
  129. #endif
  130. #endif
  131. /*********************************************************************************
  132. * reset vector
  133. *********************************************************************************/
  134. #pragma section code libc.reset
  135. #pragma optimize g
  136. void _START( void )
  137. {
  138. __init_sp();
  139. }
  140. #pragma optimize restore
  141. #pragma section code restore
  142. /*********************************************************************************
  143. * EBU boot configuration
  144. *********************************************************************************/
  145. #if defined EBU_BOOTCFG && defined __EBU_BOOTCFG_VALUE
  146. #if __EBU_BOOTCFG_INIT
  147. #pragma section farrom libc.ebu_bootcfg
  148. const int __far _EBU_BOOTCFG __protect__ = __EBU_BOOTCFG_VALUE; /* locate at RESET + EBU_ALIGN */
  149. #pragma section farrom restore
  150. #endif
  151. #endif
  152. /*********************************************************************************
  153. * initialize stackpointer
  154. *********************************************************************************/
  155. static void __noinline__ __noreturn__ __jump__ __init_sp( void )
  156. {
  157. /* The initialization of the stackpointer is done in a seperate function,
  158. because it must be executed in a function which has no stack use.
  159. When a function uses a stack it will be updated at the start of the
  160. function. In this function, however, the stackpointer has an irrelevant
  161. value at the start of the function. A starting value for the stackpointer
  162. is determined and written in this function. The update to the
  163. stackpointer at the start of the function would therefore be undone.
  164. */
  165. /*
  166. * CPU_TC.033 workaround:
  167. * The stack pointers are aligned to quad-word boundary to workaround these
  168. * functional problems CPU_TC.033.
  169. * Also the C compiler workaround for the appropriate CPU function problem
  170. * need to be enabled with --silicon-bug=cpu-tc033, to align circular
  171. * buffers on a quad-word boundary and to size all stack frames to an integral
  172. * number of quad-words.
  173. */
  174. #if __CPU_TC033_INITIAL__
  175. # define STACK_ALIGN 0xfffffff0
  176. #else
  177. # define STACK_ALIGN 0xfffffff8
  178. #endif
  179. /*
  180. * Load user stack pointer.
  181. * Disable this if not started from RESET vector. (E.g.
  182. * ROM monitors require to keep in control of vectors)
  183. */
  184. #if __USP_INIT
  185. void * sp = (void *)((unsigned int)(_lc_ue_ustack) & STACK_ALIGN);
  186. __set_sp( sp );
  187. #endif
  188. _start();
  189. }
  190. static void inline reset_psw( void )
  191. {
  192. unsigned int value = 0x980; /* Reset value */
  193. value |= (__CDC & 0x7f); /* PSW.CDC call depth counter */
  194. #if __USER_STACK
  195. value &= ~0x200; /* clear PSW.IS */
  196. #else
  197. value |= 0x200; /* set PSW.IS */
  198. #endif
  199. __mtcr(PSW, value);
  200. }
  201. /*********************************************************************************
  202. * startup code
  203. *********************************************************************************/
  204. static void __noinline__ __noreturn__ __jump__ _start( void )
  205. {
  206. #if ( defined _REGUSERDEF16X_H || defined _REGUSERDEF162_H || defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H || defined _REGTC26X_H || defined _REGTC26XB_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  207. #if __STADD_INIT_TC1
  208. /*
  209. * Set start address of TC1
  210. * Reset value is 0xAFFFC000
  211. */
  212. extern void __noinline__ __noreturn__ __jump__ _start_tc1( void );
  213. CPU1_PC.U = (unsigned int)_start_tc1;
  214. #endif
  215. #if __STADD_INIT_TC2 && !( defined _REGTC26X_H || defined _REGTC26XB_H )
  216. /*
  217. * Set start address of TC2
  218. * Reset value is 0xAFFFC000
  219. */
  220. extern void __noinline__ __noreturn__ __jump__ _start_tc2( void );
  221. CPU2_PC.U = (unsigned int)_start_tc2;
  222. #endif
  223. #if (__HALT_INIT_TC1 && (__HALT_VALUE_TC1 & 0x2)) || (__HALT_INIT_TC2 && (__HALT_VALUE_TC2 & 0x2))
  224. #if __C_INIT || __CLOCKS_PER_SEC_INIT || __PROF_ENABLE__ || _CALL_ENDINIT || (defined __CALL_INIT)
  225. /*
  226. * Clear end of c init semaphores for the cores that are released.
  227. */
  228. #if __HALT_INIT_TC1 && __HALT_VALUE_TC1==2
  229. #define __TC1_END_C_INIT_MASK 0
  230. #else
  231. #define __TC1_END_C_INIT_MASK 1<<1
  232. #endif
  233. #if __HALT_INIT_TC2 && !( defined _REGTC26X_H || defined _REGTC26XB_H ) && __HALT_VALUE_TC2==2
  234. #define __TC2_END_C_INIT_MASK 0
  235. #else
  236. #define __TC2_END_C_INIT_MASK 1<<2
  237. #endif
  238. _tcx_end_c_init = __TC1_END_C_INIT_MASK|__TC2_END_C_INIT_MASK;
  239. #endif
  240. #endif
  241. /*
  242. * A core can be released by resetting its halt state.
  243. * HALT: 0 R:running W:unchanged
  244. * 1 R:halted W:unchanged
  245. * 2 R:n.a. W:reset halt
  246. * 3 R:n.a. W:set halt when debug enable
  247. */
  248. #if __HALT_INIT_TC1
  249. CPU1_DBGSR.B.HALT=__HALT_VALUE_TC1;
  250. #endif
  251. #if __HALT_INIT_TC2 && !( defined _REGTC26X_H || defined _REGTC26XB_H )
  252. CPU2_DBGSR.B.HALT=__HALT_VALUE_TC2;
  253. #endif
  254. #endif
  255. /* Do a dsync before changing any of the csfr values, thus any previous
  256. * background state gets flushed first. Required for applications that jump
  257. * to the reset address.
  258. */
  259. __dsync();
  260. /* Set the PSW to its reset value in case of a warm start */
  261. reset_psw();
  262. /* Set the PCXS and PCXO to its reset value in case of a warm start */
  263. unsigned int pcxi = __mfcr(PCXI);
  264. pcxi &= 0xfff00000;
  265. __mtcr(PCXI, pcxi);
  266. #if ( defined _REGUSERDEF16X_H || defined _REGUSERDEF162_H || defined _REGTC21X_H || defined _REGTC22X_H || defined _REGTC23X_ADAS_H || defined _REGTC23X_H || defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H || defined _REGTC26X_H || defined _REGTC26XB_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  267. /*
  268. * Clear the ENDINIT bit in the WDTSCON0 register in order
  269. * to disable the write-protection for safety-critical registers
  270. * protected via the safety EndInit feature.
  271. */
  272. safety_endinit_set(_ENDINIT_DISABLE);
  273. /*
  274. * Disable the safety watchdog if requested. Safety watchdog
  275. * is enabled by default. The safety watchdog is disabled after
  276. * ENDINIT is set by safety_endinit_set().
  277. */
  278. #if __SAFETY_WATCHDOG_DISABLE
  279. SCU_WDTSCON1.U |= 0x8;
  280. safety_endinit_set(_ENDINIT_ENABLE);
  281. safety_endinit_set(_ENDINIT_DISABLE);
  282. ((Ifx_ASCLIN*) &MODULE_ASCLIN1 )->CLC.U=0;
  283. int test=((Ifx_ASCLIN*) &MODULE_ASCLIN1)->CLC.U;
  284. ((Ifx_GTM*) &MODULE_GTM)->CLC.B.DISR = 0;
  285. test=((Ifx_GTM*) &MODULE_GTM)->CLC.B.DISR;
  286. #endif
  287. #endif
  288. /*
  289. * Clear the ENDINIT bit in the WDT_CON0 register in order
  290. * to disable the write-protection for registers protected
  291. * via the EndInit feature (for example: WDT_CON1).
  292. */
  293. endinit_set(_ENDINIT_DISABLE);
  294. /*
  295. * Disable the Watchdog if requested. Watchdog is enabled by default.
  296. * The Watchdog is disabled after ENDINIT is set by endinit_set().
  297. */
  298. #if __WATCHDOG_DISABLE
  299. WDT_CON1.U |= 0x8;
  300. endinit_set(_ENDINIT_ENABLE);
  301. endinit_set(_ENDINIT_DISABLE);
  302. #endif
  303. /*
  304. * PMI_TC.003 workaround:
  305. * The TLB-A and TLB-B mappings are set to a page size of 16KB.
  306. */
  307. #if __PMI_TC003_INITIAL__ && defined MMU_CON
  308. unsigned int mmu_con = __mfcr(MMU_CON);
  309. mmu_con &= 0xffffffe1; /* clear SZA and SZB */
  310. mmu_con |= 0x00000014; /* set SZA=SZB=16k */
  311. __mtcr(MMU_CON, mmu_con);
  312. #endif
  313. /*
  314. * Load Base Address of Trap Vector Table.
  315. * Disable this if not started from RESET vector. (E.g.
  316. * ROM monitors require to keep in control of vectors)
  317. */
  318. #if __BTV_INIT
  319. __mtcr(BTV, (unsigned int)_lc_u_trap_tab);
  320. #endif
  321. /*
  322. * Load Base Address of Interrupt Vector Table.
  323. * Disable this if not started from RESET vector. (E.g.
  324. * ROM monitors require to keep in control of vectors)
  325. */
  326. #if __BIV_INIT
  327. #if __BIV_SINGLE_INIT && (defined _REGUSERDEF16X_H || defined _REGUSERDEF162_H || defined _REGTC21X_H || defined _REGTC22X_H || defined _REGTC23X_ADAS_H || defined _REGTC23X_H || defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H || defined _REGTC26X_H || defined _REGTC26XB_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  328. /*
  329. * Single Entry Vector Table, for core tc1.6.x, or tc1.6.2 is supported
  330. * by _sevt_isr_tc0() calling interrupt handlers installed
  331. * with _sevt_isr_install() stored in _sevt_isrs_tc0 list.
  332. * The _sevt_isr_tc0() is located at interrupt vector table
  333. * entry 64. The start address of the interrupt vector table
  334. * is defined by linker definition INTTAB0, default value
  335. *_lc_u_int_tab is 0xa00f0000+0x800.
  336. */
  337. #pragma extern _sevt_isr_tc0
  338. __mtcr(BIV, (unsigned int)(_lc_u_int_tab) | (0xff<<3) | 1 );
  339. #else
  340. #if __BIV_8BYTE_INIT && (defined _REGUSERDEF16X_H || defined _REGUSERDEF162_H || defined _REGTC21X_H || defined _REGTC22X_H || defined _REGTC23X_ADAS_H || defined _REGTC23X_H || defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H || defined _REGTC26X_H || defined _REGTC26XB_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  341. __mtcr(BIV, (unsigned int)(_lc_u_int_tab) | 1 ); /* 8 Byte vector spacing */
  342. #else
  343. __mtcr(BIV, (unsigned int)(_lc_u_int_tab)); /* 32 Byte vector spacing */
  344. #endif
  345. #endif
  346. #endif
  347. /*
  348. * Load interrupt stack pointer.
  349. * Disable this if not started from RESET vector. (E.g.
  350. * ROM monitors require to keep in control of vectors)
  351. */
  352. #if __ISP_INIT
  353. unsigned int isp = (unsigned int)(_lc_ue_istack) & STACK_ALIGN;
  354. __mtcr(ISP, isp);
  355. #endif
  356. /*
  357. * PMU/PMI configuration.
  358. */
  359. #if defined PMI_CON0 && defined __PMI_CON0_VALUE
  360. if(__PMI_CON0_INIT) PMI_CON0.U = __PMI_CON0_VALUE;
  361. #endif
  362. #if defined PMI_CON1 && defined __PMI_CON1_VALUE
  363. if(__PMI_CON1_INIT) PMI_CON1.U = __PMI_CON1_VALUE;
  364. #endif
  365. #if defined PMI_CON2 && defined __PMI_CON2_VALUE && \
  366. ( defined _REGTC1337_H || defined _REGTC1367_H || defined _REGTC1387_H || defined _REGTC1728_H \
  367. || defined _REGTC1746_H || defined _REGTC1747_H || defined _REGTC1768_H || defined _REGTC1782_H \
  368. || defined _REGTC1784_H || defined _REGTC1184_H || defined _REGTC1797_H || defined _REGTC1197_H \
  369. || defined _REGTC1767_H || defined _REGTC1736_H \
  370. || defined _REGTC1738_H || defined _REGTC1167_H || defined _REGTC1724_H )
  371. if(__PMI_CON2_INIT) PMI_CON2.U = __PMI_CON2_VALUE;
  372. #endif
  373. #if defined PMU_EIFCON && defined __PMU_EIFCON_VALUE
  374. if(__PMU_EIFCON_INIT) PMU_EIFCON.U = __PMU_EIFCON_VALUE;
  375. #endif
  376. /*
  377. * DMI configuration.
  378. */
  379. #if defined DMI_CON && defined __DMI_CON_VALUE && \
  380. ( defined _REGTC1167_H || defined _REGTC1197_H || defined _REGTC1337_H || defined _REGTC1367_H \
  381. || defined _REGTC1387_H || defined _REGTC1728_H || defined _REGTC1738_H \
  382. || defined _REGTC1746_H || defined _REGTC1747_H || defined _REGTC1767_H || defined _REGTC1768_H \
  383. || defined _REGTC1782_H || defined _REGTC1784_H || defined _REGTC1184_H || defined _REGTC1797_H \
  384. || defined _REGTC1724_H )
  385. if(__DMI_CON_INIT) DMI_CON.U = __DMI_CON_VALUE;
  386. #endif
  387. /*
  388. * Data cache control (Reset 0H).
  389. * CPU_TC013 workaround:
  390. * The 16KB D-Cache is enabled to workaround
  391. * the CPU_TC013 functional problem.
  392. * NOTE: Setting the D-Cache size must be done at the
  393. * very beginning of the startup code.
  394. */
  395. #if defined DMU_CON
  396. # if __CPU_TC013_INITIAL__
  397. DMU_CON.U = 0x1;
  398. # endif
  399. #endif
  400. /*
  401. * PCON0 configuration.
  402. */
  403. #if defined PCON0 && defined __PCON0_VALUE
  404. if(__PCON0_INIT) __mtcr(PCON0, __PCON0_VALUE);
  405. #endif
  406. /*
  407. * DCON0 configuration.
  408. */
  409. #if defined DCON0 && defined __DCON0_VALUE
  410. if(__DCON0_INIT) __mtcr(DCON0, __DCON0_VALUE);
  411. #endif
  412. /*
  413. * TC29x requires configuration of Pad Driver Mode to support EBU
  414. */
  415. #if defined P24_PDR0 && defined __P24_PDR0_VALUE
  416. if(__P24_PDR0_INIT) P24_PDR0.U = __P24_PDR0_VALUE;
  417. #endif
  418. #if defined P24_PDR1 && defined __P24_PDR1_VALUE
  419. if(__P24_PDR1_INIT) P24_PDR1.U = __P24_PDR1_VALUE;
  420. #endif
  421. #if defined P25_PDR0 && defined __P25_PDR0_VALUE
  422. if(__P25_PDR0_INIT) P25_PDR0.U = __P25_PDR0_VALUE;
  423. #endif
  424. #if defined P25_PDR1 && defined __P25_PDR1_VALUE
  425. if(__P25_PDR1_INIT) P25_PDR1.U = __P25_PDR1_VALUE;
  426. #endif
  427. #if defined P30_PDR0 && defined __P30_PDR0_VALUE
  428. if(__P30_PDR0_INIT) P30_PDR0.U = __P30_PDR0_VALUE;
  429. #endif
  430. #if defined P30_PDR1 && defined __P30_PDR1_VALUE
  431. if(__P30_PDR1_INIT) P30_PDR1.U = __P30_PDR1_VALUE;
  432. #endif
  433. #if defined P31_PDR0 && defined __P31_PDR0_VALUE
  434. if(__P31_PDR0_INIT) P31_PDR0.U = __P31_PDR0_VALUE;
  435. #endif
  436. #if defined P31_PDR1 && defined __P31_PDR1_VALUE
  437. if(__P31_PDR1_INIT) P31_PDR1.U = __P31_PDR1_VALUE;
  438. #endif
  439. /*
  440. * Bus configuration EBU_CLC within endinit.
  441. */
  442. # if defined EBU_CLC && defined __EBU_CLC_VALUE
  443. if(__EBU_CLC_INIT) EBU_CLC.U = __EBU_CLC_VALUE;
  444. # endif
  445. /*
  446. * Setting up the PLL after Reset.
  447. *
  448. * After reset, the system clock will be running at the VCO
  449. * base frequency divided by a factor KDIV. To set up the PLL
  450. * after reset, next action must be executed.
  451. */
  452. #if defined PLL_CLC && defined __PLL_CLC_VALUE
  453. #if __PLL_CLC_INIT
  454. #define __PLL_CLC_SYSFS_VALUE ((__PLL_CLC_VALUE>>2)&0x1)
  455. #define __PLL_CLC_VCOBYP_VALUE ((__PLL_CLC_VALUE>>5)&0x1)
  456. #define __PLL_CLC_VCOSEL_VALUE ((__PLL_CLC_VALUE>>6)&0x3)
  457. #define __PLL_CLC_KDIV_VALUE ((__PLL_CLC_VALUE>>8)&0xf)
  458. #define __PLL_CLC_PDIV_VALUE ((__PLL_CLC_VALUE>>13)&0x3)
  459. #define __PLL_CLC_NDIV_VALUE ((__PLL_CLC_VALUE>>16)&0x7f)
  460. if ( PLL_CLC.B.BYPPIN == 0 )
  461. {
  462. #if ( defined _REGTC1130_H )
  463. while (OSC_CON.B.OSCR==1); /* Wait until the oscillator is running */
  464. #else
  465. while (OSC_CON.B.OSCR==0); /* Wait until the oscillator is running */
  466. #endif
  467. PLL_CLC.B.VCOBYP = 1; /* Enabled the VCO Bypass Mode */
  468. PLL_CLC.B.VCOSEL = __PLL_CLC_VCOSEL_VALUE; /* Select the VCO band */
  469. PLL_CLC.B.PDIV = __PLL_CLC_PDIV_VALUE;
  470. PLL_CLC.B.KDIV = __PLL_CLC_KDIV_VALUE;
  471. PLL_CLC.B.NDIV = __PLL_CLC_NDIV_VALUE;
  472. #if __PLL_CLC_VCOBYP_VALUE==0
  473. PLL_CLC.B.OSCDISC = 0; /* Connect the oscillator to the PLL */
  474. while( PLL_CLC.B.LOCK==0); /* Wait until the PLL becomes locked */
  475. PLL_CLC.B.VCOBYP = 0; /* Disabled the VCO Bypass Mode */
  476. #endif
  477. }
  478. #if ( defined _REGTC1130_H )
  479. PLL_CLC.B.SYSFSL = __PLL_CLC_SYSFS_VALUE; /* System frequency select */
  480. #else
  481. #if ( defined _REGTC1762_H || defined _REGTC1764_H || defined _REGTC1766B_H \
  482. || defined _REGTC1164_H || defined _REGTC1166_H )
  483. PLL_CLC.B.RSV = __PLL_CLC_SYSFS_VALUE; /* System frequency select */
  484. #else
  485. PLL_CLC.B.SYSFS = __PLL_CLC_SYSFS_VALUE; /* System frequency select */
  486. #endif
  487. #endif
  488. #endif
  489. #endif
  490. /*
  491. * Configure CCU Clock Control
  492. *
  493. * For AURIX derivatives it is required to set the update request only
  494. * in the last CCUCON that is initialized. Set CCUCONx.UP where x is
  495. * the last one. A new complete parameter set is transferred to
  496. * the CCU.
  497. */
  498. #if defined SCU_CCUCON0 && defined __SCU_CCUCON0_VALUE
  499. if(__SCU_CCUCON0_INIT) SCU_CCUCON0.U = __SCU_CCUCON0_VALUE;
  500. #endif
  501. #if defined SCU_CCUCON1 && defined __SCU_CCUCON1_VALUE
  502. if(__SCU_CCUCON1_INIT) SCU_CCUCON1.U = __SCU_CCUCON1_VALUE;
  503. #endif
  504. #if defined SCU_CCUCON2 && defined __SCU_CCUCON2_VALUE
  505. if(__SCU_CCUCON2_INIT) SCU_CCUCON2.U = __SCU_CCUCON2_VALUE;
  506. #endif
  507. /*
  508. * System Oscillator configuration
  509. */
  510. #if defined SCU_OSCCON && defined __SCU_OSCCON_VALUE
  511. #if __SCU_OSCCON_INIT
  512. SCU_OSCCON.U=__SCU_OSCCON_VALUE;
  513. while(SCU_OSCCON.B.PLLLV==0); /* Oscillator not too low */
  514. while(SCU_OSCCON.B.PLLHV==0); /* Oscillator not too high */
  515. #endif
  516. #endif
  517. /*
  518. * Configure PLL Prescaler Mode.
  519. */
  520. #if defined SCU_PLLCON1 && defined __SCU_PLLCON1_VALUE
  521. #if __SCU_PLLCON1_INIT
  522. #define __SCU_PLLCON1__K1DIV_VALUE ((__SCU_PLLCON1_VALUE>>16)&0x3f)
  523. SCU_PLLCON1.B.K1DIV=__SCU_PLLCON1__K1DIV_VALUE;
  524. while(SCU_PLLSTAT.B.K1RDY==0); /* Wait until K1-Divider is ready to operate */
  525. #endif
  526. #endif
  527. #if defined SCU_PLLCON0 && defined __SCU_PLLCON0_VALUE
  528. #if __SCU_PLLCON0_INIT
  529. SCU_PLLCON0.B.VCOBYP=1; /* Enabled the VCO Bypass Mode */
  530. while(SCU_PLLSTAT.B.VCOBYST==0); /* Wait until prescaler mode is entered */
  531. #define __SCU_PLLCON0__VCOPWD_VALUE ((__SCU_PLLCON0_VALUE>>1)&0x1)
  532. #define __SCU_PLLCON0__NDIV_VALUE ((__SCU_PLLCON0_VALUE>>9)&0x7f)
  533. #define __SCU_PLLCON0__PDIV_VALUE ((__SCU_PLLCON0_VALUE>>24)&0xf)
  534. SCU_PLLCON0.B.PDIV=__SCU_PLLCON0__PDIV_VALUE;
  535. SCU_PLLCON0.B.NDIV=__SCU_PLLCON0__NDIV_VALUE;
  536. SCU_PLLCON0.B.VCOPWD=__SCU_PLLCON0__VCOPWD_VALUE; /* power down VCO */
  537. #if __SCU_PLLCON0__VCOPWD_VALUE==0
  538. /*
  539. * Configure PLL normal mode.
  540. */
  541. SCU_PLLCON0.B.OSCDISCDIS=1; /* Automatic oscillator disconnect disabled */
  542. SCU_PLLCON0.B.CLRFINDIS=1; /* Connect VCO to the oscillator */
  543. while(SCU_PLLSTAT.B.FINDIS==1); /* Wait until oscillator is connected to the VCO */
  544. SCU_PLLCON0.B.RESLD=1; /* Restart VCO lock detection */
  545. while(SCU_PLLSTAT.B.VCOLOCK==0); /* Wait until the VCO becomes locked */
  546. SCU_PLLCON0.B.VCOBYP=0; /* Disable the VCO Bypass Mode */
  547. while(SCU_PLLSTAT.B.VCOBYST==1); /* Wait until normal mode is entered */
  548. SCU_PLLCON0.B.OSCDISCDIS=0; /* Automatic oscillator disconnect enabled */
  549. #endif
  550. #endif
  551. #endif
  552. /*
  553. * PLL K2 ramp up
  554. */
  555. #if defined SCU_PLLCON1 && defined __SCU_PLLCON1_VALUE
  556. #if __SCU_PLLCON1_INIT
  557. #define __SCU_PLLCON1__K2DIV_VALUE ((__SCU_PLLCON1_VALUE>>0)&0x3f)
  558. #if __PLL_K2_RAMPUP
  559. #if defined SCU_PLLSTAT && defined __fOSC
  560. #if ( defined _REGTC1167_H || defined _REGTC1197_H || defined _REGTC1337_H || defined _REGTC1367_H \
  561. || defined _REGTC1387_H || defined _REGTC1728_H \
  562. || defined _REGTC1736_H || defined _REGTC1738_H || defined _REGTC1746_H \
  563. || defined _REGTC1747_H || defined _REGTC1748_H || defined _REGTC1767_H || defined _REGTC1768_H \
  564. || defined _REGTC1782_H || defined _REGTC1784_H || defined _REGTC1184_H || defined _REGTC1797_H \
  565. || defined _REGTC1791_H || defined _REGTC1793_H || defined _REGTC1798_H || defined _REGTC1724_H \
  566. || defined _REGTC21X_H || defined _REGTC22X_H || defined _REGTC23X_ADAS_H || defined _REGTC23X_H \
  567. || defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H \
  568. || defined _REGTC26X_H || defined _REGTC26XB_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  569. #if ( defined _REGTC21X_H || defined _REGTC22X_H || defined _REGTC23X_ADAS_H || defined _REGTC23X_H || defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H || defined _REGTC26X_H || defined _REGTC26XB_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  570. #define PLL_K2_RAMPUP_STM_CLC_RMC 1
  571. #else
  572. #define PLL_K2_RAMPUP_STM_CLC_RMC (STM_CLC.B.RMC)
  573. #endif
  574. #if ( defined _REGTC1337_H || defined _REGTC1367_H || defined _REGTC1387_H || defined _REGTC1768_H )
  575. #define PLL_K2_RAMPUP_SYSCLK(CPUCLK) (CPUCLK)
  576. #else
  577. #if ( defined _REGTC21X_H || defined _REGTC22X_H || defined _REGTC23X_ADAS_H || defined _REGTC23X_H || defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H || defined _REGTC26X_H || defined _REGTC26XB_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  578. #define PLL_K2_RAMPUP_SYSCLK(CPUCLK) (CPUCLK/SCU_CCUCON1.B.STMDIV)
  579. #else
  580. #define PLL_K2_RAMPUP_SYSCLK(CPUCLK) (CPUCLK/(SCU_CCUCON0.B.FPIDIV+1))
  581. #endif
  582. #endif
  583. #define USECCLK (PLL_K2_RAMPUP_SYSCLK(((SCU_PLLCON0.B.NDIV+1)*(__fOSC/1000000))/((SCU_PLLCON0.B.PDIV+1)*(SCU_PLLCON1.B.K2DIV+1)))/PLL_K2_RAMPUP_STM_CLC_RMC)
  584. #define WAITUSEC (USECCLK*20) /* wait 20 usec */
  585. #endif
  586. #endif
  587. #pragma tradeoff 0 /* avoid call to run-time function */
  588. while ( SCU_PLLCON1.B.K2DIV > __SCU_PLLCON1__K2DIV_VALUE )
  589. {
  590. unsigned int wait;
  591. SCU_PLLCON1.B.K2DIV-=1;
  592. wait = SYSTIME_LOW+WAITUSEC;
  593. while( SYSTIME_LOW < wait ) ;
  594. }
  595. #pragma tradeoff restore
  596. #else
  597. SCU_PLLCON1.B.K2DIV=__SCU_PLLCON1__K2DIV_VALUE;
  598. #endif
  599. #endif
  600. #endif
  601. /*
  602. * Configure flash registers.
  603. */
  604. #if defined FLASH0_FCON && defined __FLASH0_FCON_VALUE
  605. if(__FLASH0_FCON_INIT) FLASH0_FCON.U = __FLASH0_FCON_VALUE;
  606. #endif
  607. #if defined FLASH1_FCON && defined __FLASH1_FCON_VALUE
  608. if(__FLASH1_FCON_INIT) FLASH1_FCON.U = __FLASH1_FCON_VALUE;
  609. #endif
  610. /*
  611. * Configure system clock register.
  612. */
  613. #if defined STM_CLC && defined __STM_CLC_VALUE
  614. if(__STM_CLC_INIT) STM_CLC.U = __STM_CLC_VALUE;
  615. #endif
  616. /*
  617. * Enable the GTM to get MCS memory access,
  618. * required for MCS initialization which
  619. * is performed by _c_init.
  620. */
  621. #if defined GTM_CLC && defined __GTM_CLC_VALUE
  622. if(__GTM_CLC_INIT) GTM_CLC.U = __GTM_CLC_VALUE;
  623. #endif
  624. /*
  625. * Set the ENDINIT bit in the WDT_CON0 register again
  626. * to enable the write-protection.
  627. */
  628. endinit_set(_ENDINIT_ENABLE);
  629. #if ( defined _REGUSERDEF16X_H || defined _REGUSERDEF162_H || defined _REGTC21X_H || defined _REGTC22X_H || defined _REGTC23X_ADAS_H || defined _REGTC23X_H || defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H || defined _REGTC26X_H || defined _REGTC26XB_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  630. /*
  631. * Set the ENDINIT bit in the WDTSCON0 register to enable the
  632. * safety-critical register write-protection.
  633. */
  634. safety_endinit_set(_ENDINIT_ENABLE);
  635. #endif
  636. /*
  637. * Initialize Bus configuration registers:
  638. * Set register-values according to define's created by the GUI
  639. *
  640. * The recommended sequence of setting registers is as follows:
  641. * 1.EBUCON
  642. * 2.All other EBU registers except SDRAM specific registers
  643. * 3.SDRMCON0
  644. * 4.SDRMMOD0
  645. * 5.SDRMREF0
  646. * 6.SDRMCON1
  647. * 7.SDRMMOD1
  648. * 8.SDRMREF1
  649. */
  650. #if defined EBU_CON && defined __EBU_CON_VALUE
  651. if(__EBU_CON_INIT) EBU_CON.U = __EBU_CON_VALUE;
  652. #endif
  653. #if defined EBU_ADDRSEL0 && defined __EBU_ADDRSEL0_VALUE
  654. if(__EBU_ADDRSEL0_INIT) EBU_ADDRSEL0.U = __EBU_ADDRSEL0_VALUE;
  655. #endif
  656. #if defined EBU_ADDRSEL1 && defined __EBU_ADDRSEL1_VALUE
  657. if(__EBU_ADDRSEL1_INIT) EBU_ADDRSEL1.U = __EBU_ADDRSEL1_VALUE;
  658. #endif
  659. #if defined EBU_ADDRSEL2 && defined __EBU_ADDRSEL2_VALUE
  660. if(__EBU_ADDRSEL2_INIT) EBU_ADDRSEL2.U = __EBU_ADDRSEL2_VALUE;
  661. #endif
  662. #if defined EBU_ADDRSEL3 && defined __EBU_ADDRSEL3_VALUE
  663. if(__EBU_ADDRSEL3_INIT) EBU_ADDRSEL3.U = __EBU_ADDRSEL3_VALUE;
  664. #endif
  665. #if defined EBU_ADDRSEL4 && defined __EBU_ADDRSEL4_VALUE
  666. if(__EBU_ADDRSEL4_INIT) EBU_ADDRSEL4.U = __EBU_ADDRSEL4_VALUE;
  667. #endif
  668. #if defined EBU_ADDRSEL5 && defined __EBU_ADDRSEL5_VALUE
  669. if(__EBU_ADDRSEL5_INIT) EBU_ADDRSEL5.U = __EBU_ADDRSEL5_VALUE;
  670. #endif
  671. #if defined EBU_ADDRSEL6 && defined __EBU_ADDRSEL6_VALUE
  672. if(__EBU_ADDRSEL6_INIT) EBU_ADDRSEL6.U = __EBU_ADDRSEL6_VALUE;
  673. #endif
  674. #if defined EBU_BFCON && defined __EBU_BFCON_VALUE
  675. if(__EBU_BFCON_INIT) EBU_BFCON.U = __EBU_BFCON_VALUE;
  676. #endif
  677. #if defined EBU_BUSAP0 && defined __EBU_BUSAP0_VALUE
  678. if(__EBU_BUSAP0_INIT) EBU_BUSAP0.U = __EBU_BUSAP0_VALUE;
  679. #endif
  680. #if defined EBU_BUSAP1 && defined __EBU_BUSAP1_VALUE
  681. if(__EBU_BUSAP1_INIT) EBU_BUSAP1.U = __EBU_BUSAP1_VALUE;
  682. #endif
  683. #if defined EBU_BUSAP2 && defined __EBU_BUSAP2_VALUE
  684. if(__EBU_BUSAP2_INIT) EBU_BUSAP2.U = __EBU_BUSAP2_VALUE;
  685. #endif
  686. #if defined EBU_BUSAP3 && defined __EBU_BUSAP3_VALUE
  687. if(__EBU_BUSAP3_INIT) EBU_BUSAP3.U = __EBU_BUSAP3_VALUE;
  688. #endif
  689. #if defined EBU_BUSAP4 && defined __EBU_BUSAP4_VALUE
  690. if(__EBU_BUSAP4_INIT) EBU_BUSAP4.U = __EBU_BUSAP4_VALUE;
  691. #endif
  692. #if defined EBU_BUSAP5 && defined __EBU_BUSAP5_VALUE
  693. if(__EBU_BUSAP5_INIT) EBU_BUSAP5.U = __EBU_BUSAP5_VALUE;
  694. #endif
  695. #if defined EBU_BUSAP6 && defined __EBU_BUSAP6_VALUE
  696. if(__EBU_BUSAP6_INIT) EBU_BUSAP6.U = __EBU_BUSAP6_VALUE;
  697. #endif
  698. #if defined EBU_BUSCON0 && defined __EBU_BUSCON0_VALUE
  699. if(__EBU_BUSCON0_INIT) EBU_BUSCON0.U = __EBU_BUSCON0_VALUE;
  700. #endif
  701. #if defined EBU_BUSCON1 && defined __EBU_BUSCON1_VALUE
  702. if(__EBU_BUSCON1_INIT) EBU_BUSCON1.U = __EBU_BUSCON1_VALUE;
  703. #endif
  704. #if defined EBU_BUSCON2 && defined __EBU_BUSCON2_VALUE
  705. if(__EBU_BUSCON2_INIT) EBU_BUSCON2.U = __EBU_BUSCON2_VALUE;
  706. #endif
  707. #if defined EBU_BUSCON3 && defined __EBU_BUSCON3_VALUE
  708. if(__EBU_BUSCON3_INIT) EBU_BUSCON3.U = __EBU_BUSCON3_VALUE;
  709. #endif
  710. #if defined EBU_BUSCON4 && defined __EBU_BUSCON4_VALUE
  711. if(__EBU_BUSCON4_INIT) EBU_BUSCON4.U = __EBU_BUSCON4_VALUE;
  712. #endif
  713. #if defined EBU_BUSCON5 && defined __EBU_BUSCON5_VALUE
  714. if(__EBU_BUSCON5_INIT) EBU_BUSCON5.U = __EBU_BUSCON5_VALUE;
  715. #endif
  716. #if defined EBU_BUSCON6 && defined __EBU_BUSCON6_VALUE
  717. if(__EBU_BUSCON6_INIT) EBU_BUSCON6.U = __EBU_BUSCON6_VALUE;
  718. #endif
  719. #if defined EBU_EMUAS && defined __EBU_EMUAS_VALUE
  720. if(__EBU_EMUAS_INIT) EBU_EMUAS.U = __EBU_EMUAS_VALUE;
  721. #endif
  722. #if defined EBU_EMUBAP && defined __EBU_EMUBAP_VALUE
  723. if(__EBU_EMUBAP_INIT) EBU_EMUBAP.U = __EBU_EMUBAP_VALUE;
  724. #endif
  725. #if defined EBU_EMUBC && defined __EBU_EMUBC_VALUE
  726. if(__EBU_EMUBC_INIT) EBU_EMUBC.U = __EBU_EMUBC_VALUE;
  727. #endif
  728. #if defined EBU_EMUCON && defined __EBU_EMUCON_VALUE
  729. if(__EBU_EMUCON_INIT) EBU_EMUCON.U = __EBU_EMUCON_VALUE;
  730. #endif
  731. #if defined EBU_EMUOVL && defined __EBU_EMUOVL_VALUE
  732. if(__EBU_EMUOVL_INIT) EBU_EMUOVL.U = __EBU_EMUOVL_VALUE;
  733. #endif
  734. #if defined EBU_SDRMCON0 && defined __EBU_SDRMCON0_VALUE
  735. if(__EBU_SDRMCON0_INIT) EBU_SDRMCON0.U = __EBU_SDRMCON0_VALUE;
  736. #endif
  737. #if defined EBU_SDRMOD0 && defined __EBU_SDRMOD0_VALUE
  738. if(__EBU_SDRMOD0_INIT) EBU_SDRMOD0.U = __EBU_SDRMOD0_VALUE;
  739. #endif
  740. #if defined EBU_SDRMREF0 && defined __EBU_SDRMREF0_VALUE
  741. if(__EBU_SDRMREF0_INIT) EBU_SDRMREF0.U = __EBU_SDRMREF0_VALUE;
  742. #endif
  743. #if defined EBU_SDRMCON1 && defined __EBU_SDRMCON1_VALUE
  744. if(__EBU_SDRMCON1_INIT) EBU_SDRMCON1.U = __EBU_SDRMCON1_VALUE;
  745. #endif
  746. #if defined EBU_SDRMOD1 && defined __EBU_SDRMOD1_VALUE
  747. if(__EBU_SDRMOD1_INIT) EBU_SDRMOD1.U = __EBU_SDRMOD1_VALUE;
  748. #endif
  749. #if defined EBU_SDRMREF1 && defined __EBU_SDRMREF1_VALUE
  750. if(__EBU_SDRMREF1_INIT) EBU_SDRMREF1.U = __EBU_SDRMREF1_VALUE;
  751. #endif
  752. #if defined CBS_MCDBBS && defined __CBS_MCDBBS_VALUE
  753. if(__CBS_MCDBBS_INIT) CBS_MCDBBS .U = __CBS_MCDBBS_VALUE;
  754. #endif
  755. #if defined SBCU_CON && defined __SBCU_CON_VALUE
  756. if(__SBCU_CON_INIT) SBCU_CON.U = __SBCU_CON_VALUE;
  757. #endif
  758. #if defined EBU_BUSRAP1 && defined __EBU_BUSRAP1_VALUE
  759. if(__EBU_BUSRAP1_INIT) EBU_BUSRAP1.U = __EBU_BUSRAP1_VALUE;
  760. #endif
  761. #if defined EBU_BUSRAP0 && defined __EBU_BUSRAP0_VALUE
  762. if(__EBU_BUSRAP0_INIT) EBU_BUSRAP0.U = __EBU_BUSRAP0_VALUE;
  763. #endif
  764. #if defined EBU_BUSRAP2 && defined __EBU_BUSRAP2_VALUE
  765. if(__EBU_BUSRAP2_INIT) EBU_BUSRAP2.U = __EBU_BUSRAP2_VALUE;
  766. #endif
  767. #if defined EBU_BUSRAP3 && defined __EBU_BUSRAP3_VALUE
  768. if(__EBU_BUSRAP3_INIT) EBU_BUSRAP3.U = __EBU_BUSRAP3_VALUE;
  769. #endif
  770. #if defined EBU_BUSRCON0 && defined __EBU_BUSRCON0_VALUE
  771. if(__EBU_BUSRCON0_INIT) EBU_BUSRCON0.U = __EBU_BUSRCON0_VALUE;
  772. #endif
  773. #if defined EBU_BUSRCON1 && defined __EBU_BUSRCON1_VALUE
  774. if(__EBU_BUSRCON1_INIT) EBU_BUSRCON1.U = __EBU_BUSRCON1_VALUE;
  775. #endif
  776. #if defined EBU_BUSRCON2 && defined __EBU_BUSRCON2_VALUE
  777. if(__EBU_BUSRCON2_INIT) EBU_BUSRCON2.U = __EBU_BUSRCON2_VALUE;
  778. #endif
  779. #if defined EBU_BUSRCON3 && defined __EBU_BUSRCON3_VALUE
  780. if(__EBU_BUSRCON3_INIT) EBU_BUSRCON3.U = __EBU_BUSRCON3_VALUE;
  781. #endif
  782. #if defined EBU_BUSWAP0 && defined __EBU_BUSWAP0_VALUE
  783. if(__EBU_BUSWAP0_INIT) EBU_BUSWAP0.U = __EBU_BUSWAP0_VALUE;
  784. #endif
  785. #if defined EBU_BUSWAP1 && defined __EBU_BUSWAP1_VALUE
  786. if(__EBU_BUSWAP1_INIT) EBU_BUSWAP1.U = __EBU_BUSWAP1_VALUE;
  787. #endif
  788. #if defined EBU_BUSWAP2 && defined __EBU_BUSWAP2_VALUE
  789. if(__EBU_BUSWAP2_INIT) EBU_BUSWAP2.U = __EBU_BUSWAP2_VALUE;
  790. #endif
  791. #if defined EBU_BUSWAP3 && defined __EBU_BUSWAP3_VALUE
  792. if(__EBU_BUSWAP3_INIT) EBU_BUSWAP3.U = __EBU_BUSWAP3_VALUE;
  793. #endif
  794. #if defined EBU_BUSWCON0 && defined __EBU_BUSWCON0_VALUE
  795. if(__EBU_BUSWCON0_INIT) EBU_BUSWCON0.U = __EBU_BUSWCON0_VALUE;
  796. #endif
  797. #if defined EBU_BUSWCON1 && defined __EBU_BUSWCON1_VALUE
  798. if(__EBU_BUSWCON1_INIT) EBU_BUSWCON1.U = __EBU_BUSWCON1_VALUE;
  799. #endif
  800. #if defined EBU_BUSWCON2 && defined __EBU_BUSWCON2_VALUE
  801. if(__EBU_BUSWCON2_INIT) EBU_BUSWCON2.U = __EBU_BUSWCON2_VALUE;
  802. #endif
  803. #if defined EBU_BUSWCON3 && defined __EBU_BUSWCON3_VALUE
  804. if(__EBU_BUSWCON3_INIT) EBU_BUSWCON3.U = __EBU_BUSWCON3_VALUE;
  805. #endif
  806. #if defined EBU_EXTBOOT && defined __EBU_EXTBOOT_VALUE
  807. if(__EBU_EXTBOOT_INIT) EBU_EXTBOOT.U = __EBU_EXTBOOT_VALUE;
  808. #endif
  809. #if defined EBU_MODCON && defined __EBU_MODCON_VALUE
  810. if(__EBU_MODCON_INIT) EBU_MODCON.U = __EBU_MODCON_VALUE;
  811. #endif
  812. #if defined EBU_DDRNCON && defined __EBU_DDRNCON_VALUE
  813. if(__EBU_DDRNCON_INIT) EBU_DDRNCON.U = __EBU_DDRNCON_VALUE;
  814. #endif
  815. #if defined EBU_DDRNMOD && defined __EBU_DDRNMOD_VALUE
  816. if(__EBU_DDRNMOD_INIT) EBU_DDRNMOD.U = __EBU_DDRNMOD_VALUE;
  817. #endif
  818. #if defined EBU_DDRNMOD2 && defined __EBU_DDRNMOD2_VALUE
  819. if(__EBU_DDRNMOD2_INIT) EBU_DDRNMOD2.U = __EBU_DDRNMOD2_VALUE;
  820. #endif
  821. #if defined EBU_DDRNPRLD && defined __EBU_DDRNPRLD_VALUE
  822. if(__EBU_DDRNPRLD_INIT) EBU_DDRNPRLD.U = __EBU_DDRNPRLD_VALUE;
  823. #endif
  824. #if defined EBU_DDRNTAG0 && defined __EBU_DDRNTAG0_VALUE
  825. if(__EBU_DDRNTAG0_INIT) EBU_DDRNTAG0.U = __EBU_DDRNTAG0_VALUE;
  826. #endif
  827. #if defined EBU_DDRNTAG1 && defined __EBU_DDRNTAG1_VALUE
  828. if(__EBU_DDRNTAG1_INIT) EBU_DDRNTAG1.U = __EBU_DDRNTAG1_VALUE;
  829. #endif
  830. #if defined EBU_DDRNTAG2 && defined __EBU_DDRNTAG2_VALUE
  831. if(__EBU_DDRNTAG2_INIT) EBU_DDRNTAG2.U = __EBU_DDRNTAG2_VALUE;
  832. #endif
  833. #if defined EBU_DDRNTAG3 && defined __EBU_DDRNTAG3_VALUE
  834. if(__EBU_DDRNTAG3_INIT) EBU_DDRNTAG3.U = __EBU_DDRNTAG3_VALUE;
  835. #endif
  836. #if defined EBU_DLLCON && defined __EBU_DLLCON_VALUE
  837. if(__EBU_DLLCON_INIT) EBU_DLLCON.U = __EBU_DLLCON_VALUE;
  838. #endif
  839. #if defined EBU_SDRMCON && defined __EBU_SDRMCON_VALUE
  840. if(__EBU_SDRMCON_INIT) EBU_SDRMCON.U = __EBU_SDRMCON_VALUE;
  841. #endif
  842. #if defined EBU_SDRMOD && defined __EBU_SDRMOD_VALUE
  843. if(__EBU_SDRMOD_INIT) EBU_SDRMOD.U = __EBU_SDRMOD_VALUE;
  844. #endif
  845. #if defined EBU_SDRMREF && defined __EBU_SDRMREF_VALUE
  846. if(__EBU_SDRMREF_INIT) EBU_SDRMREF.U = __EBU_SDRMREF_VALUE;
  847. #endif
  848. #if defined EBU_SDRSTAT && defined __EBU_SDRSTAT_VALUE
  849. if(__EBU_SDRSTAT_INIT) EBU_SDRSTAT.U = __EBU_SDRSTAT_VALUE;
  850. #endif
  851. /*
  852. * Initialize global address registers a0/a1 to support
  853. * __a0/__a1 storage qualifiers of the C compiler.
  854. */
  855. #if __A0A1_INIT
  856. void * a0 = _SMALL_DATA_;
  857. __asm( "mov.aa\ta0,%0"::"a"(a0) );
  858. void * a1 = _LITERAL_DATA_;
  859. __asm( "mov.aa\ta1,%0"::"a"(a1) );
  860. #endif
  861. /*
  862. * Initialize global address registers a8/a9 to support
  863. * __a8/__a9 storage qualifiers of the C compiler. A8 and A9
  864. * are reserved for OS use, or for application use in cases
  865. * where the application ans OS are tightly coupled.
  866. */
  867. #if __A8A9_INIT
  868. void * a8 = _A8_DATA_;
  869. __asm( "mov.aa\ta8,%0"::"a"(a8) );
  870. void * a9 = _A9_DATA_;
  871. __asm( "mov.aa\ta9,%0"::"a"(a9) );
  872. #endif
  873. /* Setup the context save area linked list. */
  874. #if __CSA_INIT
  875. # if !__CPU_TC051_INITIAL__
  876. # define MAX_NR_OF_CSA_AREAS 1
  877. extern int _lc_ub_csa_01[][16]; /* context save area 1 begin */
  878. extern int _lc_ue_csa_01[][16]; /* context save area 1 end */
  879. static __far int (* const csa_area_begin[])[16] = { _lc_ub_csa_01 };
  880. static __far int (* const csa_area_end[])[16] = { _lc_ue_csa_01 };
  881. # else
  882. # define MAX_NR_OF_CSA_AREAS 3
  883. extern int _lc_ub_csa_01[][16]; /* context save area 1 begin */
  884. extern int _lc_ue_csa_01[][16]; /* context save area 1 end */
  885. extern int _lc_ub_csa_02[][16]; /* context save area 2 begin */
  886. extern int _lc_ue_csa_02[][16]; /* context save area 2 end */
  887. extern int _lc_ub_csa_03[][16]; /* context save area 3 begin */
  888. extern int _lc_ue_csa_03[][16]; /* context save area 3 end */
  889. static __far int (* const csa_area_begin[])[16] = { _lc_ub_csa_01, _lc_ub_csa_02, _lc_ub_csa_03 };
  890. static __far int (* const csa_area_end[])[16] = { _lc_ue_csa_01, _lc_ue_csa_02, _lc_ue_csa_03 };
  891. # endif
  892. int i, k;
  893. int no_of_csas;
  894. int * csa;
  895. unsigned int seg_nr, seg_idx, pcxi_val=0;
  896. _Bool first=true;
  897. for (i=0; i < MAX_NR_OF_CSA_AREAS; i++)
  898. {
  899. /* first calculate nr of CSAs in this area */
  900. no_of_csas = csa_area_end[i] - csa_area_begin[i];
  901. for (k=0; k < no_of_csas; k++)
  902. {
  903. csa = csa_area_begin[i][k];
  904. /* Store null pointer in last CSA (= very first time!) */
  905. *csa = pcxi_val;
  906. seg_nr = (((unsigned int)csa >> 28) & 0xf) << 16;
  907. seg_idx = (((unsigned int)csa >> 6) & 0xffff);
  908. pcxi_val = seg_nr | seg_idx;
  909. if (first)
  910. {
  911. first = false;
  912. __mtcr(LCX, pcxi_val);
  913. }
  914. }
  915. __mtcr(FCX, pcxi_val);
  916. }
  917. #endif
  918. /*
  919. * PMU_TC.004 workaround:
  920. * The split mode is disabled on the LMB bus to workaround.
  921. */
  922. #if __PMU_TC004_INITIAL__ && defined LFI_CON
  923. LFI_CON.U &= 0xfffffffe;
  924. #endif
  925. /*
  926. * Initialize and clear C variables.
  927. */
  928. #if __C_INIT
  929. _c_init(); /* initialize data */
  930. #endif
  931. /*
  932. * initialize __clocks_per_sec,
  933. * the oscillator frequency is defined by __fOSC.
  934. */
  935. #if __CLOCKS_PER_SEC_INIT
  936. setclockpersec();
  937. #endif
  938. /* initialize profiling if required
  939. */
  940. #if __PROF_ENABLE__
  941. __prof_init();
  942. #endif
  943. /*
  944. * Call a user function called _endinit() within one can initialize the
  945. * registers protected via the EndInit feature.
  946. * Beware that protected registers are unlocked
  947. * for the duration of the Time-out Period only!
  948. */
  949. #ifdef _CALL_ENDINIT
  950. endinit_set(_ENDINIT_DISABLE); /* disable the write-protection */
  951. _endinit();
  952. endinit_set(_ENDINIT_ENABLE); /* enable the write-protection */
  953. #endif
  954. /*
  955. * Call a user function before starting main().
  956. */
  957. #ifdef __CALL_INIT
  958. _call_init();
  959. #endif
  960. /*
  961. * Download image to xc800 XRAM
  962. */
  963. #if ( defined _REGTC26X_H || defined _REGTC26XB_H )
  964. #if __XC800_INIT
  965. _xc800_init();
  966. #endif
  967. #endif
  968. #if ( defined _REGUSERDEF16X_H || defined _REGUSERDEF162_H || defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H || defined _REGTC26X_H || defined _REGTC26XB_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  969. #if (__HALT_INIT_TC1 && (__HALT_VALUE_TC1 & 0x2)) || (__HALT_INIT_TC2 && (__HALT_VALUE_TC2 & 0x2))
  970. #if __C_INIT || __CLOCKS_PER_SEC_INIT || __PROF_ENABLE__ || _CALL_ENDINIT || (defined __CALL_INIT)
  971. __swapmskw( (unsigned int *)&_tcx_end_c_init, 0x1, 0x1 ); /* tc0 end of initialization */
  972. /*
  973. * Before calling main, which has code core association share,
  974. * wait until all cores have done the global initializations
  975. * to avoid race conditions on initialized shared global data.
  976. */
  977. while ( ( _tcx_end_c_init & 0x7 ) != 7 ) ;
  978. #endif
  979. #endif
  980. #endif
  981. /*
  982. * Call C main program.
  983. */
  984. #if __USE_ARGC_ARGV
  985. exit( main( _argcv( argcv, __ARGCV_BUFSIZE ), (char **)argcv ) );
  986. #else
  987. exit( main( 0, NULL ) ); /* argc is 0 */
  988. #endif
  989. /*
  990. * Default trap vectors are resolved from the C-library.
  991. */
  992. #if __BTV_INIT
  993. # if __RESOLVE_TRAP_0
  994. # pragma extern _trapmmu
  995. # endif
  996. # if __RESOLVE_TRAP_1
  997. # pragma extern _trapprotection
  998. # endif
  999. # if __RESOLVE_TRAP_2
  1000. # pragma extern _trapinstruction
  1001. # endif
  1002. # if __RESOLVE_TRAP_3
  1003. # pragma extern _trapcontext
  1004. # endif
  1005. # if __RESOLVE_TRAP_4
  1006. # pragma extern _trapbus
  1007. # endif
  1008. # if __RESOLVE_TRAP_5
  1009. # pragma extern _trapassertion
  1010. # endif
  1011. # if __RESOLVE_TRAP_6
  1012. # pragma extern _trapsystem
  1013. # endif
  1014. # if __RESOLVE_TRAP_7
  1015. # pragma extern _trapnmi
  1016. # endif
  1017. #endif
  1018. }
  1019. /**************************************************************************
  1020. *
  1021. * FUNCTION: endinit_set
  1022. *
  1023. * DESCRIPTION: Sets or Clears the ENDINIT bit in the WDT_CON0 register
  1024. * in order to enabled or disable the write-protection for
  1025. * registers protected via the EndInit feature
  1026. * (ie. BTV, BIV, ISP, PCON0, DCON0).
  1027. *
  1028. *************************************************************************/
  1029. static void inline endinit_set( _endinit_t endinit_value )
  1030. {
  1031. unsigned int wdt_con0;
  1032. #if !( defined _REGUSERDEF16X_H || defined _REGUSERDEF162_H || defined _REGTC21X_H || defined _REGTC22X_H || defined _REGTC23X_ADAS_H || defined _REGTC23X_H || defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H || defined _REGTC26X_H || defined _REGTC26XB_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  1033. unsigned int wdt_con1;
  1034. #endif
  1035. /*
  1036. * 1st step: Password access (create password and send to WDT_CON0)
  1037. */
  1038. wdt_con0 = WDT_CON0.U;
  1039. #if !( defined _REGUSERDEF16X_H || defined _REGUSERDEF162_H || defined _REGTC21X_H || defined _REGTC22X_H || defined _REGTC23X_ADAS_H || defined _REGTC23X_H || defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H || defined _REGTC26X_H || defined _REGTC26XB_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  1040. wdt_con1 = WDT_CON1.U;
  1041. #endif
  1042. wdt_con0 &= 0xffffff01; /* clear WDTLCK, WDTHPW0, WDTHPW1 */
  1043. wdt_con0 |= 0xf0; /* set WDTHPW1 to 0xf */
  1044. #if ( defined _REGUSERDEF16X_H || defined _REGUSERDEF162_H || defined _REGTC21X_H || defined _REGTC22X_H || defined _REGTC23X_ADAS_H || defined _REGTC23X_H || defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H || defined _REGTC26X_H || defined _REGTC26XB_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  1045. wdt_con0 |= 0x1; /* 1 must be written to ENDINIT for password access
  1046. * (but this will not actually modify the bit) */
  1047. #else
  1048. wdt_con1 &= 0x0c; /* copy of WDT_CON1.DR and WDT_CON1.IR (d1) */
  1049. wdt_con0 |= wdt_con1; /* HPW0 is WDT_CON1.DR | WDT_CON1.IR */
  1050. #endif
  1051. WDT_CON0.U = wdt_con0;
  1052. /*
  1053. * 2nd step: Modify access, set the bit ENDINIT to 1 or 0 to allow access to
  1054. * registers: WDT_CON1, BTV, BIV, ISP and mod_CLC
  1055. */
  1056. wdt_con0 &= 0xfffffff0; /* clear WDTHPW0, WDTLCK, ENDINIT */
  1057. wdt_con0 |= 0x02 | endinit_value; /* WDTHPW0=0, WDTLCK=1, ENDINIT=0 */
  1058. __isync();
  1059. WDT_CON0.U = wdt_con0;
  1060. WDT_CON0.U; /* read is required */
  1061. }
  1062. #if ( defined _REGUSERDEF16X_H || defined _REGUSERDEF162_H || defined _REGTC21X_H || defined _REGTC22X_H || defined _REGTC23X_ADAS_H || defined _REGTC23X_H || defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H || defined _REGTC26X_H || defined _REGTC26XB_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  1063. /**************************************************************************
  1064. *
  1065. * FUNCTION: safety_endinit_set
  1066. *
  1067. * DESCRIPTION: Sets or Clears the ENDINIT bit in the WDTSCON0 register
  1068. * in order to enabled or disable the write-protection for
  1069. * safety-critical registers protected via the EndInit feature.
  1070. *
  1071. *************************************************************************/
  1072. static void inline safety_endinit_set( _endinit_t endinit_value )
  1073. {
  1074. unsigned int wdtscon0;
  1075. /*
  1076. * 1st step: Password access (create password and send to WDTSCON0)
  1077. */
  1078. wdtscon0 = SCU_WDTSCON0.U;
  1079. wdtscon0 &= 0xffffff01; /* clear WDTLCK, WDTHPW0, WDTHPW1 */
  1080. wdtscon0 |= 0xf0; /* set WDTHPW1 to 0xf */
  1081. wdtscon0 |= 0x1; /* 1 must be written to ENDINIT for password access
  1082. * (but this will not actually modify the bit) */
  1083. SCU_WDTSCON0.U = wdtscon0;
  1084. /*
  1085. * 2nd step: Modify access, set the bit ENDINIT to 1 or 0 to allow access to
  1086. * registers: SCU_WDTSCON1, BTV, BIV, ISP and mod_CLC
  1087. */
  1088. wdtscon0 &= 0xfffffff0; /* clear WDTHPW0, WDTLCK, ENDINIT */
  1089. wdtscon0 |= 0x02 | endinit_value; /* WDTHPW0=0, WDTLCK=1, ENDINIT=0 */
  1090. __isync();
  1091. SCU_WDTSCON0.U = wdtscon0;
  1092. SCU_WDTSCON0.U; /* read is required */
  1093. }
  1094. #endif
  1095. #if __CLOCKS_PER_SEC_INIT
  1096. static void setclockpersec(void)
  1097. {
  1098. unsigned long long hz = 0;
  1099. #if defined PLL_CLC && defined __fOSC
  1100. #if ( defined _REGTC1130_H || defined _REGTC1164_H \
  1101. || defined _REGTC1166_H || defined _REGTC1762_H || defined _REGTC1764_H || defined _REGTC1766_H \
  1102. || defined _REGTC1766B_H || defined _REGTC1792_H || defined _REGTC1796_H || defined _REGTC1796B_H )
  1103. #define P (PLL_CLC.B.PDIV+1)
  1104. #define N (PLL_CLC.B.NDIV+1)
  1105. #define K (PLL_CLC.B.KDIV+1)
  1106. #if ( defined _REGTC1130_H )
  1107. #define __SYSFS__ SYSFSL
  1108. #else
  1109. #if ( defined _REGTC1762_H || defined _REGTC1764_H || defined _REGTC1766B_H \
  1110. || defined _REGTC1164_H || defined _REGTC1166_H )
  1111. #define __SYSFS__ RSV
  1112. #else
  1113. #define __SYSFS__ SYSFS
  1114. #endif
  1115. #endif
  1116. #define SYSCLK(CPUCLK) (CPUCLK/(2-PLL_CLC.B.__SYSFS__))
  1117. #define STMCLK(CPUCLK) (SYSCLK(CPUCLK)/STM_CLC.B.RMC)
  1118. #define FVCOBASE __fOSC
  1119. if ( PLL_CLC.B.BYPPIN ) /* Direct drive */
  1120. {
  1121. hz = STMCLK(__fOSC);
  1122. }
  1123. else
  1124. {
  1125. if ( PLL_CLC.B.VCOBYP ) /* VCO Bypass Mode */
  1126. {
  1127. hz = STMCLK(__fOSC/(P*K));
  1128. }
  1129. else
  1130. {
  1131. if ( !PLL_CLC.B.OSCDISC && PLL_CLC.B.LOCK ) /* PLL Mode */
  1132. {
  1133. hz = STMCLK((N*__fOSC)/(P*K));
  1134. }
  1135. else /* PLL base mode */
  1136. {
  1137. hz = STMCLK(FVCOBASE/K);
  1138. }
  1139. }
  1140. }
  1141. #endif
  1142. #endif
  1143. #if defined SCU_PLLSTAT && defined __fOSC
  1144. #if ( defined _REGTC1167_H || defined _REGTC1197_H || defined _REGTC1337_H || defined _REGTC1367_H \
  1145. || defined _REGTC1387_H || defined _REGTC1728_H \
  1146. || defined _REGTC1736_H || defined _REGTC1738_H || defined _REGTC1746_H \
  1147. || defined _REGTC1747_H || defined _REGTC1748_H || defined _REGTC1767_H || defined _REGTC1768_H \
  1148. || defined _REGTC1782_H || defined _REGTC1784_H || defined _REGTC1184_H || defined _REGTC1797_H \
  1149. || defined _REGTC1791_H || defined _REGTC1793_H || defined _REGTC1798_H || defined _REGTC1724_H \
  1150. || defined _REGTC21X_H || defined _REGTC22X_H || defined _REGTC23X_ADAS_H || defined _REGTC23X_H \
  1151. || defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H \
  1152. || defined _REGTC26X_H || defined _REGTC26XB_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  1153. #define P (SCU_PLLCON0.B.PDIV+1)
  1154. #define N (SCU_PLLCON0.B.NDIV+1)
  1155. #define K1 (SCU_PLLCON1.B.K1DIV+1)
  1156. #define K2 (SCU_PLLCON1.B.K2DIV+1)
  1157. #if ( defined _REGTC21X_H || defined _REGTC22X_H || defined _REGTC23X_ADAS_H || defined _REGTC23X_H || defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H || defined _REGTC26X_H || defined _REGTC26XB_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  1158. #define STM_CLC_RMC 1
  1159. #else
  1160. #define STM_CLC_RMC (STM_CLC.B.RMC)
  1161. #endif
  1162. #if ( defined _REGTC1337_H || defined _REGTC1367_H || defined _REGTC1387_H || defined _REGTC1768_H )
  1163. #define SYSCLK(CPUCLK) (CPUCLK)
  1164. #else
  1165. #if ( defined _REGTC21X_H || defined _REGTC22X_H || defined _REGTC23X_ADAS_H || defined _REGTC23X_H || defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H || defined _REGTC26X_H || defined _REGTC26XB_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  1166. #define SYSCLK(CPUCLK) (CPUCLK/SCU_CCUCON1.B.STMDIV)
  1167. #else
  1168. #define SYSCLK(CPUCLK) (CPUCLK/(SCU_CCUCON0.B.FPIDIV+1))
  1169. #endif
  1170. #endif
  1171. #define STMCLK(CPUCLK) (SYSCLK(CPUCLK)/STM_CLC_RMC)
  1172. #define FVCOBASE __fOSC
  1173. if ( SCU_PLLSTAT.B.VCOBYST ) /* Prescaler mode */
  1174. {
  1175. hz = STMCLK(__fOSC/K1);
  1176. }
  1177. #if ( defined _REGTC21X_H || defined _REGTC22X_H || defined _REGTC23X_ADAS_H || defined _REGTC23X_H || defined _REGTC27X_H || defined _REGTC27XB_H || defined _REGTC27XC_H || defined _REGTC27XD_H || defined _REGTC26X_H || defined _REGTC26XB_H || defined _REGTC29X_H || defined _REGTC29XB_H)
  1178. else if ( SCU_CCUCON0.B.CLKSEL==0)
  1179. {
  1180. hz = STMCLK(100000000); /* back-up clock */
  1181. }
  1182. #endif
  1183. else
  1184. {
  1185. if ( SCU_PLLSTAT.B.FINDIS ) /* Free running mode */
  1186. {
  1187. hz = STMCLK(FVCOBASE/K2);
  1188. }
  1189. else /* Normal mode */
  1190. {
  1191. hz = STMCLK((N*__fOSC)/(P*K2));
  1192. }
  1193. }
  1194. #endif
  1195. #endif
  1196. setfoschz ( hz );
  1197. }
  1198. #endif
  1199. #if ( defined _REGTC26X_H || defined _REGTC26XB_H )
  1200. #if __XC800_INIT
  1201. static _Bool scr_init( void );
  1202. static void inline scr_enable( void );
  1203. static void inline scr_wait( void );
  1204. static void scr_boot( unsigned char boot_mode );
  1205. static void qspi2_init( void );
  1206. static void qspi2_end( void );
  1207. static void inline qspi2_end_of_frame( void );
  1208. static void inline qspi2_start_of_frame( void );
  1209. static void qspi2_send_data( unsigned int data_entry );
  1210. static void qspi2_write_word( unsigned short address, unsigned short data );
  1211. static void inline scr_enable( void )
  1212. {
  1213. /*
  1214. * Set SCU_PMSWCR0.SCREN Standby Controller is enabled
  1215. * set SCU_PMSWCR0.SCRCLKSEL Both 100 KHz Oscillator
  1216. * and 100 MHz oscillator are active in Standby Mode
  1217. */
  1218. SCU_PMSWCR0.U |= (0x1 << 16) | (0x1 << 19);
  1219. /*
  1220. * Wait until Standby Controller is enabled.
  1221. * This bit is updated when PMSWCR0.SCREN bit is set.
  1222. */
  1223. while( !SCU_PMSWSTAT.B.SCR )
  1224. {
  1225. /* wait until enabled */
  1226. }
  1227. }
  1228. /*
  1229. * The SCR interacts with the main core domain via a dedicated
  1230. * SPI (QSPI2) interface and PMSWCR2 register bits.
  1231. */
  1232. static void inline scr_wait( void )
  1233. {
  1234. /*
  1235. * SCR Arbiter Busy status flag, when set the RAM is
  1236. * not accessible via QSPI as the arbiter is currently
  1237. * busy.
  1238. */
  1239. while( SCU_PMSWCR2.B.BUSY )
  1240. {
  1241. /* Wait */
  1242. }
  1243. }
  1244. static void scr_boot( unsigned char boot_mode )
  1245. {
  1246. /*
  1247. * Set SCU_PMSWCR1.SCRSTEN so SCRSTREQ can be updated
  1248. * Set SCU_PMSWCR1.SCRSTREQ for Standby Controller reset request
  1249. * set SCU_PMSWCR1.SCRCFG [23:16] boot_mode
  1250. * 00H 8 bit XRAM is not programmed
  1251. * 01H User Mode (Execution from t.b.d XRAM address)
  1252. * 02H User Mode (DAP mode active)
  1253. * 03H User Mode (SPD mode active)
  1254. *
  1255. * Note: Any change in SCRCFG is followed by a SCRSTREQ reset request
  1256. * of the xc800 controller to start off in the chosen mode.
  1257. */
  1258. SCU_PMSWCR1.U |= 0x3 | (boot_mode << 16);
  1259. /*
  1260. * Wait until Reset of Standby controller took place.
  1261. */
  1262. while( !SCU_PMSWSTAT.B.SCRST )
  1263. {
  1264. /* Wait until reset */
  1265. }
  1266. /*
  1267. * Clear PMSWSTAT.SCRST after reset took place
  1268. */
  1269. SCU_PMSWSTATCLR.B.SCRSTCLR = 1;
  1270. /*
  1271. * SCRINT [7:0] Data exchange from Standby Controller to SCU.
  1272. *
  1273. * Boot mode 0:
  1274. * At the end of startup code, an interrupt would be triggered to
  1275. * the main controller by setting bit NMICON.SCRINTTC to 1 with a
  1276. * value of 80H in SCRINT register. This is used to indicate
  1277. * that TC2x_SCR is ready for CPUx to download code to XRAM.
  1278. *
  1279. * Boot mode 1:
  1280. * XRAM pattern found then, the same interrupt is triggered to the
  1281. * main controller with a value of 80H in SCRINT register to
  1282. * indicate the execution of user code.
  1283. *
  1284. * A value of 81H in SCRINT indicates that boot failed.
  1285. */
  1286. while( SCU_PMSWCR2.B.SCRINT != 0x80
  1287. && SCU_PMSWCR2.B.SCRINT != 0x81
  1288. )
  1289. {
  1290. /* Wait until end of boot mode reached */
  1291. }
  1292. }
  1293. #ifdef _USER_MODE_1
  1294. /*
  1295. * The XRAM signature should be part of your XC800
  1296. * application ROM image. For example:
  1297. * __rom const unsigned char boot_mode_1[8] __at( 0x1ff8 ) =
  1298. * {0x55,0xaa,0x55,0xaa,0x55,0xaa,0x55,0xaa};
  1299. *
  1300. * Set XRAM User Mode 1
  1301. *
  1302. * If the User mode 1 is selected, the Boot ROM will jump to program memory
  1303. * address 0000H to execute the user code in the XRAM memory. To use this
  1304. * mode, the XRAM must be pre-loaded with user code. This is the normal
  1305. * operating mode of the TC2x_SCR. For the last 8 bytes of XRAM starting
  1306. * at address 0FF8H, user need to program 4 sets of pre-fixed bytes with
  1307. * each set containing 55H followed by AAH. User code will not be executed
  1308. * and SCR will enter an endless loop if the memory content does not match
  1309. * these data sequence. It is used to avoid an unintentional entry to User
  1310. * Mode 1. Before entering the endless loop, the start-up code will trigger
  1311. * an interrupt to the main controller by setting bit NMICON.SCRINTTC to 1
  1312. * with a value of 81H in SCRINTEXCHG register. If there is a match, the same
  1313. * interrupt is triggered to the main controller with a value of 80H in
  1314. * SCRINTEXCHG register to indicate the execution of user code.
  1315. *
  1316. */
  1317. static void inline scr_set_user_mode_1( void )
  1318. {
  1319. unsigned char i;
  1320. qspi2_start_of_frame();
  1321. for( i = 0; i < 4; i++ )
  1322. {
  1323. qspi2_write_word( 0x1FF8 + 2*i, 0xAA55 );
  1324. }
  1325. qspi2_end_of_frame();
  1326. }
  1327. #endif
  1328. static _Bool scr_init( void )
  1329. {
  1330. _Bool download = 0;
  1331. _safety_endinit_clear();
  1332. scr_enable();
  1333. /*
  1334. * For Infineon TriBoard tc26x the suspend mode is supported
  1335. * and can not be disabled. PMSWSTAT.HWCFGEVR=7
  1336. * The Standby Controller Reset Indication flag is always zero
  1337. * after any kind of reset. SCU_PMSWSTAT.SCRST=0
  1338. *
  1339. * Upon cold power-on, the XRAM can be programmed immediately
  1340. * when SCU_PMSWCR1.SCRCFG=0 indicates that it is not programmed
  1341. * and SCU_PMSWCR2.B.SCRINT=0x80 indicates that it is booted,
  1342. * else request for boot mode 0.
  1343. *
  1344. * When the SCR is executing from XRAM after TriCore reset
  1345. * it is operating in standby mode SCU_PMSWCR1.SCRCFG=1, no
  1346. * download should be required. For debugging TriCore download to
  1347. * SCR it might be required to force downloading, this requires a
  1348. * boot mode 0 request. Enable _SCR_FORCE_DOWNLOAD.
  1349. */
  1350. if ( SCU_PMSWCR1.B.SCRCFG == 0 )
  1351. {
  1352. if ( SCU_PMSWCR2.B.SCRINT == 0x80 )
  1353. {
  1354. download = 1; /* Download to XRAM */
  1355. }
  1356. else
  1357. {
  1358. /*
  1359. * When the TriCore is (soft) reset after a cold
  1360. * boot of the SCR the SCRINT is not valid any
  1361. * more, request for boot mode 0.
  1362. */
  1363. scr_boot( 0 );
  1364. if ( SCU_PMSWCR2.B.SCRINT == 0x80 )
  1365. {
  1366. download = 1; /* Download to XRAM */
  1367. }
  1368. }
  1369. }
  1370. else if ( SCU_PMSWCR1.B.SCRCFG == 1 )
  1371. {
  1372. /*
  1373. * SCR is already running in user mode 1,
  1374. * code is not downloaded by default.
  1375. */
  1376. // #define _SCR_FORCE_DOWNLOAD
  1377. #ifdef _SCR_FORCE_DOWNLOAD
  1378. scr_boot( 0 );
  1379. if ( SCU_PMSWCR2.B.SCRINT == 0x80 )
  1380. {
  1381. download = 1; /* Download to XRAM */
  1382. }
  1383. #endif
  1384. }
  1385. else
  1386. {
  1387. /*
  1388. * SCR debug DAP or SPD user mode
  1389. * no TriCore download.
  1390. */
  1391. }
  1392. _safety_endinit_set();
  1393. return download;
  1394. }
  1395. _Bool _scr_start( void )
  1396. {
  1397. _Bool start = 0;
  1398. _safety_endinit_clear();
  1399. scr_boot( 1 ); /* Reset xc800 to start user code */
  1400. if ( SCU_PMSWCR2.B.SCRINT == 0x80 )
  1401. {
  1402. start = 1; /* User code started */
  1403. }
  1404. _safety_endinit_set();
  1405. return start;
  1406. }
  1407. void _scr_write( const unsigned char *romdata, unsigned int length )
  1408. {
  1409. unsigned short addr;
  1410. unsigned short data;
  1411. qspi2_start_of_frame();
  1412. for( addr = 0; addr < length; addr += 2 )
  1413. {
  1414. data = romdata[addr] | (romdata[addr + 1] << 8);
  1415. qspi2_write_word( addr, data );
  1416. }
  1417. qspi2_end_of_frame();
  1418. }
  1419. /*
  1420. * The image of the xc800 has a fixed length
  1421. */
  1422. #define LENGTH 1024*8
  1423. extern const unsigned char _lc_ub__rodata_xc800init[LENGTH];
  1424. static void _xc800_init ( void )
  1425. {
  1426. if ( scr_init() ) /* Only download when allowed */
  1427. {
  1428. qspi2_init();
  1429. _scr_write( _lc_ub__rodata_xc800init, LENGTH ); /* write data to xc800 xram */
  1430. #ifdef _USER_MODE_1
  1431. /*
  1432. * The XRAM signature should be part of your xc800
  1433. * application ROM immage.
  1434. */
  1435. scr_set_user_mode_1(); /* Set XRAM signature */
  1436. #endif
  1437. qspi2_end();
  1438. }
  1439. }
  1440. /*
  1441. * QSPI2 Configuration for transferring data to and from XRAM.
  1442. */
  1443. enum {
  1444. TOS_CPU0=0,
  1445. TOS_CPU1=1,
  1446. TOS_CPU2=2
  1447. };
  1448. #define QSPI2_INTERRUPT_NR 255
  1449. /*
  1450. * Baud rate configuration QSPI2
  1451. * Baud rate = fBAUD2 / ((QSPI2TQ+1) * (ECONQ+1) * (ECONA+1 + ECONB + ECONC ) )
  1452. * 50Mbaud = 200Mhz/4
  1453. */
  1454. #define QSPI2TQ 0
  1455. #define ECONQ 0
  1456. #define ECONA 1
  1457. #define ECONB 2
  1458. #define ECONC 0
  1459. #define ECON ((ECONQ) | (ECONA << 6) | (ECONB << 8) | (ECONC << 10))
  1460. static void qspi2_init( void )
  1461. {
  1462. _endinit_clear();
  1463. QSPI2_CLC.U=0x8; /* Enable QSPI2 */
  1464. _endinit_set();
  1465. QSPI2_GLOBALCON.B.TQ = QSPI2TQ; /* Divide Global Time Quantum Length */
  1466. QSPI2_PISEL.B.MRIS = 0x7; /* SPI MRST input MRST2H (7=H,0..7->A..H) */
  1467. QSPI2_GLOBALCON1.U = (0x3 << 9); /* Tx and Rx Interrupt Event Enabled */
  1468. QSPI2_ECON7.U = ECON; /* Set baud rate */
  1469. QSPI2_SSOC.U = (0x1 << 31); /* Enable SLSO Output 31 */
  1470. QSPI2_GLOBALCON.B.EN = 1; /* RUN requested */
  1471. /*
  1472. * QSPI2 is serviced by TOS_CPU0
  1473. *
  1474. * [0..7] SRPN=QSPI2_TXRX_INTERRUPT_NR
  1475. * [10] Service Request enable
  1476. * [11..12] TOS=TriCore interrupt 0=CPU0, 1=CPU1, 2=CPU2, 3=SDMA, 4=DMA
  1477. */
  1478. SRC_QSPI2TX.U = QSPI2_INTERRUPT_NR | ( 1<<10 ) | (TOS_CPU0<<11);
  1479. SRC_QSPI2RX.U = QSPI2_INTERRUPT_NR | ( 1<<10 ) | (TOS_CPU0<<11);
  1480. __enable(); /* QSPI2 interrupts are serviced */
  1481. }
  1482. static void qspi2_end( void )
  1483. {
  1484. QSPI2_GLOBALCON.B.EN = 0; /* PAUSE requested */
  1485. SRC_QSPI2TX.U = 0; /* QSPI2 TX service disabled */
  1486. SRC_QSPI2RX.U = 0; /* QSPI2 RX service disabled */
  1487. __disable(); /* QSPI2 interrupts are disabled */
  1488. _endinit_clear();
  1489. QSPI2_CLC.U = 0x1; /* Disable QSPI2 */
  1490. _endinit_set();
  1491. }
  1492. static void inline qspi2_start_of_frame( void )
  1493. {
  1494. /*
  1495. * The communication between the main TC2x system and XRAM is
  1496. * based on a pre-fixed SPI software protocol.
  1497. *
  1498. * BACON_ENTRY Writes to this location are BACON configurations
  1499. * BACON.TRAIL Trailing Delay Length 2 TQSPI units
  1500. * BACON.MSB Shift MSB first
  1501. * BACON.BYTE Data length in bits
  1502. * BACON.DL Data Length 32 bits
  1503. * BACON.BS SLSO15 channel select. Access the SCR XRAM
  1504. * via QSPI2 (SLSO15) interface.
  1505. */
  1506. QSPI2_BACONENTRY.U = (0x0 |(0x1 << 16) | (0x1 << 21 ) | (0 << 22) | (0x1F << 23) | (0xF << 28));
  1507. }
  1508. static void inline qspi2_end_of_frame( void )
  1509. {
  1510. QSPI2_BACONENTRY.U = 1; /* Last frame */
  1511. /*
  1512. * When the 32-bit SPI frame contains the value FFFF:FFFFH,
  1513. * it indicates that the current frame is the End-of-Frame (EOF).
  1514. * This frame will then be ignored by the arbiter state machine.
  1515. */
  1516. qspi2_write_word( 0xFFFF, 0xFFFF );
  1517. }
  1518. static volatile _Bool __far qspi2_transmitted;
  1519. static volatile _Bool __far qspi2_received;
  1520. static unsigned int __far qspi2_rxexit;
  1521. static void qspi2_send_data( unsigned int data_entry )
  1522. {
  1523. qspi2_transmitted = 0;
  1524. scr_wait(); /* Wait until XRAM accessible */
  1525. QSPI2_DATAENTRY0.U = data_entry; /* Write TX FIFO */
  1526. while( !qspi2_transmitted ); /* Wait until transmitted */
  1527. }
  1528. /*
  1529. * qspi2_write_word and qspi2_read_word data_extry:
  1530. *
  1531. * [31] Read/Write indication:
  1532. * - 1 indicates a write access to XRAM.
  1533. * - 0 indicates a read access from XRAM.
  1534. * [30:29] Control bits:
  1535. * - Should be written with 0.
  1536. * [28:16] 13-bit XRAM address.
  1537. * Note: For smaller XRAM where the address width is
  1538. * less than 13 bits, the unused uppermost bits should
  1539. * be written with 0.
  1540. * [15:0] 16-bit data.
  1541. */
  1542. static void qspi2_write_word( unsigned short address, unsigned short data )
  1543. {
  1544. unsigned int data_entry = (0x1 << 31) | (address << 16) | data;
  1545. qspi2_send_data( data_entry );
  1546. }
  1547. void __interrupt( QSPI2_INTERRUPT_NR ) qspi2_rxtx( void )
  1548. {
  1549. if ( QSPI2_STATUS.B.TXF == 1 )
  1550. {
  1551. /*
  1552. * Transmit Interrupt Request Flag
  1553. * Flags an occurrence of a request to feed the TXFIFO,
  1554. * which is generated when an element is fetched from the
  1555. * FIFO, and the FIFO filling level is equal or less than the
  1556. * set threshold level.
  1557. *
  1558. * Transmit Event Flag Clear
  1559. * Write clears the STATUS.TXF bit.
  1560. */
  1561. QSPI2_FLAGSCLEAR.B.TXC = 1;
  1562. QSPI2_STATUS.B.TXF; /* Read back else TXF not cleared immediate, why? */
  1563. qspi2_transmitted = 1; /* Flag transmitted */
  1564. }
  1565. else if ( QSPI2_STATUS.B.RXF == 1 )
  1566. {
  1567. /*
  1568. * Receive Interrupt Request Flag
  1569. * Flags an occurrence of a request to empty the RXFIFO,
  1570. * which is generated when an element is written into the
  1571. * FIFO, and the FIFO filling level is equal or greater than
  1572. * the set threshold level.
  1573. *
  1574. * Receive Event Flag Clear
  1575. * Write clears the STATUS.RXF bit.
  1576. */
  1577. QSPI2_FLAGSCLEAR.B.RXC = 1;
  1578. /*
  1579. * RXEXIT - reads from this location deliver either
  1580. * data or data and status, based on a set of rules.
  1581. */
  1582. qspi2_rxexit = QSPI2_RXEXIT.U;
  1583. qspi2_received = 1; /* Flag received */
  1584. }
  1585. }
  1586. #endif
  1587. #endif