Contiki 3.x
micro-common.h
Go to the documentation of this file.
1 /** @file hal/micro/cortexm3/micro-common.h
2  * @brief Utility and convenience functions for STM32W108 microcontroller,
3  * common to both the full and minimal hal.
4  * See @ref micro for documentation.
5  *
6  * <!--(C) COPYRIGHT 2010 STMicroelectronics. All rights reserved. -->
7  */
8 
9 /** @addtogroup micro
10  * See also hal/micro/cortexm3/micro.h for source code.
11  *@{
12  */
13 
14 #ifndef STM32W108XX_MICRO_COMMON_H_
15 #define STM32W108XX_MICRO_COMMON_H_
16 
17 #ifndef DOXYGEN_SHOULD_SKIP_THIS
18 #ifndef __STSTATUS_TYPE__
19 #define __STSTATUS_TYPE__
20  //This is necessary here because halSleepForQsWithOptions returns an
21  //StStatus and not adding this typedef to this file breaks a
22  //whole lot of builds.
23  typedef uint8_t StStatus;
24 #endif //__STSTATUS_TYPE__
25 #endif // DOXYGEN_SHOULD_SKIP_THIS
26 
27 #define PORTA (0 << 3)
28 #define PORTB (1 << 3)
29 #define PORTC (2 << 3)
30 
31 /**
32  * @brief Some registers and variables require indentifying GPIO by
33  * a single number instead of the port and pin. This macro converts
34  * Port A pins into a single number.
35  */
36 #define PORTA_PIN(y) (PORTA|y)
37 /**
38  * @brief Some registers and variables require indentifying GPIO by
39  * a single number instead of the port and pin. This macro converts
40  * Port B pins into a single number.
41  */
42 #define PORTB_PIN(y) (PORTB|y)
43 /**
44  * @brief Some registers and variables require indentifying GPIO by
45  * a single number instead of the port and pin. This macro converts
46  * Port C pins into a single number.
47  */
48 #define PORTC_PIN(y) (PORTC|y)
49 
50 /**
51  * @brief Some registers and variables require indentifying GPIO by
52  * a single number instead of the port and pin. This macro converts
53  * Port C pins into a single number.
54  */
55 #define PORTx_PIN(x, y) (x|y)
56 
57 /**
58  * @brief Resets the watchdog timer. This function is pointed
59  * to by the macro ::halResetWatchdog().
60  * @warning Be very careful when using this as you can easily get into an
61  * infinite loop.
62  */
63 void halInternalResetWatchDog( void );
64 
65 
66 /**
67  * @brief Configure an IO pin's operating mode
68  *
69  * @param io The io pin to use, can be specified with the convenience macros
70  * PORTA_PIN(), PORTB_PIN(), PORTC_PIN()
71  * @param config The configuration mode to use.
72  *
73  */
74 void halGpioConfig(uint32_t io, uint32_t config);
75 
76 /**
77  * @brief Set/Clear single GPIO bit
78  *
79  * @param io The io pin to use, can be specified with the convenience macros
80  * PORTA_PIN(), PORTB_PIN(), PORTC_PIN()
81  * @param value A flag indicating whether to set or clear the io.
82  *
83  */
84 void halGpioSet(uint32_t io, boolean value);
85 
86 
87 /**
88  * @brief Calibrates the internal SlowRC to generate a 1024 Hz (1kHz) clock.
89  */
90 void halInternalCalibrateSlowRc( void );
91 
92 /**
93  * @brief Calibrates the internal FastRC to generate a 12Mhz clock.
94  */
96 
97 
98 /**
99  * @brief Sets the trim values for the 1.8V and 1.2V regulators based upon
100  * manufacturing configuration.
101  *
102  * @param boostMode Alter the regulator trim based upon the state
103  * of boost mode. TRUE if boost mode is active, FALSE otherwise.
104  */
105 void halInternalSetRegTrim(boolean boostMode);
106 
107 /** @brief Takes a slow ADC measurement of VDD_PADS in millivolts. Due to
108  * the conversions performed, this function takes slightly under 3.2ms with a
109  * variation across successive conversions approximately +/-2mv of the average
110  * conversion.
111  *
112  * @return A slow measurement of VDD_PADS in millivolts.
113  */
114 uint16_t stMeasureVddSlow(void);
115 
116 
117 /** @brief Takes a fast ADC measurement of VDD_PADS in millivolts.
118  * Due to the conversions performed, this function takes slightly under 150us
119  * with a variation across successive conversions approximately +/-20mv of
120  * the average conversion.
121  *
122  * @return A fast measurement of VDD_PADS in millivolts.
123  */
124 uint16_t stMeasureVddFast(void);
125 
126 
127 /**
128  * @brief Calibrates the GPIO pads. This function is called from within
129  * the stack and HAL at appropriate times.
130  */
131 void halCommonCalibratePads(void);
132 
133 /**
134  * @brief This function is intended to be called periodically, from the
135  * stack and application, to check the XTAL bias trim is within
136  * appropriate levels and adjust if not. This function is *not* designed
137  * to be used before halInternalSwitchToXtal() has been called.
138  */
139 void halCommonCheckXtalBiasTrim(void);
140 
141 /**
142  * @brief Switches to running off of the 24MHz crystal, including changing
143  * the CPU to be 24MHz (FCLK sourced from SYSCLK). The switch function
144  * will respect the BIASTRIM HI and LO flags and adjust bias trim until
145  * appropriate crystal biasing is used. This function is called from
146  * within the stack and HAL at appropriate times.
147  */
148 void halInternalSwitchToXtal(void);
149 
150 /**
151  * @brief Search for optimal 24MHz crystal bias trim, assuming no valid
152  * prior value. This function is typically called during initialization
153  * of the microcontroller.
154  */
156 
157 /** @brief Blocks the current thread of execution for the specified
158  * amount of time, in milliseconds.
159  *
160  * The function is implemented with cycle-counted busy loops
161  * and is intended to create the short delays required when interfacing with
162  * hardware peripherals. This function works by simply adding another
163  * layer on top of halCommonDelayMicroseconds().
164  *
165  * @param ms The specified time, in milliseconds.
166  */
167 void halCommonDelayMilliseconds(uint16_t ms);
168 
169 
170 /** @brief Puts the microcontroller to sleep in a specified mode, allows
171  * the GPIO wake sources to be determined at runtime. This function
172  * requires the GPIO wake sources to be defined at compile time in the board
173  * file.
174  *
175  * @note This routine always enables interrupts.
176  *
177  * @param sleepMode A microcontroller sleep mode.
178  *
179  * @param gpioWakeBitMask A bit mask of the GPIO that are allowed to wake
180  * the chip from deep sleep. A high bit in the mask will enable waking
181  * the chip if the corresponding GPIO changes state. bit0 is PA0, bit1 is
182  * PA1, bit8 is PB0, bit16 is PCO, bit23 is PC7, bits[31:24] are ignored.
183  *
184  * @sa ::SleepModes
185  */
186 void halSleepWithOptions(SleepModes sleepMode, uint32_t gpioWakeBitMask);
187 
188 
189 /**
190  * @brief Uses the system timer to enter ::SLEEPMODE_WAKETIMER for
191  * approximately the specified amount of time (provided in quarter seconds),
192  * the GPIO wake sources can be provided at runtime.
193  *
194  * This function returns ::ST_SUCCESS and the duration parameter is
195  * decremented to 0 after sleeping for the specified amount of time. If an
196  * interrupt occurs that brings the chip out of sleep, the function returns
197  * ::ST_SLEEP_INTERRUPTED and the duration parameter reports the amount of
198  * time remaining out of the original request.
199  *
200  * @note This routine always enables interrupts.
201  *
202  * @note The maximum sleep time of the hardware is limited on STM32W108 platforms
203  * to 48.5 days. Any sleep duration greater than this limit will wake up
204  * briefly (e.g. 16 microseconds) to reenable another sleep cycle.
205  *
206  * @param duration The amount of time, expressed in quarter seconds, that the
207  * micro should be placed into ::SLEEPMODE_WAKETIMER. When the function returns,
208  * this parameter provides the amount of time remaining out of the original
209  * sleep time request (normally the return value will be 0).
210  *
211  * @param gpioWakeBitMask A bit mask of the GPIO that are allowed to wake
212  * the chip from deep sleep. A high bit in the mask will enable waking
213  * the chip if the corresponding GPIO changes state. bit0 is PA0, bit1 is
214  * PA1, bit8 is PB0, bit16 is PCO, bit23 is PC7, bits[31:24] are ignored.
215  *
216  * @return An StStatus value indicating the success or
217  * failure of the command.
218  */
219 StStatus halSleepForQsWithOptions(uint32_t *duration, uint32_t gpioWakeBitMask);
220 
221 /**
222  * @brief Provides access to assembly code which triggers idle sleep.
223  */
224 void halInternalIdleSleep(void);
225 
226 /** @brief Puts the microcontroller to sleep in a specified mode. This
227  * internal function performs the actual sleep operation. This function
228  * assumes all of the wake source registers are configured properly.
229  *
230  * @note This routine always enables interrupts.
231  *
232  * @param sleepMode A microcontroller sleep mode
233  */
234 void halInternalSleep(SleepModes sleepMode);
235 
236 
237 /**
238  * @brief Obtains the events that caused the last wake from sleep. The
239  * meaning of each bit is as follows:
240  * - [31] = WakeInfoValid
241  * - [30] = SleepSkipped
242  * - [29] = CSYSPWRUPREQ
243  * - [28] = CDBGPWRUPREQ
244  * - [27] = PWRUP_WAKECORE
245  * - [26] = PWRUP_SLEEPTMRWRAP
246  * - [25] = PWRUP_SLEEPTMRCOMPB
247  * - [24] = PWRUP_SLEEPTMRCOMPA
248  * - [23:0] = corresponding GPIO activity
249  *
250  * WakeInfoValid means that ::halSleepWithOptions (::halInternalSleep) has been called
251  * at least once. Since the power on state clears the wake event info,
252  * this bit says the sleep code has been called since power on.
253  *
254  * SleepSkipped means that the chip never left the running state. Sleep can
255  * be skipped if any wake event occurs between going ::ATOMIC and transferring
256  * control from the CPU to the power management state machine. Sleep can
257  * also be skipped if the debugger is connected (JTAG/SerialWire CSYSPWRUPREQ
258  * signal is set). The net affect of skipping sleep is the Low Voltage
259  * domain never goes through a power/reset cycle.
260  *
261  * @return The events that caused the last wake from sleep.
262  */
263 uint32_t halGetWakeInfo(void);
264 
265 
266 /** @brief Seeds the ::halCommonGetRandom() pseudorandom number
267  * generator.
268  *
269  * It should be called by the application during initialization with a seed
270  * from the radio randon number generator.
271  *
272  * @param seed A seed for the pseudorandom number generator.
273  */
274 void halCommonSeedRandom(uint32_t seed);
275 
276 /** @brief Runs a standard LFSR to generate pseudorandom numbers.
277  *
278  * Called by the MAC in the stack to choose random backoff slots.
279  *
280  * Complicated implementations may improve the MAC's
281  * ability to avoid collisions in large networks, but it is \b critical to
282  * implement this function to return quickly.
283  */
284 uint16_t halCommonGetRandom(void);
285 
286 #endif //STM32W108XX_MICRO_COMMON_H_
287 
288 /**@} // END micro group
289  */
290 
void halCommonCalibratePads(void)
Calibrates the GPIO pads.
void halSleepWithOptions(SleepModes sleepMode, uint32_t gpioWakeBitMask)
Puts the microcontroller to sleep in a specified mode, allows the GPIO wake sources to be determined ...
Definition: sleep.c:848
void halInternalIdleSleep(void)
Provides access to assembly code which triggers idle sleep.
void halInternalCalibrateFastRc(void)
Calibrates the internal FastRC to generate a 12Mhz clock.
Definition: clocks.c:168
uint16_t stMeasureVddFast(void)
Takes a fast ADC measurement of VDD_PADS in millivolts.
uint16_t stMeasureVddSlow(void)
Takes a slow ADC measurement of VDD_PADS in millivolts.
uint16_t halCommonGetRandom(void)
Runs a standard LFSR to generate pseudorandom numbers.
Definition: micro.c:96
void halCommonSeedRandom(uint32_t seed)
Seeds the halCommonGetRandom() pseudorandom number generator.
Definition: micro.c:76
StStatus halSleepForQsWithOptions(uint32_t *duration, uint32_t gpioWakeBitMask)
Uses the system timer to enter ::SLEEPMODE_WAKETIMER for approximately the specified amount of time (...
Definition: system-timer.c:168
void halInternalSleep(SleepModes sleepMode)
Puts the microcontroller to sleep in a specified mode.
Definition: sleep.c:127
void halInternalSetRegTrim(boolean boostMode)
Sets the trim values for the 1.8V and 1.2V regulators based upon manufacturing configuration.
void halInternalSearchForBiasTrim(void)
Search for optimal 24MHz crystal bias trim, assuming no valid prior value.
Definition: clocks.c:338
void halGpioSet(uint32_t gpio, boolean value)
Set/Clear single GPIO bit.
Definition: micro-common.c:63
uint32_t halGetWakeInfo(void)
Obtains the events that caused the last wake from sleep.
Definition: sleep.c:122
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
void halCommonDelayMilliseconds(uint16_t ms)
Blocks the current thread of execution for the specified amount of time, in milliseconds.
void halCommonCheckXtalBiasTrim(void)
This function is intended to be called periodically, from the stack and application, to check the XTAL bias trim is within appropriate levels and adjust if not.
Definition: clocks.c:305
void halInternalResetWatchDog(void)
Resets the watchdog timer.
Definition: micro-common.c:25
void halGpioConfig(uint32_t io, uint32_t config)
Configure an IO pin&#39;s operating mode.
Definition: micro-common.c:48
void halInternalCalibrateSlowRc(void)
Calibrates the internal SlowRC to generate a 1024 Hz (1kHz) clock.
Definition: clocks.c:60