3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
27 /* Standard includes. */
30 /* FreeRTOS includes. */
35 #include "fsl_usart.h"
37 /* Demo interface include. */
38 #include "uart_demo.h"
41 * @brief The command menu presented to the user.
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 )
49 * @brief Prompt for the user input.
51 #define UART_PROMPT_STR "> "
52 #define UART_PROMPT_STR_LEN ( sizeof( UART_PROMPT_STR ) - 1 )
55 * @brief Valid commands entered by the user.
57 #define GET_TICK_COUNT_COMMAND 49 /* ASCII code for char '1'. */
58 #define GET_NUM_TASKS_COMMAND 50 /* ASCII code for char '2'. */
61 * @brief Length of the buffer used for the response.
63 #define UART_RESPONSE_BUF_LEN ( 32 )
64 /*-----------------------------------------------------------*/
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 /*-----------------------------------------------------------*/
71 void vUartDemoTask( void * pvParams )
73 /* Silence compiler warnings about unused variables. */
78 USART_WriteBlocking( USART0, UART_COMMAND_MENU, UART_COMMAND_MENU_LEN );
79 USART_WriteBlocking( USART0, UART_PROMPT_STR, UART_PROMPT_STR_LEN );
81 ulTaskNotifyTake( pdFALSE, portMAX_DELAY );
84 /*-----------------------------------------------------------*/
86 void vUartDataReceivedIRQHandler( uint32_t ulData )
88 char response[ UART_RESPONSE_BUF_LEN ];
90 if( ulData == GET_TICK_COUNT_COMMAND )
92 TickType_t xTickCount = xTaskGetTickCount();
93 snprintf( response, sizeof( response ), "%c\r\nTick Count: %u\r\n", ( char ) ulData, xTickCount );
95 else if( ulData == GET_NUM_TASKS_COMMAND )
97 UBaseType_t xTaskCount = uxTaskGetNumberOfTasks();
98 snprintf( response, sizeof( response ), "%c\r\nTask Count: %u\r\n", ( char ) ulData, xTaskCount );
102 snprintf( response, sizeof( response ), "\r\nInvalid command: %c.\r\n", ( char ) ulData );
105 /* Print the response and unblock the UART demo task. */
106 USART_WriteBlocking( USART0, response, strlen( response ) );
107 xTaskNotifyGive( xUartDemoTaskHandle );
109 /*-----------------------------------------------------------*/