Advertisement
yigitusta9

EmbeddedOS_Lab3_1

Sep 5th, 2017
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.82 KB | None | 0 0
  1. /*
  2. ===============================================================================
  3.  Name        : main.c
  4.  Author      : $(author)
  5.  Version     :
  6.  Copyright   : $(copyright)
  7.  Description : main definition
  8. ===============================================================================
  9. */
  10.  
  11. #if defined (__USE_LPCOPEN)
  12. #if defined(NO_BOARD_LIB)
  13. #include "chip.h"
  14. #else
  15. #include "board.h"
  16. #endif
  17. #endif
  18.  
  19. #include <cr_section_macros.h>
  20.  
  21. // TODO: insert other include files here
  22.  
  23. // TODO: insert other definitions and declarations here
  24.  
  25. #include "FreeRTOS.h"
  26. #include "task.h"
  27. #include "queue.h"
  28. #include "DigitalIoPin.h"
  29.  
  30. /*****************************************************************************
  31.  * Private types/enumerations/variables
  32.  ****************************************************************************/
  33.  
  34. QueueHandle_t xQueue;
  35.  
  36. typedef int qItem;
  37.  
  38. /*****************************************************************************
  39.  * Public types/enumerations/variables
  40.  ****************************************************************************/
  41.  
  42. /*****************************************************************************
  43.  * Private functions
  44.  ****************************************************************************/
  45.  
  46. /* Sets up system hardware */
  47. static void prvSetupHardware(void)
  48. {
  49.     SystemCoreClockUpdate();
  50.     Board_Init();
  51.  
  52.     /* Initial LED0 state is off */
  53.     Board_LED_Set(0, false);
  54. }
  55.  
  56. DigitalIoPin SW1(0,17,DigitalIoPin::pullup, true);
  57.  
  58. static void Task1(void *pvParameters) {
  59.     qItem charnum = 0;
  60.     for (;;)
  61.     {
  62.         vTaskDelay(configTICK_RATE_HZ / 10);
  63.         char x = (char) Board_UARTGetChar();
  64.  
  65.         if (48 <= x && x <= 122)
  66.         {
  67.             Board_UARTPutChar(x);
  68.             charnum++;
  69.         }
  70.         else if (x == '\n' || x == '\r')
  71.         {
  72.             Board_UARTPutSTR(" \r\n");
  73.             xQueueSendToBack( xQueue, &charnum, 0 );
  74.             charnum = 0;
  75.         }
  76.     }
  77. }
  78.  
  79. static void Task2(void *pvParameters) {
  80.     for (;;)
  81.     {
  82.         vTaskDelay(configTICK_RATE_HZ / 10);
  83.         if ( SW1.read() == true )
  84.          {
  85.              qItem tmp = -1;
  86.              xQueueSendToBack( xQueue, &tmp, 0 );
  87.          }
  88.     }
  89. }
  90.  
  91. static void Task3(void *pvParameters) {
  92.     int total = 0;
  93.     for (;;)
  94.     {
  95.         vTaskDelay(configTICK_RATE_HZ / 10);
  96.         char buff[40];
  97.         qItem num;
  98.         xQueueReceive( xQueue, &num, portMAX_DELAY );
  99.  
  100.         if (num == -1)
  101.         {
  102.             sprintf(buff,"You have typed %d characters. \r\n", total);
  103.             Board_UARTPutSTR(buff);
  104.             total = 0;
  105.         }
  106.         else
  107.         {
  108.             total += num;
  109.         }
  110.     }
  111. }
  112.  
  113. /*****************************************************************************
  114.  * Public functions
  115.  ****************************************************************************/
  116.  
  117. /* the following is required if runtime statistics are to be collected */
  118. extern "C" {
  119.  
  120. void vConfigureTimerForRunTimeStats( void ) {
  121.     Chip_SCT_Init(LPC_SCTSMALL1);
  122.     LPC_SCTSMALL1->CONFIG = SCT_CONFIG_32BIT_COUNTER;
  123.     LPC_SCTSMALL1->CTRL_U = SCT_CTRL_PRE_L(255) | SCT_CTRL_CLRCTR_L; // set prescaler to 256 (255 + 1), and start timer
  124. }
  125.  
  126. }
  127. /* end runtime statictics collection */
  128.  
  129. /**
  130.  * @brief   main routine for FreeRTOS blinky example
  131.  * @return  Nothing, function should not exit
  132.  */
  133. int main(void)
  134. {
  135.     prvSetupHardware();
  136.  
  137.     xQueue = xQueueCreate( 5, sizeof( int ));
  138.  
  139.     xTaskCreate(Task1, "Task1",
  140.                 configMINIMAL_STACK_SIZE * 3, NULL, (tskIDLE_PRIORITY + 1UL),
  141.                 (TaskHandle_t *) NULL);
  142.  
  143.     xTaskCreate(Task2, "Task2",
  144.                 configMINIMAL_STACK_SIZE * 3, NULL, (tskIDLE_PRIORITY + 1UL),
  145.                 (TaskHandle_t *) NULL);
  146.  
  147.     xTaskCreate(Task3, "Task3",
  148.                 configMINIMAL_STACK_SIZE * 3, NULL, (tskIDLE_PRIORITY + 1UL),
  149.                 (TaskHandle_t *) NULL);
  150.  
  151.     /* Start the scheduler */
  152.     vTaskStartScheduler();
  153.  
  154.     /* Should never arrive here */
  155.     return 1;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement