lab2-4_single_msg_queue_interrupt

This commit is contained in:
2022-10-28 16:34:25 +02:00
parent 49d8d4269d
commit 7ddfd4a1aa
20 changed files with 5999 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
idf_component_register(
SRC_DIRS "."
INCLUDE_DIRS ".")

View File

@@ -0,0 +1,2 @@
COMPONENT_SRCDIRS := .
COMPONENT_ADD_INCLUDEDIRS := .

View File

@@ -0,0 +1,170 @@
/****************************************************************************
* 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"
QueueHandle_t xQueue1;
/**************************************/
/**************************************/
/* 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 T1_PRIO = 2;
static const uint32_t T2_PRIO = 2;
static const uint32_t T3_PRIO = 3;
#else
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 (0x1FFFFF)
/* The task function. */
void vTaskFunction1(void *pvParameters);
void vTaskFunction2(void *pvParameters);
void vTaskFunction3(void *pvParameters);
void app_main(void) {
xQueue1 = xQueueCreate(5, sizeof(uint32_t));
DISPLAY("Start of app_main task, priority = %d",uxTaskPriorityGet(NULL));
vTaskSuspendAll();
xTaskCreatePinnedToCore(vTaskFunction1, "Task 1", STACK_SIZE, (void*)"Task 1", T1_PRIO, NULL,CORE_1);
xTaskCreatePinnedToCore(vTaskFunction2, "Task 2", STACK_SIZE, (void*)"Task 2", T2_PRIO, NULL,CORE_0);
xTaskCreatePinnedToCore(vTaskFunction3, "Task 3", STACK_SIZE, (void*)"Task 3", T3_PRIO, NULL,CORE_0);
xTaskResumeAll();
/*for (;;) {
vTaskDelay(100 / portTICK_PERIOD_MS); // 100 ms
DISPLAY("Hello app_main task!");
}*/
/* to ensure its exit is clean */
vTaskDelete(NULL);
}
/*-----------------------------------------------------------*/
void vTaskFunction1(void *pvParameters) {
int32_t lSentValue;
TickType_t xLastWakeTime;
xLastWakeTime= xTaskGetTickCount();
const TickType_t xDelay500ms = pdMS_TO_TICKS(500UL);
for(;;){
DISPLAY("Task 1 started , sending message");
lSentValue= 50;
xQueueSend(xQueue1, &lSentValue, 0);
// Compute time : 40 ms
COMPUTE_IN_TIME_MS(40) ;
// block periodically : 500 ms
vTaskDelayUntil(&xLastWakeTime, xDelay500ms);
DISPLAY("Task 1 done");
}
}
void vTaskFunction2(void *pvParameters) {
// Wait for message
int32_t lReceivedValue;
for(;;){
DISPLAY("Task 2 started, wait for message");
if(xQueueReceive(xQueue1,&lReceivedValue,portMAX_DELAY==pdTRUE)){
// display task number and message
DISPLAY("Task 2 : Received value : %d", lReceivedValue);
}
// Compute time : 30 ms
COMPUTE_IN_TIME_MS(30) ;
}
}
void vTaskFunction3(void *pvParameters) {
for(;;){
// Task blocked during 100 ms
DISPLAY("Task 3 started");
vTaskDelay(pdMS_TO_TICKS(100));
// Compute time : 20 ms
COMPUTE_IN_TIME_MS(20);
DISPLAY ("Task 3 done");
}
}

View 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