2022-11-04 16:42:46 +01:00

134 lines
2.9 KiB
C

/****************************************************************************
* Copyright (C) 2020 by Fabrice Muller *
* *
* This file is useful for ESP32 Design course. *
* *
****************************************************************************/
/**
* @file lab3-1_main.c
* @author Fabrice Muller
* @date 20 Oct. 2020
* @brief File containing the lab3-1 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 <string.h>
#include "esp_log.h"
/* FreeRTOS.org includes. */
#include "freertos/FreeRTOSConfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "my_helper_fct.h"
static const char* TAG = "SEM";
/* Application constants */
#define STACK_SIZE 4096
#define TABLE_SIZE 400
/* Task Priority */
const uint32_t TIMER_TASK_PRIORITY = 5;
const uint32_t INC_TABLE_TASK_PRIORITY = 4;
const uint32_t DEC_TABLE_TASK_PRIORITY = 4;
/* Communications */
SemaphoreHandle_t xSemClk;
/* Tasks */
void vTaskTimer(void *pvParameters);
void vTaskIncTable(void *pvParameters);
void vTaskDecTable(void *pvParameters);
/* Datas */
int Table[TABLE_SIZE];
/* Main function */
void app_main(void) {
/* Init Table */
memset(Table, 0, TABLE_SIZE*sizeof(int));
/* Create semaphore */
xSemClk = xSemaphoreCreateBinary();
if (xSemClk == NULL) {
ESP_LOGE(TAG, "Error xSemClk cannot be created");
vTaskDelete(NULL);
}
/* Stop scheduler */
vTaskSuspendAll();
/* Create Tasks */
xTaskCreatePinnedToCore(vTaskTimer,"Timer", STACK_SIZE, NULL, TIMER_TASK_PRIORITY, NULL,CORE_0) ;
/* Continue scheduler */
xTaskResumeAll();
/* to ensure its exit is clean */
vTaskDelete(NULL);
}
/*-----------------------------------------------------------*/
void vTaskTimer(void *pvParameters) {
TickType_t xLastWakeTime =xTaskGetTickCount();
for(;;){
vTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(250UL));
COMPUTE_IN_TIME_MS(20);
DISPLAY("Task Timer : give sem Clk");
xSemaphoreGive(xSemClk);
}
}
void vTaskIncTable(void *pvParameters) {
int index;
int NbActivations = 0;
uint32_t constNumber = ((uint32_t *)pvParameters);
for(;;){
//Wait on clk
xSemaphoreTake(xSemClk, portMAX_DELAY);
DISPLAY ("TASK IncTable is running");
if (NbActivations ==0) {
DISPLAY ("TASK IncTable is starting computation");
for (index = 0 ; index < TABLE_SIZE; index++)
Table[index] = Table[index] + constNumber;
COMPUTE_IN_TIME_MS(50);
NbActivations = 4;
}
else {
NbActivations = NbActivations - 1;
}
}
}
void vTaskDecTable(void *pvParameters) {
int index;
for(;;){
for (index = 0 ; index < TABLE_SIZE; index++)
Table[index] = Table[index] + 1;
COMPUTE_IN_TIME_MS(50);
}
}