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-Queue/main.c
2024-03-18 16:13:22 +01:00

181 lines
5.6 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 <stdio.h>
#include <stdlib.h>
#ifdef RTE_Compiler_EventRecorder
#include "EventRecorder.h"
#endif
CRC_HandleTypeDef hcrc;
static void MX_CRC_Init(void);
osThreadId_t thread1,thread2;
osMessageQueueId_t pipe1, pipe2;
osSemaphoreId_t mutexCRC;
const osThreadAttr_t thread1_attr = {
.stack_size = 1024, // Create the thread stack size
.priority = osPriorityNormal, //Set initial thread priority to high
.name = "Task2",
};
const osThreadAttr_t thread2_attr = {
.stack_size = 1024, // Create the thread stack size
.priority = osPriorityNormal, //Set initial thread priority to high
.name = "Task2",
};
const osMessageQueueAttr_t pipe1_attr = {
.name = "Pipe1",
};
const osMessageQueueAttr_t pipe2_attr = {
.name = "Pipe2",
};
const osSemaphoreAttr_t mutexCRC_attr = {
.name = "MTX_CRC", // name of the semaphore
};
//------------------------------------------------------------------------------
// 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);
}
static void MX_CRC_Init(void) {
__HAL_RCC_CRC_CLK_ENABLE();
hcrc.Instance = CRC;
hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE;
hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE;
hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE;
hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;
hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_WORDS;
if (HAL_CRC_Init(&hcrc) != HAL_OK) //Error_Handler();
{
}
}
/*----------------------------------------------------------------------------
* Thread Task1
*---------------------------------------------------------------------------*/
__NO_RETURN static void Task1(void *argument) {
uint32_t msg[]={1234,5678,41234,4356,122457,8562,45772,245735};
uint32_t crc;
osSemaphoreAcquire(mutexCRC, osWaitForever);
//MX_CRC_Init();
crc = HAL_CRC_Calculate(&hcrc, msg, 8); // TODO: CRC module init
osSemaphoreRelease(mutexCRC);
for (;;) {
// TODO: post 8 values and wait for the CRC back from Task 2
osStatus_t status;
while(1) {
for(uint8_t i = 0; i < 8;) {
status = osMessageQueuePut(pipe1, &(msg[i]), 1, 0);
if(status == osOK) i++;
osDelay(500);
}
uint32_t recData;
do {
status = osMessageQueueGet(pipe2, &recData, NULL, 0);
} while(status != osOK);
if(recData == crc) {
printf("CRC OK: %d\r\n", crc);
} else {
printf("Error ! crc = %d, and should be: %d\r\n", recData, crc);
}
}
}
}
/*----------------------------------------------------------------------------
* Thread Task2
*---------------------------------------------------------------------------*/
__NO_RETURN static void Task2(void *argument) {
uint32_t crc;
uint32_t recData;
for (;;) {
osSemaphoreAcquire(mutexCRC, osWaitForever);
MX_CRC_Init();
for(uint8_t i = 0; i < 8; i++) {
osMessageQueueGet(pipe1, &recData, NULL, osWaitForever);
crc = HAL_CRC_Accumulate(&hcrc, &recData, 1);
}
osSemaphoreRelease(mutexCRC);
osMessageQueuePut(pipe2, &crc, 1, osWaitForever);
}
}
int main (void) {
// System Initialization
SystemClock_Config();
SystemCoreClockUpdate();
#ifdef RTE_Compiler_EventRecorder
// Initialize and start Event Recorder
// Ext_UART_Init(9600);
EventRecorderInitialize(EventRecordAll, 1U);
#endif
MX_CRC_Init();
osKernelInitialize(); // Initialize CMSIS-RTOS
pipe1 = osMessageQueueNew(8, 4, &pipe1_attr);
pipe2 = osMessageQueueNew(8, 4, &pipe2_attr);
thread1 = osThreadNew(Task1, (void*)1, &thread1_attr);
thread2 = osThreadNew(Task2, (void*)2, &thread2_attr);
mutexCRC= osSemaphoreNew(1,1,&mutexCRC_attr);
//----------------------------------------------------------------------------------------------
// get names are placed for TraceAlyzer visualisation
//----------------------------------------------------------------------------------------------
osThreadGetName(thread1);
osThreadGetName(thread2);
osMessageQueueGetName(pipe1);
osMessageQueueGetName(pipe2);
osSemaphoreGetName(mutexCRC);
osKernelStart(); // Start thread execution
for (;;) {}
}