Anda di halaman 1dari 4

/* Standard includes.

*/
#include <stdlib.h>

/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"

/* Demo application includes. */


#include "partest.h"
#include "flash.h"
#include "integer.h"
#include "comtest2.h"
#include "serial.h"
#include "PollQ.h"
#include "BlockQ.h"
#include "semtest.h"
#include "dynamic.h"

/*-----------------------------------------------------------*/

/* Constants to setup I/O and processor. */


#define mainTX_ENABLE ( ( unsigned portLONG ) 0x00010000 ) /*
UART1. */
#define mainRX_ENABLE ( ( unsigned portLONG ) 0x00040000 ) /*
UART1. */
#define mainBUS_CLK_FULL ( ( unsigned portCHAR ) 0x01 )
#define mainLED_TO_OUTPUT ( ( unsigned portLONG ) 0xff0000 )

/* Constants for the ComTest demo application tasks. */


#define mainCOM_TEST_BAUD_RATE ( ( unsigned portLONG ) 115200 )
#define mainCOM_TEST_LED (3)

/* Priorities for the demo application tasks. */


//#define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
//#define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
//#define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
//#define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
//#define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
//#define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )

/* Constants used by the "check" task. As described at the head of this file
the check task toggles an LED. The rate at which the LED flashes is used to
indicate whether an error has been detected or not. If the LED toggles every
3 seconds then no errors have been detected. If the rate increases to 500ms
then an error has been detected in at least one of the demo application tasks. */
#define mainCHECK_LED (7)
#define mainNO_ERROR_FLASH_PERIOD ( ( portTickType ) 3000 /
portTICK_RATE_MS )
#define mainERROR_FLASH_PERIOD ( ( portTickType ) 500 /
portTICK_RATE_MS )

/*-----------------------------------------------------------*/

/*Batch b2
Roll no 16
Title: scheduling two tasks to print characters on serial port. Observe output with and
without using SEMAPHORE*/

#include"LPC21xx.h"
//#include"FreeRTOS.h"
//#include"task.h"
//#include"list.h"
#include"semphr.h"

#define TASK_PRIORITY1 (tskIDLE_PRIORITY + 1)


#define TASK_PRIORITY2 (tskIDLE_PRIORITY + 2)

xTaskHandle xHandle1, xHandle2; //handle to the created semaphore


xSemaphoreHandle xSemaphore = NULL;

static void uart1(void *pvParameters);


static void uart2(void *pvParameters);
void Init_hardware(void);

int main(void)
{
Init_hardware();
xTaskCreate(uart1, "uart1",configMINIMAL_STACK_SIZE,
NULL,TASK_PRIORITY1,&xHandle1);

xTaskCreate(uart2, "uart2",configMINIMAL_STACK_SIZE,
NULL,TASK_PRIORITY2,&xHandle2);

vSemaphoreCreateBinary(xSemaphore); //macro that implements


semaphore

vTaskStartScheduler();

}
void Init_hardware()
{
PINSEL0 = 0x00000005; //set line ctrl reg. for UART0 with above parameters
U0LCR = 0x83; //set bit 7 to 1 to set divisor latches
U0DLM = 0X01;
U0DLL = 0X86;
U0LCR = 0x03;

}
static void uart1(void *pvParameters)
{
long int n;
char msg[]={"\n\rTASK1"};
(void) pvParameters;
while(1)
{
n=0;
if(xSemaphore!=NULL)
{
if(xSemaphoreTake(xSemaphore,(portTickType)10)==
pdTRUE)
{
n=0;
while(msg[n]!='\0') //chk end of msg
{
U0THR = msg[n++];
while(!(U0LSR&0x40));
}
}
}
xSemaphoreGive(xSemaphore);
}
}

static void uart2(void *pvParameters)


{
long int n;
char msg[]={"\n\rTASK2"};
(void) pvParameters;
while(1)
{
n=0;
if(xSemaphore!=NULL)
{
if(xSemaphoreTake(xSemaphore,(portTickType)10)==
pdTRUE)
{
n=0;
while(msg[n]!='\0') //chk end of msg
{
U0THR = msg[n++];
while(!(U0LSR&0x40));
}
}
}
xSemaphoreGive(xSemaphore);
vTaskDelay(20);
}
}

Anda mungkin juga menyukai