common.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. * Sleep thread...
  42. */
  43. static inline void suspend(void)
  44. {
  45. vTaskSuspend(NULL);
  46. }
  47. /*
  48. * Kill thread; vTaskDelete(NULL) *should* never return...
  49. */
  50. static inline no_return exit_task(void)
  51. {
  52. vTaskDelete(NULL);
  53. while (1) {
  54. /* vTaskDelete() returned!? */
  55. vTaskSuspend(NULL);
  56. }
  57. }
  58. /*
  59. * Reboot system
  60. */
  61. extern_c void reboot_now(void);
  62. extern_c int reboot_delayed(void);
  63. /*
  64. * Main MAC address from efuses
  65. */
  66. extern_c uint8_t efuse_default_mac[6];
  67. extern_c char serial_number[16];
  68. /*
  69. * Time sync status
  70. */
  71. extern_c volatile bool time_net_sync_status;
  72. struct timeval;
  73. extern_c void time_net_sync(const struct timeval *tv);
  74. extern_c void print_time(const char *msg, const struct timeval *tv);