#include <stdio.h>
/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include "fsl_pit.h"
/* Freescale includes. */
#include "fsl_device_registers.h"
#include "fsl_debug_console.h"
#include "board.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "semphr.h"
SemaphoreHandle_t xSemaphore = NULL;
TaskHandle_t myTaskHandle = NULL;
#define PIT_IRQ_ID PIT_IRQn
/* Get source clock for PIT driver */
#define PIT_SOURCE_CLOCK CLOCK_GetFreq(kCLOCK_OscClk)
volatile bool pitIsrFlag = false;
void PIT_IRQHandler(void)
{
// Clear the interrupt flag
PIT_ClearStatusFlags(PIT, kPIT_Chnl_0, kPIT_TimerFlag);
pitIsrFlag = true;
// Notify the RTOS that an interrupt occurred
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
PRINTF("In IRQ_HANDLER\r\n");
xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );
//vTaskNotifyGiveFromISR(myTaskHandle, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
void PIT_Task(void *pvParameters)
{
PRINTF(" IN PIT TAsk\r\n");
while (1)
{
// Wait for the PIT interrupt notification
if( xSemaphoreTake( xSemaphore, portMAX_DELAY ) == pdTRUE )
{
// ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
// Handle the PIT interrupt
printf("PIT interrupt occurred\n");
}
}
}
int main(void)
{
/* Structure of initialize PIT */
pit_config_t pitConfig;
/* Board pin, clock, debug console init */
BOARD_ConfigMPU();
BOARD_InitPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();
xSemaphore = xSemaphoreCreateBinary();
if(xSemaphore != NULL)
{
PRINTF("semaphore created\r\n");
}
/* Enable clock gate for GPIO1 */
CLOCK_EnableClock(kCLOCK_Gpio1);
/* Set PERCLK_CLK source to OSC_CLK*/
CLOCK_SetMux(kCLOCK_PerclkMux, 1U);
/* Set PERCLK_CLK divider to 1 */
CLOCK_SetDiv(kCLOCK_PerclkDiv, 0U);
/*
* pitConfig.enableRunInDebug = false;
*/
PIT_GetDefaultConfig(&pitConfig);
/* Init pit module */
PIT_Init(PIT, &pitConfig);
/* Set timer period for channel 0 */
// PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, USEC_TO_COUNT(1000000, PIT_SOURCE_CLOCK));
PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, USEC_TO_COUNT(10000U, PIT_SOURCE_CLOCK));
/* Enable timer interrupts for channel 0 */
PIT_EnableInterrupts(PIT, kPIT_Chnl_0, kPIT_TimerInterruptEnable);
/* Enable at the NVIC */
EnableIRQ(PIT_IRQ_ID);
/* Start channel 0 */
PRINTF("\r\nStarting channel No.0 ...\r\n");
PIT_StartTimer(PIT, kPIT_Chnl_0);
xTaskCreate(PIT_Task, "PIT_Task", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, &myTaskHandle);
vTaskStartScheduler();
for(;;);
}