📄 util.c
字号:
/********************* (C) COPYRIGHT 2007 RAISONANCE S.A.S. *******************/
/**
*
* @file Util.c
* @brief Various utilities for STM32 CircleOS.
* @author RT
* @date 07/2007
*
**/
/******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "circle.h"
#include "adc.h"
/// @cond Internal
/* Private defines -----------------------------------------------------------*/
#define GPIO_USB_PIN GPIO_Pin_1
#define GPIOx_USB GPIOA
#define OsVersion "V 1.7" /*!< CircleOS version string. */
/* Private typedef -----------------------------------------------------------*/
enum eSpeed CurrentSpeed;
/* Private variables ---------------------------------------------------------*/
RCC_ClocksTypeDef RCC_ClockFreq;
int dummycounter = 0;
u8 fTemperatureInFahrenheit = 0; /*!< 1 : Fahrenheit, 0 : Celcius (default). */
/* Private function prototypes -----------------------------------------------*/
static void _int2str( char* ptr, s32 X, u16 digit, int flagunsigned, int fillwithzero );
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
*
* _int2str
*
*******************************************************************************/
/**
*
* Translate a 32 bit word into a string.
*
* @param[in,out] ptr A pointer to a string large enough to contain
* the translated 32 bit word.
* @param[in] X The 32 bit word to translate.
* @param[in] digit The amount of digits wanted in the result string.
* @param[in] flagunsigned Is the input word unsigned?
* @param[in] fillwithzero Fill with zeros or spaces.
*
**/
/******************************************************************************/
static void _int2str( char* ptr, s32 X, u16 digit, int flagunsigned, int fillwithzero )
{
u8 c;
u8 fFirst = 0;
u8 fNeg = 0;
u32 DIG = 1;
int i;
for( i = 1; i < digit; i++ )
{
DIG *= 10;
}
if( !flagunsigned && ( X < 0 ) )
{
fNeg = 1;
X = -X;
}
u32 r = X;
for( i = 0; i < digit; i++, DIG /= 10 )
{
c = (r/DIG);
r -= (c*DIG);
if( fillwithzero || fFirst || c || ( i == ( digit - 1 ) ) )
{
if( ( fFirst == 0 ) && !flagunsigned )
{
*ptr++ = fNeg ? '-' : ' ';
}
*ptr++ = c + 0x30;
fFirst = 1;
}
else
{
*ptr++ = ' ';
}
}
*ptr++ = 0;
}
/* Public functions for CircleOS ---------------------------------------------*/
/*******************************************************************************
*
* delay_unit
*
*******************************************************************************/
/**
*
* Called by starting_delay().
*
* @note Not in main.c to avoid inlining.
*
**/
/******************************************************************************/
void delay_unit( void )
{
dummycounter++;
}
/// @endcond
/* Public functions ----------------------------------------------------------*/
/*******************************************************************************
*
* UTIL_GetBat
*
*******************************************************************************/
/**
*
* Return the batterie tension in mV.
*
* @return Batterie tension in mV.
*
**/
/******************************************************************************/
u16 UTIL_GetBat( void )
{
#ifdef _ADC
u16 vbat;
// Measure VBAT
vbat = ADC_ConvertedValue[0]; //*( (u16*)ADC1_DR_Address ); // <=== note changed
vbat = vbat & 0xFFF;
vbat = ( vbat * VDD_VOLTAGE_MV ) / 0x1000;
return vbat;
#else
return 0;
#endif
}
/*******************************************************************************
*
* UTIL_GetTemp
*
*******************************************************************************/
/**
*
* Return the Temperature: degrees / 10, Celcius or Fahrenheit.
*
* @return The temperature (C or F) (averaging of several channels).
*
**/
/******************************************************************************/
u16 UTIL_GetTemp( void )
{
s32 temp;
s16 *p=&ADC_ConvertedValue[1]; //intent; point to first of 8 results from same source - use a short name for it!
// Measure temp
//temp = ADC_ConvertedValue[1];//*( (u16*)ADC1_DR_Address );
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
//will not help reduce mains ripple because conversions are SO FAST!!
temp = temp & 0xFFF;
temp = ( temp * VDD_VOLTAGE_MV ) / 0x1000; //finds mV
temp = (((1400-temp)*100000)/448)+25000; //gives approx temp x 1000 degrees C
//Fahrenheit = 32 + 9 / 5 * Celsius
if ( fTemperatureInFahrenheit )
{
temp = 32000 + (9 * temp) / 5 ;
}
return temp / 100;
}
/*******************************************************************************
*
* UTIL_SetTempMode
*
*******************************************************************************/
/**
*
* Set the temperature mode (F/C)
*
* @param[in] mode 0: Celcius, 1: Fahrenheit
*
**/
/******************************************************************************/
void UTIL_SetTempMode ( int mode )
{
fTemperatureInFahrenheit = mode;
return;
}
/*******************************************************************************
*
* UTIL_GetUsb
*
*******************************************************************************/
/**
*
* Return the USB connexion state.
*
* @return The USB connexion state.
*
**/
/******************************************************************************/
u8 UTIL_GetUsb( void )
{
GPIO_InitStructure.GPIO_Pin = GPIO_USB_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOx_USB, &GPIO_InitStructure );
return ( GPIO_ReadInputDataBit( GPIOx_USB, GPIO_USB_PIN ) == Bit_SET );
}
/*******************************************************************************
*
* UTIL_uint2str
*
*******************************************************************************/
/**
*
* Convert an <b>unsigned</b> integer into a string.
*
* @param [out] ptr The output string.
* @param [in] X The unsigned value to convert.
* @param [in] digit The number of digits in the output string.
* @param [in] fillwithzero \li 0 fill with blanks.
* \li 1 fill with zeros.
*
**/
/********************************************************************************/
void UTIL_uint2str( char* ptr, u32 X, u16 digit, int fillwithzero )
{
_int2str( ptr, X, digit, 1, fillwithzero);
}
/*******************************************************************************
*
* UTIL_int2str
*
*******************************************************************************/
/**
*
* Convert a <b>signed</b> integer into a string.
*
* @param [out] ptr The output string.
* @param [in] X The unsigned value to convert.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -