Contiki 2.5
crt_stm32w108.c
1 /******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
2 * File Name : stm32f10x_vector.c
3 * Author : MCD Tools Team
4 * Date First Issued : 05/14/2007
5 * Description : This file contains the vector table for STM32F10x.
6 * After Reset the Cortex-M3 processor is in Thread mode,
7 * priority is Privileged, and the Stack is set to Main.
8 ********************************************************************************
9 * History:
10 * 05/14/2007: V0.2
11 *
12 ********************************************************************************
13 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
14 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
15 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
16 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
17 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
18 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
19 *******************************************************************************/
20 #include <stdio.h>
21 #include <sys/stat.h>
22 #define RESERVED 0
23 #define IAP_BOOTLOADER_APP_SWITCH_SIGNATURE 0xb001204d
24 #define IAP_BOOTLOADER_MODE_UART 0
25 /* Includes ----------------------------------------------------------------------*/
26 #include PLATFORM_HEADER
27 
28 /* Exported types --------------------------------------------------------------*/
29 /* Exported constants --------------------------------------------------------*/
30 extern unsigned long _etext;
31 extern unsigned long _sidata; /* start address for the initialization values of the .data section. defined in linker script */
32 extern unsigned long _sdata; /* start address for the .data section. defined in linker script */
33 extern unsigned long _edata; /* end address for the .data section. defined in linker script */
34 
35 extern unsigned long _sbss; /* start address for the .bss section. defined in linker script */
36 extern unsigned long _ebss; /* end address for the .bss section. defined in linker script */
37 
38 extern unsigned long _estack; /* init value for the stack pointer. defined in linker script */
39 
41 VAR_AT_SEGMENT(const HalFixedAddressTableType halFixedAddressTable, __FAT__);
42 
43 
44 /* Private typedef -----------------------------------------------------------*/
45 
46 /* function prototypes ------------------------------------------------------*/
47 void Reset_Handler(void) __attribute__((__interrupt__));
48 extern int main(void);
49 extern void halInternalSwitchToXtal(void);
50 /*******************************************************************************
51 *
52 * Provide weak aliases for each Exception handler to the Default_Handler.
53 * As they are weak aliases, any function with the same name will override
54 * this definition.
55 *
56 *******************************************************************************/
57 
58 /* Weak definitions of handlers point to Default_Handler if not implemented */
59 void NMI_Handler() __attribute__ ((weak, alias("Default_Handler")));
60 void HardFault_Handler() __attribute__ ((weak, alias("Default_Handler")));
61 void MemManage_Handler() __attribute__ ((weak, alias("Default_Handler")));
62 void BusFault_Handler() __attribute__ ((weak, alias("Default_Handler")));
63 void UsageFault_Handler() __attribute__ ((weak, alias("Default_Handler")));
64 void SVC_Handler() __attribute__ ((weak, alias("Default_Handler")));
65 void DebugMonitor_Handler() __attribute__ ((weak, alias("Default_Handler")));
66 void PendSV_Handler() __attribute__ ((weak, alias("Default_Handler")));
67 void SysTick_Handler() __attribute__ ((weak, alias("Default_Handler")));
68 void halTimer1Isr() __attribute__ ((weak, alias("Default_Handler")));
69 void halTimer2Isr() __attribute__ ((weak, alias("Default_Handler")));
70 void halManagementIsr() __attribute__ ((weak, alias("Default_Handler")));
71 void halBaseBandIsr() __attribute__ ((weak, alias("Default_Handler")));
72 void halSleepTimerIsr() __attribute__ ((weak, alias("Default_Handler")));
73 void halSc1Isr() __attribute__ ((weak, alias("Default_Handler")));
74 void halSc2Isr() __attribute__ ((weak, alias("Default_Handler")));
75 void halSecurityIsr() __attribute__ ((weak, alias("Default_Handler")));
76 void halStackMacTimerIsr() __attribute__ ((weak, alias("Default_Handler")));
77 void stmRadioTransmitIsr() __attribute__ ((weak, alias("Default_Handler")));
78 void stmRadioReceiveIsr() __attribute__ ((weak, alias("Default_Handler")));
79 void halAdcIsr() __attribute__ ((weak, alias("Default_Handler")));
80 void halIrqAIsr() __attribute__ ((weak, alias("Default_Handler")));
81 void halIrqBIsr() __attribute__ ((weak, alias("Default_Handler")));
82 void halIrqCIsr() __attribute__ ((weak, alias("Default_Handler")));
83 void halIrqDIsr() __attribute__ ((weak, alias("Default_Handler")));
84 void halDebugIsr() __attribute__ ((weak, alias("Default_Handler")));
85 
86 void __attribute__ ((weak)) Default_Handler()
87 {
88  /* Hang here */
89  while(1)
90  {
91  }
92 }
93 
94 
95 /******************************************************************************
96 *
97 * The minimal vector table for a Cortex M3. Note that the proper constructs
98 * must be placed on this to ensure that it ends up at physical address
99 * 0x0000.0000.
100 *
101 ******************************************************************************/
102 
103 
104 __attribute__ ((section(".isr_vector")))
105 void (* const g_pfnVectors[])(void) =
106 {
107  (void (*)(void))&_estack, // The initial stack pointer
108  Reset_Handler, // 1 The reset handler
109  NMI_Handler, // 2
110  HardFault_Handler, // 3
111  MemManage_Handler, // 4
112  BusFault_Handler, // 5
113  UsageFault_Handler, // 6
114  RESERVED, // 7
115  RESERVED, // 8
116  RESERVED, // 9
117  RESERVED, // 10
118  SVC_Handler, // 11
119  DebugMonitor_Handler, // 12
120  RESERVED, // 13
121  PendSV_Handler, // 14
122  SysTick_Handler, // 15
123  halTimer1Isr, // 16
124  halTimer2Isr, // 17
125  halManagementIsr, // 18
126  halBaseBandIsr, // 19
127  halSleepTimerIsr, // 20
128  halSc1Isr, // 21
129  halSc2Isr, // 22
130  halSecurityIsr, // 23
131  halStackMacTimerIsr, // 24
132  stmRadioTransmitIsr, // 25
133  stmRadioReceiveIsr, // 26
134  halAdcIsr, // 27
135  halIrqAIsr, // 28
136  halIrqBIsr, // 29
137  halIrqCIsr, // 30
138  halIrqDIsr, // 31
139  halDebugIsr, // 32
140 };
141 
142 static void setStackPointer(int32u address) __attribute__((noinline));
143 static void setStackPointer(int32u address)
144 {
145  // This code is needed to generate the instruction below
146  // that GNU ASM is refusing to add
147  // asm("MOVS SP, r0");
148  asm(".short 0x4685");
149 }
150 
151 static const int16u blOffset[] = {
152  0x0715 - 0x03ad - 0x68,
153  0x0719 - 0x03ad - 0x6C
154 };
155 
156 /*******************************************************************************
157 * Function Name : Reset_Handler
158 * Description : This is the code that gets called when the processor first starts execution
159 * following a reset event. Only the absolutely necessary set is performed,
160 * after which the application supplied main() routine is called.
161 * Input :
162 * Output :
163 * Return :
164 *******************************************************************************/
165 void Reset_Handler(void)
166 {
167  //Ensure there is enough margin on VREG_1V8 for stable RAM reads by
168  //setting it to a code of 6. VREG_1V2 can be left at its reset value.
169  VREG = 0x00000307;
170 
171  // This code should be careful about the use of local variables in case the
172  // reset type happens to be a deep sleep reset. If the reset is not from
173  // deep sleep, then locals can be freely used
174 
175  //When the Cortex-M3 exits reset, interrupts are enable. Explicitely
176  //disable them immediately using the standard set PRIMASK instruction.
177  //Injecting an assembly instruction this early does not effect optimization.
178  asm("CPSID i");
179 
180  //It is quite possible that when the Cortex-M3 begins executing code the
181  //Core Reset Vector Catch is still left enabled. Because this VC would
182  //cause us to halt at reset if another reset event tripped, we should
183  //clear it as soon as possible. If a debugger wants to halt at reset,
184  //it will set this bit again.
185  DEBUG_EMCR &= ~DEBUG_EMCR_VC_CORERESET;
186 
187  //Configure flash access for optimal current consumption early
188  //during boot to save as much current as we can.
189  FLASH_ACCESS = (FLASH_ACCESS_PREFETCH_EN |
190  (1<<FLASH_ACCESS_CODE_LATENCY_BIT));
191 
192  ////---- Always Configure Interrupt Priorities ----////
193  //The STM32W support 5 bits of priority configuration.
194  // The cortex allows this to be further divided into preemption and a
195  // "tie-breaker" sub-priority.
196  //We configure a scheme that allows for 3 bits (8 values) of preemption and
197  // 2 bits (4 values) of tie-breaker by using the value 4 in PRIGROUP.
198  //The value 0x05FA0000 is a special key required to write to this register.
199  SCS_AIRCR = (0x05FA0000 | (4 <<SCS_AIRCR_PRIGROUP_BIT));
200 
201  //A few macros to help with interrupt priority configuration. Really only
202  // uses 6 of the possible levels, and ignores the tie-breaker sub-priority
203  // for now.
204  //Don't forget that the priority level values need to be shifted into the
205  // top 5 bits of the 8 bit priority fields. (hence the <<3)
206  //
207  // NOTE: The ATOMIC and DISABLE_INTERRUPTS macros work by setting the
208  // current priority to a value of 12, which still allows CRITICAL and
209  // HIGH priority interrupts to fire, while blocking MED and LOW.
210  // If a different value is desired, spmr.s79 will need to be edited.
211  #define CRITICAL (0 <<3)
212  #define HIGH (8 <<3)
213  #define MED (16 <<3)
214  #define LOW (28 <<3)
215  #define NONE (31 <<3)
216 
217  //With optimization turned on, the compiler will indentify all the values
218  //and variables used here as constants at compile time and will truncate
219  //this entire block of code to 98 bytes, comprised of 7 load-load-store
220  //operations.
221  //vect00 is fixed //Stack pointer
222  //vect01 is fixed //Reset Vector
223  //vect02 is fixed //NMI Handler
224  //vect03 is fixed //Hard Fault Handler
225  SCS_SHPR_7to4 = ((CRITICAL <<SCS_SHPR_7to4_PRI_4_BIT) | //Memory Fault Handler
226  (CRITICAL <<SCS_SHPR_7to4_PRI_5_BIT) | //Bus Fault Handler
227  (CRITICAL <<SCS_SHPR_7to4_PRI_6_BIT) | //Usage Fault Handler
228  (NONE <<SCS_SHPR_7to4_PRI_7_BIT)); //Reserved
229  SCS_SHPR_11to8 = ((NONE <<SCS_SHPR_11to8_PRI_8_BIT) | //Reserved
230  (NONE <<SCS_SHPR_11to8_PRI_9_BIT) | //Reserved
231  (NONE <<SCS_SHPR_11to8_PRI_10_BIT) | //Reserved
232  (HIGH <<SCS_SHPR_11to8_PRI_11_BIT)); //SVCall Handler
233  SCS_SHPR_15to12 = ((MED <<SCS_SHPR_15to12_PRI_12_BIT) | //Debug Monitor Handler
234  (NONE <<SCS_SHPR_15to12_PRI_13_BIT) | //Reserved
235  (HIGH <<SCS_SHPR_15to12_PRI_14_BIT) | //PendSV Handler
236  (MED <<SCS_SHPR_15to12_PRI_15_BIT)); //SysTick Handler
237  NVIC_IPR_3to0 = ((MED <<NVIC_IPR_3to0_PRI_0_BIT) | //Timer 1 Handler
238  (MED <<NVIC_IPR_3to0_PRI_1_BIT) | //Timer 2 Handler
239  (HIGH <<NVIC_IPR_3to0_PRI_2_BIT) | //Management Handler
240  (MED <<NVIC_IPR_3to0_PRI_3_BIT)); //BaseBand Handler
241  NVIC_IPR_7to4 = ((MED <<NVIC_IPR_7to4_PRI_4_BIT) | //Sleep Timer Handler
242  (MED <<NVIC_IPR_7to4_PRI_5_BIT) | //SC1 Handler
243  (MED <<NVIC_IPR_7to4_PRI_6_BIT) | //SC2 Handler
244  (MED <<NVIC_IPR_7to4_PRI_7_BIT)); //Security Handler
245  NVIC_IPR_11to8 = ((MED <<NVIC_IPR_11to8_PRI_8_BIT) | //MAC Timer Handler
246  (MED <<NVIC_IPR_11to8_PRI_9_BIT) | //MAC TX Handler
247  (MED <<NVIC_IPR_11to8_PRI_10_BIT) | //MAC RX Handler
248  (MED <<NVIC_IPR_11to8_PRI_11_BIT)); //ADC Handler
249  NVIC_IPR_15to12 = ((MED <<NVIC_IPR_15to12_PRI_12_BIT) | //GPIO IRQA Handler
250  (MED <<NVIC_IPR_15to12_PRI_13_BIT) | //GPIO IRQB Handler
251  (MED <<NVIC_IPR_15to12_PRI_14_BIT) | //GPIO IRQC Handler
252  (MED <<NVIC_IPR_15to12_PRI_15_BIT)); //GPIO IRQD Handler
253  NVIC_IPR_19to16 = ((LOW <<NVIC_IPR_19to16_PRI_16_BIT)); //Debug Handler
254  //vect33 not implemented
255  //vect34 not implemented
256  //vect35 not implemented
257 
258  ////---- Always Configure System Handlers Control and Configuration ----////
259  SCS_CCR = SCS_CCR_DIV_0_TRP_MASK;
260  SCS_SHCSR = ( SCS_SHCSR_USGFAULTENA_MASK
261  | SCS_SHCSR_BUSFAULTENA_MASK
262  | SCS_SHCSR_MEMFAULTENA_MASK );
263 
264 
265  if((RESET_EVENT&RESET_DSLEEP) == RESET_DSLEEP) {
266  //Since the 13 NVIC registers above are fixed values, they are restored
267  //above (where they get set anyways during normal boot sequences) instead
268  //of inside of the halInternalSleep code:
269 
270  void halTriggerContextRestore(void);
271  extern volatile boolean halPendSvSaveContext;
272  halPendSvSaveContext = 0; //0 means restore context
273  SCS_ICSR |= SCS_ICSR_PENDSVSET; //pend halPendSvIsr to enable later
274  halTriggerContextRestore(); //sets MSP, enables interrupts
275  //if the context restore worked properly, we should never return here
276  while(1) { ; }
277  }
278 
279  //USART bootloader software activation check
280  if ((*((int32u *)RAM_BOTTOM) == IAP_BOOTLOADER_APP_SWITCH_SIGNATURE) && (*((int8u *)(RAM_BOTTOM+4)) == IAP_BOOTLOADER_MODE_UART)){
281  int8u cut = *(volatile int8u *) 0x08040798;
282  int16u offset = 0;
283  typedef void (*EntryPoint)(void);
284  offset = (halFixedAddressTable.baseTable.version == 3) ? blOffset[cut - 2] : 0;
285  *((int32u *)RAM_BOTTOM) = 0;
286  if (offset) {
288  }
289  EntryPoint entryPoint = (EntryPoint)(*(int32u *)(FIB_BOTTOM+4) - offset);
290  setStackPointer(*(int32u *)FIB_BOTTOM);
291  entryPoint();
292  }
293 
294  INTERRUPTS_OFF();
295  asm("CPSIE i");
296 
297  /*==================================*/
298  /* Choose if segment initialization */
299  /* should be done or not. */
300  /* Return: 0 to omit seg_init */
301  /* 1 to run seg_init */
302  /*==================================*/
303  //return 1;
304  unsigned long *pulSrc, *pulDest;
305 
306  //
307  // Copy the data segment initializers from flash to SRAM.
308  //
309  pulSrc = &_sidata;
310  for(pulDest = &_sdata; pulDest < &_edata; )
311  {
312  *(pulDest++) = *(pulSrc++);
313  }
314 
315  //
316  // Zero fill the bss segment.
317  //
318  for(pulDest = &_sbss; pulDest < &_ebss; )
319  {
320  *(pulDest++) = 0;
321  }
322 
323  //
324  // Call the application's entry point.
325  //
326  main();
327 }
328 #ifdef USE_HEAP
329 static unsigned char __HEAP_START[1024*3-560+0x200];
330 caddr_t _sbrk ( int incr )
331 {
332  static unsigned char *heap = NULL;
333  unsigned char *prev_heap;
334 
335  //printf ("_sbrk (%d)\n\r", incr);
336  if (heap == NULL) {
337  heap = (unsigned char *)__HEAP_START;
338  }
339  prev_heap = heap;
340  /* check removed to show basic approach */
341  if ((heap + incr) > (__HEAP_START + sizeof(__HEAP_START))) {
342  prev_heap = NULL;
343  } else {
344  heap += incr;
345  }
346  if (prev_heap == NULL) {
347  printf ("_sbrk %d return %p\n\r", incr, prev_heap);
348  }
349  return (caddr_t) prev_heap;
350 }
351 #else
352 caddr_t _sbrk ( int incr )
353 {
354  return NULL;
355 }
356 #endif
357 int _lseek (int file,
358  int ptr,
359  int dir)
360 {
361  return 0;
362 }
363 int _close (int file)
364 {
365  return -1;
366 }
367 
368 void _exit (int n)
369 {
370  /* FIXME: return code is thrown away. */
371  while(1);
372 }
373 
374 
375 
376 int _kill (int n, int m)
377 {
378  return -1;
379 }
380 int _fstat(int file, struct stat *st)
381 {
382  st->st_mode = S_IFCHR;
383  return 0;
384 }
385 int _isatty (int fd)
386 {
387  return 1;
388  fd = fd;
389 }
390 int _getpid (int n)
391 {
392  return -1;
393 }
394 int _open (const char * path,
395  int flags,
396  ...)
397 {
398  return -1;
399 }
400 int _fflush_r(struct _reent *r, FILE *f)
401 {
402  return 0;
403 }
404 
405 /********************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/
406 
407