1 /*
2 * Copyright (c) 2022 Hunan OpenValley Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <string.h>
17 #include <stdlib.h>
18 #include "esp_osal.h"
19 #include "task.h"
20 #include "queue.h"
21 #include "timers.h"
22 #include "los_config.h"
23 #include "los_task.h"
24 #include "los_debug.h"
25 #include "los_queue.h"
26 #include "los_memory.h"
27 #include "esp_osal/event_groups.h"
28 #include "esp_rom_uart.h"
29 #include "xtensa_rtos.h"
30 #include "esp_log.h"
31 #include "nvs_flash.h"
32 #include <errno.h>
33 #include "hal/uart_ll.h"
34
35 extern UINT64 OsTickCount;
36 // =================================== 调试相关
37 extern BOOL g_taskScheduled;
38 static UINT32 s_LogMuxHandle = 0;
39 extern UINT16 g_losTaskLock;
s_vprintf(const char * fmt,va_list ap)40 static void s_vprintf(const char* fmt, va_list ap)
41 {
42 int len;
43 uint8_t taskLock;
44 static char buf[2][128 * 3];
45 char* pbuf;
46 if (xPortInterruptedFromISRContext() || g_losTaskLock || (!g_taskScheduled))
47 {
48 taskLock = 1;
49 pbuf = buf[1];
50 }
51 else
52 {
53 taskLock = 0;
54 pbuf = buf[0];
55 }
56 if (!taskLock)
57 {
58 while (!s_LogMuxHandle)
59 {
60 LOS_MuxCreate(&s_LogMuxHandle);
61 LOS_TaskDelay(1);
62 }
63 LOS_MuxPend(s_LogMuxHandle, LOS_WAIT_FOREVER);
64 }
65 len = vsnprintf(pbuf, sizeof(buf[0]) - 4, fmt, ap);
66 if (len > 0)
67 {
68 uint16_t fill_len;
69 for (fill_len = uart_ll_get_txfifo_len(&UART0); fill_len < len;)
70 {
71 if (fill_len)
72 {
73 uart_ll_write_txfifo(&UART0, (uint8_t*)pbuf, fill_len);
74 len -= fill_len;
75 pbuf += fill_len;
76 }
77 if (!taskLock)
78 LOS_TaskDelay(1); // LOS_TaskYield();
79 fill_len = uart_ll_get_txfifo_len(&UART0);
80 }
81 if (len > 0)
82 uart_ll_write_txfifo(&UART0, (uint8_t*)pbuf, len);
83 // while(!uart_ll_is_tx_idle(&UART0))LOS_TaskYield();
84 }
85 if (!taskLock)
86 LOS_MuxPost(s_LogMuxHandle);
87 }
88
89 // 获取当前滴答时间
xTaskGetTickCount(void)90 TickType_t IRAM_ATTR xTaskGetTickCount(void)
91 {
92 return (TickType_t)OsTickCount;
93 }
94
95 // 获取当前滴答时间(中断内)
xTaskGetTickCountFromISR(void)96 TickType_t IRAM_ATTR xTaskGetTickCountFromISR(void)
97 {
98 return (TickType_t)OsTickCount;
99 }
100
101 #ifndef MALLOC_CAP_8BIT
102 #define MALLOC_CAP_8BIT (1 << 2)
103 void* heap_caps_malloc(size_t size, uint32_t caps);
104 void heap_caps_free(void* ptr);
105 void* heap_caps_aligned_alloc(size_t alignment, size_t size, uint32_t caps);
106 #endif
107
__wrap_OsMemSystemInit(VOID)108 UINT32 __wrap_OsMemSystemInit(VOID)
109 {
110 return LOS_OK;
111 }
112
__wrap_LOS_MemAlloc(VOID * pool,UINT32 size)113 IRAM_ATTR VOID* __wrap_LOS_MemAlloc(VOID* pool, UINT32 size)
114 {
115 return heap_caps_malloc(size, MALLOC_CAP_8BIT);
116 }
117
__wrap_LOS_MemFree(VOID * pool,VOID * ptr)118 IRAM_ATTR UINT32 __wrap_LOS_MemFree(VOID* pool, VOID* ptr)
119 {
120 if (!ptr) {
121 return LOS_NOK;
122 }
123 else {
124 heap_caps_free(ptr);
125 return LOS_OK;
126 }
127 }
128
__wrap_LOS_MemAllocAlign(VOID * pool,UINT32 size,UINT32 boundary)129 IRAM_ATTR VOID* __wrap_LOS_MemAllocAlign(VOID* pool, UINT32 size, UINT32 boundary)
130 {
131 return heap_caps_aligned_alloc(boundary, size, MALLOC_CAP_8BIT);
132 }
133