Mpc5748g-freertos-semaphore

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Mpc5748g-freertos-semaphore

1,047 Views
kalai
Contributor I

HI,

 

  i tried to run the simple freertos with semaphore and queue,but samphore its not working.i paste the code also please check the code and suggest me how to solve it.

 

we have to change any software setting for heap?

/*********************************************************************************************************/


/* Including needed modules to compile this module/procedure */
#include "Cpu.h"
#include "FreeRTOS.h"
#include "dmaController1.h"
#include "pin_mux.h"
#include "uart_pal1.h"
#include "clockMan1.h"
#include "serialbuf.h"
#include"usr7s4.h"
#include"ringbuf.h"

volatile int exit_code = 0;
/* User includes (#include below this line is not maintained by Processor Expert) */
#include <stdint.h>
#include <stdbool.h>
#include "task.h"


#define mainLED_DELAY ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
#define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )


#include <string.h>

#define welcomeMsg "This example is an simple echo using UART_PAL\r\n\
it will send back any character you send to it.\r\n\
The board will greet you if you send 'Hello!'\r\
\nNow you can begin typing:\r\n"

#define msg "create semaphore \r\n"
#define msg1 "semaphore not create \r\n"
#define msg2 "create semaphore inside \r\n"
/* Error message displayed at the console, in case data is received erroneously */
#define errorMsg "An error occurred! The application will stop!\r\n"

/* Timeout in ms for blocking operations */
#define TIMEOUT 200UL
#define BUFFER_SIZE 25UL

#define DEVKIT
#define START_OS_DEMO 1
/* Buffer used to receive data from the console */
uint8_t buffer[BUFFER_SIZE];
uint8_t bufferIdx;
uint32_t data;
//uint8_t buffer1[BUFFER_SIZE];
static QueueHandle_t recv_queue1;
static SemaphoreHandle_t binarysem;
//static SemaphoreHandle_t uart_mx;
//static SemaphoreHandle_t data_a_sem;
/* UART rx callback for continuous reception, byte by byte*/
void rx_Callback(void *driverState, uart_event_t event, void *userData)
{
//* Unused parameters
(void)driverState;
(void)userData;
// BaseType_t xHigherPriorityTaskWoken=pdFALSE;
// uint32_t leftNum;
/*
if(event == UART_EVENT_TX_EMPTY)
{
UART_SendDataBlocking(&uart_pal1_instance, (uint8_t *)msg, strlen(msg), TIMEOUT);
}
*/
//* Check the event type
// sprintf("callback inside");
if (event == UART_EVENT_RX_FULL)
{
//* The reception stops when newline is received or the buffer is full
if ((buffer[bufferIdx] != '\n') && (bufferIdx != (BUFFER_SIZE - 2U)))
{
//* Update the buffer index and the rx buffer
bufferIdx++;
UART_SetRxBuffer(&uart_pal1_instance, &buffer[bufferIdx], 1U);
}

}

 

}

/*void set_app1(){


serial_init();
usr7s4_init();
// serial_recv_task();

}*/
//void binarcreate()
//{
// binarysem=xSemaphoreCreateBinary();
// if( binarysem == NULL )
// {
// UART_SendDataBlocking(&uart_pal1_instance, (uint8_t *)msg, strlen(msg), TIMEOUT);
// }
// else
// {
// UART_SendDataBlocking(&uart_pal1_instance, (uint8_t *)msg1, strlen(msg1), TIMEOUT);
// }
//
//}

#if START_OS_DEMO

