I’m new here. I’ve searched the forum, but can’t find anything related to this. So, I thought about posting here.
The problem statement is that, we have 2 tasks - matrix and communication.
- Matrix has higher priority than communication.
- Calculate the execution time of both the tasks using vApplicationTickHook()
- If communication has execution time >1000ms, increase it’s priority to 4 or if it’s execution time is <200ms, decrease it’s priority to 2. This has to be done in a separate task.
My problem is that first priority_set_task() is executed then matrix() and then communication. After that the simulator is caught in an infinite loop ( I think so, because I don’t receive any output after it. I think it’s priority_set_task(), but I’m not sure about it) I can’t seem to understand where I’m going wrong. I’m sure you guys will notice it, because it’s definitely something silly.
#define SIZE 10
#define ROW SIZE
#define COL SIZE
int count_matrix = 0;
int count_communication = 0;
int count_priority = 0;
uint32_t count = 0;
xTaskHandle matrix_handle, communication_handle, prioritysettask_handle;
int comm_priority = 1;
static void matrix_task()
{
vApplicationTickHook();
printf("Matrix_task is started\n");
fflush(stdout);
int i;
double** a = (double**)pvPortMalloc(ROW * sizeof(double*));
for (i = 0; i < ROW; i++) a[i] = (double*)pvPortMalloc(COL * sizeof(double));
double** b = (double**)pvPortMalloc(ROW * sizeof(double*));
for (i = 0; i < ROW; i++) b[i] = (double*)pvPortMalloc(COL * sizeof(double));
double** c = (double**)pvPortMalloc(ROW * sizeof(double*));
for (i = 0; i < ROW; i++) c[i] = (double*)pvPortMalloc(COL * sizeof(double));
double sum = 0.0;
int j, k, l;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
a[i][j] = 1.5;
b[i][j] = 2.6;
}
}
while (1) {
/*
* In an embedded systems, matrix multiplication would block the CPU for a long time
* but since this is a PC simulator we must add one additional dummy delay.
*/
count_matrix = count;
long simulationdelay;
for (simulationdelay = 0; simulationdelay < 1000000000; simulationdelay++)
;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
c[i][j] = 0.0;
}
}
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
sum = 0.0;
for (k = 0; k < SIZE; k++) {
for (l = 0; l < 10; l++) {
sum = sum + a[i][k] * b[k][j];
}
}
c[i][j] = sum;
}
}
vTaskDelay(100);
count_matrix = count - count_matrix;
vTaskPrioritySet(prioritysettask_handle, 5);
printf("matrix_task has stopped\n");
fflush(stdout);
printf("Execution time of matrix_task is %i ms\n", count_matrix);
fflush(stdout);
count_matrix = count;
}
}
static void communication_task()
{
while (1) {
count_priority = count;
vTaskSuspend(prioritysettask_handle);
printf("Sending data..................................................................\n");
fflush(stdout);
vTaskDelay(100);
printf("Data sent!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
fflush(stdout);
vTaskDelay(100);
count_communication = count - count_communication;
printf("Execution time of communication_task is %i ms\n", count_communication);
fflush(stdout);
count_communication = count;
vTaskResume(prioritysettask_handle);
vTaskPrioritySet(prioritysettask_handle, 5);
}
}
static void priority_set_task()
{
printf("Priority task has begun execution. Comm_priority is : %d. ", comm_priority);
if ((count_communication > 1000) && (comm_priority != 4))
{
comm_priority = 4;
printf("Execution time of communication_task is %i ms, increasing priority to 4\n", count_communication);
fflush(stdout);
vTaskPrioritySet(communication_handle, comm_priority);
//count_communication = 0;
}
else if ((count_communication < 200) && (comm_priority != 2))
{
comm_priority = 2;
printf("Execution time of communication_task is %i ms, decreasing priority to 2\n", count_communication);
fflush(stdout);
vTaskPrioritySet(communication_handle, comm_priority);
//count_communication = 0;
}
printf("priority set task has finished execution once. Its Execution time is %d ms\n", count_priority);
fflush(stdout);
count_priority = 0;
vTaskPrioritySet(prioritysettask_handle, 1);
}
int main(void)
{
prvInitialiseHeap();
vTraceEnable(TRC_START);
xTaskCreate((pdTASK_CODE)matrix_task, (signed char*)"Matrix", 1000, NULL, 3, &matrix_handle);
xTaskCreate((pdTASK_CODE)communication_task, (signed char*)"Communication", configMINIMAL_STACK_SIZE, NULL, 1, &communication_handle);
xTaskCreate(priority_set_task, "priority_set", configMINIMAL_STACK_SIZE, NULL, 5, &prioritysettask_handle);
vTaskStartScheduler();
for (;;);
return 0;
}
void vApplicationTickHook(void)
{
if (count == 0xFFFFFFFF)
{
count = 0;
count_matrix = 0;
count_communication = 0;
}
else
{
count++;
}
#if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
{
vFullDemoTickHookFunction();
}
#endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */
}