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 61 #ifdef ENABLE_SEGGER_RTT 62 63 #define RTT_POLL_INTERVAL_MS 50 64 65 static btstack_timer_source_t stdin_timer_source; 66 67 static void btstack_stdin_timer_handler(btstack_timer_source_t *ts){ 68 if (SEGGER_RTT_HasKey()){ 69 int stdin_character = SEGGER_RTT_GetKey(); 70 (*stdin_handler)((uint8_t)stdin_character); 71 } 72 btstack_run_loop_set_timer(ts, RTT_POLL_INTERVAL_MS); 73 btstack_run_loop_add_timer(ts); 74 } 75 76 #else 77 78 static btstack_data_source_t stdin_data_source; 79 80 // callback from hal_stdin is from irq context 81 82 volatile int stdin_character_received; 83 volatile char stdin_character; 84 85 static void btstack_stdin_handler(char c){ 86 stdin_character = c; 87 stdin_character_received = 1; 88 btstack_run_loop_poll_data_sources_from_irq(); 89 } 90 91 static void btstack_stdin_process(struct btstack_data_source *ds, btstack_data_source_callback_type_t callback_type){ 92 UNUSED(ds); 93 UNUSED(callback_type); 94 if (!stdin_character_received) { 95 return; 96 } 97 (*stdin_handler)(stdin_character); 98 stdin_character_received = 0; 99 } 100 101 #endif 102 103 void btstack_stdin_setup(void (*handler)(char c)){ 104 // set handler 105 stdin_handler = handler; 106 107 #ifdef ENABLE_SEGGER_RTT 108 // start polling with timer 109 btstack_run_loop_set_timer_handler(&stdin_timer_source, &btstack_stdin_timer_handler); 110 btstack_run_loop_set_timer(&stdin_timer_source, RTT_POLL_INTERVAL_MS); 111 btstack_run_loop_add_timer(&stdin_timer_source); 112 #else 113 // set up polling data_source 114 btstack_run_loop_set_data_source_handler(&stdin_data_source, &btstack_stdin_process); 115 btstack_run_loop_enable_data_source_callbacks(&stdin_data_source, DATA_SOURCE_CALLBACK_POLL); 116 btstack_run_loop_add_data_source(&stdin_data_source); 117 118 // start receiving via hal_stdin.h 119 hal_stdin_setup(&btstack_stdin_handler); 120 #endif 121 } 122