Advertisement
yigitusta9

EmbeddedOS_Lab3_3

Sep 13th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.38 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. #include "stdlib.h"
  30. #include "ITM_write.h"
  31.  
  32. /*****************************************************************************
  33.  * Private types/enumerations/variables
  34.  ****************************************************************************/
  35.  
  36. QueueHandle_t xQueue;
  37.  
  38. typedef int qItem;
  39.  
  40. struct debugEvent {
  41.     char *format;
  42.     uint32_t data[3];
  43. };
  44.  
  45. char *debugFormat[] = {"Received %d characters at %d \r\n", "Pressed SW1 for %d \r\n"};
  46.  
  47. /*****************************************************************************
  48.  * Public types/enumerations/variables
  49.  ****************************************************************************/
  50.  
  51. /*****************************************************************************
  52.  * Private functions
  53.  ****************************************************************************/
  54.  
  55. /* Sets up system hardware */
  56. static void prvSetupHardware(void)
  57. {
  58.     SystemCoreClockUpdate();
  59.     Board_Init();
  60.  
  61.     /* Initial LED0 state is off */
  62.     Board_LED_Set(0, false);
  63. }
  64.  
  65. DigitalIoPin SW1(0,17,DigitalIoPin::pullup, true);
  66.  
  67. static void Task1(void *pvParameters) {
  68.     qItem charnum = 0;
  69.     debugEvent e;
  70.     for (;;)
  71.     {
  72.         vTaskDelay(100);
  73.         char x = (char) Board_UARTGetChar();
  74.  
  75.         if ( (32 <= x && x < 127) || x == 9 || x == '\n' || x == '\r')
  76.         {
  77.             if (x != '\n' && x != '\r')
  78.             {
  79.                 Board_UARTPutChar(x);
  80.                 if (32 < x && x < 127)
  81.                 {
  82.                     charnum++;
  83.                 }
  84.             }
  85.  
  86.             if ( (x == 32 || x == 9 || x == '\n' || x == '\r') )
  87.             {
  88.                 e.format = debugFormat[0];
  89.                 e.data[0] = charnum;
  90.                 e.data[1] = xTaskGetTickCount();
  91.                 if (x == '\n' || x == '\r')
  92.                 {
  93.                     Board_UARTPutSTR(" \r\n");
  94.                 }
  95.                 xQueueSendToBack( xQueue, &e, 0 );
  96.                 charnum = 0;
  97.             }
  98.         }
  99.     }
  100. }
  101.  
  102. static void Task2(void *pvParameters) {
  103.     bool onread = false;
  104.     debugEvent e;
  105.     TickType_t t = 0;
  106.     for (;;)
  107.     {
  108.         if ( SW1.read() == true)
  109.         {
  110.             if (t == 0)
  111.                 t = xTaskGetTickCount();
  112.         }
  113.         else if ( t > 0 )
  114.         {
  115.             e.format = debugFormat[1];
  116.             e.data[0] = xTaskGetTickCount() - t;
  117.             xQueueSendToBack( xQueue, &e, 0 );
  118.             t = 0;
  119.         }
  120.  
  121.         vTaskDelay(10);
  122.     }
  123. }
  124.  
  125. static void Task3(void *pvParameters) {
  126.     char buffer[64];
  127.     debugEvent e;
  128.     for (;;)
  129.     {
  130.         xQueueReceive( xQueue, &e, portMAX_DELAY );
  131.         snprintf(buffer, 64, e.format, e.data[0], e.data[1], e.data[3]);
  132.         ITM_write(buffer);
  133.     }
  134. }
  135.  
  136. /*****************************************************************************
  137.  * Public functions
  138.  ****************************************************************************/
  139.  
  140. /* the following is required if runtime statistics are to be collected */
  141. extern "C" {
  142.  
  143. void vConfigureTimerForRunTimeStats( void ) {
  144.     Chip_SCT_Init(LPC_SCTSMALL1);
  145.     LPC_SCTSMALL1->CONFIG = SCT_CONFIG_32BIT_COUNTER;
  146.     LPC_SCTSMALL1->CTRL_U = SCT_CTRL_PRE_L(255) | SCT_CTRL_CLRCTR_L; // set prescaler to 256 (255 + 1), and start timer
  147. }
  148.  
  149. }
  150. /* end runtime statictics collection */
  151.  
  152. /**
  153.  * @brief   main routine for FreeRTOS blinky example
  154.  * @return  Nothing, function should not exit
  155.  */
  156. int main(void)
  157. {
  158.     prvSetupHardware();
  159.     ITM_init();
  160.     xQueue = xQueueCreate( 20, sizeof( debugEvent ));
  161.  
  162.     xTaskCreate(Task1, "Task1",
  163.                 configMINIMAL_STACK_SIZE * 3, NULL, (tskIDLE_PRIORITY + 2UL),
  164.                 (TaskHandle_t *) NULL);
  165.  
  166.     xTaskCreate(Task2, "Task2",
  167.                 configMINIMAL_STACK_SIZE * 3, NULL, (tskIDLE_PRIORITY + 2UL),
  168.                 (TaskHandle_t *) NULL);
  169.  
  170.     xTaskCreate(Task3, "Task3",
  171.                 configMINIMAL_STACK_SIZE * 3, NULL, (tskIDLE_PRIORITY + 1UL),
  172.                 (TaskHandle_t *) NULL);
  173.  
  174.     /* Start the scheduler */
  175.     vTaskStartScheduler();
  176.  
  177.     /* Should never arrive here */
  178.     return 1;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement