crt_stm32w108.c

00001 /******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
00002 * File Name          : stm32f10x_vector.c
00003 * Author             : MCD Tools Team
00004 * Date First Issued  : 05/14/2007
00005 * Description        : This file contains the vector table for STM32F10x.
00006 *                        After Reset the Cortex-M3 processor is in Thread mode,
00007 *                        priority is Privileged, and the Stack is set to Main.
00008 ********************************************************************************
00009 * History:
00010 * 05/14/2007: V0.2
00011 *
00012 ********************************************************************************
00013 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00014 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
00015 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
00016 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
00017 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
00018 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
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 /* Includes ----------------------------------------------------------------------*/
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 /* Exported types --------------------------------------------------------------*/
00055 /* Exported constants --------------------------------------------------------*/
00056 extern unsigned long _etext;
00057 extern unsigned long _sidata;           /* start address for the initialization values of the .data section. defined in linker script */
00058 extern unsigned long _sdata;            /* start address for the .data section. defined in linker script */
00059 extern unsigned long _edata;            /* end address for the .data section. defined in linker script */
00060 
00061 extern unsigned long _sbss;                     /* start address for the .bss section. defined in linker script */
00062 extern unsigned long _ebss;                     /* end address for the .bss section. defined in linker script */
00063 
00064 extern void _estack;            /* init value for the stack pointer. defined in linker script */
00065 
00066 #include "hal/micro/cortexm3/memmap.h"
00067 VAR_AT_SEGMENT(const HalFixedAddressTableType halFixedAddressTable, __FAT__);
00068 
00069 
00070 /* Private typedef -----------------------------------------------------------*/
00071 
00072 /* function prototypes ------------------------------------------------------*/
00073 void Reset_Handler(void) __attribute__((__interrupt__));
00074 extern int main(void);
00075 extern void  halInternalSwitchToXtal(void);
00076 
00077 /******************************************************************************
00078 *
00079 * The minimal vector table for a Cortex M3.  Note that the proper constructs
00080 * must be placed on this to ensure that it ends up at physical address
00081 * 0x0000.0000.
00082 *
00083 ******************************************************************************/
00084 
00085 
00086 __attribute__ ((section(".isr_vector")))
00087 void (* const g_pfnVectors[])(void) =
00088 {
00089   &_estack,            // The initial stack pointer
00090   Reset_Handler,             // 1 The reset handler
00091   NMI_Handler,              // 2
00092   HardFault_Handler,         // 3
00093   MemManage_Handler,         // 4
00094   BusFault_Handler,          // 5
00095   UsageFault_Handler,        // 6
00096   RESERVED,                  // 7
00097   RESERVED,                  // 8
00098   RESERVED,                  // 9
00099   RESERVED,                  // 10
00100   SVC_Handler,               // 11
00101   DebugMonitor_Handler,      // 12
00102   RESERVED,                  // 13
00103   PendSV_Handler,            // 14
00104   SysTick_Handler,           // 15
00105   halTimer1Isr,              // 16
00106   halTimer2Isr,              // 17
00107   halManagementIsr,          // 18
00108   halBaseBandIsr,            // 19
00109   halSleepTimerIsr,          // 20
00110   halSc1Isr,                 // 21
00111   halSc2Isr,                 // 22
00112   halSecurityIsr,            // 23
00113   halStackMacTimerIsr,       // 24
00114   stmRadioTransmitIsr,       // 25
00115   stmRadioReceiveIsr,        // 26
00116   halAdcIsr,                 // 27
00117   halIrqAIsr,                // 28
00118   halIrqBIsr,                // 29
00119   halIrqCIsr,                // 30
00120   halIrqDIsr,                // 31
00121   halDebugIsr,               // 32
00122 };
00123 
00124 static  void setStackPointer(int32u address) __attribute__((noinline));
00125 static void setStackPointer(int32u address)
00126 {
00127   // This code is needed to generate the instruction below
00128   // that GNU ASM is refusing to add
00129   // asm("MOVS SP, r0");
00130   asm(".short 0x4685");
00131 }
00132 
00133 static const int16u blOffset[] = {
00134   0x0715 - 0x03ad - 0x68,
00135   0x0719 - 0x03ad - 0x6C
00136 };
00137 
00138 /*******************************************************************************
00139 * Function Name  : Reset_Handler
00140 * Description    : This is the code that gets called when the processor first starts execution
00141 *                      following a reset event.  Only the absolutely necessary set is performed,
00142 *                      after which the application supplied main() routine is called. 
00143 * Input          :
00144 * Output         :
00145 * Return         :
00146 *******************************************************************************/
00147 void Reset_Handler(void)
00148 {
00149   //Ensure there is enough margin on VREG_1V8 for stable RAM reads by
00150   //setting it to a code of 6.  VREG_1V2 can be left at its reset value.
00151   VREG = 0x00000307;
00152   
00153   // This code should be careful about the use of local variables in case the
00154   // reset type happens to be a deep sleep reset.  If the reset is not from
00155   // deep sleep, then locals can be freely used
00156 
00157   //When the Cortex-M3 exits reset, interrupts are enable.  Explicitely
00158   //disable them immediately using the standard set PRIMASK instruction.
00159   //Injecting an assembly instruction this early does not effect optimization.
00160   asm("CPSID i");
00161 
00162   //It is quite possible that when the Cortex-M3 begins executing code the
00163   //Core Reset Vector Catch is still left enabled.  Because this VC would
00164   //cause us to halt at reset if another reset event tripped, we should
00165   //clear it as soon as possible.  If a debugger wants to halt at reset,
00166   //it will set this bit again.
00167   DEBUG_EMCR &= ~DEBUG_EMCR_VC_CORERESET;
00168 
00169   //Configure flash access for optimal current consumption early
00170   //during boot to save as much current as we can.
00171   FLASH_ACCESS = (FLASH_ACCESS_PREFETCH_EN          |
00172                   (1<<FLASH_ACCESS_CODE_LATENCY_BIT));
00173 
00174   ////---- Always Configure Interrupt Priorities ----////
00175   //The STM32W support 5 bits of priority configuration.
00176   //  The cortex allows this to be further divided into preemption and a
00177   //  "tie-breaker" sub-priority.
00178   //We configure a scheme that allows for 3 bits (8 values) of preemption and
00179   //   2 bits (4 values) of tie-breaker by using the value 4 in PRIGROUP.
00180   //The value 0x05FA0000 is a special key required to write to this register.
00181   SCS_AIRCR = (0x05FA0000 | (4 <<SCS_AIRCR_PRIGROUP_BIT));
00182     
00183   //A few macros to help with interrupt priority configuration.  Really only 
00184   //  uses 6 of the possible levels, and ignores the tie-breaker sub-priority
00185   //  for now.
00186   //Don't forget that the priority level values need to be shifted into the
00187   //  top 5 bits of the 8 bit priority fields. (hence the <<3)
00188   //
00189   // NOTE: The ATOMIC and DISABLE_INTERRUPTS macros work by setting the 
00190   //       current priority to a value of 12, which still allows CRITICAL and 
00191   //       HIGH priority interrupts to fire, while blocking MED and LOW.
00192   //       If a different value is desired, spmr.s79 will need to be edited.
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   //With optimization turned on, the compiler will indentify all the values
00200   //and variables used here as constants at compile time and will truncate
00201   //this entire block of code to 98 bytes, comprised of 7 load-load-store
00202   //operations.
00203                     //vect00 is fixed                        //Stack pointer
00204                     //vect01 is fixed                        //Reset Vector
00205                     //vect02 is fixed                        //NMI Handler
00206                     //vect03 is fixed                        //Hard Fault Handler
00207   SCS_SHPR_7to4   = ((CRITICAL <<SCS_SHPR_7to4_PRI_4_BIT) |  //Memory Fault Handler 
00208                      (CRITICAL <<SCS_SHPR_7to4_PRI_5_BIT) |  //Bus Fault Handler
00209                      (CRITICAL <<SCS_SHPR_7to4_PRI_6_BIT) |  //Usage Fault Handler
00210                      (NONE <<SCS_SHPR_7to4_PRI_7_BIT));      //Reserved
00211   SCS_SHPR_11to8  = ((NONE <<SCS_SHPR_11to8_PRI_8_BIT)  |    //Reserved
00212                      (NONE <<SCS_SHPR_11to8_PRI_9_BIT)  |    //Reserved
00213                      (NONE <<SCS_SHPR_11to8_PRI_10_BIT) |    //Reserved
00214                      (HIGH <<SCS_SHPR_11to8_PRI_11_BIT));    //SVCall Handler
00215   SCS_SHPR_15to12 = ((MED  <<SCS_SHPR_15to12_PRI_12_BIT) |   //Debug Monitor Handler
00216                      (NONE <<SCS_SHPR_15to12_PRI_13_BIT) |   //Reserved
00217                      (HIGH <<SCS_SHPR_15to12_PRI_14_BIT) |   //PendSV Handler
00218                      (MED  <<SCS_SHPR_15to12_PRI_15_BIT));   //SysTick Handler
00219   NVIC_IPR_3to0   = ((MED  <<NVIC_IPR_3to0_PRI_0_BIT) |      //Timer 1 Handler
00220                      (MED  <<NVIC_IPR_3to0_PRI_1_BIT) |      //Timer 2 Handler
00221                      (HIGH <<NVIC_IPR_3to0_PRI_2_BIT) |      //Management Handler
00222                      (MED  <<NVIC_IPR_3to0_PRI_3_BIT));      //BaseBand Handler
00223   NVIC_IPR_7to4   = ((MED  <<NVIC_IPR_7to4_PRI_4_BIT) |      //Sleep Timer Handler
00224                      (MED  <<NVIC_IPR_7to4_PRI_5_BIT) |      //SC1 Handler
00225                      (MED  <<NVIC_IPR_7to4_PRI_6_BIT) |      //SC2 Handler
00226                      (MED  <<NVIC_IPR_7to4_PRI_7_BIT));      //Security Handler
00227   NVIC_IPR_11to8  = ((MED  <<NVIC_IPR_11to8_PRI_8_BIT)  |    //MAC Timer Handler
00228                      (MED  <<NVIC_IPR_11to8_PRI_9_BIT)  |    //MAC TX Handler
00229                      (MED  <<NVIC_IPR_11to8_PRI_10_BIT) |    //MAC RX Handler
00230                      (MED  <<NVIC_IPR_11to8_PRI_11_BIT));    //ADC Handler
00231   NVIC_IPR_15to12 = ((MED  <<NVIC_IPR_15to12_PRI_12_BIT) |   //GPIO IRQA Handler
00232                      (MED  <<NVIC_IPR_15to12_PRI_13_BIT) |   //GPIO IRQB Handler
00233                      (MED  <<NVIC_IPR_15to12_PRI_14_BIT) |   //GPIO IRQC Handler
00234                      (MED  <<NVIC_IPR_15to12_PRI_15_BIT));   //GPIO IRQD Handler
00235   NVIC_IPR_19to16 = ((LOW  <<NVIC_IPR_19to16_PRI_16_BIT));   //Debug Handler
00236                     //vect33 not implemented
00237                     //vect34 not implemented
00238                     //vect35 not implemented
00239 
00240   ////---- Always Configure System Handlers Control and Configuration ----////
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     //Since the 13 NVIC registers above are fixed values, they are restored
00249     //above (where they get set anyways during normal boot sequences) instead
00250     //of inside of the halInternalSleep code:
00251 
00252     void halTriggerContextRestore(void);
00253     extern volatile boolean halPendSvSaveContext;
00254     halPendSvSaveContext = 0;       //0 means restore context
00255     SCS_ICSR |= SCS_ICSR_PENDSVSET; //pend halPendSvIsr to enable later
00256     halTriggerContextRestore();     //sets MSP, enables interrupts
00257     //if the context restore worked properly, we should never return here
00258     while(1) { ; }
00259   }
00260 
00261   //USART bootloader software activation check
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   /* Choose if segment initialization */
00281   /* should be done or not.           */
00282   /* Return: 0 to omit seg_init       */
00283   /*         1 to run seg_init        */
00284   /*==================================*/
00285   //return 1;
00286   unsigned long *pulSrc, *pulDest;
00287 
00288   //
00289   // Copy the data segment initializers from flash to SRAM.
00290   //
00291   pulSrc = &_sidata;
00292   for(pulDest = &_sdata; pulDest < &_edata; )
00293     {
00294       *(pulDest++) = *(pulSrc++);
00295     }
00296   
00297   //
00298   // Zero fill the bss segment.
00299   //
00300   for(pulDest = &_sbss; pulDest < &_ebss; )
00301     {
00302       *(pulDest++) = 0;
00303     }
00304   
00305   //
00306   // Call the application's entry point.
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   //printf ("_sbrk (%d)\n\r", incr);
00318   if (heap == NULL) {
00319     heap = (unsigned char *)__HEAP_START;
00320   }
00321   prev_heap = heap;
00322   /* check removed to show basic approach */
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   /* FIXME: return code is thrown away.  */
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 /********************* (C) COPYRIGHT 2007 STMicroelectronics  *****END OF FILE****/
00388 
00389 

Generated on Mon Apr 11 14:23:40 2011 for Contiki 2.5 by  doxygen 1.6.1