]> begriffs open source - cmsis-freertos/blob - Demo/Safer_Interrupts_M33F_NXP_LPC55S69_MCUXpresso/Application/uart_demo.c
Updated pack to FreeRTOS 10.4.6
[cmsis-freertos] / Demo / Safer_Interrupts_M33F_NXP_LPC55S69_MCUXpresso / Application / uart_demo.c
1 /*
2  * FreeRTOS V202111.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  */
26
27 /* Standard includes. */
28 #include <stdio.h>
29
30 /* FreeRTOS includes. */
31 #include "FreeRTOS.h"
32 #include "task.h"
33
34 #include "board.h"
35 #include "fsl_usart.h"
36
37 /* Demo interface include. */
38 #include "uart_demo.h"
39
40 /**
41  * @brief The command menu presented to the user.
42  */
43 #define UART_COMMAND_MENU           "\r\nChoose one of the following:\r\n" \
44                                     "1. Get current tick count.\r\n"       \
45                                     "2. Get number of tasks.\r\n"
46 #define UART_COMMAND_MENU_LEN       ( sizeof( UART_COMMAND_MENU ) - 1 )
47
48 /**
49  * @brief Prompt for the user input.
50  */
51 #define UART_PROMPT_STR             "> "
52 #define UART_PROMPT_STR_LEN         ( sizeof( UART_PROMPT_STR ) - 1 )
53
54 /**
55  * @brief Valid commands entered by the user.
56  */
57 #define GET_TICK_COUNT_COMMAND      49 /* ASCII code for char '1'. */
58 #define GET_NUM_TASKS_COMMAND       50 /* ASCII code for char '2'. */
59
60 /**
61  * @brief Length of the buffer used for the response.
62  */
63 #define UART_RESPONSE_BUF_LEN      ( 32 )
64 /*-----------------------------------------------------------*/
65
66 /* The following needs to be placed in the shared memory as it is accessed in
67  * the UART received IRQ handler which is unprivileged. */
68 TaskHandle_t xUartDemoTaskHandle __attribute__( ( section( "user_irq_shared_memory" ) ) );
69 /*-----------------------------------------------------------*/
70
71 void vUartDemoTask( void * pvParams )
72 {
73     /* Silence compiler warnings about unused variables. */
74     ( void ) pvParams;
75
76     for( ;; )
77     {
78         USART_WriteBlocking( USART0, UART_COMMAND_MENU, UART_COMMAND_MENU_LEN );
79         USART_WriteBlocking( USART0, UART_PROMPT_STR, UART_PROMPT_STR_LEN );
80
81         ulTaskNotifyTake( pdFALSE, portMAX_DELAY );
82     }
83 }
84 /*-----------------------------------------------------------*/
85
86 void vUartDataReceivedIRQHandler( uint32_t ulData )
87 {
88     char response[ UART_RESPONSE_BUF_LEN ];
89
90     if( ulData == GET_TICK_COUNT_COMMAND )
91     {
92         TickType_t xTickCount = xTaskGetTickCount();
93         snprintf( response, sizeof( response ), "%c\r\nTick Count: %u\r\n", ( char ) ulData, xTickCount );
94     }
95     else if( ulData == GET_NUM_TASKS_COMMAND )
96     {
97         UBaseType_t xTaskCount = uxTaskGetNumberOfTasks();
98         snprintf( response, sizeof( response ), "%c\r\nTask Count: %u\r\n", ( char ) ulData, xTaskCount );
99     }
100     else
101     {
102         snprintf( response, sizeof( response ), "\r\nInvalid command: %c.\r\n", ( char ) ulData );
103     }
104
105     /* Print the response and unblock the UART demo task. */
106     USART_WriteBlocking( USART0, response, strlen( response ) );
107     xTaskNotifyGive( xUartDemoTaskHandle );
108 }
109 /*-----------------------------------------------------------*/