定时器 代码¶
组件架构¶
driver/timer/CMakeLists.txt¶
set(src_dirs
.
)
set(include_dirs
include
)
set(requires
driver
esp_timer
led
)
idf_component_register(SRC_DIRS ${src_dirs} INCLUDE_DIRS ${include_dirs} REQUIRES ${requires})
Note
注意,在驱动程序中,我们使用了 ESP-IDF 内置的 driver
库中的 gpio,因此,我们需要在 CMakeLists.txt
文件的 REQUIRES
字段中指定此依赖项。 此外,我们使用LED来体现外部中断的效果,因此我们需要在REQUIRES
字段中添加led
。 再有,对于定时器,我们需要在REQUIRES
字段中添加esp_timer
。
tim.h¶
/**
* @file tim.h
* @author SHUAIWEN CUI (SHUAIWEN001@e.ntu.edu.sg)
* @brief This file contains the function prototypes for the TIM component.
* @version 1.0
* @date 2024-11-18
*
* @copyright Copyright (c) 2024
*
*/
#ifndef __TIM_H__
#define __TIM_H__
/* Dependencies */
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_tim.h"
#include "led.h"
/* Function Prototypes */
/**
* @brief Initialize a high-precision timer (ESP_TIMER)
* @param tps: Timer period in microseconds (μs). For example, to execute the timer interrupt once every second,
* set tps = 1s = 1000000μs.
* @retval None
*/
void esptim_int_init(uint64_t tps);
/**
* @brief Timer callback function
* @param arg: No arguments passed
* @retval None
*/
void esptim_callback(void *arg);
#endif /*__TIM_H__*/
tim.c¶
/**
* @file tim.c
* @author SHUAIWEN CUI (SHUAIWEN001@e.ntu.edu.sg)
* @brief This file contains the implementation of the TIM component.
* @version 1.0
* @date 2024-11-18
*
* @copyright Copyright (c) 2024
*
*/
#include "tim.h"
/**
* @brief Initialize a high-precision timer (ESP_TIMER)
* @param tps: Timer period in microseconds (μs). For example, to execute the timer interrupt once every second,
* set tps = 1s = 1000000μs.
* @retval None
*/
void esptim_int_init(uint64_t tps)
{
esp_timer_handle_t esp_tim_handle; /* Timer callback function handle */
/* Define a timer configuration structure */
esp_timer_create_args_t tim_periodic_arg = {
.callback = &esptim_callback, /* Set the callback function */
.arg = NULL, /* No arguments passed */
};
esp_timer_create(&tim_periodic_arg, &esp_tim_handle); /* Create a timer event */
esp_timer_start_periodic(esp_tim_handle, tps); /* Trigger periodically based on the timer period */
}
/**
* @brief Timer callback function
* @param arg: No arguments passed
* @retval None
*/
void esptim_callback(void *arg)
{
led_toggle();
}
main.c¶
/**
* @file main.c
* @author SHUAIWEN CUI (SHUAIWEN001@e.ntu.edu.sg)
* @brief
* @version 1.0
* @date 2024-11-17
*
* @copyright Copyright (c) 2024
*
*/
/* Dependencies */
// Basic
#include "esp_system.h"
#include "esp_chip_info.h"
#include "esp_psram.h"
#include "esp_flash.h"
#include "nvs_flash.h"
#include "esp_log.h"
// RTOS
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
// BSP
#include "led.h"
#include "exit.h"
#include "tim.h"
/**
* @brief Entry point of the program
* @param None
* @retval None
*/
void app_main(void)
{
esp_err_t ret;
uint32_t flash_size;
esp_chip_info_t chip_info;
// uint8_t key;
// Initialize NVS
ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase()); // Erase if needed
ret = nvs_flash_init();
}
// Get FLASH size
esp_flash_get_size(NULL, &flash_size);
esp_chip_info(&chip_info);
// Display CPU core count
printf("CPU Cores: %d\n", chip_info.cores);
// Display FLASH size
printf("Flash size: %ld MB flash\n", flash_size / (1024 * 1024));
// Display PSRAM size
printf("PSRAM size: %d bytes\n", esp_psram_get_size());
// BSP
led_init();
// key_init();
exit_init();
esptim_int_init(1000000); // 1s enable timer, of which the callback function toggles the LED
while (1)
{
printf("Hello-ESP32\r\n");
vTaskDelay(1000);
}
}