00001 /** @file hal/micro/cortexm3/micro-common.h 00002 * @brief Utility and convenience functions for STM32W108 microcontroller, 00003 * common to both the full and minimal hal. 00004 * See @ref micro for documentation. 00005 * 00006 * <!--(C) COPYRIGHT 2010 STMicroelectronics. All rights reserved. --> 00007 */ 00008 00009 /** @addtogroup micro 00010 * See also hal/micro/cortexm3/micro.h for source code. 00011 *@{ 00012 */ 00013 00014 #ifndef __STM32W108XX_MICRO_COMMON_H__ 00015 #define __STM32W108XX_MICRO_COMMON_H__ 00016 00017 #ifndef DOXYGEN_SHOULD_SKIP_THIS 00018 #ifndef __STSTATUS_TYPE__ 00019 #define __STSTATUS_TYPE__ 00020 //This is necessary here because halSleepForQsWithOptions returns an 00021 //StStatus and not adding this typedef to this file breaks a 00022 //whole lot of builds. 00023 typedef int8u StStatus; 00024 #endif //__STSTATUS_TYPE__ 00025 #endif // DOXYGEN_SHOULD_SKIP_THIS 00026 00027 #define PORTA (0 << 3) 00028 #define PORTB (1 << 3) 00029 #define PORTC (2 << 3) 00030 00031 /** 00032 * @brief Some registers and variables require indentifying GPIO by 00033 * a single number instead of the port and pin. This macro converts 00034 * Port A pins into a single number. 00035 */ 00036 #define PORTA_PIN(y) (PORTA|y) 00037 /** 00038 * @brief Some registers and variables require indentifying GPIO by 00039 * a single number instead of the port and pin. This macro converts 00040 * Port B pins into a single number. 00041 */ 00042 #define PORTB_PIN(y) (PORTB|y) 00043 /** 00044 * @brief Some registers and variables require indentifying GPIO by 00045 * a single number instead of the port and pin. This macro converts 00046 * Port C pins into a single number. 00047 */ 00048 #define PORTC_PIN(y) (PORTC|y) 00049 00050 /** 00051 * @brief Some registers and variables require indentifying GPIO by 00052 * a single number instead of the port and pin. This macro converts 00053 * Port C pins into a single number. 00054 */ 00055 #define PORTx_PIN(x, y) (x|y) 00056 00057 /** 00058 * @brief Resets the watchdog timer. This function is pointed 00059 * to by the macro ::halResetWatchdog(). 00060 * @warning Be very careful when using this as you can easily get into an 00061 * infinite loop. 00062 */ 00063 void halInternalResetWatchDog( void ); 00064 00065 00066 /** 00067 * @brief Configure an IO pin's operating mode 00068 * 00069 * @param io The io pin to use, can be specified with the convenience macros 00070 * PORTA_PIN(), PORTB_PIN(), PORTC_PIN() 00071 * @param config The configuration mode to use. 00072 * 00073 */ 00074 void halGpioConfig(int32u io, int32u config); 00075 00076 /** 00077 * @brief Set/Clear single GPIO bit 00078 * 00079 * @param io The io pin to use, can be specified with the convenience macros 00080 * PORTA_PIN(), PORTB_PIN(), PORTC_PIN() 00081 * @param value A flag indicating whether to set or clear the io. 00082 * 00083 */ 00084 void halGpioSet(int32u io, boolean value); 00085 00086 00087 /** 00088 * @brief Calibrates the internal SlowRC to generate a 1024 Hz (1kHz) clock. 00089 */ 00090 void halInternalCalibrateSlowRc( void ); 00091 00092 /** 00093 * @brief Calibrates the internal FastRC to generate a 12Mhz clock. 00094 */ 00095 void halInternalCalibrateFastRc(void); 00096 00097 00098 /** 00099 * @brief Sets the trim values for the 1.8V and 1.2V regulators based upon 00100 * manufacturing configuration. 00101 * 00102 * @param boostMode Alter the regulator trim based upon the state 00103 * of boost mode. TRUE if boost mode is active, FALSE otherwise. 00104 */ 00105 void halInternalSetRegTrim(boolean boostMode); 00106 00107 /** @brief Takes a slow ADC measurement of VDD_PADS in millivolts. Due to 00108 * the conversions performed, this function takes slightly under 3.2ms with a 00109 * variation across successive conversions approximately +/-2mv of the average 00110 * conversion. 00111 * 00112 * @return A slow measurement of VDD_PADS in millivolts. 00113 */ 00114 int16u stMeasureVddSlow(void); 00115 00116 00117 /** @brief Takes a fast ADC measurement of VDD_PADS in millivolts. 00118 * Due to the conversions performed, this function takes slightly under 150us 00119 * with a variation across successive conversions approximately +/-20mv of 00120 * the average conversion. 00121 * 00122 * @return A fast measurement of VDD_PADS in millivolts. 00123 */ 00124 int16u stMeasureVddFast(void); 00125 00126 00127 /** 00128 * @brief Calibrates the GPIO pads. This function is called from within 00129 * the stack and HAL at appropriate times. 00130 */ 00131 void halCommonCalibratePads(void); 00132 00133 /** 00134 * @brief This function is intended to be called periodically, from the 00135 * stack and application, to check the XTAL bias trim is within 00136 * appropriate levels and adjust if not. This function is *not* designed 00137 * to be used before halInternalSwitchToXtal() has been called. 00138 */ 00139 void halCommonCheckXtalBiasTrim(void); 00140 00141 /** 00142 * @brief Switches to running off of the 24MHz crystal, including changing 00143 * the CPU to be 24MHz (FCLK sourced from SYSCLK). The switch function 00144 * will respect the BIASTRIM HI and LO flags and adjust bias trim until 00145 * appropriate crystal biasing is used. This function is called from 00146 * within the stack and HAL at appropriate times. 00147 */ 00148 void halInternalSwitchToXtal(void); 00149 00150 /** 00151 * @brief Search for optimal 24MHz crystal bias trim, assuming no valid 00152 * prior value. This function is typically called during initialization 00153 * of the microcontroller. 00154 */ 00155 void halInternalSearchForBiasTrim(void); 00156 00157 /** @brief Blocks the current thread of execution for the specified 00158 * amount of time, in milliseconds. 00159 * 00160 * The function is implemented with cycle-counted busy loops 00161 * and is intended to create the short delays required when interfacing with 00162 * hardware peripherals. This function works by simply adding another 00163 * layer on top of halCommonDelayMicroseconds(). 00164 * 00165 * @param ms The specified time, in milliseconds. 00166 */ 00167 void halCommonDelayMilliseconds(int16u ms); 00168 00169 00170 /** @brief Puts the microcontroller to sleep in a specified mode, allows 00171 * the GPIO wake sources to be determined at runtime. This function 00172 * requires the GPIO wake sources to be defined at compile time in the board 00173 * file. 00174 * 00175 * @note This routine always enables interrupts. 00176 * 00177 * @param sleepMode A microcontroller sleep mode. 00178 * 00179 * @param gpioWakeBitMask A bit mask of the GPIO that are allowed to wake 00180 * the chip from deep sleep. A high bit in the mask will enable waking 00181 * the chip if the corresponding GPIO changes state. bit0 is PA0, bit1 is 00182 * PA1, bit8 is PB0, bit16 is PCO, bit23 is PC7, bits[31:24] are ignored. 00183 * 00184 * @sa ::SleepModes 00185 */ 00186 void halSleepWithOptions(SleepModes sleepMode, int32u gpioWakeBitMask); 00187 00188 00189 /** 00190 * @brief Uses the system timer to enter ::SLEEPMODE_WAKETIMER for 00191 * approximately the specified amount of time (provided in quarter seconds), 00192 * the GPIO wake sources can be provided at runtime. 00193 * 00194 * This function returns ::ST_SUCCESS and the duration parameter is 00195 * decremented to 0 after sleeping for the specified amount of time. If an 00196 * interrupt occurs that brings the chip out of sleep, the function returns 00197 * ::ST_SLEEP_INTERRUPTED and the duration parameter reports the amount of 00198 * time remaining out of the original request. 00199 * 00200 * @note This routine always enables interrupts. 00201 * 00202 * @note The maximum sleep time of the hardware is limited on STM32W108 platforms 00203 * to 48.5 days. Any sleep duration greater than this limit will wake up 00204 * briefly (e.g. 16 microseconds) to reenable another sleep cycle. 00205 * 00206 * @nostackusage 00207 * 00208 * @param duration The amount of time, expressed in quarter seconds, that the 00209 * micro should be placed into ::SLEEPMODE_WAKETIMER. When the function returns, 00210 * this parameter provides the amount of time remaining out of the original 00211 * sleep time request (normally the return value will be 0). 00212 * 00213 * @param gpioWakeBitMask A bit mask of the GPIO that are allowed to wake 00214 * the chip from deep sleep. A high bit in the mask will enable waking 00215 * the chip if the corresponding GPIO changes state. bit0 is PA0, bit1 is 00216 * PA1, bit8 is PB0, bit16 is PCO, bit23 is PC7, bits[31:24] are ignored. 00217 * 00218 * @return An StStatus value indicating the success or 00219 * failure of the command. 00220 */ 00221 StStatus halSleepForQsWithOptions(int32u *duration, int32u gpioWakeBitMask); 00222 00223 /** 00224 * @brief Provides access to assembly code which triggers idle sleep. 00225 */ 00226 void halInternalIdleSleep(void); 00227 00228 /** @brief Puts the microcontroller to sleep in a specified mode. This 00229 * internal function performs the actual sleep operation. This function 00230 * assumes all of the wake source registers are configured properly. 00231 * 00232 * @note This routine always enables interrupts. 00233 * 00234 * @param sleepMode A microcontroller sleep mode 00235 */ 00236 void halInternalSleep(SleepModes sleepMode); 00237 00238 00239 /** 00240 * @brief Obtains the events that caused the last wake from sleep. The 00241 * meaning of each bit is as follows: 00242 * - [31] = WakeInfoValid 00243 * - [30] = SleepSkipped 00244 * - [29] = CSYSPWRUPREQ 00245 * - [28] = CDBGPWRUPREQ 00246 * - [27] = PWRUP_WAKECORE 00247 * - [26] = PWRUP_SLEEPTMRWRAP 00248 * - [25] = PWRUP_SLEEPTMRCOMPB 00249 * - [24] = PWRUP_SLEEPTMRCOMPA 00250 * - [23:0] = corresponding GPIO activity 00251 * 00252 * WakeInfoValid means that ::halSleepWithOptions (::halInternalSleep) has been called 00253 * at least once. Since the power on state clears the wake event info, 00254 * this bit says the sleep code has been called since power on. 00255 * 00256 * SleepSkipped means that the chip never left the running state. Sleep can 00257 * be skipped if any wake event occurs between going ::ATOMIC and transferring 00258 * control from the CPU to the power management state machine. Sleep can 00259 * also be skipped if the debugger is connected (JTAG/SerialWire CSYSPWRUPREQ 00260 * signal is set). The net affect of skipping sleep is the Low Voltage 00261 * domain never goes through a power/reset cycle. 00262 * 00263 * @return The events that caused the last wake from sleep. 00264 */ 00265 int32u halGetWakeInfo(void); 00266 00267 00268 /** @brief Seeds the ::halCommonGetRandom() pseudorandom number 00269 * generator. 00270 * 00271 * It should be called by the application during initialization with a seed 00272 * from the radio randon number generator. 00273 * 00274 * @param seed A seed for the pseudorandom number generator. 00275 */ 00276 void halCommonSeedRandom(int32u seed); 00277 00278 /** @brief Runs a standard LFSR to generate pseudorandom numbers. 00279 * 00280 * Called by the MAC in the stack to choose random backoff slots. 00281 * 00282 * Complicated implementations may improve the MAC's 00283 * ability to avoid collisions in large networks, but it is \b critical to 00284 * implement this function to return quickly. 00285 */ 00286 int16u halCommonGetRandom(void); 00287 00288 #endif //__STM32W108XX_MICRO_COMMON_H__ 00289 00290 /**@} // END micro group 00291 */ 00292