1 /********************* (C) COPYRIGHT 2007 RAISONANCE S.A.S. *******************/
5 * @brief Various utilities for STM32 CircleOS.
10 /******************************************************************************/
12 /* Includes ------------------------------------------------------------------*/
19 /* Private defines -----------------------------------------------------------*/
20 #define GPIO_USB_PIN GPIO_Pin_1
21 #define GPIOx_USB GPIOA
22 #define OsVersion "V 1.7" /*!< CircleOS version string. */
24 /* Private typedef -----------------------------------------------------------*/
25 enum eSpeed CurrentSpeed;
27 /* Private variables ---------------------------------------------------------*/
28 RCC_ClocksTypeDef RCC_ClockFreq;
30 u8 fTemperatureInFahrenheit = 0; /*!< 1 : Fahrenheit, 0 : Celcius (default). */
32 /* Private function prototypes -----------------------------------------------*/
33 static void _int2str( char* ptr, s32 X, u16 digit, int flagunsigned, int fillwithzero );
35 /* Private functions ---------------------------------------------------------*/
37 /*******************************************************************************
41 *******************************************************************************/
44 * Translate a 32 bit word into a string.
46 * @param[in,out] ptr A pointer to a string large enough to contain
47 * the translated 32 bit word.
48 * @param[in] X The 32 bit word to translate.
49 * @param[in] digit The amount of digits wanted in the result string.
50 * @param[in] flagunsigned Is the input word unsigned?
51 * @param[in] fillwithzero Fill with zeros or spaces.
54 /******************************************************************************/
55 static void _int2str( char* ptr, s32 X, u16 digit, int flagunsigned, int fillwithzero )
63 for( i = 1; i < digit; i++ )
68 if( !flagunsigned && ( X < 0 ) )
76 for( i = 0; i < digit; i++, DIG /= 10 )
81 if( fillwithzero || fFirst || c || ( i == ( digit - 1 ) ) )
83 if( ( fFirst == 0 ) && !flagunsigned )
85 *ptr++ = fNeg ? '-' : ' ';
100 /* Public functions for CircleOS ---------------------------------------------*/
102 /*******************************************************************************
106 *******************************************************************************/
109 * Called by starting_delay().
111 * @note Not in main.c to avoid inlining.
114 /******************************************************************************/
115 void delay_unit( void )
122 /* Public functions ----------------------------------------------------------*/
124 /*******************************************************************************
128 *******************************************************************************/
131 * Return the batterie tension in mV.
133 * @return Batterie tension in mV.
136 /******************************************************************************/
137 u16 UTIL_GetBat( void )
143 vbat = ADC_ConvertedValue[0]; //*( (u16*)ADC1_DR_Address ); // <=== note changed
145 vbat = ( vbat * VDD_VOLTAGE_MV ) / 0x1000;
153 /*******************************************************************************
157 *******************************************************************************/
160 * Return the Temperature: degrees / 10, Celcius or Fahrenheit.
162 * @return The temperature (C or F) (averaging of several channels).
165 /******************************************************************************/
166 u16 UTIL_GetTemp( void )
169 s16 *p=&ADC_ConvertedValue[1]; //intent; point to first of 8 results from same source - use a short name for it!
172 //temp = ADC_ConvertedValue[1];//*( (u16*)ADC1_DR_Address );
173 temp = (p[0]+p[1]+p[2]+p[3]+p[4]+p[5]+p[6]+p[7])/8; //take avg of burst of 8 temp reads. may only help reject hi freq noise a bit
174 //will not help reduce mains ripple because conversions are SO FAST!!
176 temp = ( temp * VDD_VOLTAGE_MV ) / 0x1000; //finds mV
177 temp = (((1400-temp)*100000)/448)+25000; //gives approx temp x 1000 degrees C
179 //Fahrenheit = 32 + 9 / 5 * Celsius
180 if ( fTemperatureInFahrenheit )
182 temp = 32000 + (9 * temp) / 5 ;
188 /*******************************************************************************
192 *******************************************************************************/
195 * Set the temperature mode (F/C)
197 * @param[in] mode 0: Celcius, 1: Fahrenheit
200 /******************************************************************************/
201 void UTIL_SetTempMode ( int mode )
203 fTemperatureInFahrenheit = mode;
209 /*******************************************************************************
213 *******************************************************************************/
216 * Return the USB connexion state.
218 * @return The USB connexion state.
221 /******************************************************************************/
222 u8 UTIL_GetUsb( void )
224 GPIO_InitStructure.GPIO_Pin = GPIO_USB_PIN;
225 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
226 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
228 GPIO_Init( GPIOx_USB, &GPIO_InitStructure );
230 return ( GPIO_ReadInputDataBit( GPIOx_USB, GPIO_USB_PIN ) == Bit_SET );
233 /*******************************************************************************
237 *******************************************************************************/
240 * Convert an <b>unsigned</b> integer into a string.
242 * @param [out] ptr The output string.
243 * @param [in] X The unsigned value to convert.
244 * @param [in] digit The number of digits in the output string.
245 * @param [in] fillwithzero \li 0 fill with blanks.
246 * \li 1 fill with zeros.
249 /********************************************************************************/
250 void UTIL_uint2str( char* ptr, u32 X, u16 digit, int fillwithzero )
252 _int2str( ptr, X, digit, 1, fillwithzero);
255 /*******************************************************************************
259 *******************************************************************************/
262 * Convert a <b>signed</b> integer into a string.
264 * @param [out] ptr The output string.
265 * @param [in] X The unsigned value to convert.
266 * @param [in] digit The number of digits in the output string.
267 * @param [in] fillwithzero \li 0 fill with blanks.
268 * \li 1 fill with zeros.
271 /******************************************************************************/
272 void UTIL_int2str( char* ptr, s32 X, u16 digit, int fillwithzero )
274 _int2str( ptr, X, digit, 0, fillwithzero);
277 /*******************************************************************************
281 *******************************************************************************/
284 * Set clock frequency (lower to save energy)
286 * @param [in] speed New clock speed from very low to very fast.
289 /******************************************************************************/
290 void UTIL_SetPll( enum eSpeed speed )
292 /* Select PLL as system clock source */
293 RCC_SYSCLKConfig( RCC_SYSCLKSource_HSI );
296 RCC_PLLCmd( DISABLE );
298 if( ( speed < SPEED_VERY_LOW ) || ( speed > SPEED_VERY_HIGH ) )
300 speed = SPEED_MEDIUM;
303 CurrentSpeed = speed;
308 case SPEED_VERY_LOW :
309 /* PLLCLK = 6MHz * 3 = 18 MHz */
310 RCC_PLLConfig( RCC_PLLSource_HSE_Div2, RCC_PLLMul_3 );
315 /* PLLCLK = 12MHz * 2 = 24 MHz */
316 RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_2 );
322 /* PLLCLK = 12MHz * 3 = 36 MHz */
323 RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_3 );
328 /* PLLCLK = 12MHz * 4 = 48 MHz */
329 RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_4 );
333 case SPEED_VERY_HIGH :
334 /* PLLCLK = 12MHz * 6 = 72 MHz */
335 RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_6 );
340 RCC_PLLCmd( ENABLE );
342 /* Wait till PLL is ready */
343 while( RCC_GetFlagStatus( RCC_FLAG_PLLRDY ) == RESET )
346 /* Select PLL as system clock source */
347 RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
349 /* Wait till PLL is used as system clock source */
350 while( RCC_GetSYSCLKSource() != 0x08 )
353 /* This function fills a RCC_ClocksTypeDef structure with the current frequencies
354 of different on chip clocks (for debug purpose) */
355 RCC_GetClocksFreq( &RCC_ClockFreq );
358 /*******************************************************************************
362 *******************************************************************************/
365 * Get clock frequency
367 * @return Current clock speed from very low to very fast.
370 /******************************************************************************/
371 enum eSpeed UTIL_GetPll( void )
376 /*******************************************************************************
380 *******************************************************************************/
383 * Get CircleOS version.
385 * @return A pointer to a string containing the CircleOS version.
388 /******************************************************************************/
389 const char* UTIL_GetVersion( void )
394 /*******************************************************************************
396 * UTIL_ReadBackupRegister
398 *******************************************************************************/
401 * Reads data from the specified Data Backup Register.
403 * @param[in] BKP_DR Specifies the Data Backup Register. This parameter can be BKP_DRx where x:[1, 10]
405 * @return The content of the specified Data Backup Register.
408 /******************************************************************************/
409 u16 UTIL_ReadBackupRegister( u16 BKP_DR )
411 return (*(vu16 *)( BKP_BASE + 4 * BKP_DR ) );
414 /*******************************************************************************
416 * UTIL_WriteBackupRegister
418 *******************************************************************************/
421 * Writes data to the specified Data Backup Register.
423 * @param[in] BKP_DR Specifies the Data Backup Register. This parameter can be BKP_DRx where x:[1, 10]
424 * @param[in] Data The data to write.
427 /********************************************************************************/
428 void UTIL_WriteBackupRegister( u16 BKP_DR, u16 Data )
430 *(vu16 *)( BKP_BASE + 4 * BKP_DR ) = Data;
434 /*******************************************************************************
438 *******************************************************************************/
441 * Redirect an IRQ handler.
443 * @param[in] Offs Address in the NVIC table
444 * @param[in] pHDL Pointer to the handler.
447 /********************************************************************************/
448 void UTIL_SetIrqHandler( int Offs, tHandler pHDL )
450 if ( (Offs >= 8) && (Offs<0x100) )
451 *(tHandler *)( CIRCLEOS_RAM_BASE + Offs ) = pHDL;
454 /*******************************************************************************
458 *******************************************************************************/
461 * Get the current IRQ handler.
462 * Since (V1.6) the vector table is relocated in RAM, the vectors can be easily modified
463 * by the applications.
465 * @param[in] Offs Address in the NVIC table
466 * @return A pointer to the current handler.
469 /********************************************************************************/
470 tHandler UTIL_GetIrqHandler( int Offs )
472 if ( (Offs >= 8) && (Offs<0x100) )
473 return *(tHandler *)( CIRCLEOS_RAM_BASE + Offs );
478 /*******************************************************************************
482 *******************************************************************************/
485 * Redirect a SCHEDULER handler.
486 * Set the current SCHEDULER handler. With UTIL_GetSchHandler(), these functions
487 * allow to take the control of the different handler. You can:
488 * - replace them (get-Set)by your own handler
489 * - disable a handler: UTIL_SetSchHandler(Ix,0);
490 * - create a new handler (using the unused handlers).
491 * See scheduler.c to understand further...
493 * @param[in] Ix ID if the SCH Handler
494 * @param[in] pHDL Pointer to the handler.
497 /********************************************************************************/
498 void UTIL_SetSchHandler( enum eSchHandler Ix, tHandler pHDL )
501 SchHandler[Ix] = pHDL;
504 /*******************************************************************************
508 *******************************************************************************/
511 * Get the current SCHEDULER handler. With UTIL_SetSchHandler(), these functions
512 * allow to take the control of the different handler. You can:
513 * - replace them (get-Set)by your own handler
514 * - disable a handler: UTIL_SetSchHandler(Ix,0);
515 * - create a new handler (using the unused handlers).
516 * See scheduler.c to understand further...
518 * @param[in] Ix ID is the SCH Handler
519 * @return A pointer to the current handler.
522 /********************************************************************************/
523 tHandler UTIL_GetSchHandler( enum eSchHandler Ix )
525 if ( Ix<SCH_HDL_MAX )
526 return SchHandler [Ix] ;