• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
2 //
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 #include <stdint.h>
16 #include <time.h>
17 #include <sys/time.h>
18 #include "esp_osal/esp_osal.h"
19 #include "esp_osal/task.h"
20 #include "esp_osal/semphr.h"
21 #include "hal/cpu_hal.h" // for cpu_hal_get_cycle_count()
22 #include "esp_log.h"
23 #include "esp_log_private.h"
24 
25 
26 // Maximum time to wait for the mutex in a logging statement.
27 #define MAX_MUTEX_WAIT_MS 10
28 #define MAX_MUTEX_WAIT_TICKS ((MAX_MUTEX_WAIT_MS + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS)
29 
30 static SemaphoreHandle_t s_log_mutex = NULL;
31 
esp_log_impl_lock(void)32 void esp_log_impl_lock(void)
33 {
34     if (!s_log_mutex) {
35         s_log_mutex = xSemaphoreCreateMutex();
36     }
37     xSemaphoreTake(s_log_mutex, portMAX_DELAY);
38 }
39 
esp_log_impl_lock_timeout(void)40 bool esp_log_impl_lock_timeout(void)
41 {
42     if (!s_log_mutex) {
43         s_log_mutex = xSemaphoreCreateMutex();
44     }
45     return xSemaphoreTake(s_log_mutex, MAX_MUTEX_WAIT_TICKS) == pdTRUE;
46 }
47 
esp_log_impl_unlock(void)48 void esp_log_impl_unlock(void)
49 {
50     xSemaphoreGive(s_log_mutex);
51 }
52 
esp_log_system_timestamp(void)53 char *esp_log_system_timestamp(void)
54 {
55     static char buffer[18] = {0};
56     static _lock_t bufferLock = 0;
57 
58     if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
59         uint32_t timestamp = esp_log_early_timestamp();
60         for (uint8_t i = 0; i < sizeof(buffer); i++) {
61             if ((timestamp > 0) || (i == 0)) {
62                 for (uint8_t j = sizeof(buffer) - 1; j > 0; j--) {
63                     buffer[j] = buffer[j - 1];
64                 }
65                 buffer[0] = (char)(timestamp % 10) + '0';
66                 timestamp /= 10;
67             } else {
68                 buffer[i] = 0;
69                 break;
70             }
71         }
72         return buffer;
73     } else {
74         struct timeval tv;
75         struct tm timeinfo;
76 
77         gettimeofday(&tv, NULL);
78         localtime_r(&tv.tv_sec, &timeinfo);
79 
80         _lock_acquire(&bufferLock);
81         snprintf(buffer, sizeof(buffer),
82                  "%02d:%02d:%02d.%03ld",
83                  timeinfo.tm_hour,
84                  timeinfo.tm_min,
85                  timeinfo.tm_sec,
86                  tv.tv_usec / 1000);
87         _lock_release(&bufferLock);
88 
89         return buffer;
90     }
91 }
92 
esp_log_timestamp(void)93 uint32_t esp_log_timestamp(void)
94 {
95     if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
96         return esp_log_early_timestamp();
97     }
98     static uint32_t base = 0;
99     if (base == 0 && xPortGetCoreID() == 0) {
100         base = esp_log_early_timestamp();
101     }
102     TickType_t tick_count = xPortInIsrContext() ? xTaskGetTickCountFromISR() : xTaskGetTickCount();
103     return base + tick_count * (1000 / configTICK_RATE_HZ);
104 }
105 
106 /* FIXME: define an API for getting the timestamp in soc/hal IDF-2351 */
esp_log_early_timestamp(void)107 uint32_t esp_log_early_timestamp(void)
108 {
109 #if CONFIG_IDF_TARGET_ESP32
110     /* ESP32 ROM stores separate clock rate values for each CPU, but we want the PRO CPU value always */
111     extern uint32_t g_ticks_per_us_pro;
112     return cpu_hal_get_cycle_count() / (g_ticks_per_us_pro * 1000);
113 #else
114     return cpu_hal_get_cycle_count() / (ets_get_cpu_frequency() * 1000);
115 #endif
116 }
117