zef
This commit is contained in:
3
part3_freertos/lab1-3_periodic_task/main/CMakeLists.txt
Normal file
3
part3_freertos/lab1-3_periodic_task/main/CMakeLists.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
idf_component_register(
|
||||
SRC_DIRS "."
|
||||
INCLUDE_DIRS ".")
|
||||
2
part3_freertos/lab1-3_periodic_task/main/component.mk
Normal file
2
part3_freertos/lab1-3_periodic_task/main/component.mk
Normal file
@@ -0,0 +1,2 @@
|
||||
COMPONENT_SRCDIRS := .
|
||||
COMPONENT_ADD_INCLUDEDIRS := .
|
||||
197
part3_freertos/lab1-3_periodic_task/main/main.c
Normal file
197
part3_freertos/lab1-3_periodic_task/main/main.c
Normal file
@@ -0,0 +1,197 @@
|
||||
/****************************************************************************
|
||||
* Copyright (C) 2020 by Fabrice Muller *
|
||||
* *
|
||||
* This file is useful for ESP32 Design course. *
|
||||
* *
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file lab1-2_main.c
|
||||
* @author Fabrice Muller
|
||||
* @date 12 Oct. 2020
|
||||
* @brief File containing the lab1-2 of Part 3.
|
||||
*
|
||||
* @see https://github.com/fmuller-pns/esp32-vscode-project-template
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "esp_log.h"
|
||||
#include "soc/rtc_wdt.h"
|
||||
|
||||
/* FreeRTOS.org includes. */
|
||||
#include "freertos/FreeRTOSConfig.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "esp_freertos_hooks.h"
|
||||
|
||||
#include "my_helper_fct.h"
|
||||
|
||||
|
||||
/**************************************/
|
||||
/**************************************/
|
||||
/* Scenarios */
|
||||
|
||||
#define DIFF_PRIORITY
|
||||
|
||||
#define PINNED_TO_CORE 0x00
|
||||
// 0x00: Task 1: Core 0, Task 2: Core 0
|
||||
// 0x01: Task 1: Core 0, Task 2: Core 1
|
||||
// 0x10: Task 1: Core 1, Task 2: Core 0
|
||||
// 0x11: Task 1: Core 1, Task 2: Core 1
|
||||
|
||||
#define IDLE_HOOKS
|
||||
|
||||
#define TASK_DELAY
|
||||
|
||||
#define PERIODIC_TASK_DELAY
|
||||
|
||||
/**************************************/
|
||||
/**************************************/
|
||||
|
||||
/* Default stack size for tasks */
|
||||
static const uint32_t STACK_SIZE = 4000;
|
||||
|
||||
#ifdef DIFF_PRIORITY
|
||||
|
||||
static const uint32_t T3_PRIO = 1;
|
||||
static const uint32_t T1_PRIO = 5;
|
||||
static const uint32_t T2_PRIO = 6;
|
||||
|
||||
#else
|
||||
|
||||
static const uint32_t T3_PRIO = 1;
|
||||
static const uint32_t T1_PRIO = 5;
|
||||
static const uint32_t T2_PRIO = 5;
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef IDLE_HOOKS
|
||||
|
||||
volatile uint32_t countIdle0 = 0;
|
||||
volatile uint32_t countIdle1 = 0;
|
||||
|
||||
bool vApplicationIdleHook_0 ( void ){
|
||||
countIdle0++;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool vApplicationIdleHook_1 ( void ){
|
||||
countIdle1++;
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifndef PERIODIC_TASK_DELAY
|
||||
/* Buffer to extract trace information */
|
||||
static char buffer[40*12];
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/* Used as a loop counter to create a very crude delay. */
|
||||
#define mainDELAY_LOOP_COUNT (0xFFFFF)
|
||||
|
||||
/* The task function. */
|
||||
void vTaskFunction(void *pvParameters);
|
||||
|
||||
|
||||
void app_main(void) {
|
||||
|
||||
DISPLAY("Start of app_main task, priority = %d",uxTaskPriorityGet(NULL));
|
||||
|
||||
#ifdef IDLE_HOOKS
|
||||
esp_register_freertos_idle_hook_for_cpu(vApplicationIdleHook_0, CORE_0);
|
||||
esp_register_freertos_idle_hook_for_cpu(vApplicationIdleHook_1, CORE_1);
|
||||
#endif
|
||||
|
||||
vTaskSuspendAll();
|
||||
|
||||
/* Create tasks. */
|
||||
#if PINNED_TO_CORE == 0x00
|
||||
xTaskCreatePinnedToCore(vTaskFunction, "Task 1", STACK_SIZE, (void*)"Task 1", T1_PRIO, NULL,CORE_0);
|
||||
xTaskCreatePinnedToCore(vTaskFunction, "Task 2", STACK_SIZE, (void*)"Task 2", T2_PRIO, NULL,CORE_0);
|
||||
#elif PINNED_TO_CORE == 0x01
|
||||
xTaskCreatePinnedToCore(vTaskFunction, "Task 1", STACK_SIZE, (void*)"Task 1", T1_PRIO, NULL,CORE_0);
|
||||
xTaskCreatePinnedToCore(vTaskFunction, "Task 2", STACK_SIZE, (void*)"Task 2", T2_PRIO, NULL,CORE_1);
|
||||
#elif PINNED_TO_CORE == 0x10
|
||||
xTaskCreatePinnedToCore(vTaskFunction, "Task 1", STACK_SIZE, (void*)"Task 1", T1_PRIO, NULL,CORE_1);
|
||||
xTaskCreatePinnedToCore(vTaskFunction, "Task 2", STACK_SIZE, (void*)"Task 2", T2_PRIO, NULL,CORE_0);
|
||||
#elif PINNED_TO_CORE == 0x11
|
||||
xTaskCreatePinnedToCore(vTaskFunction, "Task 1", STACK_SIZE, (void*)"Task 1", T1_PRIO, NULL,CORE_1);
|
||||
xTaskCreatePinnedToCore(vTaskFunction, "Task 2", STACK_SIZE, (void*)"Task 2", T2_PRIO, NULL,CORE_1);
|
||||
#else
|
||||
xTaskCreate(vTaskFunction, "Task 1", STACK_SIZE, (void*)"Task 1", T1_PRIO, NULL);
|
||||
xTaskCreate(vTaskFunction, "Task 2", STACK_SIZE, (void*)"Task 2", T2_PRIO, NULL);
|
||||
#endif
|
||||
xTaskResumeAll();
|
||||
|
||||
xTaskCreatePinnedToCore(vTaskFunction, "Task 3", STACK_SIZE, (void*)"Task 3", T3_PRIO, NULL,CORE_0);
|
||||
|
||||
|
||||
#ifdef IDLE_HOOKS
|
||||
|
||||
#ifndef PERIODIC_TASK_DELAY
|
||||
/* Print task list */
|
||||
vTaskList(buffer);
|
||||
DISPLAY("Trace information");
|
||||
printf("--------------------------------------------------------\n");
|
||||
printf("task\t\tstate\tprio\tstack\ttsk id\tcore id\n");
|
||||
printf("--------------------------------------------------------\n");
|
||||
printf(buffer);
|
||||
#endif
|
||||
|
||||
for (;;) {
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS); // 100 ms
|
||||
DISPLAY("IDLE0: %d, IDLE1:%d", countIdle0, countIdle1);
|
||||
}
|
||||
#else
|
||||
for (;;) {
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS); // 100 ms
|
||||
DISPLAY("Hello app_main task!");
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* to ensure its exit is clean */
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
void vTaskFunction(void *pvParameters) {
|
||||
char *pcTaskName;
|
||||
TickType_t xLastWakeTime;
|
||||
const TickType_t xDelay250ms = pdMS_TO_TICKS(250UL);
|
||||
volatile uint32_t ul;
|
||||
|
||||
/* The string to print out is passed in via the parameter. Cast this to a
|
||||
character pointer. */
|
||||
pcTaskName = (char *)pvParameters;
|
||||
xLastWakeTime= xTaskGetTickCount();
|
||||
DISPLAY("Start of %s task, priority = %d",pcTaskName, uxTaskPriorityGet(NULL));
|
||||
|
||||
/* As per most tasks, this task is implemented in an infinite loop. */
|
||||
for (;; ) {
|
||||
DISPLAY("Run computation of %s", pcTaskName);
|
||||
|
||||
/* Delay for simulating a computation */
|
||||
for (ul = 0; ul < mainDELAY_LOOP_COUNT; ul++){
|
||||
}
|
||||
|
||||
// To run APP_MAIN TASK
|
||||
#ifdef TASK_DELAY
|
||||
/* Approximated/Periodic Delay */
|
||||
#ifdef PERIODIC_TASK_DELAY
|
||||
DISPLAY("Periodic Delay of %s", pcTaskName);
|
||||
vTaskDelayUntil (& xLastWakeTime , pdMS_TO_TICKS (300));
|
||||
#else
|
||||
DISPLAY("Approximated Delay of %s", pcTaskName);
|
||||
vTaskDelay(pdMS_TO_TICKS (300));
|
||||
#endif
|
||||
#endif
|
||||
|
||||
DISPLAY("End of %s", pcTaskName);
|
||||
}
|
||||
}
|
||||
47
part3_freertos/lab1-3_periodic_task/main/my_helper_fct.h
Normal file
47
part3_freertos/lab1-3_periodic_task/main/my_helper_fct.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef _MY_HELPER_FCT_
|
||||
#define _MY_HELPER_FCT_
|
||||
|
||||
#include <unistd.h>
|
||||
#include "esp_log.h"
|
||||
|
||||
|
||||
/* Core constants fot xTaskCreatePinnedToCore() function */
|
||||
const uint32_t CORE_0 = 0;
|
||||
const uint32_t CORE_1 = 1;
|
||||
const uint32_t PRO_CPU = 0;
|
||||
const uint32_t APP_CPU = 1;
|
||||
|
||||
/**
|
||||
* @brief Macro to display the buffer with arguments. msg is a formatted string as printf() function.
|
||||
*
|
||||
*/
|
||||
#define DISPLAY(msg, ...) printf("%d:%d>\t"msg"\r\n", xTaskGetTickCount(), xPortGetCoreID(), ##__VA_ARGS__);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Macro to display error/warning/info for the buffer with arguments. msg is a formatted string as printf() function.
|
||||
*
|
||||
*/
|
||||
#define DISPLAYE(tag, msg, ...) ESP_LOGE(tag, "%d:%d>\t"msg"", xTaskGetTickCount(), xPortGetCoreID(), ##__VA_ARGS__);
|
||||
|
||||
#define DISPLAYW(tag, msg, ...) ESP_LOGW(tag, "%d:%d>\t"msg"", xTaskGetTickCount(), xPortGetCoreID(), ##__VA_ARGS__);
|
||||
|
||||
#define DISPLAYI(tag, msg, ...) ESP_LOGI(tag, "%d:%d>\t"msg"", xTaskGetTickCount(), xPortGetCoreID(), ##__VA_ARGS__);
|
||||
|
||||
/**
|
||||
* @brief Macro to display the buffer without argument
|
||||
*
|
||||
*/
|
||||
#define DISPLAYB(msg) printf("%d:%d>\t", xTaskGetTickCount(), xPortGetCoreID());\
|
||||
printf(msg);\
|
||||
printf("\r\n");
|
||||
|
||||
|
||||
/* Consume CPU cycles. Time parameter is for the default frequency: 160MHz (ESP32_DEFAULT_CPU_FREQ_MHZ=160) */
|
||||
#define COMPUTE_IN_TIME_US(time_us) {register uint32_t i=(uint32_t)time_us*5.02*100/22; while (i-- > 0) {asm(" nop");}}
|
||||
|
||||
#define COMPUTE_IN_TIME_MS(time_ms) {register uint32_t i=(uint32_t)time_ms*1000*5.02*100/22; while (i-- > 0) {asm(" nop");}}
|
||||
|
||||
#define COMPUTE_IN_TICK(tick) {register uint32_t i=(uint32_t)(tick*1000/configTICK_RATE_HZ)*1000*5.02*100/22; while (i-- > 0) {asm(" nop");}}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user