battery.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "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;
  31. TimerHandle_t timer;
  32. } battery = {
  33. .channel = CONFIG_BAT_CHANNEL,
  34. .cells = 2,
  35. };
  36. /****************************************************************************************
  37. *
  38. */
  39. int battery_value_svc(void) {
  40. return battery.avg;
  41. }
  42. /****************************************************************************************
  43. *
  44. */
  45. uint8_t battery_level_svc(void) {
  46. // TODO: this is totally incorrect
  47. return battery.avg ? (battery.avg - (3.0 * battery.cells)) / ((4.2 - 3.0) * battery.cells) * 100 : 0;
  48. }
  49. /****************************************************************************************
  50. *
  51. */
  52. static void battery_callback(TimerHandle_t xTimer) {
  53. battery.sum += adc1_get_raw(battery.channel) * battery.scale / 4095.0;
  54. if (++battery.count == 30) {
  55. battery.avg = battery.sum / battery.count;
  56. battery.sum = battery.count = 0;
  57. ESP_LOGI(TAG, "Voltage %.2fV", battery.avg);
  58. }
  59. }
  60. /****************************************************************************************
  61. *
  62. */
  63. void battery_svc_init(void) {
  64. #ifdef CONFIG_BAT_SCALE
  65. battery.scale = atof(CONFIG_BAT_SCALE);
  66. #endif
  67. char *nvs_item = config_alloc_get_default(NVS_TYPE_STR, "bat_config", "n", 0);
  68. if (nvs_item) {
  69. char *p;
  70. #ifndef CONFIG_BAT_LOCKED
  71. if ((p = strcasestr(nvs_item, "channel")) != NULL) battery.channel = atoi(strchr(p, '=') + 1);
  72. if ((p = strcasestr(nvs_item, "scale")) != NULL) battery.scale = atof(strchr(p, '=') + 1);
  73. #endif
  74. if ((p = strcasestr(nvs_item, "cells")) != NULL) battery.cells = atof(strchr(p, '=') + 1);
  75. free(nvs_item);
  76. }
  77. if (battery.channel != -1) {
  78. adc1_config_width(ADC_WIDTH_BIT_12);
  79. adc1_config_channel_atten(battery.channel, ADC_ATTEN_DB_0);
  80. battery.avg = adc1_get_raw(battery.channel) * battery.scale / 4095.0;
  81. battery.timer = xTimerCreate("battery", BATTERY_TIMER / portTICK_RATE_MS, pdTRUE, NULL, battery_callback);
  82. xTimerStart(battery.timer, portMAX_DELAY);
  83. ESP_LOGI(TAG, "Battery measure channel: %u, scale %f, cells %u, avg %.2fV", battery.channel, battery.scale, battery.cells, battery.avg);
  84. } else {
  85. ESP_LOGI(TAG, "No battery");
  86. }
  87. }