crt_stm32w108.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <stdio.h>
00021 #include <sys/stat.h>
00022 #define RESERVED 0
00023 #define IAP_BOOTLOADER_APP_SWITCH_SIGNATURE 0xb001204d
00024 #define IAP_BOOTLOADER_MODE_UART 0
00025
00026 #include PLATFORM_HEADER
00027 void NMI_Handler(void);
00028 void HardFault_Handler(void);
00029 void MemManage_Handler(void);
00030 void BusFault_Handler(void);
00031 void UsageFault_Handler(void);
00032 void SVC_Handler(void);
00033 void DebugMonitor_Handler(void);
00034 void PendSV_Handler(void);
00035 void SysTick_Handler(void);
00036 void halTimer1Isr(void);
00037 void halTimer2Isr(void);
00038 void halManagementIsr(void);
00039 void halBaseBandIsr(void);
00040 void halSleepTimerIsr(void);
00041 void halSc1Isr(void);
00042 void halSc2Isr(void);
00043 void halSecurityIsr(void);
00044 void halStackMacTimerIsr(void);
00045 void stmRadioTransmitIsr(void);
00046 void stmRadioReceiveIsr(void);
00047 void halAdcIsr(void);
00048 void halIrqAIsr(void);
00049 void halIrqBIsr(void);
00050 void halIrqCIsr(void);
00051 void halIrqDIsr(void);
00052 void halDebugIsr(void);
00053
00054
00055
00056 extern unsigned long _etext;
00057 extern unsigned long _sidata;
00058 extern unsigned long _sdata;
00059 extern unsigned long _edata;
00060
00061 extern unsigned long _sbss;
00062 extern unsigned long _ebss;
00063
00064 extern void _estack;
00065
00066 #include "hal/micro/cortexm3/memmap.h"
00067 VAR_AT_SEGMENT(const HalFixedAddressTableType halFixedAddressTable, __FAT__);
00068
00069
00070
00071
00072
00073 void Reset_Handler(void) __attribute__((__interrupt__));
00074 extern int main(void);
00075 extern void halInternalSwitchToXtal(void);
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 __attribute__ ((section(".isr_vector")))
00087 void (* const g_pfnVectors[])(void) =
00088 {
00089 &_estack,
00090 Reset_Handler,
00091 NMI_Handler,
00092 HardFault_Handler,
00093 MemManage_Handler,
00094 BusFault_Handler,
00095 UsageFault_Handler,
00096 RESERVED,
00097 RESERVED,
00098 RESERVED,
00099 RESERVED,
00100 SVC_Handler,
00101 DebugMonitor_Handler,
00102 RESERVED,
00103 PendSV_Handler,
00104 SysTick_Handler,
00105 halTimer1Isr,
00106 halTimer2Isr,
00107 halManagementIsr,
00108 halBaseBandIsr,
00109 halSleepTimerIsr,
00110 halSc1Isr,
00111 halSc2Isr,
00112 halSecurityIsr,
00113 halStackMacTimerIsr,
00114 stmRadioTransmitIsr,
00115 stmRadioReceiveIsr,
00116 halAdcIsr,
00117 halIrqAIsr,
00118 halIrqBIsr,
00119 halIrqCIsr,
00120 halIrqDIsr,
00121 halDebugIsr,
00122 };
00123
00124 static void setStackPointer(int32u address) __attribute__((noinline));
00125 static void setStackPointer(int32u address)
00126 {
00127
00128
00129
00130 asm(".short 0x4685");
00131 }
00132
00133 static const int16u blOffset[] = {
00134 0x0715 - 0x03ad - 0x68,
00135 0x0719 - 0x03ad - 0x6C
00136 };
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 void Reset_Handler(void)
00148 {
00149
00150
00151 VREG = 0x00000307;
00152
00153
00154
00155
00156
00157
00158
00159
00160 asm("CPSID i");
00161
00162
00163
00164
00165
00166
00167 DEBUG_EMCR &= ~DEBUG_EMCR_VC_CORERESET;
00168
00169
00170
00171 FLASH_ACCESS = (FLASH_ACCESS_PREFETCH_EN |
00172 (1<<FLASH_ACCESS_CODE_LATENCY_BIT));
00173
00174
00175
00176
00177
00178
00179
00180
00181 SCS_AIRCR = (0x05FA0000 | (4 <<SCS_AIRCR_PRIGROUP_BIT));
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 #define CRITICAL (0 <<3)
00194 #define HIGH (8 <<3)
00195 #define MED (16 <<3)
00196 #define LOW (28 <<3)
00197 #define NONE (31 <<3)
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 SCS_SHPR_7to4 = ((CRITICAL <<SCS_SHPR_7to4_PRI_4_BIT) |
00208 (CRITICAL <<SCS_SHPR_7to4_PRI_5_BIT) |
00209 (CRITICAL <<SCS_SHPR_7to4_PRI_6_BIT) |
00210 (NONE <<SCS_SHPR_7to4_PRI_7_BIT));
00211 SCS_SHPR_11to8 = ((NONE <<SCS_SHPR_11to8_PRI_8_BIT) |
00212 (NONE <<SCS_SHPR_11to8_PRI_9_BIT) |
00213 (NONE <<SCS_SHPR_11to8_PRI_10_BIT) |
00214 (HIGH <<SCS_SHPR_11to8_PRI_11_BIT));
00215 SCS_SHPR_15to12 = ((MED <<SCS_SHPR_15to12_PRI_12_BIT) |
00216 (NONE <<SCS_SHPR_15to12_PRI_13_BIT) |
00217 (HIGH <<SCS_SHPR_15to12_PRI_14_BIT) |
00218 (MED <<SCS_SHPR_15to12_PRI_15_BIT));
00219 NVIC_IPR_3to0 = ((MED <<NVIC_IPR_3to0_PRI_0_BIT) |
00220 (MED <<NVIC_IPR_3to0_PRI_1_BIT) |
00221 (HIGH <<NVIC_IPR_3to0_PRI_2_BIT) |
00222 (MED <<NVIC_IPR_3to0_PRI_3_BIT));
00223 NVIC_IPR_7to4 = ((MED <<NVIC_IPR_7to4_PRI_4_BIT) |
00224 (MED <<NVIC_IPR_7to4_PRI_5_BIT) |
00225 (MED <<NVIC_IPR_7to4_PRI_6_BIT) |
00226 (MED <<NVIC_IPR_7to4_PRI_7_BIT));
00227 NVIC_IPR_11to8 = ((MED <<NVIC_IPR_11to8_PRI_8_BIT) |
00228 (MED <<NVIC_IPR_11to8_PRI_9_BIT) |
00229 (MED <<NVIC_IPR_11to8_PRI_10_BIT) |
00230 (MED <<NVIC_IPR_11to8_PRI_11_BIT));
00231 NVIC_IPR_15to12 = ((MED <<NVIC_IPR_15to12_PRI_12_BIT) |
00232 (MED <<NVIC_IPR_15to12_PRI_13_BIT) |
00233 (MED <<NVIC_IPR_15to12_PRI_14_BIT) |
00234 (MED <<NVIC_IPR_15to12_PRI_15_BIT));
00235 NVIC_IPR_19to16 = ((LOW <<NVIC_IPR_19to16_PRI_16_BIT));
00236
00237
00238
00239
00240
00241 SCS_CCR = SCS_CCR_DIV_0_TRP_MASK;
00242 SCS_SHCSR = ( SCS_SHCSR_USGFAULTENA_MASK
00243 | SCS_SHCSR_BUSFAULTENA_MASK
00244 | SCS_SHCSR_MEMFAULTENA_MASK );
00245
00246
00247 if((RESET_EVENT&RESET_DSLEEP) == RESET_DSLEEP) {
00248
00249
00250
00251
00252 void halTriggerContextRestore(void);
00253 extern volatile boolean halPendSvSaveContext;
00254 halPendSvSaveContext = 0;
00255 SCS_ICSR |= SCS_ICSR_PENDSVSET;
00256 halTriggerContextRestore();
00257
00258 while(1) { ; }
00259 }
00260
00261
00262 if ((*((int32u *)RAM_BOTTOM) == IAP_BOOTLOADER_APP_SWITCH_SIGNATURE) && (*((int8u *)(RAM_BOTTOM+4)) == IAP_BOOTLOADER_MODE_UART)){
00263 int8u cut = *(volatile int8u *) 0x08040798;
00264 int16u offset = 0;
00265 typedef void (*EntryPoint)(void);
00266 offset = (halFixedAddressTable.baseTable.version == 3) ? blOffset[cut - 2] : 0;
00267 *((int32u *)RAM_BOTTOM) = 0;
00268 if (offset) {
00269 halInternalSwitchToXtal();
00270 }
00271 EntryPoint entryPoint = (EntryPoint)(*(int32u *)(FIB_BOTTOM+4) - offset);
00272 setStackPointer(*(int32u *)FIB_BOTTOM);
00273 entryPoint();
00274 }
00275
00276 INTERRUPTS_OFF();
00277 asm("CPSIE i");
00278
00279
00280
00281
00282
00283
00284
00285
00286 unsigned long *pulSrc, *pulDest;
00287
00288
00289
00290
00291 pulSrc = &_sidata;
00292 for(pulDest = &_sdata; pulDest < &_edata; )
00293 {
00294 *(pulDest++) = *(pulSrc++);
00295 }
00296
00297
00298
00299
00300 for(pulDest = &_sbss; pulDest < &_ebss; )
00301 {
00302 *(pulDest++) = 0;
00303 }
00304
00305
00306
00307
00308 main();
00309 }
00310 #ifdef USE_HEAP
00311 static unsigned char __HEAP_START[1024*3-560+0x200];
00312 caddr_t _sbrk ( int incr )
00313 {
00314 static unsigned char *heap = NULL;
00315 unsigned char *prev_heap;
00316
00317
00318 if (heap == NULL) {
00319 heap = (unsigned char *)__HEAP_START;
00320 }
00321 prev_heap = heap;
00322
00323 if ((heap + incr) > (__HEAP_START + sizeof(__HEAP_START))) {
00324 prev_heap = NULL;
00325 } else {
00326 heap += incr;
00327 }
00328 if (prev_heap == NULL) {
00329 printf ("_sbrk %d return %p\n\r", incr, prev_heap);
00330 }
00331 return (caddr_t) prev_heap;
00332 }
00333 #else
00334 caddr_t _sbrk ( int incr )
00335 {
00336 return NULL;
00337 }
00338 #endif
00339 int _lseek (int file,
00340 int ptr,
00341 int dir)
00342 {
00343 return 0;
00344 }
00345 int _close (int file)
00346 {
00347 return -1;
00348 }
00349
00350 void _exit (int n)
00351 {
00352
00353 while(1);
00354 }
00355
00356
00357
00358 int _kill (int n, int m)
00359 {
00360 return -1;
00361 }
00362 int _fstat(int file, struct stat *st)
00363 {
00364 st->st_mode = S_IFCHR;
00365 return 0;
00366 }
00367 int _isatty (int fd)
00368 {
00369 return 1;
00370 fd = fd;
00371 }
00372 int _getpid (int n)
00373 {
00374 return -1;
00375 }
00376 int _open (const char * path,
00377 int flags,
00378 ...)
00379 {
00380 return -1;
00381 }
00382 int _fflush_r(struct _reent *r, FILE *f)
00383 {
00384 return 0;
00385 }
00386
00387
00388
00389