battery.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 = CONFIG_BAT_CHANNEL,
  34. .cells = 2,
  35. .attenuation = ADC_ATTEN_DB_0,
  36. };
  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. ESP_LOGI(TAG, "Voltage %.2fV", battery.avg);
  60. }
  61. }
  62. /****************************************************************************************
  63. *
  64. */
  65. void battery_svc_init(void) {
  66. #ifdef CONFIG_BAT_SCALE
  67. battery.scale = atof(CONFIG_BAT_SCALE);
  68. #endif
  69. char *nvs_item = config_alloc_get_default(NVS_TYPE_STR, "bat_config", "n", 0);
  70. if (nvs_item) {
  71. char *p;
  72. #ifndef CONFIG_BAT_LOCKED
  73. if ((p = strcasestr(nvs_item, "channel")) != NULL) battery.channel = atoi(strchr(p, '=') + 1);
  74. if ((p = strcasestr(nvs_item, "scale")) != NULL) battery.scale = atof(strchr(p, '=') + 1);
  75. if ((p = strcasestr(nvs_item, "atten")) != NULL) battery.attenuation = atoi(strchr(p, '=') + 1);
  76. #endif
  77. if ((p = strcasestr(nvs_item, "cells")) != NULL) battery.cells = atof(strchr(p, '=') + 1);
  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, cells %u, avg %.2fV", battery.channel, battery.scale, battery.cells, battery.avg);
  87. } else {
  88. ESP_LOGI(TAG, "No battery");
  89. }
  90. }