monitor.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 "monitor.h"
  16. #include "driver/gpio.h"
  17. #include "buttons.h"
  18. #include "led.h"
  19. #include "globdefs.h"
  20. #include "config.h"
  21. #include "accessors.h"
  22. #define MONITOR_TIMER (10*1000)
  23. #define SCRATCH_SIZE 256
  24. static const char *TAG = "monitor";
  25. static TimerHandle_t monitor_timer;
  26. static struct {
  27. int gpio;
  28. int active;
  29. } jack = { CONFIG_JACK_GPIO, 0 },
  30. spkfault = { CONFIG_SPKFAULT_GPIO, 0 };
  31. void (*jack_handler_svc)(bool inserted);
  32. bool jack_inserted_svc(void);
  33. void (*spkfault_handler_svc)(bool inserted);
  34. bool spkfault_svc(void);
  35. /****************************************************************************************
  36. *
  37. */
  38. static void task_stats( void ) {
  39. #ifdef CONFIG_FREERTOS_USE_TRACE_FACILITY
  40. static struct {
  41. TaskStatus_t *tasks;
  42. uint32_t total, n;
  43. } current, previous;
  44. current.n = uxTaskGetNumberOfTasks();
  45. current.tasks = malloc( current.n * sizeof( TaskStatus_t ) );
  46. current.n = uxTaskGetSystemState( current.tasks, current.n, &current.total );
  47. static EXT_RAM_ATTR char scratch[SCRATCH_SIZE];
  48. *scratch = '\0';
  49. #ifdef CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
  50. uint32_t elapsed = current.total - previous.total;
  51. for(int i = 0, n = 0; i < current.n; i++ ) {
  52. for (int j = 0; j < previous.n; j++) {
  53. if (current.tasks[i].xTaskNumber == previous.tasks[j].xTaskNumber) {
  54. n += snprintf(scratch + n, SCRATCH_SIZE - n, "%16s (%u) %2u%% s:%5u", current.tasks[i].pcTaskName,
  55. current.tasks[i].eCurrentState,
  56. 100 * (current.tasks[i].ulRunTimeCounter - previous.tasks[j].ulRunTimeCounter) / elapsed,
  57. current.tasks[i].usStackHighWaterMark);
  58. if (i % 3 == 2 || i == current.n - 1) {
  59. ESP_LOGI(TAG, "%s", scratch);
  60. n = 0;
  61. }
  62. break;
  63. }
  64. }
  65. }
  66. #else
  67. for (int i = 0, n = 0; i < current.n; i ++) {
  68. n += sprintf(scratch + n, "%16s s:%5u\t", current.tasks[i].pcTaskName, current.tasks[i].usStackHighWaterMark);
  69. if (i % 3 == 2 || i == current.n - 1) {
  70. ESP_LOGI(TAG, "%s", scratch);
  71. n = 0;
  72. }
  73. }
  74. #endif
  75. if (previous.tasks) free(previous.tasks);
  76. previous = current;
  77. #endif
  78. }
  79. /****************************************************************************************
  80. *
  81. */
  82. static void monitor_callback(TimerHandle_t xTimer) {
  83. ESP_LOGI(TAG, "Heap internal:%zu (min:%zu) external:%zu (min:%zu)",
  84. heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  85. heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  86. heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
  87. heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM));
  88. task_stats();
  89. }
  90. /****************************************************************************************
  91. *
  92. */
  93. static void jack_handler_default(void *id, button_event_e event, button_press_e mode, bool long_press) {
  94. ESP_LOGD(TAG, "Jack %s", event == BUTTON_PRESSED ? "inserted" : "removed");
  95. if (jack_handler_svc) (*jack_handler_svc)(event == BUTTON_PRESSED);
  96. }
  97. /****************************************************************************************
  98. *
  99. */
  100. bool jack_inserted_svc (void) {
  101. if (jack.gpio != -1) return button_is_pressed(jack.gpio, NULL);
  102. else return true;
  103. }
  104. /****************************************************************************************
  105. *
  106. */
  107. static void spkfault_handler_default(void *id, button_event_e event, button_press_e mode, bool long_press) {
  108. ESP_LOGD(TAG, "Speaker status %s", event == BUTTON_PRESSED ? "faulty" : "normal");
  109. if (event == BUTTON_PRESSED) led_on(LED_RED);
  110. else led_off(LED_RED);
  111. if (spkfault_handler_svc) (*spkfault_handler_svc)(event == BUTTON_PRESSED);
  112. }
  113. /****************************************************************************************
  114. *
  115. */
  116. bool spkfault_svc (void) {
  117. return button_is_pressed(spkfault.gpio, NULL);
  118. }
  119. /****************************************************************************************
  120. *
  121. */
  122. #ifndef CONFIG_JACK_LOCKED
  123. static void set_jack_gpio(int gpio, char *value) {
  124. if (strcasestr(value, "jack")) {
  125. char *p;
  126. jack.gpio = gpio;
  127. if ((p = strchr(value, ':')) != NULL) jack.active = atoi(p + 1);
  128. }
  129. }
  130. #endif
  131. /****************************************************************************************
  132. *
  133. */
  134. #ifndef CONFIG_SPKFAULT_LOCKED
  135. static void set_spkfault_gpio(int gpio, char *value) {
  136. if (strcasestr(value, "spkfault")) {
  137. char *p;
  138. spkfault.gpio = gpio;
  139. if ((p = strchr(value, ':')) != NULL) spkfault.active = atoi(p + 1);
  140. }
  141. }
  142. #endif
  143. /****************************************************************************************
  144. *
  145. */
  146. void monitor_svc_init(void) {
  147. ESP_LOGI(TAG, "Initializing monitoring");
  148. #ifdef CONFIG_JACK_GPIO_LEVEL
  149. jack.active = CONFIG_JACK_GPIO_LEVEL;
  150. #endif
  151. #ifndef CONFIG_JACK_LOCKED
  152. parse_set_GPIO(set_jack_gpio);
  153. #endif
  154. // re-use button management for jack handler, it's a GPIO after all
  155. if (jack.gpio != -1) {
  156. ESP_LOGI(TAG,"Adding jack (%s) detection GPIO %d", jack.active ? "high" : "low", jack.gpio);
  157. button_create(NULL, jack.gpio, jack.active ? BUTTON_HIGH : BUTTON_LOW, false, 250, jack_handler_default, 0, -1);
  158. }
  159. #ifdef CONFIG_SPKFAULT_GPIO_LEVEL
  160. spkfault.active = CONFIG_SPKFAULT_GPIO_LEVEL;
  161. #endif
  162. #ifndef CONFIG_SPKFAULT_LOCKED
  163. parse_set_GPIO(set_spkfault_gpio);
  164. #endif
  165. // re-use button management for speaker fault handler, it's a GPIO after all
  166. if (spkfault.gpio != -1) {
  167. ESP_LOGI(TAG,"Adding speaker fault (%s) detection GPIO %d", spkfault.active ? "high" : "low", spkfault.gpio);
  168. button_create(NULL, spkfault.gpio, spkfault.active ? BUTTON_HIGH : BUTTON_LOW, false, 0, spkfault_handler_default, 0, -1);
  169. }
  170. // do we want stats
  171. char *p = config_alloc_get_default(NVS_TYPE_STR, "stats", "n", 0);
  172. if (p && (*p == '1' || *p == 'Y' || *p == 'y')) {
  173. monitor_timer = xTimerCreate("monitor", MONITOR_TIMER / portTICK_RATE_MS, pdTRUE, NULL, monitor_callback);
  174. xTimerStart(monitor_timer, portMAX_DELAY);
  175. }
  176. free(p);
  177. ESP_LOGI(TAG, "Heap internal:%zu (min:%zu) external:%zu (min:%zu)",
  178. heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  179. heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  180. heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
  181. heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM));
  182. }