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-DBG/lab-dbg/main.c

176 lines
4.8 KiB
C
Raw Permalink Normal View History

2024-03-04 11:49:28 +00:00
/*----------------------------------------------------------------------------
* DBG
*---------------------------------------------------------------------------*/
#include "stm32f7xx_hal.h"
#include "RTE_Components.h"
#include CMSIS_device_header
#include "cmsis_os2.h"
#include <stdio.h>
#ifdef RTE_Compiler_EventRecorder
#include "EventRecorder.h"
#endif
osThreadId_t thread1,thread2,thread3,thread4;
void Thread_1 (void *argument);
void Thread_2 (void *argument);
void Thread_3 (void *argument);
void Thread_4 (void *argument);
const osThreadAttr_t thread1_attr = {
.stack_size = 1024,
2024-03-04 14:41:13 +00:00
.priority = osPriorityNormal, // 24
.name = "FACT"
2024-03-04 11:49:28 +00:00
};
const osThreadAttr_t thread2_attr = {
.stack_size = 1024,
2024-03-04 14:41:13 +00:00
.priority = osPriorityBelowNormal4, // 20
.name = "RESUMER"
2024-03-04 11:49:28 +00:00
};
const osThreadAttr_t thread3_attr = {
2024-03-04 14:41:13 +00:00
.stack_size = 1024,
.priority = osPriorityBelowNormal, // 16
.name = "BLOCKER"
2024-03-04 11:49:28 +00:00
};
const osThreadAttr_t thread4_attr = {
.stack_size = 256,
2024-03-04 14:41:13 +00:00
.priority = osPriorityLow, // 8
.name = "FLAG"
2024-03-04 11:49:28 +00:00
};
//------------------------------------------------------------------------------
// 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);
}
/*----------------------------------------------------------------------------
* Factorial function calculation
*---------------------------------------------------------------------------*/
double Fact(double number)
{
if(number <= 1)
return 1;
return (number * Fact(number-1));
}
/*----------------------------------------------------------------------------
2024-03-04 14:41:13 +00:00
* Thread 1 - FACT.
2024-03-04 11:49:28 +00:00
*---------------------------------------------------------------------------*/
void Thread_1 (void *argument) {
uint32_t x = 1;
double result;
for (;;)
{
2024-03-04 14:41:13 +00:00
result = 1;
for(int i = 1; i<=x;i++) {
result *= i;
}
//result = Fact(x);
printf("Fact of %d is %a\r\n",x, result);
2024-03-04 11:49:28 +00:00
x++;
2024-03-04 14:41:13 +00:00
//if(x > 100) x = 1;
2024-03-04 11:49:28 +00:00
osDelay(200);
}
}
/*----------------------------------------------------------------------------
2024-03-04 14:41:13 +00:00
* Thread 2 - RESUMER
2024-03-04 11:49:28 +00:00
*---------------------------------------------------------------------------*/
void Thread_2 (void *argument) {
osThreadState_t state;
for (;;)
{
osDelay(20);
2024-03-04 14:41:13 +00:00
state = osThreadGetState(thread4);
2024-03-04 11:49:28 +00:00
if(state == osThreadBlocked)
{
2024-03-04 14:41:13 +00:00
osThreadResume(thread4);
2024-03-04 11:49:28 +00:00
}
}
}
/*----------------------------------------------------------------------------
2024-03-04 14:41:13 +00:00
* Thread 3 - BLOCKER
2024-03-04 11:49:28 +00:00
*---------------------------------------------------------------------------*/
void Thread_3 (void *argument) {
osThreadState_t state;
for (;;)
{
osDelay(50);
2024-03-04 14:41:13 +00:00
state = osThreadGetState(thread4);
2024-03-04 11:49:28 +00:00
if(state != osThreadBlocked)
{
2024-03-04 14:41:13 +00:00
osThreadSuspend(thread4);
2024-03-04 11:49:28 +00:00
}
}
}
/*----------------------------------------------------------------------------
2024-03-04 14:41:13 +00:00
* Thread 4 - FLAG
2024-03-04 11:49:28 +00:00
*---------------------------------------------------------------------------*/
void Thread_4 (void *argument) {
2024-03-04 14:41:13 +00:00
char str[10] = "Flag is:0";
2024-03-04 11:49:28 +00:00
uint8_t flag = '0';
for (;;)
{
if(flag == '0')
{
flag = '1';
}
else
{
flag = '0';
}
str[8]= flag;
puts(str);
2024-03-04 14:41:13 +00:00
EventRecordData(0x0000, str, 10);
2024-03-04 11:49:28 +00:00
osDelay(100);
}
}
int main (void) {
// System Initialization
SystemClock_Config();
SystemCoreClockUpdate();
#ifdef RTE_Compiler_EventRecorder
// Initialize and start Event Recorder
EventRecorderInitialize(EventRecordAll, 1U);
#endif
osKernelInitialize();
thread1 = osThreadNew(Thread_1, NULL, &thread1_attr);
thread2 = osThreadNew(Thread_2, NULL, &thread2_attr);
thread3 = osThreadNew(Thread_3, NULL, &thread3_attr);
thread4 = osThreadNew(Thread_4, NULL, &thread4_attr);
osKernelStart();
for (;;) {}
}