I have a program testing on semaphores and mutex. However, when remove the static keyword I get some garbage values in my output. I have posted my main and my tasks below along with screenshots of the output:
int main(void)
{
/* USER CODE BEGIN 1 */
BaseType_t status;
volatile int tbuf[limit];
char task_name[12];
const static int args [] = {0,1,2,3,4};
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
FilledSlotSem = xSemaphoreCreateCounting(5,0);
EmptySlotSem = xSemaphoreCreateCounting(5,5);
mutex = xSemaphoreCreateMutex();
if(mutex != NULL && FilledSlotSem != NULL && EmptySlotSem != NULL )
{
// for(int i = 0; i < prodTask;i++)
// {
// tbuf[i]= i;
// sprintf(task_name,"Producer %i", i);
// status =xTaskCreate(producer,task_name, 600,(void*)(uint32_t)&tbuf[i], 1, NULL);
// configASSERT(status == pdPASS);
// }
status =xTaskCreate(producer,"Producer 0", 600,(void*)(uint32_t)&args[0],1, NULL);
configASSERT(status == pdPASS);
status =xTaskCreate(producer,"Producer 1", 600,(void*)(uint32_t)&args[1],1, NULL);
configASSERT(status == pdPASS);
status =xTaskCreate(producer,"Producer 2", 600,(void*)(uint32_t)&args[2],1, NULL);
configASSERT(status == pdPASS);
status =xTaskCreate(producer,"Producer 3", 600,(void*)(uint32_t)&args[3],1, NULL);
configASSERT(status == pdPASS);
status =xTaskCreate(producer,"Producer 4", 600,(void*)(uint32_t)&args[4],1, NULL);
configASSERT(status == pdPASS);
for(int i = 0; i < consuTask;i++)
{
sprintf(task_name, "Consumer %i", i);
status =xTaskCreate(consumer,task_name,600,NULL, 2, NULL);
configASSERT(status == pdPASS);
}
vTaskStartScheduler();
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/* USER CODE BEGIN 4 */
void producer(void *parameters)
{
char tbuf[28];
int num = *(int *)parameters;
for (int i = 0; i<numWrite; i++)
{
xSemaphoreTake(EmptySlotSem,portMAX_DELAY);
//Lock access to the buffer
xSemaphoreTake(mutex,portMAX_DELAY);
sprintf(tbuf,"Printing val of %i\n",num);
HAL_UART_Transmit(&huart1,(uint8_t*)tbuf, strlen(tbuf),HAL_MAX_DELAY);
buf[head] = num;
head = (head +1)%limit;
vTaskDelay(pdMS_TO_TICKS(500));
xSemaphoreGive(mutex);//Release lock
//Signal to consumer task is ready to run
xSemaphoreGive(FilledSlotSem);
}
vTaskDelete(NULL);
}
void consumer(void *parameters)
{
int val;
char tbuf[8];
for (;;)
{
xSemaphoreTake(FilledSlotSem,portMAX_DELAY);
val = buf[tail];
tail = (tail+1)%limit;
xSemaphoreTake(mutex,portMAX_DELAY);
sprintf(tbuf,"%i\n",val);
HAL_UART_Transmit(&huart1,(uint8_t*)tbuf,strlen(tbuf),HAL_MAX_DELAY);
vTaskDelay(pdMS_TO_TICKS(500));
xSemaphoreGive(mutex);
//Signal to producer task is ready to run
xSemaphoreGive(EmptySlotSem);
}
}