Contiki 3.x
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 uint16_t seed0 = 0xbeef;
74 static uint16_t seed1 = 0xface;
75 
76 void halCommonSeedRandom(uint32_t seed)
77 {
78  seed0 = (uint16_t) seed;
79  if (seed0 == 0)
80  seed0 = 0xbeef;
81  seed1 = (uint16_t) (seed >> 16);
82  if (seed1 == 0)
83  seed1 = 0xface;
84 }
85 
86 static uint16_t shift(uint16_t *val, uint16_t taps)
87 {
88  uint16_t newVal = *val;
89 
90  if (newVal & 0x8000)
91  newVal ^= taps;
92  *val = newVal << 1;
93  return newVal;
94 }
95 
96 uint16_t halCommonGetRandom(void)
97 {
98  return (shift(&seed0, 0x0062)
99  ^ shift(&seed1, 0x100B));
100 }
101 
102 void halCommonMemCopy(void *dest, const void *source, uint8_t bytes)
103 {
104  memcpy(dest, source, bytes);
105 }
106 
107 int8_t halCommonMemCompare(const void *source0, const void *source1, uint8_t bytes)
108 {
109  return memcmp(source0, source1, bytes);
110 }
111 
112 void halCommonMemSet(void *dest, uint8_t val, uint16_t bytes)
113 {
114  memset(dest, val, bytes);
115 }
116 
117 #pragma pack(1)
118 typedef struct appSwitchStruct {
119  uint32_t signature;
120  uint8_t mode;
121  uint8_t channel;
122  union {
123  uint16_t panID;
124  uint16_t offset;
125  } param;
126 } appSwitchStructType;
127 #pragma pack()
128 static appSwitchStructType *appSwitch = (appSwitchStructType *) RAM_BOTTOM;
129 
130 StStatus halBootloaderStart(uint8_t mode, uint8_t channel, uint16_t panID)
131 {
132  if (mode == IAP_BOOTLOADER_MODE_UART) {
133  uint8_t cut = *(volatile uint8_t *) 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 (*((uint32_t *) (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 
155 }
STM32W108 series memory map definitions used by the full hal.
StStatus halBootloaderStart(uint8_t mode, uint8_t channel, uint16_t panID)
Request the appplication to enter in bootloader mode.
Definition: micro.c:130
void halInit(void)
Initializes microcontroller-specific peripherals.
Definition: micro.c:23
void halInternalCalibrateFastRc(void)
Calibrates the internal FastRC to generate a 12Mhz clock.
Definition: clocks.c:168
void halInternalEnableWatchDog(void)
Enables the watchdog timer.
Definition: micro-common.c:17
#define IAP_BOOTLOADER_MODE_OTA
IAP bootloader OTA mode.
#define ST_BAD_ARGUMENT(x02)
An invalid value was passed as an argument to a function.
Definition: error-def.h:65
#define IAP_BOOTLOADER_DEFAULT_CHANNEL
Radio default channel for OTA bootloader.
Utility and convenience functions for STM32W108 microcontroller, common to both the full and minimal ...
void halBoardPowerDown(void)
Perform board specific action to power down the system, usually before going to deep sleep...
Definition: board.c:284
void halPowerDown(void)
Powers down microcontroller peripherals and board peripherals.
Definition: micro.c:60
void halCommonCalibratePads(void)
Calibrates the GPIO pads.
void halPowerUp(void)
Powers up microcontroller peripherals and board peripherals.
Definition: micro.c:65
void halCommonSeedRandom(uint32_t seed)
Seeds the halCommonGetRandom() pseudorandom number generator.
Definition: micro.c:76
void halInternalSwitchToXtal(void)
Switches to running off of the 24MHz crystal, including changing the CPU to be 24MHz (FCLK sourced fr...
Definition: clocks.c:397
#define INTERRUPTS_OFF()
Disable global interrupts without regard to the current or previous state.
Definition: gnu.h:438
IAP bootloader specific definitions.
#define IAP_BOOTLOADER_MODE_UART
IAP bootloader uart mode.
Minimal Hal functions common across all microcontroller-specific files.
uint16_t halCommonGetRandom(void)
Runs a standard LFSR to generate pseudorandom numbers.
Definition: micro.c:96
Header for A/D converter.
void halBoardPowerUp(void)
Perform board specific action to power up the system.
Definition: board.c:350
#define FALSE
An alias for zero, used for clarity.
void halReboot(void)
Restarts the microcontroller and therefore everything else.
Definition: micro.c:43
void halBoardInit(void)
Initialize the board description data structure after autodetect of the boards based on the CIB Board...
Definition: board.c:250
void halInternalSetRegTrim(boolean boostMode)
Sets the trim values for the 1.8V and 1.2V regulators based upon manufacturing configuration.
uint16_t halInternalStartSystemTimer(void)
Initializes the system tick.
Definition: micro-common.c:74
#define IAP_BOOTLOADER_APP_SWITCH_SIGNATURE
Signature used for switch to application.
Header file for system_timer APIs.
uint8_t StStatus
Return type for St functions.
Definition: error.h:18
#define ST_ERR_FATAL(x01)
The generic "fatal error" message.
Definition: error-def.h:55