os_wrapper.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2015-2021 Espressif Systems (Shanghai) PTE LTD
  2. /* SPDX-License-Identifier: GPL-2.0 OR Apache-2.0 */
  3. #ifndef __OS_WRAPPER_H
  4. #define __OS_WRAPPER_H
  5. #include "os_header.h"
  6. #include "esp_task.h"
  7. #include <signal.h>
  8. #include <unistd.h>
  9. #include <sys/types.h>
  10. #include "mempool.h"
  11. //#include "esp_hosted_config.h"
  12. #include "esp_hosted_config.h"
  13. #include "hosted_os_adapter.h"
  14. #include "esp_timer.h"
  15. #include "esp_event.h"
  16. #include "esp_netif_types.h"
  17. #include "esp_wifi_types.h"
  18. #include "esp_wifi_default.h"
  19. ESP_EVENT_DECLARE_BASE(WIFI_EVENT);
  20. #define MCU_SYS 1
  21. #include "common.h"
  22. #include "esp_dma_utils.h"
  23. #define MAX_PAYLOAD_SIZE (MAX_TRANSPORT_BUFFER_SIZE-H_ESP_PAYLOAD_HEADER_OFFSET)
  24. #define RPC__TIMER_ONESHOT 0
  25. #define RPC__TIMER_PERIODIC 1
  26. #define HOSTED_BLOCKING -1
  27. #define HOSTED_NON_BLOCKING 0
  28. #define thread_handle_t TaskHandle_t
  29. #define queue_handle_t QueueHandle_t
  30. #define semaphore_handle_t SemaphoreHandle_t
  31. #define mutex_handle_t SemaphoreHandle_t
  32. #define spinlock_handle_t portMUX_TYPE
  33. #define gpio_port_handle_t (void*)
  34. #define FAST_RAM_ATTR IRAM_ATTR
  35. /* this is needed when there is no gpio port being used */
  36. #define H_GPIO_PORT_DEFAULT -1
  37. #define gpio_pin_state_t int
  38. #define HOSTED_BLOCK_MAX portMAX_DELAY
  39. #define RPC_TASK_STACK_SIZE (5*1024)
  40. #define RPC_TASK_PRIO 23
  41. #define DFLT_TASK_STACK_SIZE (5*1024)
  42. #define DFLT_TASK_PRIO 23
  43. #define H_GPIO_MODE_DEF_DISABLE (0)
  44. #define H_GPIO_MODE_DEF_INPUT (BIT0) ///< bit mask for input
  45. #define H_GPIO_MODE_DEF_OUTPUT (BIT1) ///< bit mask for output
  46. #define H_GPIO_MODE_DEF_OD (BIT2) ///< bit mask for OD mode
  47. enum {
  48. H_GPIO_MODE_DISABLE = H_GPIO_MODE_DEF_DISABLE, /*!< GPIO mode : disable input and output */
  49. H_GPIO_MODE_INPUT = H_GPIO_MODE_DEF_INPUT, /*!< GPIO mode : input only */
  50. H_GPIO_MODE_OUTPUT = H_GPIO_MODE_DEF_OUTPUT, /*!< GPIO mode : output only mode */
  51. H_GPIO_MODE_OUTPUT_OD = ((H_GPIO_MODE_DEF_OUTPUT) | (H_GPIO_MODE_DEF_OD)), /*!< GPIO mode : output only with open-drain mode */
  52. H_GPIO_MODE_INPUT_OUTPUT_OD = ((H_GPIO_MODE_DEF_INPUT) | (H_GPIO_MODE_DEF_OUTPUT) | (H_GPIO_MODE_DEF_OD)), /*!< GPIO mode : output and input with open-drain mode*/
  53. H_GPIO_MODE_INPUT_OUTPUT = ((H_GPIO_MODE_DEF_INPUT) | (H_GPIO_MODE_DEF_OUTPUT)), /*!< GPIO mode : output and input mode */
  54. };
  55. #if 0
  56. #if 0 //defined OS_CMSIS
  57. #define thread_handle_t osThreadId
  58. #define osThreadId osThreadId
  59. #define semaphore_handle_t osSemaphoreId
  60. #define mutex_handle_t osMutexId
  61. #else //if defined OS_FREERTOS
  62. #define thread_handle_t TaskHandle_t
  63. #define queue_handle_t QueueHandle_t
  64. #define semaphore_handle_t SemaphoreHandle_t
  65. #define mutex_handle_t SemaphoreHandle_t
  66. #define osDelay vTaskDelay
  67. //#else
  68. //#error "Port the os calls to your system"
  69. #endif
  70. #endif
  71. #define RET_OK 0
  72. #define RET_FAIL -1
  73. #define RET_INVALID -2
  74. #define RET_FAIL_MEM -3
  75. #define RET_FAIL4 -4
  76. #define RET_FAIL_TIMEOUT -5
  77. /* without alignment */
  78. #define MALLOC(x) malloc(x)
  79. /* This is [malloc + aligned DMA] */
  80. #define MEM_ALLOC(x) ({ \
  81. esp_dma_mem_info_t dma_mem_info = { \
  82. .extra_heap_caps = 0, \
  83. .dma_alignment_bytes = 64, \
  84. }; \
  85. void *tmp_buf = NULL; \
  86. size_t actual_size = 0; \
  87. esp_err_t err = ESP_OK; \
  88. err = esp_dma_capable_malloc((x), &dma_mem_info, &tmp_buf, &actual_size);\
  89. if (err) tmp_buf = NULL; \
  90. tmp_buf;})
  91. #define FREE(x) free(x);
  92. /** Enumeration **/
  93. enum hardware_type_e {
  94. HARDWARE_TYPE_ESP32,
  95. HARDWARE_TYPE_OTHER_ESP_CHIPSETS,
  96. HARDWARE_TYPE_INVALID,
  97. };
  98. //TODO: redesign common code over
  99. #define MILLISEC_TO_SEC 1000
  100. #define TICKS_PER_SEC(x) (1000*(x) / portTICK_PERIOD_MS)
  101. #define SEC_TO_MILLISEC(x) (1000*(x))
  102. #define SEC_TO_MICROSEC(x) (1000*1000*(x))
  103. #define MEM_DUMP(s) \
  104. printf("%s free:%lu min-free:%lu lfb-def:%u lfb-8bit:%u\n\n", s, \
  105. (unsigned long int)esp_get_free_heap_size(), (unsigned long int)esp_get_minimum_free_heap_size(), \
  106. heap_caps_get_largest_free_block(MALLOC_CAP_DEFAULT), \
  107. heap_caps_get_largest_free_block(MALLOC_CAP_8BIT))
  108. #if 0
  109. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0)
  110. #define ESP_MUTEX_INIT(mUtEx) portMUX_INITIALIZE(&(mUtEx));
  111. #else
  112. #define ESP_MUTEX_INIT(mUtEx) vPortCPUInitializeMutex(&(mUtEx));
  113. #endif
  114. #endif
  115. /* -------- Create handle ------- */
  116. #define HOSTED_CREATE_HANDLE(tYPE, hANDLE) { \
  117. hANDLE = (tYPE *)g_h.funcs->_h_malloc(sizeof(tYPE)); \
  118. if (!hANDLE) { \
  119. printf("%s:%u Mem alloc fail while create handle\n", __func__,__LINE__); \
  120. return NULL; \
  121. } \
  122. }
  123. /* -------- Calloc, Free handle ------- */
  124. #define HOSTED_FREE(buff) if (buff) { g_h.funcs->_h_free(buff); buff = NULL; }
  125. #define HOSTED_CALLOC(struct_name, buff, nbytes, gotosym) do { \
  126. buff = (struct_name *)g_h.funcs->_h_calloc(1, nbytes); \
  127. if (!buff) { \
  128. printf("%s, Failed to allocate memory \n", __func__); \
  129. goto gotosym; \
  130. } \
  131. } while(0);
  132. #define HOSTED_MALLOC(struct_name, buff, nbytes, gotosym) do { \
  133. buff = (struct_name *)g_h.funcs->_h_malloc(nbytes); \
  134. if (!buff) { \
  135. printf("%s, Failed to allocate memory \n", __func__); \
  136. goto gotosym; \
  137. } \
  138. } while(0);
  139. /* Driver Handle */
  140. struct serial_drv_handle_t;
  141. /* Timer handle */
  142. struct timer_handle_t;
  143. extern struct mempool * nw_mp_g;
  144. #endif /*__OS_WRAPPER_H*/