vbat.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #include "app/vbat/vbat.h"
  2. #include "drivers/adc/adc.h"
  3. #include "core/gpio.h"
  4. #include "core/main.h" // SystemClock_HSI
  5. #include "core/config_pins.h"
  6. #include "core/csect.h"
  7. static bool VBat_Init();
  8. static void VBat_DeInit();
  9. static bool VBat_Serve();
  10. static uint32_t VBat_GetVoltage();
  11. static void VBat_SetInterval(size_t ms);
  12. static void VBat_IC_Enable();
  13. static void VBat_IC_Disable();
  14. #define pADCHandle (&ADC1Handle)
  15. #define VBAT_ADC_RESOLUTION eADCRes_12bit
  16. static bool bVBATInitialized = false;
  17. static uint32_t VBatMontor_LastTick = 0;
  18. static uint32_t VBatMontor_Interval = VBAT_MONITOR_INTERVAL;
  19. static uint32_t g_VBatVoltage = VBAT_INVALID_VALUE;
  20. static uint32_t g_VDDAVoltage = VBAT_INVALID_VALUE;
  21. #if VBAT_AVERAGING_POINTS > 0
  22. static uint32_t aVBatVoltage_Avg[ VBAT_AVERAGING_POINTS ] = {0};
  23. static size_t VBatVoltageAvgIndex = 1; // VBatVoltageAvgIndex=1 means the averaging mode is active since startup
  24. #if VBAT_AVERAGING_POINTS > 3
  25. #include "stdlib.h" //qsort
  26. #endif
  27. #endif
  28. const sVBat_Handle_t VBatHandle = {
  29. .Init = VBat_Init,
  30. .SetMeasureInterval = VBat_SetInterval,
  31. .ServeMonitor = VBat_Serve,
  32. .GetVoltage = VBat_GetVoltage,
  33. .DeInit = VBat_DeInit,
  34. };
  35. static bool VBat_Init()
  36. {
  37. bool bRet = false;
  38. __DI__ bRet = bVBATInitialized; __EI__
  39. if( !bRet )
  40. {
  41. if( SystemClock_HSI( true ) )
  42. {
  43. if( pADCHandle->Init( VBAT_ADC_RESOLUTION ) )
  44. {
  45. VBat_IC_Disable();
  46. {
  47. GPIO_InitTypeDef GPIO_InitStruct = {0};
  48. // Configure the pin muxing
  49. GPIO_InitStruct.Pin = CONFIG_PIN__VBAT_ADC;
  50. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  51. GPIO_InitStruct.Pull = GPIO_NOPULL;
  52. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  53. HAL_GPIO_Init(CONFIG_PORT__VBAT_ADC, &GPIO_InitStruct);
  54. }
  55. {
  56. GPIO_InitTypeDef GPIO_InitStruct = {0};
  57. // Configure the pin muxing
  58. GPIO_InitStruct.Pin = CONFIG_PIN__VBAT_EN;
  59. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  60. GPIO_InitStruct.Pull = GPIO_NOPULL;
  61. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  62. HAL_GPIO_Init(CONFIG_PORT__VBAT_EN, &GPIO_InitStruct);
  63. }
  64. #if VBAT_INIT_FOR_MEASURE == 0
  65. __DI__
  66. g_VBatVoltage = VBAT_INVALID_VALUE;
  67. #if VBAT_AVERAGING_POINTS > 0
  68. for( size_t i = 0; i < VBAT_AVERAGING_POINTS; ++i )
  69. { aVBatVoltage_Avg[ i ] = 0; }
  70. VBatVoltageAvgIndex = 1; // VBatVoltageAvgIndex=1 means the averaging mode is active since startup
  71. #endif
  72. __EI__
  73. #endif
  74. bRet = true;
  75. }
  76. }
  77. }
  78. if( !bRet )
  79. {
  80. VBat_DeInit();
  81. SystemClock_HSI( false );
  82. }
  83. __DI__ bVBATInitialized = bRet; __EI__
  84. return bRet;
  85. }
  86. static void VBat_DeInit()
  87. {
  88. DI();
  89. if( !bVBATInitialized )
  90. {
  91. EI();
  92. return;
  93. }
  94. EI();
  95. VBat_IC_Disable();
  96. {
  97. GPIO_InitTypeDef GPIO_InitStruct = {0};
  98. // Configure the pin muxing
  99. GPIO_InitStruct.Pin = CONFIG_PIN__VBAT_ADC;
  100. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  101. GPIO_InitStruct.Pull = GPIO_NOPULL;
  102. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  103. HAL_GPIO_Init(CONFIG_PORT__VBAT_ADC, &GPIO_InitStruct);
  104. }
  105. {
  106. GPIO_InitTypeDef GPIO_InitStruct = {0};
  107. // Configure the pin muxing
  108. GPIO_InitStruct.Pin = CONFIG_PIN__VBAT_EN;
  109. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  110. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  111. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  112. HAL_GPIO_Init(CONFIG_PORT__VBAT_EN, &GPIO_InitStruct);
  113. }
  114. pADCHandle->Stop( CONFIG_ADC__VBAT_ADC );
  115. pADCHandle->DeInit();
  116. SystemClock_HSI( false );
  117. __DI__ bVBATInitialized = false; __EI__
  118. }
  119. static void VBat_SetInterval(uint32_t ms)
  120. {
  121. __DI__ VBatMontor_Interval = ms; __EI__
  122. }
  123. // VBat_IC_Enable
  124. // Enable external battery sensing circuit (MOSFET)
  125. static void VBat_IC_Enable()
  126. {
  127. HAL_GPIO_WritePin( CONFIG_PORT__VBAT_EN, CONFIG_PIN__VBAT_EN, GPIO_PIN_SET );
  128. // wait until transition process completes in external RC-circuit
  129. HAL_Wait_us( 100 );
  130. }
  131. // VBat_IC_Disable
  132. // Disable external battery sensing circuit (MOSFET)
  133. static void VBat_IC_Disable()
  134. {
  135. HAL_GPIO_WritePin( CONFIG_PORT__VBAT_EN, CONFIG_PIN__VBAT_EN, GPIO_PIN_RESET );
  136. }
  137. #if VBAT_AVERAGING_POINTS == 3
  138. uint16_t middle_of_3(uint16_t a, uint16_t b, uint16_t c)
  139. {
  140. uint16_t middle;
  141. if ((a <= b) && (a <= c)){
  142. middle = (b <= c) ? b : c;
  143. }
  144. else{
  145. if ((b <= a) && (b <= c)){
  146. middle = (a <= c) ? a : c;
  147. }
  148. else{
  149. middle = (a <= b) ? a : b;
  150. }
  151. }
  152. return middle;
  153. }
  154. #endif
  155. #if VBAT_AVERAGING_POINTS > 3
  156. static int qsort_compare( const uint32_t * i, const uint32_t * j )
  157. {
  158. return ((*i > *j)?1:( (*i < *j)?-1:0 ));
  159. }
  160. #endif
  161. // VBat_Serve()
  162. // VBat: Battery Voltage sensing measurement
  163. // Implements the battery voltage sensing using periodical measurements.
  164. // Take a look on the "picture":
  165. //
  166. // |<--------- T ---------->|
  167. // | |
  168. // *------------------------|-|-|---------------------------|-|-|------------....--> time
  169. // ^ Startup Take several measurements Take several measurements
  170. // Function takes several measurements each interval T (@VBatMontor_Interval) - package of measurements
  171. // During performing these several measurements the function peforms median filtering.
  172. // Variable @VBatVoltageAvgIndex is responsible for indexing each measurement in the package.
  173. static bool VBat_Serve()
  174. {
  175. bool bServe = false;
  176. bool bUpdated = false;
  177. // Is averaging enabled?
  178. #if VBAT_AVERAGING_POINTS > 0
  179. // averaging enabled
  180. __DI__
  181. #if VBAT_INIT_FOR_MEASURE == 0
  182. if( bVBATInitialized )
  183. {
  184. #endif
  185. // Averaging is active if VBatVoltageAvgIndex>0 and VBatVoltageAvgIndex <= VBAT_AVERAGING_POINTS
  186. if( (VBatVoltageAvgIndex > 0) && (VBatVoltageAvgIndex <= VBAT_AVERAGING_POINTS) )
  187. {
  188. // averaging interval
  189. // Is @VBAT_AVERAGING_INTERVAL timeout ran out?
  190. bServe = (HAL_GetTick() - VBatMontor_LastTick > VBAT_AVERAGING_INTERVAL );
  191. }
  192. else
  193. {
  194. // measurement interval (control interval)
  195. // Is @VBatMontor_LastTick timeout ran out?
  196. bServe = (HAL_GetTick() - VBatMontor_LastTick > VBatMontor_Interval);
  197. }
  198. #if VBAT_INIT_FOR_MEASURE == 0
  199. }
  200. #endif
  201. __EI__
  202. #else
  203. // averaging disabled
  204. #if VBAT_INIT_FOR_MEASURE
  205. __DI__ bServe = (HAL_GetTick() - VBatMontor_LastTick > VBatMontor_Interval); __EI__
  206. #else
  207. __DI__ bServe = bVBATInitialized && (HAL_GetTick() - VBatMontor_LastTick > VBatMontor_Interval); __EI__
  208. #endif
  209. #endif
  210. if( bServe )
  211. {
  212. #if VBAT_INIT_FOR_MEASURE
  213. if( VBat_Init() )
  214. {
  215. #endif
  216. VBat_IC_Enable();
  217. bool bResult = false;
  218. uint32_t xVDDAVoltage = 0;
  219. uint32_t xVBatVoltage = 0;
  220. uint32_t xVDDAVoltage_ADC = VBAT_INVALID_VALUE;
  221. uint32_t xVBatVoltage_ADC = VBAT_INVALID_VALUE;
  222. // To measure actual voltage it is required
  223. // to measure actual power supply voltage using ADC_CHANNEL_VREFINT
  224. if( pADCHandle->Start( ADC_CHANNEL_VREFINT ) )
  225. {
  226. if( pADCHandle->Measure( VBAT_MONITOR_MEAS_TIMEOUT, &xVDDAVoltage_ADC ) )
  227. {
  228. if( pADCHandle->Start( CONFIG_ADC__VBAT_ADC ) )
  229. {
  230. if( pADCHandle->Measure( VBAT_MONITOR_MEAS_TIMEOUT, &xVBatVoltage_ADC ) )
  231. {
  232. // Calculate VDDA voltage first
  233. xVDDAVoltage = (ADC_VREF_VOLTAGE_mV * ADC_GetFullScale( VBAT_ADC_RESOLUTION ) / xVDDAVoltage_ADC);
  234. // Find out the target voltage using VDDA:
  235. xVBatVoltage = xVDDAVoltage * xVBatVoltage_ADC / ADC_GetFullScale( VBAT_ADC_RESOLUTION );
  236. bResult = true;
  237. }
  238. }
  239. }
  240. }
  241. __DI__
  242. #if VBAT_AVERAGING_POINTS > 0
  243. #if VBAT_AVERAGING_POINTS < 3
  244. #error Invalid VBAT_AVERAGING_POINTS value: the value greater than 2 is required for meadian filter
  245. #endif
  246. #if VBAT_AVERAGING_POINTS % 2 == 0
  247. #error Invalid VBAT_AVERAGING_POINTS value: an odd value is required for meadian filter
  248. #endif
  249. if( bResult )
  250. {
  251. // The program reach this point due to the following reasons:
  252. // - in interval mode: @VBatMontor_Interval timeout expires;
  253. // so it is required to activate averaging mode and collect samples.
  254. // - in averaging mode: @VBAT_AVERAGING_INTERVAL timeout expires;
  255. // so it is required to collect current sample and wait for the next one.
  256. if( VBatVoltageAvgIndex == 0 )
  257. {
  258. // so, it is required to switch to averaging mode if it is not active
  259. VBatVoltageAvgIndex = 1;
  260. }
  261. // update VDDA voltage without averaging
  262. g_VDDAVoltage = xVDDAVoltage;
  263. // collecting samples
  264. aVBatVoltage_Avg[ VBatVoltageAvgIndex - 1 ] = xVBatVoltage;
  265. // take the sample in account, shift to the next filter cell
  266. VBatVoltageAvgIndex++;
  267. // are filter cells full?
  268. if( VBatVoltageAvgIndex > VBAT_AVERAGING_POINTS )
  269. {
  270. VBatVoltageAvgIndex = 0; // disable averaging, switch to the interval mode
  271. bUpdated = true;
  272. #if VBAT_AVERAGING_POINTS == 3
  273. g_VBatVoltage = middle_of_3( aVBatVoltage_Avg[0], aVBatVoltage_Avg[1], aVBatVoltage_Avg[2] );
  274. #elif VBAT_AVERAGING_POINTS > 3
  275. #warning Uneffective sorting, you sould use VBAT_AVERAGING_POINTS = 3
  276. qsort( aVBatVoltage_Avg,
  277. VBAT_AVERAGING_POINTS,
  278. sizeof(aVBatVoltage_Avg[0]),
  279. (int(*) (const void *, const void *))qsort_compare );
  280. g_VBatVoltage = aVBatVoltage_Avg[ ((VBAT_AVERAGING_POINTS - 1U) / 2U) ];
  281. #else
  282. #error Invalid VBAT_AVERAGING_POINTS value
  283. #endif
  284. }
  285. }
  286. else
  287. {
  288. g_VDDAVoltage = VBAT_INVALID_VALUE;
  289. g_VBatVoltage = VBAT_INVALID_VALUE;
  290. }
  291. #else
  292. if( bResult )
  293. {
  294. g_VDDAVoltage = xVDDAVoltage;
  295. g_VBatVoltage = xVBatVoltage;
  296. bUpdated = true;
  297. }
  298. else
  299. {
  300. g_VDDAVoltage = VBAT_INVALID_VALUE;
  301. g_VBatVoltage = VBAT_INVALID_VALUE;
  302. }
  303. #endif
  304. VBatMontor_LastTick = HAL_GetTick();
  305. __EI__
  306. VBat_IC_Disable();
  307. #if VBAT_INIT_FOR_MEASURE
  308. VBat_DeInit();
  309. }
  310. else
  311. {
  312. __DI__ g_VBatVoltage = VBAT_INVALID_VALUE; __EI__
  313. }
  314. #endif
  315. }
  316. (void)g_VDDAVoltage;
  317. return bUpdated;
  318. }
  319. static uint32_t VBat_GetVoltage()
  320. {
  321. uint32_t xValue;
  322. #if VBAT_VOLTAGE_MULTIPLIER > 0 || VBAT_VOLTAGE_DIVIDER > 0
  323. #if VBAT_VOLTAGE_MULTIPLIER == 2 && VBAT_VOLTAGE_DIVIDER < 2
  324. __DI__ xValue = (g_VBatVoltage << 1); __EI__
  325. #else
  326. __DI__ xValue = g_VBatVoltage; __EI__
  327. #if VBAT_VOLTAGE_MULTIPLIER > 0
  328. xValue *= VBAT_VOLTAGE_MULTIPLIER;
  329. #endif
  330. #if VBAT_VOLTAGE_DIVIDER > 0
  331. xValue /= VBAT_VOLTAGE_DIVIDER;
  332. #endif
  333. #endif
  334. #else
  335. __DI__ xValue = g_VBatVoltage; __EI__
  336. #endif
  337. return xValue;
  338. }