Contiki 2.5
micro.c
Go to the documentation of this file.
1 /** @file micro.c
2  * @brief STM32W108 micro specific minimal HAL functions
3  *
4  *
5  * <!--(C) COPYRIGHT 2010 STMicroelectronics. All rights reserved. -->
6  */
7 
8 #include PLATFORM_HEADER
9 #include BOARD_HEADER
10 #include "error.h"
11 #include "hal/micro/micro-common.h"
13 #include "micro/system-timer.h"
14 #include "micro/adc.h"
15 #include "micro/cortexm3/memmap.h"
17 
18 #include <stdlib.h>
19 #include <string.h>
20 
21 extern void halBoardInit(void);
22 
23 void halInit(void)
24 {
25  //Disable the REG_EN external regulator enable signal. Out of reset this
26  //signal overrides PA7. By disabling it early, PA7 is reclaimed as a GPIO.
27  //If an external regulator is required, the following line of code should
28  //be deleted.
29  GPIO_DBGCFG &= ~GPIO_EXTREGEN;
31  halBoardInit();
32  halPowerUp();
34 
35  #ifndef DISABLE_WATCHDOG
37  #endif
38 
40 }
41 
42 
43 void halReboot(void)
44 {
46 
47 
48  //FCLK must be 6MHz to allow the SYSRESETREQ signal to cleanly
49  //propagate and reset the chip. Switch SYSCLK first since we need
50  //the cycles used by switching FCLK to guarantee the SYSCLK is
51  //stable and ready for SYSRESETREQ.
52  OSC24M_CTRL = OSC24M_CTRL_RESET; //Guarantee SYSCLK is sourced from OSCHF
53  CPU_CLKSEL = CPU_CLKSEL_RESET; //Guarantee FCLK is sourced from PCLK
54 
55  SCS_AIRCR = (0x05FA0000 | SCS_AIRCR_SYSRESETREQ); // trigger the reset
56  //NOTE: SYSRESETREQ is not the same as nRESET. It will not do the debug
57  //pieces: DWT, ITM, FPB, vector catch, etc
58 }
59 
60 void halPowerDown(void)
61 {
63 }
64 
65 void halPowerUp(void)
66 {
67  halInternalInitAdc();
71 }
72 
73 static int16u seed0 = 0xbeef;
74 static int16u seed1 = 0xface;
75 
76 void halCommonSeedRandom(int32u seed)
77 {
78  seed0 = (int16u) seed;
79  if (seed0 == 0)
80  seed0 = 0xbeef;
81  seed1 = (int16u) (seed >> 16);
82  if (seed1 == 0)
83  seed1 = 0xface;
84 }
85 
86 static int16u shift(int16u *val, int16u taps)
87 {
88  int16u newVal = *val;
89 
90  if (newVal & 0x8000)
91  newVal ^= taps;
92  *val = newVal << 1;
93  return newVal;
94 }
95 
96 int16u halCommonGetRandom(void)
97 {
98  return (shift(&seed0, 0x0062)
99  ^ shift(&seed1, 0x100B));
100 }
101 
102 void halCommonMemCopy(void *dest, const void *source, int8u bytes)
103 {
104  memcpy(dest, source, bytes);
105 }
106 
107 int8s halCommonMemCompare(const void *source0, const void *source1, int8u bytes)
108 {
109  return memcmp(source0, source1, bytes);
110 }
111 
112 void halCommonMemSet(void *dest, int8u val, int16u bytes)
113 {
114  memset(dest, val, bytes);
115 }
116 
117 #pragma pack(1)
118 typedef struct appSwitchStruct {
119  int32u signature;
120  int8u mode;
121  int8u channel;
122  union {
123  int16u panID;
124  int16u offset;
125  } param;
126 } appSwitchStructType;
127 #pragma pack()
128 static appSwitchStructType *appSwitch = (appSwitchStructType *) RAM_BOTTOM;
129 
130 StStatus halBootloaderStart(int8u mode, int8u channel, int16u panID)
131 {
132  if (mode == IAP_BOOTLOADER_MODE_UART) {
133  int8u cut = *(volatile int8u *) 0x08040798;
134  if (!( (halFixedAddressTable.baseTable.type == FIXED_ADDRESS_TABLE_TYPE) &&
135  ( ( (halFixedAddressTable.baseTable.version & FAT_MAJOR_VERSION_MASK)
136  == 0x0000 ) &&
137  (halFixedAddressTable.baseTable.version == 0x0003) //checking presence of valid version
138  ) && (cut >= 2) && (cut <= 3)))
139  /* Cut not supported */
140  return ST_ERR_FATAL;
141  } else {
142  /* Check that OTA bootloader is at the base of the flash */
143  if (*((int32u *) (MFB_BOTTOM + 28)) == IAP_BOOTLOADER_APP_SWITCH_SIGNATURE) {
144  appSwitch->channel = ((channel >= 11) && (channel <= 26)) ? channel :IAP_BOOTLOADER_DEFAULT_CHANNEL;
145  appSwitch->param.panID = panID;
146  } else {
147  return ST_ERR_FATAL;
148  }
149  }
150  appSwitch->signature = IAP_BOOTLOADER_APP_SWITCH_SIGNATURE;
151  appSwitch->mode = mode;
152  halReboot();
153 
154  return (mode <= IAP_BOOTLOADER_MODE_OTA) ? ST_ERR_FATAL: ST_BAD_ARGUMENT;
155 }