void vUARTTask1()
{
// Write your local variable definition here
status_t status;
uint32_t bytesRemaining = 0;
BaseType_t xHigherPriorityTaskWoken=pdFALSE;

for( ;; )
{
xSemaphoreTake(binarysem , portMAX_DELAY );
// xSemaphoreTake(binarysem , portMAX_DELAY );
UART_ReceiveData(&uart_pal1_instance, buffer, 1U);
// Wait for transfer to be completed
while(UART_GetReceiveStatus(&uart_pal1_instance, &bytesRemaining) == STATUS_BUSY);

// Check the status
status = UART_GetReceiveStatus(&uart_pal1_instance, &bytesRemaining);

xQueueSend(recv_queue1,&buffer,portMAX_DELAY);
xSemaphoreGive(binarysem);
vTaskDelay(1);
}
}

void vUARTTask2()
{
uint8_t buffer1[BUFFER_SIZE];
uint8_t buffer2[BUFFER_SIZE];
uint32_t data1;
while(1)
{
xSemaphoreTake(binarysem , portMAX_DELAY );
xQueueReceive(recv_queue1,&buffer1,portMAX_DELAY);


UART_SendDataBlocking(&uart_pal1_instance,(uint8_t*)buffer1, strlen(buffer1), 1000);
xSemaphoreGive(binarysem);

vTaskDelay(1);
}
}

#endif

int main(void)
{
/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
#ifdef PEX_RTOS_INIT
PEX_RTOS_INIT(); /* Initialization of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of Processor Expert internal initialization. ***/

 

/* Initialize and configure clocks
* - Setup system clocks, dividers
* - see clock manager component for more details
*/
CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_FORCIBLE);

/* Initialize pins */
PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);

/* Initialize UART PAL over LINFlexD */
UART_Init(&uart_pal1_instance, &uart_pal1_Config0);
//binarcreate();
recv_queue1= xQueueCreate(2,sizeof(buffer));
//SemaphoreHandle_t binarysem=NULL;
binarysem=xSemaphoreCreateBinary();
if( binarysem == NULL )
{
UART_SendDataBlocking(&uart_pal1_instance, (uint8_t *)msg1, strlen(msg1), TIMEOUT);
}
else
{
UART_SendData(&uart_pal1_instance, (uint8_t *)msg, strlen(msg));
}

 

// if(recv_queue1==NULL)
// {
// UART_SendData(&uart_pal1_instance, (uint8_t *)msg,12);
// // UART_SendDataBlocking(&uart_pal1_instance, "error queue", 12, TIMEOUT);
// }
// else
// {
// UART_SendData(&uart_pal1_instance, (uint8_t *)msg,17);
// // UART_SendDataBlocking(&uart_pal1_instance, "receive creation", 17, TIMEOUT);
// }

#if START_OS_DEMO


//xTaskCreate( vUARTTask, ( const char * const ) "UartTask", configMINIMAL_STACK_SIZE, (void*)1, mainLED_TASK_PRIORITY+1, NULL );

xTaskCreate( vUARTTask2, ( const char * const ) "UartTask", 128,NULL, mainLED_TASK_PRIORITY+2, NULL );
xTaskCreate( vUARTTask1, ( const char * const ) "UartTask_tx", 128, NULL, mainLED_TASK_PRIORITY+1, NULL );


/* Start the scheduler. */
vTaskStartScheduler();
#endif

 

/*** Don't write any code pass this line, or it will be deleted during code generation. ***/
/*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
#ifdef PEX_RTOS_START
PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of RTOS startup code. ***/
/*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
for(;;) {
if(exit_code != 0) {
break;
}
}
return exit_code;
/*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
}

 

thanks

kalai

 

0 Kudos
Reply
1 Reply

1,028 Views
PetrS
NXP TechSupport
NXP TechSupport

Hi,

not sure how this code would run.
I see both task is taking semaphore first, blocking task indefinitely. Who is "giving" semaphore to unblock tasks?
I guess the code will run using just queue.

I can recommend to check FreeRTOS support/community to get more info:
https://www.freertos.org/RTOS-contact-and-support.html 

Semaphores usage is described in :
https://www.freertos.org/Embedded-RTOS-Binary-Semaphores.html

BR, Petr

0 Kudos
Reply