common.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #pragma once
  2. #define _GNU_SOURCE 1
  3. #include "compiler.h"
  4. /* Standard C headers */
  5. #include <inttypes.h>
  6. #include <stdarg.h>
  7. #include <stdbool.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. /* Arduino headers */
  12. #include <Arduino.h>
  13. #include <FreeRTOS.h>
  14. /* ESP-IDF headers */
  15. #include <sdkconfig.h>
  16. #include <esp_err.h>
  17. #include <esp_event.h>
  18. #include <esp_log.h>
  19. #include "xmalloc.h"
  20. #ifndef MODULE
  21. # define MODULE ""
  22. #endif
  23. #if DEBUG
  24. # define CMSG(...) printf(__VA_ARGS__)
  25. #else
  26. # define CMSG(...) ((void)0)
  27. #endif
  28. #define MSG(...) CMSG(MODULE ": " __VA_ARGS__)
  29. /*
  30. * Hack to deal with the lack of this information in FreeRTOS proper
  31. */
  32. #define EVENT_BITS (configUSE_16_BIT_TICKS ? 8 : 24)
  33. #define EVENT_ALL_BITS ((1U << EVENT_BITS)-1)
  34. /*
  35. * Common types for callbacks
  36. */
  37. typedef void *token_t;
  38. typedef int (*read_func_t)(token_t token, void *buf, size_t len);
  39. typedef int (*write_func_t)(token_t token, const void *buf, size_t len);
  40. /*
  41. * Bad SPIRAM memory?
  42. */
  43. extern_c bool _spiram_broken;
  44. static inline bool spiram_broken(void)
  45. {
  46. return _spiram_broken;
  47. }
  48. /*
  49. * Sleep thread...
  50. */
  51. static inline void suspend(void)
  52. {
  53. vTaskSuspend(NULL);
  54. }
  55. /*
  56. * Kill thread; vTaskDelete(NULL) *should* never return...
  57. */
  58. static inline no_return exit_task(void)
  59. {
  60. vTaskDelete(NULL);
  61. while (1) {
  62. /* vTaskDelete() returned!? */
  63. vTaskSuspend(NULL);
  64. }
  65. }
  66. /*
  67. * Convert a NULL pointer string to ""
  68. */
  69. static inline const char *notempty(const char *str)
  70. {
  71. return str ? str : "";
  72. }
  73. static inline char *dupstr(const char *str)
  74. {
  75. return strdup(notempty(str));
  76. }
  77. /*
  78. * Reboot system
  79. */
  80. extern_c void reboot_now(void);
  81. extern_c int reboot_delayed(void);
  82. /*
  83. * Main MAC address from efuses
  84. */
  85. extern_c uint8_t efuse_default_mac[6];
  86. extern_c char serial_number[16];
  87. /*
  88. * Time sync status
  89. */
  90. extern_c volatile bool time_net_sync_status;
  91. struct timeval;
  92. extern_c void time_net_sync(const struct timeval *tv);
  93. extern_c void print_time(const char *msg, const struct timeval *tv);
  94. /*
  95. * Log output
  96. */
  97. extern_c volatile bool do_log_config_status;
  98. extern_c void __fmt_printf(2,3)
  99. logmsg(const char *module, const char *fmt, ...);
  100. extern_c void heap_info(void);