Contiki 2.5
micro-common.c
Go to the documentation of this file.
1 /** @file micro-common.c
2  * @brief STM32W108 micro specific HAL functions common to
3  * full and minimal hal
4  *
5  *
6  * <!--(C) COPYRIGHT 2010 STMicroelectronics. All rights reserved. -->
7  */
8 
9 
10 
11 #include PLATFORM_HEADER
12 #include BOARD_HEADER
13 #include "error.h"
14 #include "hal/micro/micro-common.h"
16 
18 {
19  //Just to be on the safe side, restart the watchdog before enabling it
20  WDOG_RESET = 1;
21  WDOG_KEY = 0xEABE;
22  WDOG_CFG = WDOG_ENABLE;
23 }
24 
26 {
27  //Writing any value will restart the watchdog
28  WDOG_RESET = 1;
29 }
30 
31 void halInternalDisableWatchDog(int8u magicKey)
32 {
33  if (magicKey == MICRO_DISABLE_WATCH_DOG_KEY) {
34  WDOG_KEY = 0xDEAD;
35  WDOG_CFG = WDOG_DISABLE;
36  }
37 }
38 
40 {
41  if(WDOG_CFG&WDOG_ENABLE) {
42  return TRUE;
43  } else {
44  return FALSE;
45  }
46 }
47 
48 void halGpioConfig(int32u io, int32u config)
49 {
50  static volatile int32u *const configRegs[] =
51  { (volatile int32u *)GPIO_PACFGL_ADDR,
52  (volatile int32u *)GPIO_PACFGH_ADDR,
53  (volatile int32u *)GPIO_PBCFGL_ADDR,
54  (volatile int32u *)GPIO_PBCFGH_ADDR,
55  (volatile int32u *)GPIO_PCCFGL_ADDR,
56  (volatile int32u *)GPIO_PCCFGH_ADDR };
57  int32u portcfg;
58  portcfg = *configRegs[io/4]; // get current config
59  portcfg = portcfg & ~((0xF)<<((io&3)*4)); // mask out config of this pin
60  *configRegs[io/4] = portcfg | (config <<((io&3)*4));
61 }
62 
63 void halGpioSet(int32u gpio, boolean value)
64 {
65  if(gpio/8 < 3) {
66  if (value) {
67  *((volatile int32u *)(GPIO_PxSET_BASE+(GPIO_Px_OFFSET*(gpio/8)))) = BIT(gpio&7);
68  } else {
69  *((volatile int32u *)(GPIO_PxCLR_BASE+(GPIO_Px_OFFSET*(gpio/8)))) = BIT(gpio&7);
70  }
71  }
72 }
73 
75 {
76  //Since the SleepTMR is the only timer maintained during deep sleep, it is
77  //used as the System Timer (RTC). We maintain a 32 bit hardware timer
78  //configured for a tick value time of 1024 ticks/second (0.9765625 ms/tick)
79  //using either the 10 kHz internal SlowRC clock divided and calibrated to
80  //1024 Hz or the external 32.768 kHz crystal divided to 1024 Hz.
81  //With a tick time of ~1ms, this 32bit timer will wrap after ~48.5 days.
82 
83  //disable top-level interrupt while configuring
84  INT_CFGCLR = INT_SLEEPTMR;
85 
86  #ifdef ENABLE_OSC32K
87  #ifdef DIGITAL_OSC32_EXT
88  //Disable both OSC32K and SLOWRC if using external digital clock input
89  SLEEPTMR_CLKEN = 0;
90  #else//!DIGITAL_OSC32_EXT
91  //Enable the 32kHz XTAL (and disable SlowRC since it is not needed)
92  SLEEPTMR_CLKEN = SLEEPTMR_CLK32KEN;
93  #endif
94  //Sleep timer configuration is the same for crystal and external clock
95  SLEEPTMR_CFG = (SLEEPTMR_ENABLE | //enable TMR
96  (0 << SLEEPTMR_DBGPAUSE_BIT)| //TMR paused when halted
97  (5 << SLEEPTMR_CLKDIV_BIT) | //divide down to 1024Hz
98  (1 << SLEEPTMR_CLKSEL_BIT)) ; //select XTAL
99  #else //!ENABLE_OSC32K
100  //Enable the SlowRC (and disable 32kHz XTAL since it is not needed)
101  SLEEPTMR_CLKEN = SLEEPTMR_CLK10KEN;
102  SLEEPTMR_CFG = (SLEEPTMR_ENABLE | //enable TMR
103  (0 << SLEEPTMR_DBGPAUSE_BIT)| //TMR paused when halted
104  (0 << SLEEPTMR_CLKDIV_BIT) | //already 1024Hz
105  (0 << SLEEPTMR_CLKSEL_BIT)) ; //select SlowRC
106  #ifndef DISABLE_RC_CALIBRATION
107  halInternalCalibrateSlowRc(); //calibrate SlowRC to 1024Hz
108  #endif//DISABLE_RC_CALIBRATION
109  #endif//ENABLE_OSC32K
110 
111  //clear out any stale interrupts
112  INT_SLEEPTMRFLAG = (INT_SLEEPTMRWRAP | INT_SLEEPTMRCMPA | INT_SLEEPTMRCMPB);
113  //turn off second level interrupts. they will be enabled elsewhere as needed
114  INT_SLEEPTMRCFG = INT_SLEEPTMRCFG_RESET;
115  //enable top-level interrupt
116  INT_CFGSET = INT_SLEEPTMR;
117 
118  return 0;
119 }
120 
121