Advertisement
KingKj52

Untitled

Feb 11th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.55 KB | None | 0 0
  1. #include "fsl_flexcan.h"
  2. #include "board.h"
  3. #include "pin_mux.h"
  4. #include "clock_config.h"
  5. #include "TransmitReceive.c"
  6. #include "SDCARD_Interface.c"
  7.  
  8. #include <stdio.h>
  9.  
  10. /*Kernal includes*/
  11. #include <FreeRTOS.h>
  12. #include "task.h"
  13. #include "queue.h"
  14. #include "PIT.c"
  15. #include "PIT.h"
  16. #include "cubesat.h"
  17. #include "CAN.c"
  18.  
  19. /*!
  20.  * @brief Application entry point.
  21.  */
  22.  
  23. /*Generating Queue*/
  24. xQueueHandle Global_Queue_Handle = 0;
  25.  
  26. //declaring task handlers-the binary semephore
  27. TaskHandle_t xTask1 = NULL, xTask2 = NULL;
  28. //static TaskHandle_t xHandle = NULL;
  29.  
  30. //eTaskState eTaskGetState(TaskHandle_t xHandle);
  31.  
  32. int key = 0;
  33.  
  34. int Queue_Length = 10;
  35.  
  36. /*void PIT_HANDLER(void)
  37. {
  38.     //BaseType_t xYielldRequired;
  39.  
  40.  
  41.      Clear interrupt flag.
  42.     PIT_ClearStatusFlags(PIT, kPIT_Chnl_0, PIT_TFLG_TIF_MASK);
  43.     //printf("\r\n Channel No.0 interrupt is occured !");
  44.     xTaskResumeFromISR(xHandle);
  45.     xYielldRequired = xTaskResumeFromISR(xHandle);
  46.     if(xYielldRequired == pdTRUE)
  47.     {
  48.         kPIT_Chnl_0YIELD_FROM_ISR();
  49.     }
  50.     //xTaskNotifyGive(xHandle);
  51.     //pitIsrFlag = true;
  52. }*/
  53.  
  54.  
  55. static void sender_task(void*p)
  56. {
  57.     for(;;)
  58.     {
  59.         printf("sender_task - Running         \n\r");
  60.         vTaskDelay(500);
  61.         printf("sender_task - sending         \n\r");
  62.         // xTask2 +1
  63.         //xTaskNotifyGive(xTask2);
  64.         printf("sender_task - sleep         \n\r");
  65.         // xTask2 -1
  66.         ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  67.     }
  68.     //////////////////////////////
  69. /*  int i = 0;
  70.     while(1){
  71. //if(xTask1 != NULL){
  72.     //ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  73.         //vTaskResume(xTask2);
  74.         printf("\n\rSending %i to receiver task", i);
  75.         if (!xQueueSend(Global_Queue_Handle, &i, 1000)){
  76.             puts("\n\rFailed to send to queue");
  77.         }
  78.  
  79.         //++key;
  80.         ++i;
  81.         vTaskDelay(2000);
  82.  
  83.         ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  84.         xTaskNotifyGive(xTask2);
  85.  
  86.  
  87.         //vTaskResume(xTask2);
  88. //}
  89.     }*/
  90.  
  91.     /////////////////////////////
  92. }
  93. //-------------------------------------------------------------
  94. /* This function must be defined in a C source file, not the FreeRTOSConfig.h header
  95. file. */
  96. /*void vAssertCalled( const char *pcFile, uint32_t ulLine )
  97. {
  98.   Inside this function, pcFile holds the name of the source file that contains
  99.  the line that detected the error, and ulLine holds the line number in the source
  100.  file. The pcFile and ulLine values can be printed out, or otherwise recorded,
  101.  before the following infinite loop is entered.
  102. printf( pcFile,"\n\r"  );
  103. printf( "\n\r%i",ulLine );
  104.   Disable interrupts so the tick interrupt stops executing, then sit in a loop
  105.  so execution does not move past the line that failed the assertion.
  106.  //taskDISABLE_INTERRUPTS();
  107.  for( ;; );
  108. }*/
  109. /*-----------------------------------------------------------*/
  110.  
  111. cs_event recv_event;
  112.  
  113. static void receiver_task(void*p)
  114. {
  115.     for(;;)
  116.     {
  117.         printf("receiver_task - sleep        \n\r");
  118.         // xTask1 -1 therefore blocked
  119.         ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  120.         printf("receiver_task - running       \n\r");
  121.  
  122.         recv_event.timestamp = -1;
  123.  
  124.         xQueueReceive(Global_Queue_Handle, &recv_event, 0);
  125.  
  126.         printf("Pulled event from queue.  Message source:%d\n\r", recv_event.sourcedest);
  127.  
  128.         if(recv_event.sourcedest == FROM_CAN){
  129.             printf("Word1:0x%08x\n\r",recv_event.canframe.dataWord0);
  130.             printf("Word1:0x%08x\n\r",recv_event.canframe.dataWord1);
  131.         }
  132.  
  133.  
  134.         // xTask1 +1 therefore unblocked
  135.         //xTaskNotifyGive(xTask1);
  136.         //xTaskNotifyGive(xTask1);
  137.     }
  138.  
  139.     //////////////////////////////////////////////////
  140.     /*int rx_int = 0;
  141.     //while(key>0){
  142.     while(1){
  143.         //if(xTask1 != NULL){
  144.             ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  145.         //if(ulTaskNotifyTake(pdTRUE, portMAX_DELAY)){
  146.         //while(key>0){
  147.         if (xQueueReceive(Global_Queue_Handle, &rx_int, 1000)){
  148.             printf("\n\rReceived %i", rx_int);
  149.         }
  150.         else{
  151.             puts("\n\rTime elapsed without receiving data");
  152.         }
  153.         //--key;
  154.         xTaskNotifyGive(xTask1);
  155.         //}
  156.     }*/
  157.     ////////////////////////////////////////////
  158.     }
  159.  
  160.     //vTaskSuspend(NULL);
  161.  
  162.  
  163.  
  164.     //}
  165. int pit_task_counter = 0;
  166.  
  167. static void PIT_task(void*p)
  168. {
  169.     uint32_t ulNotificationValue;
  170.     const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 200 );
  171.     for(;;)
  172.     {
  173.         printf("\r\n PIT_task - 1");
  174. /*      if(xHandle == NULL)
  175.         {
  176.             printf("\r\n xHandle == NULL");
  177.         }*/
  178.  
  179.         //configASSERT( xHandle == NULL );
  180.  
  181.         /* Store the handle of the calling task. */
  182.         xHandle = xTaskGetCurrentTaskHandle();
  183.         //printf(" 1\n");
  184.         //printf("0x%08x",xHandle);
  185.         //configASSERT( xHandle == NULL );
  186.         //printf(" PIT_task - %d",pit_task_counter++);
  187.  
  188.  
  189.  
  190.         /* Store the handle of the calling task. */
  191.         //xHandle = xTaskGetCurrentTaskHandle();
  192.         printf("\r\n PIT_task - putting itself to sleep now\r\n");
  193.         //ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
  194.         //ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  195.         //printf("\r\n PIT_task - Channel No.0 task is occured !");
  196.         //xTaskNotifyGive(xHandle);
  197.         // xHandle -1 therefore unblocked
  198.         //taskENABLE_INTERRUPTS();
  199.         ulNotificationValue = ulTaskNotifyTake( pdFALSE, portMAX_DELAY );
  200.         //printf("0x%08x",ulNotificationValue);
  201.         if( ulNotificationValue == 1 )
  202.            {
  203.                 printf("The transmission ended as expected.");
  204.                 //ulTaskNotifyTake( pdTRUE, xMaxBlockTime );
  205.             }
  206.             else
  207.             {
  208.                   printf("The call to ulTaskNotifyTake() timed out.");
  209.             }
  210.     }
  211. }
  212. int main(void){
  213.  
  214.     /* Init board hardware. */
  215.  
  216.     Board_Init();
  217.     MPU_Enable(MPU, false);
  218.     NVIC_SetPriority ( 48, 14 ); /* CAN0 is IRQ 75.  MAX priority is 5, so we need to set to
  219.     next highest even number than 2*5, so priority = 12 */
  220.     NVIC_SetPriority ( CAN0_ORed_Message_buffer_IRQn, 12 );
  221.  
  222.     PIT_INIT();
  223.  
  224.     FLEXCAN_INIT();
  225.     /* Add your code here */
  226.  
  227.  
  228.     /*Creating Queue*/
  229.     Global_Queue_Handle = xQueueCreate(Queue_Length, sizeof(cs_event));
  230.  
  231.     xTaskCreate(sender_task, (signed char*) "tx", 500, NULL, tskIDLE_PRIORITY, &xTask1);
  232.     xTaskCreate(receiver_task, (signed char*) "rx", 500, NULL, tskIDLE_PRIORITY, &xTask2);
  233.     //xTaskCreate(PIT_task, (signed char*) "pit", 500, NULL, tskIDLE_PRIORITY, &xHandle);
  234.  
  235.     vTaskStartScheduler();
  236.  
  237.  
  238.  
  239. /*  char a = 0;
  240.     while(1){
  241.         printf("\n\rC&DH Monitor>");
  242.         a = getchar();
  243.         printf("%c\n\r",a);
  244.         switch ( a )
  245.         {
  246.  
  247.         case 'l':
  248.             //xTaskCreate(Transmit_RTR, (signed char*) "Transmit_RTR", 1024, NULL, 1, NULL);
  249.  
  250.         case 't':
  251.             //xTaskCreate(Transmit, (signed char*) "Transmit_RTR", 1024, NULL, 1, NULL);
  252.             xTaskCreate(sender_task, (signed char*) "tx", 500, NULL, tskIDLE_PRIORITY, xTask1);
  253.             break;
  254.         case 'r':
  255.             //xTaskCreate(Receive, (signed char*) "Transmit_RTR", 1024, NULL, 1, NULL);
  256.             xTaskCreate(receiver_task, (signed char*) "rx", 500, NULL, tskIDLE_PRIORITY, xTask2);
  257.             break;
  258.         case 's':
  259.             //xTaskCreate(SDCARD_Interface, (signed char*) "Transmit_RTR", 1024, NULL, 1, NULL);
  260.             printf("012345678901234556789\n\r");
  261.             break;
  262.         case 'q':
  263.             //xTaskCreate(hello, (signed char*) "hello", 512, NULL, 1, NULL);
  264.             vTaskStartScheduler();
  265.             break;
  266.         default:
  267.         {
  268.             //vTaskStartScheduler();
  269.             break;
  270.         }
  271.         }
  272.     }*/
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.     //xTaskCreate(sender_task, (signed char*) "tx", 1024, NULL, 1, NULL);
  280.     //xTaskCreate(receiver_task, (signed char*) "rx", 1024, NULL, 1, NULL);
  281.     /*  if (pdPASS !=
  282.             xTaskCreate(AccessCardTask, "AccessCardTask", AccessCardTask_STACK_SIZE, NULL, SD_TASK_PRIORITY, NULL))
  283.     {
  284.         return -1;
  285.     }*/
  286.  
  287.  
  288.  
  289.  
  290.     /* Start the tasks and timer running. */
  291.     //vTaskStartScheduler();
  292.  
  293.     /* Scheduler should never reach this point. */
  294.     while (true)
  295.     {
  296.         PrintCanStatus();
  297.     }
  298.  
  299.     for(;;) { /* Infinite loop to avoid leaving the main function */
  300.         __asm("NOP"); /* something to use as a breakpoint stop while looping */
  301.     }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement