1 /* 2 * Copyright (C) 2017 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define BTSTACK_FILE__ "btstack_stdin_embedded.c" 39 40 /* 41 * btstack_stdin_embedded.c 42 * 43 * Common code to provide console input using an IRQ-driven console UART in hal_stdin.h 44 * 45 */ 46 47 #include "btstack_config.h" 48 49 #include "btstack_stdin.h" 50 #include "btstack_run_loop.h" 51 #include "btstack_run_loop_embedded.h" 52 53 #ifdef ENABLE_SEGGER_RTT 54 #include "SEGGER_RTT.h" 55 #else 56 #include "hal_stdin.h" 57 #endif 58 59 static void (*stdin_handler)(char c); 60 static btstack_data_source_t stdin_data_source; 61 62 #ifndef ENABLE_SEGGER_RTT 63 64 // callback from hal_stdin is from irq context 65 66 volatile int stdin_character_received; 67 volatile char stdin_character; 68 69 static void btstack_stdin_handler(char c){ 70 stdin_character = c; 71 stdin_character_received = 1; 72 btstack_run_loop_poll_data_sources_from_irq(); 73 } 74 75 static void btstack_stdin_process(struct btstack_data_source *ds, btstack_data_source_callback_type_t callback_type){ 76 if (!stdin_character_received) { 77 return; 78 } 79 (*stdin_handler)(stdin_character); 80 stdin_character_received = 0; 81 } 82 83 #else 84 85 static void btstack_stdin_process(struct btstack_data_source *ds, btstack_data_source_callback_type_t callback_type){ 86 if (SEGGER_RTT_HasKey()){ 87 int stdin_character = SEGGER_RTT_GetKey(); 88 (*stdin_handler)((uint8_t)stdin_character); 89 } 90 } 91 92 #endif 93 94 void btstack_stdin_setup(void (*handler)(char c)){ 95 // set handler 96 stdin_handler = handler; 97 98 // set up polling data_source 99 btstack_run_loop_set_data_source_handler(&stdin_data_source, &btstack_stdin_process); 100 btstack_run_loop_enable_data_source_callbacks(&stdin_data_source, DATA_SOURCE_CALLBACK_POLL); 101 btstack_run_loop_add_data_source(&stdin_data_source); 102 103 #ifndef ENABLE_SEGGER_RTT 104 // start receiving via hal_stdin.h 105 hal_stdin_setup(&btstack_stdin_handler); 106 #endif 107 } 108