Cm3Start.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*******************************************************************************
  2. * File Name: Cm3Start.c
  3. * Version 4.0
  4. *
  5. * Description:
  6. * Startup code for the ARM CM3.
  7. *
  8. ********************************************************************************
  9. * Copyright 2008-2013, Cypress Semiconductor Corporation. All rights reserved.
  10. * You may use this file only in accordance with the license, terms, conditions,
  11. * disclaimers, and limitations in the end user license agreement accompanying
  12. * the software package with which this file was provided.
  13. *******************************************************************************/
  14. #include <limits.h>
  15. #include "cydevice_trm.h"
  16. #include "cytypes.h"
  17. #include "cyfitter_cfg.h"
  18. #include "CyLib.h"
  19. #include "CyDmac.h"
  20. #include "cyfitter.h"
  21. #define CY_NUM_INTERRUPTS (32u)
  22. #define CY_NUM_VECTORS (CYINT_IRQ_BASE + CY_NUM_INTERRUPTS)
  23. #define CY_NUM_ROM_VECTORS (4u)
  24. #define CY_NVIC_APINT_PTR ((reg32 *) CYREG_NVIC_APPLN_INTR)
  25. #define CY_NVIC_CFG_CTRL_PTR ((reg32 *) CYREG_NVIC_CFG_CONTROL)
  26. #define CY_NVIC_APINT_PRIGROUP_3_5 (0x00000400u) /* Priority group 3.5 split */
  27. #define CY_NVIC_APINT_VECTKEY (0x05FA0000u) /* This key is required in order to write the NVIC_APINT register */
  28. #define CY_NVIC_CFG_STACKALIGN (0x00000200u) /* This specifies that the exception stack must be 8 byte aligned */
  29. /* Extern functions */
  30. extern void CyBtldr_CheckLaunch(void);
  31. /* Function prototypes */
  32. void initialize_psoc(void);
  33. CY_ISR(IntDefaultHandler);
  34. void Reset(void);
  35. CY_ISR(IntDefaultHandler);
  36. #if defined(__ARMCC_VERSION)
  37. #define INITIAL_STACK_POINTER ((cyisraddress)(uint32)&Image$$ARM_LIB_STACK$$ZI$$Limit)
  38. #elif defined (__GNUC__)
  39. #define INITIAL_STACK_POINTER (&__cy_stack)
  40. #elif defined (__ICCARM__)
  41. #pragma language=extended
  42. #pragma segment="CSTACK"
  43. #define INITIAL_STACK_POINTER { .__ptr = __sfe( "CSTACK" ) }
  44. extern void __iar_program_start( void );
  45. extern void __iar_data_init3 (void);
  46. #endif /* (__ARMCC_VERSION) */
  47. /* Global variables */
  48. #if !defined (__ICCARM__)
  49. CY_NOINIT static uint32 cySysNoInitDataValid;
  50. #endif /* !defined (__ICCARM__) */
  51. /*******************************************************************************
  52. * Default Ram Interrupt Vector table storage area. Must be 256-byte aligned.
  53. *******************************************************************************/
  54. #if defined (__ICCARM__)
  55. #pragma location=".ramvectors"
  56. #pragma data_alignment=256
  57. #else
  58. CY_SECTION(".ramvectors")
  59. CY_ALIGN(256)
  60. #endif /* defined (__ICCARM__) */
  61. cyisraddress CyRamVectors[CY_NUM_VECTORS];
  62. /*******************************************************************************
  63. * Function Name: IntDefaultHandler
  64. ********************************************************************************
  65. *
  66. * Summary:
  67. * This function is called for all interrupts, other than reset, that get
  68. * called before the system is setup.
  69. *
  70. * Parameters:
  71. * None
  72. *
  73. * Return:
  74. * None
  75. *
  76. * Theory:
  77. * Any value other than zero is acceptable.
  78. *
  79. *******************************************************************************/
  80. CY_ISR(IntDefaultHandler)
  81. {
  82. while(1)
  83. {
  84. /***********************************************************************
  85. * We should never get here. If we do, a serious problem occured, so go
  86. * into an infinite loop.
  87. ***********************************************************************/
  88. }
  89. }
  90. #if defined(__ARMCC_VERSION)
  91. /* Local function for the device reset. */
  92. extern void Reset(void);
  93. /* Application entry point. */
  94. extern void $Super$$main(void);
  95. /* Linker-generated Stack Base addresses, Two Region and One Region */
  96. extern uint32 Image$$ARM_LIB_STACK$$ZI$$Limit;
  97. /* RealView C Library initialization. */
  98. extern int __main(void);
  99. /*******************************************************************************
  100. * Function Name: Reset
  101. ********************************************************************************
  102. *
  103. * Summary:
  104. * This function handles the reset interrupt for the RVDS/MDK toolchains.
  105. * This is the first bit of code that is executed at startup.
  106. *
  107. * Parameters:
  108. * None
  109. *
  110. * Return:
  111. * None
  112. *
  113. *******************************************************************************/
  114. void Reset(void)
  115. {
  116. #if(CYDEV_PROJ_TYPE != CYDEV_PROJ_TYPE_LOADABLE)
  117. /* For PSoC 5LP, debugging is enabled by default */
  118. #if(CYDEV_DEBUGGING_ENABLE == 0)
  119. *(reg32 *)(CYDEV_DEBUG_ENABLE_REGISTER) |= CYDEV_DEBUG_ENABLE_MASK;
  120. #endif /* (CYDEV_DEBUGGING_ENABLE) */
  121. /* Reset Status Register has Read-to-clear SW access mode.
  122. * Preserve current RESET_SR0 state to make it available for next reading.
  123. */
  124. *(reg32 *)(CYREG_PHUB_CFGMEM23_CFG1) = *(reg32 *)(CYREG_RESET_SR0);
  125. #endif /* (CYDEV_PROJ_TYPE != CYDEV_PROJ_TYPE_LOADABLE) */
  126. #if(CYDEV_BOOTLOADER_ENABLE)
  127. CyBtldr_CheckLaunch();
  128. #endif /* (CYDEV_BOOTLOADER_ENABLE) */
  129. __main();
  130. }
  131. /*******************************************************************************
  132. * Function Name: $Sub$$main
  133. ********************************************************************************
  134. *
  135. * Summary:
  136. * This function is called imediatly before the users main
  137. *
  138. * Parameters:
  139. * None
  140. *
  141. * Return:
  142. * None
  143. *
  144. *******************************************************************************/
  145. void $Sub$$main(void)
  146. {
  147. initialize_psoc();
  148. /* Call original main */
  149. $Super$$main();
  150. while (1)
  151. {
  152. /* If main returns it is undefined what we should do. */
  153. }
  154. }
  155. #elif defined(__GNUC__)
  156. void Start_c(void);
  157. /* Stack Base address */
  158. extern void __cy_stack(void);
  159. /* Application entry point. */
  160. extern int main(void);
  161. /* The static objects constructors initializer */
  162. extern void __libc_init_array(void);
  163. typedef unsigned char __cy_byte_align8 __attribute ((aligned (8)));
  164. struct __cy_region
  165. {
  166. __cy_byte_align8 *init; /* Initial contents of this region. */
  167. __cy_byte_align8 *data; /* Start address of region. */
  168. size_t init_size; /* Size of initial data. */
  169. size_t zero_size; /* Additional size to be zeroed. */
  170. };
  171. extern const struct __cy_region __cy_regions[];
  172. extern const char __cy_region_num __attribute__((weak));
  173. #define __cy_region_num ((size_t)&__cy_region_num)
  174. /*******************************************************************************
  175. * Function Name: Reset
  176. ********************************************************************************
  177. *
  178. * Summary:
  179. * This function handles the reset interrupt for the GCC toolchain. This is the
  180. * first bit of code that is executed at startup.
  181. *
  182. * Parameters:
  183. * None
  184. *
  185. * Return:
  186. * None
  187. *
  188. *******************************************************************************/
  189. void Reset(void)
  190. {
  191. #if(CYDEV_PROJ_TYPE != CYDEV_PROJ_TYPE_LOADABLE)
  192. /* For PSoC 5LP, debugging is enabled by default */
  193. #if(CYDEV_DEBUGGING_ENABLE == 0)
  194. *(reg32 *)(CYDEV_DEBUG_ENABLE_REGISTER) |= CYDEV_DEBUG_ENABLE_MASK;
  195. #endif /* (CYDEV_DEBUGGING_ENABLE) */
  196. /* Reset Status Register has Read-to-clear SW access mode.
  197. * Preserve current RESET_SR0 state to make it available for next reading.
  198. */
  199. *(reg32 *)(CYREG_PHUB_CFGMEM23_CFG1) = *(reg32 *)(CYREG_RESET_SR0);
  200. #endif /* (CYDEV_PROJ_TYPE != CYDEV_PROJ_TYPE_LOADABLE) */
  201. #if(CYDEV_BOOTLOADER_ENABLE)
  202. CyBtldr_CheckLaunch();
  203. #endif /* (CYDEV_BOOTLOADER_ENABLE) */
  204. Start_c();
  205. }
  206. __attribute__((weak))
  207. void _exit(int status)
  208. {
  209. /* Cause a divide by 0 exception */
  210. int x = status / INT_MAX;
  211. x = 4 / x;
  212. while(1)
  213. {
  214. }
  215. }
  216. /*******************************************************************************
  217. * Function Name: Start_c
  218. ********************************************************************************
  219. *
  220. * Summary:
  221. * This function handles initializing the .data and .bss sections in
  222. * preperation for running standard C code. Once initialization is complete
  223. * it will call main(). This function will never return.
  224. *
  225. * Parameters:
  226. * None
  227. *
  228. * Return:
  229. * None
  230. *
  231. *******************************************************************************/
  232. void Start_c(void) __attribute__ ((noreturn));
  233. void Start_c(void)
  234. {
  235. unsigned regions = __cy_region_num;
  236. const struct __cy_region *rptr = __cy_regions;
  237. /* Initialize memory */
  238. for (regions = __cy_region_num, rptr = __cy_regions; regions--; rptr++)
  239. {
  240. uint32 *src = (uint32 *)rptr->init;
  241. uint32 *dst = (uint32 *)rptr->data;
  242. unsigned limit = rptr->init_size;
  243. unsigned count;
  244. for (count = 0u; count != limit; count += sizeof (uint32))
  245. {
  246. *dst++ = *src++;
  247. }
  248. limit = rptr->zero_size;
  249. for (count = 0u; count != limit; count += sizeof (uint32))
  250. {
  251. *dst++ = 0u;
  252. }
  253. }
  254. /* Invoke static objects constructors */
  255. __libc_init_array();
  256. (void) main();
  257. while (1)
  258. {
  259. /* If main returns, make sure we don't return. */
  260. }
  261. }
  262. #elif defined (__ICCARM__)
  263. /*******************************************************************************
  264. * Function Name: __low_level_init
  265. ********************************************************************************
  266. *
  267. * Summary:
  268. * This function perform early initializations for the IAR Embedded
  269. * Workbench IDE. It is executed in the context of reset interrupt handler
  270. * before the data sections are initialized.
  271. *
  272. * Parameters:
  273. * None
  274. *
  275. * Return:
  276. * The value that determines whether or not data sections should be initialized
  277. * by the system startup code:
  278. * 0 - skip data sections initialization;
  279. * 1 - initialize data sections;
  280. *
  281. *******************************************************************************/
  282. int __low_level_init(void)
  283. {
  284. #if(CYDEV_PROJ_TYPE != CYDEV_PROJ_TYPE_LOADABLE)
  285. /* For PSoC 5LP, debugging is enabled by default */
  286. #if(CYDEV_DEBUGGING_ENABLE == 0)
  287. *(reg32 *)(CYDEV_DEBUG_ENABLE_REGISTER) |= CYDEV_DEBUG_ENABLE_MASK;
  288. #endif /* (CYDEV_DEBUGGING_ENABLE) */
  289. /* Reset Status Register has Read-to-clear SW access mode.
  290. * Preserve current RESET_SR0 state to make it available for next reading.
  291. */
  292. *(reg32 *)(CYREG_PHUB_CFGMEM23_CFG1) = *(reg32 *)(CYREG_RESET_SR0);
  293. #endif /* (CYDEV_PROJ_TYPE != CYDEV_PROJ_TYPE_LOADABLE) */
  294. #if (CYDEV_BOOTLOADER_ENABLE)
  295. CyBtldr_CheckLaunch();
  296. #endif /* CYDEV_BOOTLOADER_ENABLE */
  297. /* Initialize data sections */
  298. __iar_data_init3();
  299. initialize_psoc();
  300. return 0;
  301. }
  302. #endif /* __GNUC__ */
  303. /*******************************************************************************
  304. *
  305. * Default Rom Interrupt Vector table.
  306. *
  307. *******************************************************************************/
  308. #if defined(__ARMCC_VERSION)
  309. /* Suppress diagnostic message 1296-D: extended constant initialiser used */
  310. #pragma diag_suppress 1296
  311. #endif /* defined(__ARMCC_VERSION) */
  312. #if defined (__ICCARM__)
  313. #pragma location=".romvectors"
  314. const intvec_elem __vector_table[CY_NUM_ROM_VECTORS] =
  315. #else
  316. CY_SECTION(".romvectors")
  317. const cyisraddress RomVectors[CY_NUM_ROM_VECTORS] =
  318. #endif /* defined (__ICCARM__) */
  319. {
  320. INITIAL_STACK_POINTER, /* The initial stack pointer 0 */
  321. #if defined (__ICCARM__) /* The reset handler 1 */
  322. __iar_program_start,
  323. #else
  324. (cyisraddress)&Reset,
  325. #endif /* defined (__ICCARM__) */
  326. &IntDefaultHandler, /* The NMI handler 2 */
  327. &IntDefaultHandler, /* The hard fault handler 3 */
  328. };
  329. #if defined(__ARMCC_VERSION)
  330. #pragma diag_default 1296
  331. #endif /* defined(__ARMCC_VERSION) */
  332. /*******************************************************************************
  333. * Function Name: initialize_psoc
  334. ********************************************************************************
  335. *
  336. * Summary:
  337. * This function used to initialize the PSoC chip before calling main.
  338. *
  339. * Parameters:
  340. * None
  341. *
  342. * Return:
  343. * None
  344. *
  345. *******************************************************************************/
  346. #if (defined(__GNUC__) && !defined(__ARMCC_VERSION))
  347. __attribute__ ((constructor(101)))
  348. #endif
  349. void initialize_psoc(void)
  350. {
  351. uint32 i;
  352. /* Set Priority group 5. */
  353. /* Writes to NVIC_APINT register require the VECTKEY in the upper half */
  354. *CY_NVIC_APINT_PTR = CY_NVIC_APINT_VECTKEY | CY_NVIC_APINT_PRIGROUP_3_5;
  355. *CY_NVIC_CFG_CTRL_PTR |= CY_NVIC_CFG_STACKALIGN;
  356. /* Set Ram interrupt vectors to default functions. */
  357. for (i = 0u; i < CY_NUM_VECTORS; i++)
  358. {
  359. #if defined (__ICCARM__)
  360. CyRamVectors[i] = (i < CY_NUM_ROM_VECTORS) ? __vector_table[i].__fun : &IntDefaultHandler;
  361. #else
  362. CyRamVectors[i] = (i < CY_NUM_ROM_VECTORS) ? RomVectors[i] : &IntDefaultHandler;
  363. #endif /* defined (__ICCARM__) */
  364. }
  365. /* Was stored in CFGMEM to avoid being cleared while SRAM gets cleared */
  366. CyResetStatus = CY_GET_REG8(CYREG_PHUB_CFGMEM23_CFG1);
  367. /* Point NVIC at the RAM vector table. */
  368. *CYINT_VECT_TABLE = CyRamVectors;
  369. /* Initialize the configuration registers. */
  370. cyfitter_cfg();
  371. #if(0u != DMA_CHANNELS_USED__MASK0)
  372. /* Setup DMA - only necessary if the design contains a DMA component. */
  373. CyDmacConfigure();
  374. #endif /* (0u != DMA_CHANNELS_USED__MASK0) */
  375. #if !defined (__ICCARM__)
  376. /* Actually, no need to clean this variable, just to make compiler happy. */
  377. cySysNoInitDataValid = 0u;
  378. #endif /* !defined (__ICCARM__) */
  379. }
  380. /* [] END OF FILE */