This repository has been archived on 2024-10-30. You can view files and clone it, but cannot push or open issues or pull requests.
PTR-Events/main.c
Klagarge eeff72a57b practical work N°1 finish
We measure that switching context time is around:
- 20us without optimization, with EventRecorder
- 6us without optimization, without EventRecorder
- 2us with optimization (o2), without EventRecorder
2024-04-08 15:19:41 +02:00

153 lines
5.1 KiB
C

/*----------------------------------------------------------------------------
* CMSIS-RTOS 'main' function template
*---------------------------------------------------------------------------*/
#include "stm32f7xx_hal.h"
#include "RTE_Components.h"
#include CMSIS_device_header
#include "cmsis_os2.h"
#include "string.h"
#include <stdlib.h>
#include "ext_uart.h"
#ifdef RTE_Compiler_EventRecorder
#include "EventRecorder.h"
#endif
#define NBR_COUNTER 4
osThreadId_t idTask1, idTask2, idTask3, idTask4, idTask5;
osMutexId_t mutexCounter[NBR_COUNTER];
const osThreadAttr_t AttrTask1 = {
.stack_size = 512, // Create the thread stack size
.priority = osPriorityNormal, //Set initial thread priority to high
.name = "Task 1"
};
const osThreadAttr_t AttrTask2 = {
.stack_size = 512, // Create the thread stack size
.priority = osPriorityNormal, //Set initial thread priority to high
.name = "Task 2"
};
const osThreadAttr_t AttrTask3 = {
.stack_size = 512, // Create the thread stack size
.priority = osPriorityNormal, //Set initial thread priority to high
.name = "Task 3"
};
const osThreadAttr_t AttrTask4 = {
.stack_size = 512, // Create the thread stack size
.priority = osPriorityNormal, //Set initial thread priority to high
.name = "Task 4"
};
const osThreadAttr_t AttrTask5 = {
.stack_size = 1024, // Create the thread stack size
.priority = osPriorityHigh, //Set initial thread priority to high
.name = "Task 5",
};
uint32_t counter[NBR_COUNTER];
/*----------------------------------------------------------------------------
* Thread Task counter
*---------------------------------------------------------------------------*/
__NO_RETURN static void taskCounter(void *argument) {
uint8_t idCounter = (uint8_t) *((uint8_t* )argument);
for (;;) {
osMutexAcquire(mutexCounter[idCounter], osWaitForever);
counter[idCounter]++;
osMutexRelease(mutexCounter[idCounter]);
}
}
/*----------------------------------------------------------------------------
* Thread Task 5
*---------------------------------------------------------------------------*/
__NO_RETURN static void task5(void *argument) {
uint32_t freq = osKernelGetTickFreq();
uint32_t countTick = freq*5;
for (;;) {
uint32_t sum = 0;
for(uint8_t i = 0; i < NBR_COUNTER; i++)
{
osMutexAcquire(mutexCounter[i], osWaitForever);
sum += counter[i];
printf("[%d] ", counter[i]);
osMutexRelease(mutexCounter[i]);
}
printf("\r\nSum = %u\r\n", sum);
osDelay(countTick);
}
}
//------------------------------------------------------------------------------
// Setup system clock to 216MHz
//------------------------------------------------------------------------------
void SystemClock_Config (void) {
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
/* Enable HSE Oscillator and activate PLL with HSE as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 432;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 9;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
/* Activate the OverDrive to reach the 216 MHz Frequency */
HAL_PWREx_EnableOverDrive();
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7);
}
int main (void) {
uint8_t task1_parameter = 0;
uint8_t task2_parameter = 1;
uint8_t task3_parameter = 2;
uint8_t task4_parameter = 3;
// System Initialization
SystemCoreClockUpdate();
SystemClock_Config();
#ifdef RTE_Compiler_EventRecorder
// Initialize and start Event Recorder
// Ext_UART_Init(9600);
EventRecorderInitialize(EventRecordAll, 1U);
#endif
osKernelInitialize(); // Initialize CMSIS-RTOS
idTask1 = osThreadNew(taskCounter, &task1_parameter, &AttrTask1);
idTask2 = osThreadNew(taskCounter, &task2_parameter, &AttrTask2);
idTask3 = osThreadNew(taskCounter, &task3_parameter, &AttrTask3);
idTask4 = osThreadNew(taskCounter, &task4_parameter, &AttrTask4);
idTask5 = osThreadNew(task5, NULL, &AttrTask5);
//----------------------------------------------------------------------------------------------
// get names are placed for TraceAlyzer visualisation
//----------------------------------------------------------------------------------------------
osThreadGetName(idTask1);
osThreadGetName(idTask2);
osThreadGetName(idTask3);
osThreadGetName(idTask4);
osThreadGetName(idTask5);
osKernelStart();
// Start thread execution
for (;;) {
}
}