Contiki 2.5
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 int8u 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(int32u io, int32u 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(int32u 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 int16u 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 int16u 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(int16u 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, int32u 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  * @nostackusage
207  *
208  * @param duration The amount of time, expressed in quarter seconds, that the
209  * micro should be placed into ::SLEEPMODE_WAKETIMER. When the function returns,
210  * this parameter provides the amount of time remaining out of the original
211  * sleep time request (normally the return value will be 0).
212  *
213  * @param gpioWakeBitMask A bit mask of the GPIO that are allowed to wake
214  * the chip from deep sleep. A high bit in the mask will enable waking
215  * the chip if the corresponding GPIO changes state. bit0 is PA0, bit1 is
216  * PA1, bit8 is PB0, bit16 is PCO, bit23 is PC7, bits[31:24] are ignored.
217  *
218  * @return An StStatus value indicating the success or
219  * failure of the command.
220  */
221 StStatus halSleepForQsWithOptions(int32u *duration, int32u gpioWakeBitMask);
222 
223 /**
224  * @brief Provides access to assembly code which triggers idle sleep.
225  */
226 void halInternalIdleSleep(void);
227 
228 /** @brief Puts the microcontroller to sleep in a specified mode. This
229  * internal function performs the actual sleep operation. This function
230  * assumes all of the wake source registers are configured properly.
231  *
232  * @note This routine always enables interrupts.
233  *
234  * @param sleepMode A microcontroller sleep mode
235  */
236 void halInternalSleep(SleepModes sleepMode);
237 
238 
239 /**
240  * @brief Obtains the events that caused the last wake from sleep. The
241  * meaning of each bit is as follows:
242  * - [31] = WakeInfoValid
243  * - [30] = SleepSkipped
244  * - [29] = CSYSPWRUPREQ
245  * - [28] = CDBGPWRUPREQ
246  * - [27] = PWRUP_WAKECORE
247  * - [26] = PWRUP_SLEEPTMRWRAP
248  * - [25] = PWRUP_SLEEPTMRCOMPB
249  * - [24] = PWRUP_SLEEPTMRCOMPA
250  * - [23:0] = corresponding GPIO activity
251  *
252  * WakeInfoValid means that ::halSleepWithOptions (::halInternalSleep) has been called
253  * at least once. Since the power on state clears the wake event info,
254  * this bit says the sleep code has been called since power on.
255  *
256  * SleepSkipped means that the chip never left the running state. Sleep can
257  * be skipped if any wake event occurs between going ::ATOMIC and transferring
258  * control from the CPU to the power management state machine. Sleep can
259  * also be skipped if the debugger is connected (JTAG/SerialWire CSYSPWRUPREQ
260  * signal is set). The net affect of skipping sleep is the Low Voltage
261  * domain never goes through a power/reset cycle.
262  *
263  * @return The events that caused the last wake from sleep.
264  */
265 int32u halGetWakeInfo(void);
266 
267 
268 /** @brief Seeds the ::halCommonGetRandom() pseudorandom number
269  * generator.
270  *
271  * It should be called by the application during initialization with a seed
272  * from the radio randon number generator.
273  *
274  * @param seed A seed for the pseudorandom number generator.
275  */
276 void halCommonSeedRandom(int32u seed);
277 
278 /** @brief Runs a standard LFSR to generate pseudorandom numbers.
279  *
280  * Called by the MAC in the stack to choose random backoff slots.
281  *
282  * Complicated implementations may improve the MAC's
283  * ability to avoid collisions in large networks, but it is \b critical to
284  * implement this function to return quickly.
285  */
286 int16u halCommonGetRandom(void);
287 
288 #endif //__STM32W108XX_MICRO_COMMON_H__
289 
290 /**@} // END micro group
291  */
292