Hi Joseph,
I had just tested the following source code now and confirmed the problem. In the main.c file, on top, if you define #define QUEUE_RECEIVE_STK_SIZE 64, code would work (keep sending and receiving of binary semaphore in the “queue_receive_task()”. But if you define it to be #define QUEUE_RECEIVE_STK_SIZE 512, everything hangs up and none of the 2 LEDs would even blink; and I got: (from the printf output)
Error:FreeRTOS\queue.c,730
semaphore sent failed in q receive. 1
Error:FreeRTOS\queue.c,1245
I am using uVision5 compiler by the way,and again it’s a STM32f103.
Thank you so much for checking!
------------------ the following is the source code May 16 2024 --------------------------
#include "system.h"
#include "SysTick.h"
#include "led.h"
#include "usart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "custom_GPIO.h"
#include "queue.h"
#include "FreeRTOSConfig.h"
//------------------------1A
#include "semphr.h"
//------------------------1A
#define START_TASK_PRIO 1
#define START_STK_SIZE 128
TaskHandle_t StartTask_Handler;
void start_task(void *pvParameters);
#define LED1_TASK_PRIO 1
#define LED2_TASK_PRIO 2
#define KEY_SCAN_PRIO 3
#define QUEUE_RECEIVE_PRIO 5
#define SEMAPHORE_RECEIVE_PRIO 4
#define LED1_STK_SIZE 50
#define LED2_STK_SIZE 50
#define KEY_SCAN_STK_SIZE 1024
#define SEMAPHORE_RECEIVE_STK_SIZE 512
//------------ PROBLEM POINT --------
//May 16 2024, 64 would work; but
//#define QUEUE_RECEIVE_STK_SIZE 64
//May 16 2024, but 512 would hang up the whole thing.
#define QUEUE_RECEIVE_STK_SIZE 512
//---------------
TaskHandle_t LED1Task_handler;
TaskHandle_t LED2Task_handler;
TaskHandle_t KeyScanTask_handler;
TaskHandle_t QueueReceiveTask_handler;
//SemaphoreHandle_t BinarySem_Handle=NULL;
SemaphoreHandle_t BinarySem_Handle;
void led1_task(void *pvParameters);
void led2_task(void *pvParameters);
void key_scan_and_send_task(void *pvParameters);
void queue_receive_task(void *pvParameters); //Apr 23 2024 queue receiver
void semaphore_receive_task(void *pvParameters); //Apr 24 2024 semaphore Receiver
void send_task(void * pvParameters);
QueueHandle_t Test_Queue =NULL;
#define QUEUE_LEN 4
#define QUEUE_SIZE 4
/*************************************
* FN NAME :main
**************************************/
int main(void)
{
static uint16_t i;
LED_Init();
PB4_PB5_Input_Init(); //added Apr 20 2024
SysTick_Init(72); //problem if ran
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
My_USART2_Init(115200); //note: in lecture, it's USART1_INIT(115200);
xTaskCreate( (TaskFunction_t ) start_task,
(const char* ) "start_task",
(uint16_t ) START_STK_SIZE,
(void* ) NULL,
(UBaseType_t ) START_TASK_PRIO,
(TaskHandle_t* ) &StartTask_Handler
);
vTaskStartScheduler();
}
void start_task(void *pvParameters)
{
BaseType_t x1stReturnValue=pdPASS; //May 7 2024. create a return value variable and init it to pdPASS(=pdTRUE).
uint16_t i;
taskENTER_CRITICAL();
Test_Queue = xQueueCreate ( (UBaseType_t) QUEUE_LEN,
(UBaseType_t) QUEUE_SIZE
);
BinarySem_Handle = xSemaphoreCreateBinary();
if(BinarySem_Handle!=NULL)
{ printf("BinarySem_Handle created successfully \n" );
printf("BinarySem_Handle's value: %6x\n", BinarySem_Handle );
printf("-----------------------------\n" );
}
for (i=0; i<2; i++)
{
//---------------1G
x1stReturnValue=xSemaphoreGive( BinarySem_Handle); //May 7 2024.
if(x1stReturnValue==pdPASS)
{ printf("Initial semaphore sent successfully! %d\n",i);
}
else
{ printf("Initial semaphore sent failed. %d\n",i);
}
//--------------1G
//------------------1H
x1stReturnValue=xSemaphoreTake( BinarySem_Handle, portMAX_DELAY );
if(x1stReturnValue==pdPASS)
{ printf("Initial semaphore received successfully! %d\n",i);
}
else
{ printf("Initial semaphore received failed. %d\n",i);
}
//------------------1H
}
xTaskCreate( (TaskFunction_t ) led1_task,
(const char* ) "led1_task",
(uint16_t ) LED1_STK_SIZE,
(void* ) NULL,
(UBaseType_t ) LED1_TASK_PRIO,
(TaskHandle_t* ) &LED1Task_handler
);
xTaskCreate( (TaskFunction_t ) led2_task,
(const char* ) "led2_task",
(uint16_t ) LED2_STK_SIZE,
(void* ) NULL,
(UBaseType_t ) LED2_TASK_PRIO,
(TaskHandle_t* ) &LED2Task_handler
);
xTaskCreate( (TaskFunction_t ) queue_receive_task,
(const char* ) "queue receive_task",
(uint16_t ) QUEUE_RECEIVE_STK_SIZE,
(void* ) NULL,
(UBaseType_t ) QUEUE_RECEIVE_PRIO,
(TaskHandle_t* ) &QueueReceiveTask_handler
);
xTaskCreate( (TaskFunction_t ) key_scan_and_send_task,
(const char* ) "key_scan_and_send_task",
(uint16_t ) KEY_SCAN_STK_SIZE,
(void* ) NULL,
(UBaseType_t ) KEY_SCAN_PRIO,
(TaskHandle_t* ) &KeyScanTask_handler
);
xTaskCreate( (TaskFunction_t ) semaphore_receive_task,
(const char* ) "semphr receive_task",
(uint16_t ) SEMAPHORE_RECEIVE_STK_SIZE,
(void* ) NULL,
(UBaseType_t ) SEMAPHORE_RECEIVE_PRIO,
(TaskHandle_t* ) &BinarySem_Handle
);
vTaskDelete(StartTask_Handler);
taskEXIT_CRITICAL();
}
void led1_task(void * pvParameters)
{
//============================
BaseType_t x1stReturnValue=pdPASS; //May 7 2024.
uint16_t i;
while(1)
{
PBout(6) = 1; //RED LED.
vTaskDelay(1000);
PBout(6) = 0; //RED LED.
vTaskDelay(1000);
}
}
void led2_task(void * pvParameters)
{
while(1)
{
PBout(7) = 0; //BLUE LED.
vTaskDelay(10);
PBout(7) = 1; //BLUE LED.
vTaskDelay(120);
PBout(7) = 0; //BLUE LED.
vTaskDelay(10);
PBout(7) = 1; //BLUE LED.
vTaskDelay(1500);
}
}
void key_scan_and_send_task(void *pvParameters)
{
BaseType_t xReturnValue=pdPASS;
//Apr 23 2024. create a return value variable and init it to pdPASS(=pdTRUE).
uint32_t dataPB4_to_send=0xb4; //send it upon PB4 pin grouding.
uint32_t dataPB5_to_send=0xb5; //send it upon PB5 pin grouding.
while(1)
{ if(PBin(4)==0) //semaphore test: manually grounded PB4.
{ //-------------------1C
//the following suspension code worked. commented out to make room for semaphore tests.
//Apr 24 2024
//printf("PB4 pin depressed. \n");
//printf("Both blue and red led tasks suspended! \n");
//vTaskSuspend(LED1Task_handler);
//vTaskSuspend(LED2Task_handler);
//-------------------1C
//---------------1E
xReturnValue=xSemaphoreGive( BinarySem_Handle); //did not give successfully. not sure why.Apr 24 2024.
if(xReturnValue==pdPASS)
{ printf("PB4 semaphore sent successfully! \n");
}
else
{ printf("Failed sending PB4 semaphore. \n");
}
//--------------1E
}
/*-------------1J
if(PBin(5)==0) //queue test: manually grounded PB5.
{ printf("PB5 pin from scan task depressed. \n");
}
1J--------*/
vTaskDelay(100);
}
}
void queue_receive_task(void *pvParameters)
//Apr 23 2024 receiver of list messages/items.
{
BaseType_t xReturnValue=pdTRUE; //create return value variable and set it to pdTRUE for now.
uint32_t received_from_queue; //create a variable for receiving.
BaseType_t x1stReturnValue=pdPASS; //May 7 2024.
uint16_t i=0;
while(1)
{
i++;
x1stReturnValue=xSemaphoreGive( BinarySem_Handle); //May 7 2024.
if(x1stReturnValue==pdPASS)
{ printf("semaphore sent ok in q receive! %d \n", i);
}
else
{ printf("semaphore sent failed in q receive. %d \n", i);
}
//--------------1G
x1stReturnValue=xSemaphoreTake( BinarySem_Handle, portMAX_DELAY );
if(x1stReturnValue==pdPASS)
{ printf("semaphore received ok in q receive! %d \n", i);
}
else
{ printf("semaphore received failed in q receive. %d \n", i);
}
vTaskDelay(1000);
}
}
void semaphore_receive_task(void *pvParameters)
//Apr 23 2024 receiver of list messages/items.
{
BaseType_t xReturnValue=pdTRUE; //create return value variable and set it to pdTRUE for now.
//uint32_t received_from_semaphore; //create a variable for receiving.
while(1)
{ if(PBin(5)==0) //queue test: manually grounded PB5.
{
printf("PB5 pin from receive task depressed. \n");
//xReturnValue=xSemaphoreTake( BinarySem_Handle, portMAX_DELAY );
//xReturnValue=xSemaphoreTake( BinarySem_Handle, 0 );
//will keep waiting until it gets the message. This waiting is equivalent to
//vTaskDelay(), which introduce a blocked state of this task. a block state
//for any task must exist otherwise it will consume cpu resources whenever
//possible, or keep other tasks from executing if it had a higher priority.
if(xReturnValue==pdTRUE)
{ printf("-------Semaphore received successfully. \n");
}
else
{ printf("ERROR receiving semaphore.\n");
}
vTaskDelay(50);
}
}
}
//---------------- end of test source code May 16 2024 -----------------------