battery.c 2.6 KB

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