mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
docs: add cn trans for system_time
This commit is contained in:
parent
1f47466664
commit
4be8497391
@ -1,46 +1,56 @@
|
|||||||
System Time
|
System Time
|
||||||
===========
|
===========
|
||||||
|
:link_to_translation:`zh_CN:[中文]`
|
||||||
|
|
||||||
|
{IDF_TARGET_RTC_CLK_FRE:default="Not updated", esp32="150 kHz", esp32s2="90 kHz", esp32s3="136 kHz", esp32c3="136 kHz", esp32c2="136 kHz"}
|
||||||
|
{IDF_TARGET_INT_OSC_FRE:default="Not updated", esp32="8.5 MHz", esp32s2="8.5 MHz", esp32s3="17.5 MHz", esp32c3="17.5 MHz", esp32c2="17.5 MHz"}
|
||||||
|
{IDF_TARGET_INT_OSC_FRE_DIVIDED:default="Not updated", esp32="~33 kHz", esp32s2="~33 kHz", esp32s3="~68 kHz", esp32c3="~68 kHz", esp32c2="~68 kHz"}
|
||||||
|
{IDF_TARGET_EXT_CRYSTAL_PIN:default="Not updated", esp32="32K_XP and 32K_XN", esp32s2="XTAL_32K_P and XTAL_32K_N", esp32s3="XTAL_32K_P and XTAL_32K_N", esp32c3="XTAL_32K_P and XTAL_32K_N"}
|
||||||
|
{IDF_TARGET_EXT_OSC_PIN:default="Not updated", esp32="32K_XN", esp32s2="XTAL_32K_P", esp32s3="XTAL_32K_P", esp32c3="XTAL_32K_P", esp32c2="GPIO0"}
|
||||||
|
{IDF_TARGET_HARDWARE_DESIGN_URL:default="Not updated",esp32="`ESP32 Hardware Design Guidelines <https://www.espressif.com/sites/default/files/documentation/esp32_hardware_design_guidelines_en.pdf#page=11>`_", esp32s2="`ESP32-S2 Hardware Design Guidelines <https://www.espressif.com/sites/default/files/documentation/esp32-s2_hardware_design_guidelines_en.pdf#page=10>`_", esp32s3="`ESP32-S3 Hardware Design Guidelines <https://www.espressif.com/sites/default/files/documentation/esp32-s3_hardware_design_guidelines_en.pdf#page=11>`_", esp32c3="`ESP32-C3 Hardware Design Guidelines <https://www.espressif.com/sites/default/files/documentation/esp32-c3_hardware_design_guidelines_en.pdf#page=9>`_"}
|
||||||
|
|
||||||
{IDF_TARGET_RTC_CLK_FRE:default="150kHz", esp32s2="90kHz"}
|
|
||||||
{IDF_TARGET_HARDWARE_DESIGN_URL:default="`ESP32 Hardware Design Guidelines <https://www.espressif.com/sites/default/files/documentation/esp32_hardware_design_guidelines_en.pdf#page=10>`_", esp32="`ESP32 Hardware Design Guidelines <https://www.espressif.com/sites/default/files/documentation/esp32_hardware_design_guidelines_en.pdf#page=10>`_", esp32s2="`ESP32-S2 Hardware Design Guidelines <https://www.espressif.com/sites/default/files/documentation/esp32-s2_hardware_design_guidelines_en.pdf#page=10>`_", esp32s3="`ESP32-S3 Hardware Design Guidelines <https://www.espressif.com/sites/default/files/documentation/esp32-s3_hardware_design_guidelines_en.pdf#page=11>`"}
|
|
||||||
|
|
||||||
Overview
|
Overview
|
||||||
--------
|
--------
|
||||||
|
|
||||||
System time can be kept using either one time source or two time sources simultaneously. The choice depends on the application purpose and accuracy requirements for system time.
|
{IDF_TARGET_NAME} uses two hardware timers for the purpose of keeping system time. System time can be kept by using either one or both of the hardware timers depending on the application's purpose and accuracy requirements for system time. The two hardware timers are:
|
||||||
|
|
||||||
There are the following two time sources:
|
- **RTC timer**: This timer allows time keeping in various sleep modes, and can also persist time keeping across any resets (with the exception of power-on resets which reset the RTC timer). The frequency deviation depends on the `RTC Timer Clock Sources`_ and affects the accuracy only in sleep modes, in which case the time will be measured at 6.6667 μs resolution.
|
||||||
|
|
||||||
- **RTC timer**: Allows keeping the system time during any resets and sleep modes, only the power-up reset leads to resetting the RTC timer. The frequency deviation depends on an `RTC Clock Source`_ and affects accuracy only in sleep modes, in which case the time will be measured at 6.6667 us resolution.
|
- **High-resolution timer**: This timer is not available in sleep modes and will not persist over a reset, but has greater accuracy. The timer uses the APB_CLK clock source (typically 80 MHz), which has a frequency deviation of less than ±10 ppm. Time will be measured at 1 μs resolution.
|
||||||
|
|
||||||
- **High-resolution timer**: Not available during any reset and sleep modes. The reason for using this timer is to achieve greater accuracy. It uses the APB_CLK clock source (typically 80 MHz), which has a frequency deviation of less than ±10 ppm. Time will be measured at 1 us resolution.
|
The possible combinations of hardware timers used to keep system time are listed below:
|
||||||
|
|
||||||
The settings for the system time source are as follows:
|
|
||||||
|
|
||||||
- RTC and high-resolution timer (default)
|
- RTC and high-resolution timer (default)
|
||||||
- RTC
|
- RTC
|
||||||
- High-resolution timer
|
- High-resolution timer
|
||||||
- None
|
- None
|
||||||
|
|
||||||
It is recommended to stick to the default setting which provides maximum accuracy. If you want to choose a different timer, configure :ref:`CONFIG_NEWLIB_TIME_SYSCALL` in project configuration.
|
It is recommended that users stick to the default option as it provides the highest accuracy. However, users can also select a different setting via the :ref:`CONFIG_NEWLIB_TIME_SYSCALL` configuration option.
|
||||||
|
|
||||||
|
|
||||||
RTC Clock Source
|
RTC Timer Clock Sources
|
||||||
----------------
|
------------------------
|
||||||
|
|
||||||
The RTC timer has the following clock sources:
|
The RTC timer has the following clock sources:
|
||||||
|
|
||||||
- ``Internal {IDF_TARGET_RTC_CLK_FRE} RC oscillator`` (default): Features the lowest deep sleep current consumption and no dependence on any external components. However, as frequency stability is affected by temperature fluctuations, time may drift in both Deep and Light sleep modes.
|
.. list::
|
||||||
|
|
||||||
- ``External 32kHz crystal``: Requires a 32kHz crystal to be connected to the 32K_XP and 32K_XN pins. Provides better frequency stability at the expense of slightly higher (by 1 uA) Deep sleep current consumption.
|
- ``Internal {IDF_TARGET_RTC_CLK_FRE} RC oscillator`` (default): Features the lowest Deep-sleep current consumption and no dependence on any external components. However, the frequency stability of this clock source is affected by temperature fluctuations, so time may drift in both Deep-sleep and Light-sleep modes.
|
||||||
|
|
||||||
- ``External 32kHz oscillator at 32K_XN pin``: Allows using 32kHz clock generated by an external circuit. The external clock signal must be connected to the 32K_XN pin. The amplitude should be less than 1.2 V for sine wave signal and less than 1 V for square wave signal. Common mode voltage should be in the range of 0.1 < Vcm < 0.5xVamp, where Vamp is signal amplitude. Additionally, a 1 nF capacitor must be placed between the 32K_XP pin and ground. In this case, the 32K_XP pin cannot be used as a GPIO pin.
|
:not esp32c2: - ``External 32 kHz crystal``: Requires a 32 kHz crystal to be connected to the {IDF_TARGET_EXT_CRYSTAL_PIN} pins. This source provides a better frequency stability at the expense of a slightly higher (by 1 μA) Deep-sleep current consumption.
|
||||||
|
|
||||||
- ``Internal 8.5MHz oscillator, divided by 256 (~33kHz)``: Provides better frequency stability than the ``internal {IDF_TARGET_RTC_CLK_FRE} RC oscillator`` at the expense of higher (by 5 uA) deep sleep current consumption. It also does not require external components.
|
- ``External 32 kHz oscillator at {IDF_TARGET_EXT_OSC_PIN} pin``: Allows using 32 kHz clock generated by an external circuit. The external clock signal must be connected to the {IDF_TARGET_EXT_OSC_PIN} pin. The amplitude should be less than 1.2 V for sine wave signal and less than 1 V for square wave signal. Common mode voltage should be in the range of 0.1 < Vcm < 0.5xVamp, where Vamp stands for signal amplitude. In this case, the {IDF_TARGET_EXT_OSC_PIN} pin cannot be used as a GPIO pin.
|
||||||
|
|
||||||
|
- ``Internal {IDF_TARGET_INT_OSC_FRE} oscillator, divided by 256 ({IDF_TARGET_INT_OSC_FRE_DIVIDED})``: Provides better frequency stability than the ``Internal {IDF_TARGET_RTC_CLK_FRE} RC oscillator`` at the expense of a higher (by 5 μA) Deep-sleep current consumption. It also does not require external components.
|
||||||
|
|
||||||
The choice depends on your requirements for system time accuracy and power consumption in sleep modes. To modify the RTC clock source, set :ref:`CONFIG_RTC_CLK_SRC` in project configuration.
|
The choice depends on your requirements for system time accuracy and power consumption in sleep modes. To modify the RTC clock source, set :ref:`CONFIG_RTC_CLK_SRC` in project configuration.
|
||||||
|
|
||||||
More details on wiring requirements for the ``External 32kHz crystal`` and ``External 32kHz oscillator at 32K_XN pin`` sources can be found in Section *Crystal Oscillator* of {IDF_TARGET_HARDWARE_DESIGN_URL}.
|
.. Need to add esp32c2 hardware design guideline link after it is publsihed.
|
||||||
|
|
||||||
|
.. only:: not esp32c2
|
||||||
|
|
||||||
|
More details about the wiring requirements for the external crystal or external oscillator, please refer to {IDF_TARGET_HARDWARE_DESIGN_URL}.
|
||||||
|
|
||||||
|
|
||||||
Get Current Time
|
Get Current Time
|
||||||
@ -62,9 +72,9 @@ To get the current time, use the POSIX function ``gettimeofday()``. Additionally
|
|||||||
strftime
|
strftime
|
||||||
adjtime*
|
adjtime*
|
||||||
|
|
||||||
\* – To stop smooth time adjustment and update the current time immediately, use the POSIX function ``settimeofday()``.
|
To stop smooth time adjustment and update the current time immediately, use the POSIX function ``settimeofday()``.
|
||||||
|
|
||||||
If you need to obtain time with one second resolution, use the following method:
|
If you need to obtain time with one second resolution, use the following code snippet:
|
||||||
|
|
||||||
.. code-block:: c
|
.. code-block:: c
|
||||||
|
|
||||||
@ -96,17 +106,17 @@ SNTP Time Synchronization
|
|||||||
|
|
||||||
To set the current time, you can use the POSIX functions ``settimeofday()`` and ``adjtime()``. They are used internally in the lwIP SNTP library to set current time when a response from the NTP server is received. These functions can also be used separately from the lwIP SNTP library.
|
To set the current time, you can use the POSIX functions ``settimeofday()`` and ``adjtime()``. They are used internally in the lwIP SNTP library to set current time when a response from the NTP server is received. These functions can also be used separately from the lwIP SNTP library.
|
||||||
|
|
||||||
A function to use inside the lwIP SNTP library depends on a sync mode for system time. Use the function :cpp:func:`sntp_set_sync_mode` to set one of the following sync modes:
|
The function to use inside the lwIP SNTP library depends on the sync mode for system time. Use the function :cpp:func:`sntp_set_sync_mode` to set one of the following sync modes:
|
||||||
|
|
||||||
- ``SNTP_SYNC_MODE_IMMED`` (default) updates system time immediately upon receiving a response from the SNTP server after using ``settimeofday()``.
|
- :cpp:enumerator:`SNTP_SYNC_MODE_IMMED` (default): Updates system time immediately upon receiving a response from the SNTP server after using ``settimeofday()``.
|
||||||
- ``SNTP_SYNC_MODE_SMOOTH`` updates time smoothly by gradually reducing time error using the function ``adjtime()``. If the difference between the SNTP response time and system time is more than 35 minutes, update system time immediately by using ``settimeofday()``.
|
- :cpp:enumerator:`SNTP_SYNC_MODE_SMOOTH`: Updates time smoothly by gradually reducing time error using the function ``adjtime()``. If the difference between the SNTP response time and system time is more than 35 minutes, update system time immediately by using ``settimeofday()``.
|
||||||
|
|
||||||
The lwIP SNTP library has API functions for setting a callback function for a certain event. You might need the following functions:
|
The lwIP SNTP library has API functions for setting a callback function for a certain event. You might need the following functions:
|
||||||
|
|
||||||
- ``sntp_set_time_sync_notification_cb()`` - use it for setting a callback function that will notify of the time synchronization process
|
- :cpp:func:`sntp_set_time_sync_notification_cb()`: Can be used to set a callback function that will notify of the time synchronization process.
|
||||||
- ``sntp_get_sync_status()`` and ``sntp_set_sync_status()`` - use it to get/set time synchronization status
|
- :cpp:func:`sntp_get_sync_status()` and :cpp:func:`sntp_set_sync_status()`: Can be used to get/set time synchronization status.
|
||||||
|
|
||||||
To start synchronization via SNTP, just call the following three functions.
|
To start synchronization via SNTP, just call the following three functions:
|
||||||
|
|
||||||
.. code-block:: c
|
.. code-block:: c
|
||||||
|
|
||||||
@ -114,35 +124,35 @@ To start synchronization via SNTP, just call the following three functions.
|
|||||||
sntp_setservername(0, "pool.ntp.org");
|
sntp_setservername(0, "pool.ntp.org");
|
||||||
sntp_init();
|
sntp_init();
|
||||||
|
|
||||||
An application with this initialization code will periodically synchronize the time. The time synchronization period is determined by :ref:`CONFIG_LWIP_SNTP_UPDATE_DELAY` (default value is one hour). To modify the variable, set :ref:`CONFIG_LWIP_SNTP_UPDATE_DELAY` in project configuration.
|
An application with this initialization code will periodically synchronize the time. The time synchronization period is determined by :ref:`CONFIG_LWIP_SNTP_UPDATE_DELAY` (the default value is one hour). To modify the variable, set :ref:`CONFIG_LWIP_SNTP_UPDATE_DELAY` in project configuration.
|
||||||
|
|
||||||
A code example that demonstrates the implementation of time synchronization based on the lwIP SNTP library is provided in :example:`protocols/sntp` directory.
|
A code example that demonstrates the implementation of time synchronization based on the lwIP SNTP library is provided in the :example:`protocols/sntp` directory.
|
||||||
|
|
||||||
|
|
||||||
Timezones
|
Timezones
|
||||||
---------
|
---------
|
||||||
|
|
||||||
To set local timezone, use the following POSIX functions:
|
To set the local timezone, use the following POSIX functions:
|
||||||
|
|
||||||
1. Call ``setenv()`` to set the ``TZ`` environment variable to the correct value depending on the device location. The format of the time string is the same as described in the `GNU libc documentation <https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html>`_ (although the implementation is different).
|
1. Call ``setenv()`` to set the ``TZ`` environment variable to the correct value based on the device location. The format of the time string is the same as described in the `GNU libc documentation <https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html>`_ (although the implementation is different).
|
||||||
2. Call ``tzset()`` to update C library runtime data for the new time zone.
|
2. Call ``tzset()`` to update C library runtime data for the new timezone.
|
||||||
|
|
||||||
Once these steps are completed, call the standard C library function ``localtime()``, and it will return correct local time taking into account the time zone offset and daylight saving time.
|
Once these steps are completed, call the standard C library function ``localtime()``, and it will return the correct local time taking into account the timezone offset and daylight saving time.
|
||||||
|
|
||||||
|
|
||||||
64-bit ``time_t``
|
64-bit ``time_t``
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
ESP-IDF uses 32-bit ``time_t`` type by default. To address Y2K38 issue, you may need to use 64-bit ``time_t`` type when building the application.
|
ESP-IDF uses 32-bit ``time_t`` type by default. To address the Y2K38 issue, you may need to use 64-bit ``time_t`` type when building the application.
|
||||||
|
|
||||||
Currently this requires building the cross-compiler toolchain from scratch. See the instructions for building the toolchain in :doc:`/get-started/linux-macos-setup`. To enable 64-bit ``time_t`` support in the toolchain, you need to remove the ``--enable-newlib-long-time_t`` option from the ``crosstool-NG/samples/xtensa-esp32-elf/crosstool.config`` file before building the toolchain.
|
Currently, this requires building the cross-compiler toolchain from scratch. See the instructions for building the toolchain in :doc:`/get-started/linux-macos-setup`. To enable 64-bit ``time_t`` support in the toolchain, you need to remove the ``--enable-newlib-long-time_t`` option from the ``crosstool-NG/samples/xtensa-esp32-elf/crosstool.config`` file before building the toolchain.
|
||||||
|
|
||||||
If you need to make the program compatible with both 32-bit and 64-bit ``time_t``, you may use the following methods:
|
If you need to make the program compatible with both 32-bit and 64-bit ``time_t``, you may use the following methods:
|
||||||
|
|
||||||
- In C or C++ source files, ``_USE_LONG_TIME_T`` preprocessor macro will be defined if 32-bit ``time_t`` is used. You need to include ``<sys/types.h>`` to make this macro available.
|
- In C or C++ source files, ``_USE_LONG_TIME_T`` preprocessor macro will be defined if 32-bit ``time_t`` is used. You need to include ``<sys/types.h>`` to make this macro available.
|
||||||
- In CMake files, ``TIME_T_SIZE`` IDF build property will be set to the size of ``time_t``, in bytes. You may call ``idf_build_get_property(var TIME_T_SIZE)`` to get the value of this property into a CMake variable ``var``. See :ref:`build system API reference <cmake_buildsystem_api>` for more information about ``idf_build_get_property``.
|
- In CMake files, ``TIME_T_SIZE`` IDF build property will be set to the size of ``time_t`` in bytes. You may call ``idf_build_get_property(var TIME_T_SIZE)`` to get the value of this property into a CMake variable ``var``. See :ref:`ESP-IDF CMake Build System API <cmake_buildsystem_api>` for more information about ``idf_build_get_property``.
|
||||||
|
|
||||||
Note that the size of ``time_t`` type also affects the sizes of other types, for example ``struct timeval``, ``struct stat``, ``struct utimbuf``.
|
Note that the size of ``time_t`` type also affects the sizes of other types, for example, ``struct timeval``, ``struct stat``, and ``struct utimbuf``.
|
||||||
|
|
||||||
|
|
||||||
API Reference
|
API Reference
|
||||||
|
@ -1 +1,161 @@
|
|||||||
.. include:: ../../../en/api-reference/system/system_time.rst
|
系统时间
|
||||||
|
=========
|
||||||
|
:link_to_translation:`en:[English]`
|
||||||
|
|
||||||
|
{IDF_TARGET_RTC_CLK_FRE:default="未更新", esp32="150 kHz", esp32s2="90 kHz", esp32s3="136 kHz", esp32c3="136 kHz", esp32c2="136 kHz"}
|
||||||
|
{IDF_TARGET_INT_OSC_FRE:default="未更新", esp32="8.5 MHz", esp32s2="8.5 MHz", esp32s3="17.5 MHz", esp32c3="17.5 MHz", esp32c2="17.5 MHz"}
|
||||||
|
{IDF_TARGET_INT_OSC_FRE_DIVIDED:default="未更新", esp32="~33 kHz", esp32s2="~33 kHz", esp32s3="~68 kHz", esp32c3="~68 kHz", esp32c2="~68 kHz"}
|
||||||
|
{IDF_TARGET_EXT_CRYSTAL_PIN:default="未更新", esp32="32K_XP 和 32K_XN", esp32s2="XTAL_32K_P 和 XTAL_32K_N", esp32s3="XTAL_32K_P 和 XTAL_32K_N", esp32c3="XTAL_32K_P 和 XTAL_32K_N"}
|
||||||
|
{IDF_TARGET_EXT_OSC_PIN:default="未更新", esp32="32K_XN", esp32s2="XTAL_32K_P", esp32s3="XTAL_32K_P", esp32c3="XTAL_32K_P", esp32c2="GPIO0"}
|
||||||
|
{IDF_TARGET_HARDWARE_DESIGN_URL:default="未更新", esp32="`ESP32 硬件设计指南 <https://www.espressif.com/sites/default/files/documentation/esp32_hardware_design_guidelines_cn.pdf#page=10>`_", esp32s2="`ESP32-S2 硬件设计指南 <https://www.espressif.com/sites/default/files/documentation/esp32-s2_hardware_design_guidelines_cn.pdf#page=10>`_", esp32s3="`ESP32-S3 硬件设计指南 <https://www.espressif.com/sites/default/files/documentation/esp32-s3_hardware_design_guidelines_cn.pdf#page=12>`_", esp32c3="`ESP32-C3 硬件设计指南 <https://www.espressif.com/sites/default/files/documentation/esp32-c3_hardware_design_guidelines_cn.pdf#page=9>`_"}
|
||||||
|
|
||||||
|
|
||||||
|
概述
|
||||||
|
------
|
||||||
|
|
||||||
|
{IDF_TARGET_NAME} 使用两种硬件时钟源建立和保持系统时间。根据应用目的及对系统时间的精度要求,既可以仅使用其中一种时钟源,也可以同时使用两种时钟源。这两种硬件时钟源为:
|
||||||
|
|
||||||
|
- **RTC 定时器**:RTC 定时器在任何睡眠模式下及在任何复位后均可保持系统时间(上电复位除外,因为上电复位会重置 RTC 定时器)。时钟频率偏差取决于 `RTC 定时器时钟源`_,该偏差只会在睡眠模式下影响时间精度。睡眠模式下,时间分辨率为 6.667 μs。
|
||||||
|
|
||||||
|
- **高分辨率定时器**:高分辨率定时器在睡眠模式下及在复位后不可用,但其时间精度更高。该定时器使用 APB_CLK 时钟源(通常为 80 MHz),时钟频率偏差小于 ±10 ppm,时间分辨率为 1 μs。
|
||||||
|
|
||||||
|
可供选择的硬件时钟源组合如下所示:
|
||||||
|
|
||||||
|
- RTC 和高分辨率定时器(默认)
|
||||||
|
- RTC
|
||||||
|
- 高分辨率定时器
|
||||||
|
- 无
|
||||||
|
|
||||||
|
默认时钟源的时间精度最高,建议使用该配置。此外,用户也可以通过配置选项 :ref:`CONFIG_NEWLIB_TIME_SYSCALL` 来选择其他时钟源。
|
||||||
|
|
||||||
|
|
||||||
|
RTC 定时器时钟源
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
RTC 定时器有以下时钟源:
|
||||||
|
|
||||||
|
.. list::
|
||||||
|
|
||||||
|
- ``内置 {IDF_TARGET_RTC_CLK_FRE} RC 振荡器`` (默认):Deep-sleep 模式下电流消耗最低,不依赖任何外部元件。但由于温度波动会影响该时钟源的频率稳定性,在 Deep-sleep 和 Light-sleep 模式下都有可能发生时间偏移。
|
||||||
|
|
||||||
|
:not esp32c2: - ``外置 32 kHz 晶振``:需要将一个 32 kHz 晶振连接到 {IDF_TARGET_EXT_CRYSTAL_PIN} 管脚。频率稳定性更高,但在 Deep-sleep 模式下电流消耗略高(比默认模式高 1 μA)。
|
||||||
|
|
||||||
|
- ``管脚 {IDF_TARGET_EXT_OSC_PIN} 外置 32 kHz 振荡器``:允许使用由外部电路产生的 32 kHz 时钟。外部时钟信号必须连接到管脚 {IDF_TARGET_EXT_OSC_PIN}。正弦波信号的振幅应小于 1.2 V,方波信号的振幅应小于 1 V。正常模式下,电压范围应为 0.1 < Vcm < 0.5 xVamp,其中 Vamp 代表信号振幅。使用此时钟源时,管脚 {IDF_TARGET_EXT_OSC_PIN} 无法用作 GPIO 管脚。
|
||||||
|
|
||||||
|
- ``内置 {IDF_TARGET_INT_OSC_FRE} 振荡器的 256 分频时钟 ({IDF_TARGET_INT_OSC_FRE_DIVIDED})``:频率稳定性优于 ``内置 {IDF_TARGET_RTC_CLK_FRE} RC 振荡器``,同样无需外部元件,但 Deep-sleep 模式下电流消耗更高(比默认模式高 5 μA)。
|
||||||
|
|
||||||
|
时钟源的选择取决于系统时间精度要求和睡眠模式下的功耗要求。要修改 RTC 时钟源,请在项目配置中设置 :ref:`CONFIG_RTC_CLK_SRC`。
|
||||||
|
|
||||||
|
.. Need to add esp32c2 hardware design guideline link after it is publsihed.
|
||||||
|
|
||||||
|
.. only:: not esp32c2
|
||||||
|
|
||||||
|
想要了解外置晶振或外置振荡器的更多布线要求,请参考 {IDF_TARGET_HARDWARE_DESIGN_URL}。
|
||||||
|
|
||||||
|
|
||||||
|
获取当前时间
|
||||||
|
--------------
|
||||||
|
|
||||||
|
要获取当前时间,请使用 POSIX 函数 ``gettimeofday()``。此外,您也可以使用以下标准 C 库函数来获取时间并对其进行操作:
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
gettimeofday
|
||||||
|
time
|
||||||
|
asctime
|
||||||
|
clock
|
||||||
|
ctime
|
||||||
|
difftime
|
||||||
|
gmtime
|
||||||
|
localtime
|
||||||
|
mktime
|
||||||
|
strftime
|
||||||
|
adjtime*
|
||||||
|
|
||||||
|
如需立即更新当前时间,并暂停平滑时间校正,请使用 POSIX 函数 ``settimeofday()``。
|
||||||
|
|
||||||
|
若要求时间的分辨率为 1 s,请使用以下代码片段:
|
||||||
|
|
||||||
|
.. code-block:: c
|
||||||
|
|
||||||
|
time_t now;
|
||||||
|
char strftime_buf[64];
|
||||||
|
struct tm timeinfo;
|
||||||
|
|
||||||
|
time(&now);
|
||||||
|
// 将时区设置为中国标准时间
|
||||||
|
setenv("TZ", "CST-8", 1);
|
||||||
|
tzset();
|
||||||
|
|
||||||
|
localtime_r(&now, &timeinfo);
|
||||||
|
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
|
||||||
|
ESP_LOGI(TAG, "The current date/time in Shanghai is: %s", strftime_buf);
|
||||||
|
|
||||||
|
若要求时间的分辨率为 1 μs,请使用以下代码片段:
|
||||||
|
|
||||||
|
.. code-block:: c
|
||||||
|
|
||||||
|
struct timeval tv_now;
|
||||||
|
gettimeofday(&tv_now, NULL);
|
||||||
|
int64_t time_us = (int64_t)tv_now.tv_sec * 1000000L + (int64_t)tv_now.tv_usec;
|
||||||
|
|
||||||
|
.. _system-time-sntp-sync:
|
||||||
|
|
||||||
|
SNTP 时间同步
|
||||||
|
---------------
|
||||||
|
|
||||||
|
要设置当前时间,可以使用 POSIX 函数 ``settimeofday()`` 和 ``adjtime()``。lwIP 中的 SNTP 库会在收到 NTP 服务器的响应报文后,调用这两个函数以更新当前的系统时间。当然,用户可以在 lwIP SNTP 库之外独立地使用这两个函数。
|
||||||
|
|
||||||
|
在 lwIP SNTP 库内部调用的函数依赖于系统时间的同步模式。可使用函数 :cpp:func:`sntp_set_sync_mode` 来设置下列同步模式之一。
|
||||||
|
|
||||||
|
- :cpp:enumerator:`SNTP_SYNC_MODE_IMMED` (默认):使用函数 ``settimeofday()`` 后,收到 SNTP 服务器响应时立即更新系统时间。
|
||||||
|
- :cpp:enumerator:`SNTP_SYNC_MODE_SMOOTH`:使用函数 ``adjtime()`` 后,通过逐渐减小时间误差,平滑地更新时间。如果 SNTP 响应报文中的时间与当前系统时间相差大于 35 分钟,则会通过 ``settimeofday()`` 立即更新系统时间。
|
||||||
|
|
||||||
|
lwIP SNTP 库提供了 API 函数,用于设置某个事件的回调函数。您可能需要使用以下函数:
|
||||||
|
|
||||||
|
- :cpp:func:`sntp_set_time_sync_notification_cb()`:用于设置回调函数,通知时间同步的过程。
|
||||||
|
- :cpp:func:`sntp_get_sync_status()` 和 :cpp:func:`sntp_set_sync_status()`:用于获取或设置时间同步状态。
|
||||||
|
|
||||||
|
通过 SNTP 开始时间同步,只需调用以下三个函数:
|
||||||
|
|
||||||
|
.. code-block:: c
|
||||||
|
|
||||||
|
sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
||||||
|
sntp_setservername(0, "pool.ntp.org");
|
||||||
|
sntp_init();
|
||||||
|
|
||||||
|
添加此初始化代码后,应用程序将定期同步时间。时间同步周期由 :ref:`CONFIG_LWIP_SNTP_UPDATE_DELAY` 设置(默认为一小时)。如需修改,请在项目配置中设置 :ref:`CONFIG_LWIP_SNTP_UPDATE_DELAY`。
|
||||||
|
|
||||||
|
如需查看示例代码,请前往 :example:`protocols/sntp` 目录。该目录下的示例展示了如何基于 lwIP SNTP 库实现时间同步。
|
||||||
|
|
||||||
|
|
||||||
|
时区
|
||||||
|
------
|
||||||
|
|
||||||
|
要设置本地时区,请使用以下 POSIX 函数:
|
||||||
|
|
||||||
|
1. 调用 ``setenv()``,将 ``TZ`` 环境变量根据设备位置设置为正确的值。时间字符串的格式与 `GNU libc 文档 <https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html>`_ 中描述的相同(但实现方式不同)。
|
||||||
|
2. 调用 ``tzset()``,为新的时区更新 C 库的运行数据。
|
||||||
|
|
||||||
|
完成上述步骤后,请调用标准 C 库函数 ``localtime()``。该函数将返回排除时区偏差和夏令时干扰后的准确本地时间。
|
||||||
|
|
||||||
|
|
||||||
|
64 位 ``time_t``
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
ESP-IDF 默认使用 32 位的 ``time_t`` 类型。为解决 Y2K38 漏洞,您在构建应用程序时可能需要使用 64 位的 ``time_t`` 类型。
|
||||||
|
|
||||||
|
目前,完成这一操作需要从头开始构建交叉编译器工具链,具体步骤请参阅 :doc:`/get-started/linux-macos-setup`。要在工具链中启用对 64 位 ``time_t`` 的支持,您需要在构建工具链之前从 ``crosstool-NG/samples/xtensa-esp32-elf/crosstool.config`` 文件中删除 ``--enable-newlib-long-time_t`` 选项。
|
||||||
|
|
||||||
|
如需使程序同时兼容 32 位和 64 位的 ``time_t``,可以使用以下方法:
|
||||||
|
|
||||||
|
- 在 C 或 C++ 源文件中,如果 ``time_t`` 是 32 位的,编译器会预定义 ``_USE_LONG_TIME_T`` 宏,该宏定义在 ``<sys/types.h>`` 中。
|
||||||
|
- 在 CMake 文件中,ESP-IDF 构建属性 ``TIME_T_SIZE`` 将被设置为 ``time_t`` 的大小,单位为字节。您可以调用 ``idf_build_get_property(var TIME_T_SIZE)`` 来获取该属性的值,并将其放入 CMake 变量 ``var`` 中。了解更多关于 ``idf_build_get_property`` 的信息,参见 :ref:`ESP-IDF CMake 构建系统 API <cmake_buildsystem_api>`。
|
||||||
|
|
||||||
|
注意, ``time_t`` 类型的大小也会影响其他类型的大小,例如 ``struct timeval``、 ``struct stat`` 和 ``struct utimbuf``。
|
||||||
|
|
||||||
|
|
||||||
|
API 参考
|
||||||
|
-------------
|
||||||
|
|
||||||
|
.. include-build-file:: inc/esp_sntp.inc
|
||||||
|
Loading…
x
Reference in New Issue
Block a user