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
2024-03-04 12:49:28 +01:00

166 lines
4.6 KiB
C

/*----------------------------------------------------------------------------
* 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,
.priority = osPriorityNormal,
};
const osThreadAttr_t thread2_attr = {
.stack_size = 1024,
.priority = osPriorityBelowNormal4,
};
const osThreadAttr_t thread3_attr = {
.stack_size = 4096,
.priority = osPriorityBelowNormal,
};
const osThreadAttr_t thread4_attr = {
.stack_size = 256,
.priority = osPriorityLow,
};
//------------------------------------------------------------------------------
// 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));
}
/*----------------------------------------------------------------------------
* Thread 1
*---------------------------------------------------------------------------*/
void Thread_1 (void *argument) {
uint32_t x = 1;
double result;
for (;;)
{
result = Fact(x);
printf("Fact of %d is %f\r\n",x,result);
x++;
osDelay(200);
}
}
/*----------------------------------------------------------------------------
* Thread 2
*---------------------------------------------------------------------------*/
void Thread_2 (void *argument) {
osThreadState_t state;
for (;;)
{
osDelay(20);
state = osThreadGetState(Thread_4);
if(state == osThreadBlocked)
{
osThreadResume(Thread_4);
}
}
}
/*----------------------------------------------------------------------------
* Thread 3
*---------------------------------------------------------------------------*/
void Thread_3 (void *argument) {
osThreadState_t state;
for (;;)
{
osDelay(50);
state = osThreadGetState(Thread_4);
if(state != osThreadBlocked)
{
osThreadSuspend(Thread_4);
}
}
}
/*----------------------------------------------------------------------------
* Thread 4
*---------------------------------------------------------------------------*/
void Thread_4 (void *argument) {
char * str = "Flag is:0";
uint8_t flag = '0';
for (;;)
{
if(flag == '0')
{
flag = '1';
}
else
{
flag = '0';
}
str[8]= flag;
puts(str);
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 (;;) {}
}