Contiki 2.6

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 
00028 /* Exported types --------------------------------------------------------------*/
00029 /* Exported constants --------------------------------------------------------*/
00030 extern unsigned long _etext;
00031 extern unsigned long _sidata;           /* start address for the initialization values of the .data section. defined in linker script */
00032 extern unsigned long _sdata;            /* start address for the .data section. defined in linker script */
00033 extern unsigned long _edata;            /* end address for the .data section. defined in linker script */
00034 
00035 extern unsigned long _sbss;                     /* start address for the .bss section. defined in linker script */
00036 extern unsigned long _ebss;                     /* end address for the .bss section. defined in linker script */
00037 
00038 extern unsigned long _estack;           /* init value for the stack pointer. defined in linker script */
00039 
00040 #include "hal/micro/cortexm3/memmap.h"
00041 VAR_AT_SEGMENT(const HalFixedAddressTableType halFixedAddressTable, __FAT__);
00042 
00043 
00044 /* Private typedef -----------------------------------------------------------*/
00045 
00046 /* function prototypes ------------------------------------------------------*/
00047 void Reset_Handler(void) __attribute__((__interrupt__));
00048 extern int main(void);
00049 extern void  halInternalSwitchToXtal(void);
00050 /*******************************************************************************
00051 *
00052 * Provide weak aliases for each Exception handler to the Default_Handler.
00053 * As they are weak aliases, any function with the same name will override
00054 * this definition.
00055 *
00056 *******************************************************************************/
00057 
00058 /* Weak definitions of handlers point to Default_Handler if not implemented */
00059 void NMI_Handler() __attribute__ ((weak, alias("Default_Handler")));
00060 void HardFault_Handler() __attribute__ ((weak, alias("Default_Handler")));
00061 void MemManage_Handler() __attribute__ ((weak, alias("Default_Handler")));
00062 void BusFault_Handler() __attribute__ ((weak, alias("Default_Handler")));
00063 void UsageFault_Handler() __attribute__ ((weak, alias("Default_Handler")));
00064 void SVC_Handler() __attribute__ ((weak, alias("Default_Handler")));
00065 void DebugMonitor_Handler() __attribute__ ((weak, alias("Default_Handler")));
00066 void PendSV_Handler() __attribute__ ((weak, alias("Default_Handler")));
00067 void SysTick_Handler() __attribute__ ((weak, alias("Default_Handler")));
00068 void halTimer1Isr() __attribute__ ((weak, alias("Default_Handler")));
00069 void halTimer2Isr() __attribute__ ((weak, alias("Default_Handler")));
00070 void halManagementIsr() __attribute__ ((weak, alias("Default_Handler")));
00071 void halBaseBandIsr() __attribute__ ((weak, alias("Default_Handler")));
00072 void halSleepTimerIsr() __attribute__ ((weak, alias("Default_Handler")));
00073 void halSc1Isr() __attribute__ ((weak, alias("Default_Handler")));
00074 void halSc2Isr() __attribute__ ((weak, alias("Default_Handler")));
00075 void halSecurityIsr() __attribute__ ((weak, alias("Default_Handler")));
00076 void halStackMacTimerIsr() __attribute__ ((weak, alias("Default_Handler")));
00077 void stmRadioTransmitIsr() __attribute__ ((weak, alias("Default_Handler")));
00078 void stmRadioReceiveIsr() __attribute__ ((weak, alias("Default_Handler")));
00079 void halAdcIsr() __attribute__ ((weak, alias("Default_Handler")));
00080 void halIrqAIsr() __attribute__ ((weak, alias("Default_Handler")));
00081 void halIrqBIsr() __attribute__ ((weak, alias("Default_Handler")));
00082 void halIrqCIsr() __attribute__ ((weak, alias("Default_Handler")));
00083 void halIrqDIsr() __attribute__ ((weak, alias("Default_Handler")));
00084 void halDebugIsr() __attribute__ ((weak, alias("Default_Handler")));
00085 
00086 void __attribute__ ((weak)) Default_Handler()
00087 {
00088         /* Hang here */
00089         while(1)
00090         {
00091         }
00092 }
00093 
00094 
00095 /******************************************************************************
00096 *
00097 * The minimal vector table for a Cortex M3.  Note that the proper constructs
00098 * must be placed on this to ensure that it ends up at physical address
00099 * 0x0000.0000.
00100 *
00101 ******************************************************************************/
00102 
00103 
00104 __attribute__ ((section(".isr_vector")))
00105 void (* const g_pfnVectors[])(void) =
00106 {
00107   (void (*)(void))&_estack,            // The initial stack pointer
00108   Reset_Handler,             // 1 The reset handler
00109   NMI_Handler,              // 2
00110   HardFault_Handler,         // 3
00111   MemManage_Handler,         // 4
00112   BusFault_Handler,          // 5
00113   UsageFault_Handler,        // 6
00114   RESERVED,                  // 7
00115   RESERVED,                  // 8
00116   RESERVED,                  // 9
00117   RESERVED,                  // 10
00118   SVC_Handler,               // 11
00119   DebugMonitor_Handler,      // 12
00120   RESERVED,                  // 13
00121   PendSV_Handler,            // 14
00122   SysTick_Handler,           // 15
00123   halTimer1Isr,              // 16
00124   halTimer2Isr,              // 17
00125   halManagementIsr,          // 18
00126   halBaseBandIsr,            // 19
00127   halSleepTimerIsr,          // 20
00128   halSc1Isr,                 // 21
00129   halSc2Isr,                 // 22
00130   halSecurityIsr,            // 23
00131   halStackMacTimerIsr,       // 24
00132   stmRadioTransmitIsr,       // 25
00133   stmRadioReceiveIsr,        // 26
00134   halAdcIsr,                 // 27
00135   halIrqAIsr,                // 28
00136   halIrqBIsr,                // 29
00137   halIrqCIsr,                // 30
00138   halIrqDIsr,                // 31
00139   halDebugIsr,               // 32
00140 };
00141 
00142 static  void setStackPointer(int32u address) __attribute__((noinline));
00143 static void setStackPointer(int32u address)
00144 {
00145   // This code is needed to generate the instruction below
00146   // that GNU ASM is refusing to add
00147   // asm("MOVS SP, r0");
00148   asm(".short 0x4685");
00149 }
00150 
00151 static const int16u blOffset[] = {
00152   0x0715 - 0x03ad - 0x68,
00153   0x0719 - 0x03ad - 0x6C
00154 };
00155 
00156 /*******************************************************************************
00157 * Function Name  : Reset_Handler
00158 * Description    : This is the code that gets called when the processor first starts execution
00159 *                      following a reset event.  Only the absolutely necessary set is performed,
00160 *                      after which the application supplied main() routine is called. 
00161 * Input          :
00162 * Output         :
00163 * Return         :
00164 *******************************************************************************/
00165 void Reset_Handler(void)
00166 {
00167   //Ensure there is enough margin on VREG_1V8 for stable RAM reads by
00168   //setting it to a code of 6.  VREG_1V2 can be left at its reset value.
00169   VREG = 0x00000307;
00170   
00171   // This code should be careful about the use of local variables in case the
00172   // reset type happens to be a deep sleep reset.  If the reset is not from
00173   // deep sleep, then locals can be freely used
00174 
00175   //When the Cortex-M3 exits reset, interrupts are enable.  Explicitely
00176   //disable them immediately using the standard set PRIMASK instruction.
00177   //Injecting an assembly instruction this early does not effect optimization.
00178   asm("CPSID i");
00179 
00180   //It is quite possible that when the Cortex-M3 begins executing code the
00181   //Core Reset Vector Catch is still left enabled.  Because this VC would
00182   //cause us to halt at reset if another reset event tripped, we should
00183   //clear it as soon as possible.  If a debugger wants to halt at reset,
00184   //it will set this bit again.
00185   DEBUG_EMCR &= ~DEBUG_EMCR_VC_CORERESET;
00186 
00187   //Configure flash access for optimal current consumption early
00188   //during boot to save as much current as we can.
00189   FLASH_ACCESS = (FLASH_ACCESS_PREFETCH_EN          |
00190                   (1<<FLASH_ACCESS_CODE_LATENCY_BIT));
00191 
00192   ////---- Always Configure Interrupt Priorities ----////
00193   //The STM32W support 5 bits of priority configuration.
00194   //  The cortex allows this to be further divided into preemption and a
00195   //  "tie-breaker" sub-priority.
00196   //We configure a scheme that allows for 3 bits (8 values) of preemption and
00197   //   2 bits (4 values) of tie-breaker by using the value 4 in PRIGROUP.
00198   //The value 0x05FA0000 is a special key required to write to this register.
00199   SCS_AIRCR = (0x05FA0000 | (4 <<SCS_AIRCR_PRIGROUP_BIT));
00200     
00201   //A few macros to help with interrupt priority configuration.  Really only 
00202   //  uses 6 of the possible levels, and ignores the tie-breaker sub-priority
00203   //  for now.
00204   //Don't forget that the priority level values need to be shifted into the
00205   //  top 5 bits of the 8 bit priority fields. (hence the <<3)
00206   //
00207   // NOTE: The ATOMIC and DISABLE_INTERRUPTS macros work by setting the 
00208   //       current priority to a value of 12, which still allows CRITICAL and 
00209   //       HIGH priority interrupts to fire, while blocking MED and LOW.
00210   //       If a different value is desired, spmr.s79 will need to be edited.
00211   #define CRITICAL (0  <<3)
00212   #define HIGH     (8  <<3)
00213   #define MED      (16 <<3)
00214   #define LOW      (28 <<3)
00215   #define NONE     (31 <<3)
00216 
00217   //With optimization turned on, the compiler will indentify all the values
00218   //and variables used here as constants at compile time and will truncate
00219   //this entire block of code to 98 bytes, comprised of 7 load-load-store
00220   //operations.
00221                     //vect00 is fixed                        //Stack pointer
00222                     //vect01 is fixed                        //Reset Vector
00223                     //vect02 is fixed                        //NMI Handler
00224                     //vect03 is fixed                        //Hard Fault Handler
00225   SCS_SHPR_7to4   = ((CRITICAL <<SCS_SHPR_7to4_PRI_4_BIT) |  //Memory Fault Handler 
00226                      (CRITICAL <<SCS_SHPR_7to4_PRI_5_BIT) |  //Bus Fault Handler
00227                      (CRITICAL <<SCS_SHPR_7to4_PRI_6_BIT) |  //Usage Fault Handler
00228                      (NONE <<SCS_SHPR_7to4_PRI_7_BIT));      //Reserved
00229   SCS_SHPR_11to8  = ((NONE <<SCS_SHPR_11to8_PRI_8_BIT)  |    //Reserved
00230                      (NONE <<SCS_SHPR_11to8_PRI_9_BIT)  |    //Reserved
00231                      (NONE <<SCS_SHPR_11to8_PRI_10_BIT) |    //Reserved
00232                      (HIGH <<SCS_SHPR_11to8_PRI_11_BIT));    //SVCall Handler
00233   SCS_SHPR_15to12 = ((MED  <<SCS_SHPR_15to12_PRI_12_BIT) |   //Debug Monitor Handler
00234                      (NONE <<SCS_SHPR_15to12_PRI_13_BIT) |   //Reserved
00235                      (HIGH <<SCS_SHPR_15to12_PRI_14_BIT) |   //PendSV Handler
00236                      (MED  <<SCS_SHPR_15to12_PRI_15_BIT));   //SysTick Handler
00237   NVIC_IPR_3to0   = ((MED  <<NVIC_IPR_3to0_PRI_0_BIT) |      //Timer 1 Handler
00238                      (MED  <<NVIC_IPR_3to0_PRI_1_BIT) |      //Timer 2 Handler
00239                      (HIGH <<NVIC_IPR_3to0_PRI_2_BIT) |      //Management Handler
00240                      (MED  <<NVIC_IPR_3to0_PRI_3_BIT));      //BaseBand Handler
00241   NVIC_IPR_7to4   = ((MED  <<NVIC_IPR_7to4_PRI_4_BIT) |      //Sleep Timer Handler
00242                      (MED  <<NVIC_IPR_7to4_PRI_5_BIT) |      //SC1 Handler
00243                      (MED  <<NVIC_IPR_7to4_PRI_6_BIT) |      //SC2 Handler
00244                      (MED  <<NVIC_IPR_7to4_PRI_7_BIT));      //Security Handler
00245   NVIC_IPR_11to8  = ((MED  <<NVIC_IPR_11to8_PRI_8_BIT)  |    //MAC Timer Handler
00246                      (MED  <<NVIC_IPR_11to8_PRI_9_BIT)  |    //MAC TX Handler
00247                      (MED  <<NVIC_IPR_11to8_PRI_10_BIT) |    //MAC RX Handler
00248                      (MED  <<NVIC_IPR_11to8_PRI_11_BIT));    //ADC Handler
00249   NVIC_IPR_15to12 = ((MED  <<NVIC_IPR_15to12_PRI_12_BIT) |   //GPIO IRQA Handler
00250                      (MED  <<NVIC_IPR_15to12_PRI_13_BIT) |   //GPIO IRQB Handler
00251                      (MED  <<NVIC_IPR_15to12_PRI_14_BIT) |   //GPIO IRQC Handler
00252                      (MED  <<NVIC_IPR_15to12_PRI_15_BIT));   //GPIO IRQD Handler
00253   NVIC_IPR_19to16 = ((LOW  <<NVIC_IPR_19to16_PRI_16_BIT));   //Debug Handler
00254                     //vect33 not implemented
00255                     //vect34 not implemented
00256                     //vect35 not implemented
00257 
00258   ////---- Always Configure System Handlers Control and Configuration ----////
00259   SCS_CCR = SCS_CCR_DIV_0_TRP_MASK;
00260   SCS_SHCSR = ( SCS_SHCSR_USGFAULTENA_MASK
00261                 | SCS_SHCSR_BUSFAULTENA_MASK
00262                 | SCS_SHCSR_MEMFAULTENA_MASK );
00263 
00264 
00265   if((RESET_EVENT&RESET_DSLEEP) == RESET_DSLEEP) {
00266     //Since the 13 NVIC registers above are fixed values, they are restored
00267     //above (where they get set anyways during normal boot sequences) instead
00268     //of inside of the halInternalSleep code:
00269 
00270     void halTriggerContextRestore(void);
00271     extern volatile boolean halPendSvSaveContext;
00272     halPendSvSaveContext = 0;       //0 means restore context
00273     SCS_ICSR |= SCS_ICSR_PENDSVSET; //pend halPendSvIsr to enable later
00274     halTriggerContextRestore();     //sets MSP, enables interrupts
00275     //if the context restore worked properly, we should never return here
00276     while(1) { ; }
00277   }
00278 
00279   //USART bootloader software activation check
00280   if ((*((int32u *)RAM_BOTTOM) == IAP_BOOTLOADER_APP_SWITCH_SIGNATURE) && (*((int8u *)(RAM_BOTTOM+4)) == IAP_BOOTLOADER_MODE_UART)){
00281     int8u cut = *(volatile int8u *) 0x08040798;
00282     int16u offset = 0;
00283     typedef void (*EntryPoint)(void);     
00284     offset = (halFixedAddressTable.baseTable.version == 3) ? blOffset[cut - 2] : 0;
00285     *((int32u *)RAM_BOTTOM) = 0;
00286     if (offset) {
00287       halInternalSwitchToXtal();
00288     }
00289     EntryPoint entryPoint = (EntryPoint)(*(int32u *)(FIB_BOTTOM+4) - offset);
00290     setStackPointer(*(int32u *)FIB_BOTTOM);
00291     entryPoint();
00292   }
00293 
00294   INTERRUPTS_OFF();
00295   asm("CPSIE i");
00296 
00297   /*==================================*/
00298   /* Choose if segment initialization */
00299   /* should be done or not.           */
00300   /* Return: 0 to omit seg_init       */
00301   /*         1 to run seg_init        */
00302   /*==================================*/
00303   //return 1;
00304   unsigned long *pulSrc, *pulDest;
00305 
00306   //
00307   // Copy the data segment initializers from flash to SRAM.
00308   //
00309   pulSrc = &_sidata;
00310   for(pulDest = &_sdata; pulDest < &_edata; )
00311     {
00312       *(pulDest++) = *(pulSrc++);
00313     }
00314   
00315   //
00316   // Zero fill the bss segment.
00317   //
00318   for(pulDest = &_sbss; pulDest < &_ebss; )
00319     {
00320       *(pulDest++) = 0;
00321     }
00322   
00323   //
00324   // Call the application's entry point.
00325   //
00326   main();
00327 }
00328 #ifdef USE_HEAP
00329 static unsigned char __HEAP_START[1024*3-560+0x200];
00330 caddr_t _sbrk ( int incr )
00331 {
00332   static unsigned char *heap = NULL;
00333   unsigned char *prev_heap;
00334 
00335   //printf ("_sbrk (%d)\n\r", incr);
00336   if (heap == NULL) {
00337     heap = (unsigned char *)__HEAP_START;
00338   }
00339   prev_heap = heap;
00340   /* check removed to show basic approach */
00341   if ((heap + incr) > (__HEAP_START + sizeof(__HEAP_START))) {
00342     prev_heap = NULL;
00343   } else {
00344     heap += incr;
00345   }
00346   if (prev_heap == NULL) {
00347     printf ("_sbrk %d return %p\n\r", incr, prev_heap);
00348   }
00349   return (caddr_t) prev_heap;
00350 }
00351 #else
00352 caddr_t _sbrk ( int incr )
00353 {
00354     return NULL;
00355 }
00356 #endif
00357 int _lseek (int file,
00358         int ptr,
00359         int dir)
00360 {
00361   return 0;
00362 }
00363 int _close (int file)
00364 {
00365   return -1;
00366 }
00367 
00368 void _exit (int n)
00369 {
00370   /* FIXME: return code is thrown away.  */
00371   while(1);
00372 }
00373 
00374 
00375 
00376 int _kill (int n, int m)
00377 {
00378    return -1;
00379 }
00380 int _fstat(int file, struct stat *st)
00381 {
00382   st->st_mode = S_IFCHR;
00383   return 0;
00384 }
00385 int _isatty (int fd)
00386 {
00387   return 1;
00388   fd = fd;
00389 }
00390 int _getpid     (int n)
00391 {
00392    return -1;
00393 }
00394 int _open (const char * path,
00395        int          flags,
00396        ...)
00397 {
00398   return -1;
00399 }
00400 int _fflush_r(struct _reent *r, FILE *f)
00401 {
00402   return 0;
00403 }
00404 
00405 /********************* (C) COPYRIGHT 2007 STMicroelectronics  *****END OF FILE****/
00406 
00407