Hi everyone,
I am working with freertos10_xilinx and trying to solve a challenge presented (Digikey freertos challenge, video 7 on youtube)
In short, I need to create N Producer tasks that store their task number 3 times, in common global buffer and M Consumer tasks that print whatever there is in the global buffer.
Following is the code I am using
//To implement problem from DigiKey Introduction to RTOS Part 7.
//https://www.youtube.com/watch?v=95yUbClyf3E&list=PLEBQazB0HUyQ4hAPU1cJED6t3DU0h34bz&index=7
/* Task 1,2,3,4:
* Send task number to a queue
* Task 5,6:
* Receive data from the same queue and print it
*/
/****************************************** Application specific includes ******************************************/
#include "RtosUtil.h"
#include "app.h"
/****************************************** 'Main' specific ******************************************/
//Task Related
#define TASK_STACK_SIZE (1024)
#define BUFFER_SIZE (100)
#define NUM_OF_PROD_TASKS (5)
#define NUM_OF_CONS_TASKS (2)
//Semaphore Related
SemaphoreHandle_t SemCount;
//Queue Related
QueueHandle_t queue1;
QueueHandle_t queue2;
//Task Related
TaskHandle_t TaskProdHandle[NUM_OF_PROD_TASKS];
TaskHandle_t TaskConsHandle[NUM_OF_CONS_TASKS];
static void TaskProd(void *);
static void TaskCons();
//UART related
char* rcvstr=NULL;
char UartByte;
char dumbuf[BUFFER_SIZE];
uint32_t head=0,tail=0;
unsigned int i_ind;
/* Task 1,2,3,4:
* Send task number to a queue
*/
static void TaskProd(void *pvParameters){
//Use to load global buffer and control UART
taskENTER_CRITICAL();
for(int i=0;i<3;i++){
dumbuf[head]=*(uint32_t *)pvParameters;
head=(head+1) % BUFFER_SIZE;
}
xil_printf("Received in TaskProd :%d\r\n",*(uint8_t *)pvParameters);
taskEXIT_CRITICAL();
xSemaphoreGive(SemCount);
// vTaskDelete(TaskProdHandle[*(uint32_t *)pvParameters]);
}
/* Task 5,6:
* Receive data from the same queue and print it
*/
static void TaskCons(){
while(1){
xSemaphoreTake(SemCount,0);
//Use to load global buffer and control UART
taskENTER_CRITICAL();
xil_printf("Task number %d\r\n",dumbuf[tail]);
tail=(tail+1) % BUFFER_SIZE;
taskEXIT_CRITICAL();
//Add delay for the task
vTaskDelay(1000/portTICK_RATE_MS);
}
}
int main(){
xil_printf("Application started\r\n");
AllInit();
//Create Semaphores
SemCount=xSemaphoreCreateCounting(NUM_OF_PROD_TASKS,0);
if(SemCount!=NULL){
xil_printf("Counting Semaphore Created\r\n");
}
//Create Queues to send parameters among tasks
queue1=xQueueCreate(MAX_QUEUE_LEN,
sizeof(int));
queue2=xQueueCreate(MAX_QUEUE_LEN,
sizeof(int));
if(queue1==NULL && queue2==NULL)
xil_printf("Queues NOT created\r\n");
//Create Producer tasks
for(i_ind=0;i_ind<NUM_OF_PROD_TASKS;i_ind++){
sprintf(dumbuf,"Producer Task %d\r\n",i_ind);
//Create Task to send the task number.
xTaskCreate(TaskProd,
dumbuf,
TASK_STACK_SIZE,
(void *)&i_ind,
1,
&TaskProdHandle[i_ind]);
// xSemaphoreTake(SemCount, 0);
}
//Create Consumer tasks
for(i_ind=0;i_ind<NUM_OF_CONS_TASKS;i_ind++){
sprintf(dumbuf,"Consumer Task %d\r\n",i_ind);
//Create task to send task number
xTaskCreate(TaskCons,
dumbuf,
TASK_STACK_SIZE,
NULL,
1,
&TaskConsHandle[i_ind]);
}
//Start scheduler
vTaskStartScheduler();
return 0;
}
This is the output I am getting.
Application started
XGpio_CfgInitialize successful
Counting Semaphore Created
Received in TaskProd :2
Received in TaskProd :2
Received in TaskProd :2
Received in TaskProd :2
Received in TaskProd :2
Task number 2
Task number 2
I don’t see what I am doing wrong. Can someone guide me here?
Thanks in advance,
-ZoroIchimonji