battery.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/timers.h"
  13. #include "esp_system.h"
  14. #include "esp_log.h"
  15. #include "driver/adc.h"
  16. #include "battery.h"
  17. #include "platform_config.h"
  18. /*
  19. There is a bug in esp32 which causes a spurious interrupt on gpio 36/39 when
  20. using ADC, AMP and HALL sensor. Rather than making battery aware, we just ignore
  21. if as the interrupt lasts 80ns and should be debounced (and the ADC read does not
  22. happen very often)
  23. */
  24. #define BATTERY_TIMER (10*1000)
  25. static const char *TAG = "battery";
  26. static struct {
  27. int channel;
  28. float sum, avg, scale;
  29. int count;
  30. int cells, attenuation;
  31. TimerHandle_t timer;
  32. } battery = {
  33. .channel = -1,
  34. .cells = 2,
  35. };
  36. void (*battery_handler_svc)(float value, int cells);
  37. /****************************************************************************************
  38. *
  39. */
  40. float battery_value_svc(void) {
  41. return battery.avg;
  42. }
  43. /****************************************************************************************
  44. *
  45. */
  46. uint8_t battery_level_svc(void) {
  47. // TODO: this is vastly incorrect
  48. int level = battery.avg ? (battery.avg - (3.0 * battery.cells)) / ((4.2 - 3.0) * battery.cells) * 100 : 0;
  49. return level < 100 ? level : 100;
  50. }
  51. /****************************************************************************************
  52. *
  53. */
  54. static void battery_callback(TimerHandle_t xTimer) {
  55. battery.sum += adc1_get_raw(battery.channel) * battery.scale / 4095.0;
  56. if (++battery.count == 30) {
  57. battery.avg = battery.sum / battery.count;
  58. battery.sum = battery.count = 0;
  59. if (battery_handler_svc) (battery_handler_svc)(battery.avg, battery.cells);
  60. ESP_LOGI(TAG, "Voltage %.2fV", battery.avg);
  61. }
  62. }
  63. /****************************************************************************************
  64. *
  65. */
  66. void battery_svc_init(void) {
  67. char *nvs_item = config_alloc_get_default(NVS_TYPE_STR, "bat_config", "", 0);
  68. #ifdef CONFIG_BAT_LOCKED
  69. char *p = nvs_item;
  70. asprintf(&nvs_item, CONFIG_BAT_CONFIG ",%s", p);
  71. free(p);
  72. #endif
  73. if (nvs_item) {
  74. PARSE_PARAM(nvs_item, "channel", '=', battery.channel);
  75. PARSE_PARAM_FLOAT(nvs_item, "scale", '=', battery.scale);
  76. PARSE_PARAM(nvs_item, "atten", '=', battery.attenuation);
  77. PARSE_PARAM(nvs_item, "cells", '=', battery.cells);
  78. free(nvs_item);
  79. }
  80. if (battery.channel != -1) {
  81. adc1_config_width(ADC_WIDTH_BIT_12);
  82. adc1_config_channel_atten(battery.channel, battery.attenuation);
  83. battery.avg = adc1_get_raw(battery.channel) * battery.scale / 4095.0;
  84. battery.timer = xTimerCreate("battery", BATTERY_TIMER / portTICK_RATE_MS, pdTRUE, NULL, battery_callback);
  85. xTimerStart(battery.timer, portMAX_DELAY);
  86. ESP_LOGI(TAG, "Battery measure channel: %u, scale %f, atten %d, cells %u, avg %.2fV", battery.channel, battery.scale, battery.attenuation, battery.cells, battery.avg);
  87. } else {
  88. ESP_LOGI(TAG, "No battery");
  89. }
  90. }