1c894dca1SMatthias Ringwald /******************************************************************************* 2c894dca1SMatthias Ringwald * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. 3c894dca1SMatthias Ringwald * Author: Ismail H. Kose <[email protected]> 4c894dca1SMatthias Ringwald * 5c894dca1SMatthias Ringwald * Permission is hereby granted, free of charge, to any person obtaining a 6c894dca1SMatthias Ringwald * copy of this software and associated documentation files (the "Software"), 7c894dca1SMatthias Ringwald * to deal in the Software without restriction, including without limitation 8c894dca1SMatthias Ringwald * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9c894dca1SMatthias Ringwald * and/or sell copies of the Software, and to permit persons to whom the 10c894dca1SMatthias Ringwald * Software is furnished to do so, subject to the following conditions: 11c894dca1SMatthias Ringwald * 12c894dca1SMatthias Ringwald * The above copyright notice and this permission notice shall be included 13c894dca1SMatthias Ringwald * in all copies or substantial portions of the Software. 14c894dca1SMatthias Ringwald * 15c894dca1SMatthias Ringwald * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 16c894dca1SMatthias Ringwald * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17c894dca1SMatthias Ringwald * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18c894dca1SMatthias Ringwald * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES 19c894dca1SMatthias Ringwald * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20c894dca1SMatthias Ringwald * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21c894dca1SMatthias Ringwald * OTHER DEALINGS IN THE SOFTWARE. 22c894dca1SMatthias Ringwald * 23c894dca1SMatthias Ringwald * Except as contained in this notice, the name of Maxim Integrated 24c894dca1SMatthias Ringwald * Products, Inc. shall not be used except as stated in the Maxim Integrated 25c894dca1SMatthias Ringwald * Products, Inc. Branding Policy. 26c894dca1SMatthias Ringwald * 27c894dca1SMatthias Ringwald * The mere transfer of this software does not imply any licenses 28c894dca1SMatthias Ringwald * of trade secrets, proprietary technology, copyrights, patents, 29c894dca1SMatthias Ringwald * trademarks, maskwork rights, or any other form of intellectual 30c894dca1SMatthias Ringwald * property whatsoever. Maxim Integrated Products, Inc. retains all 31c894dca1SMatthias Ringwald * ownership rights. 32c894dca1SMatthias Ringwald ******************************************************************************* 33c894dca1SMatthias Ringwald */ 34c894dca1SMatthias Ringwald 35c894dca1SMatthias Ringwald #include <stdio.h> 36c894dca1SMatthias Ringwald #include <string.h> 3784e26812SMatthias Ringwald 3884e26812SMatthias Ringwald // MXC 39c894dca1SMatthias Ringwald #include "lp.h" 40c894dca1SMatthias Ringwald #include "uart.h" 41c894dca1SMatthias Ringwald #include "board.h" 4224efff18SMatthias Ringwald #include "led.h" 43c894dca1SMatthias Ringwald 4484e26812SMatthias Ringwald // BTstack Core 4584e26812SMatthias Ringwald #include "btstack_debug.h" 46c894dca1SMatthias Ringwald #include "btstack.h" 47c894dca1SMatthias Ringwald #include "btstack_config.h" 48c894dca1SMatthias Ringwald #include "btstack_run_loop_embedded.h" 49c894dca1SMatthias Ringwald #include "btstack_chipset_cc256x.h" 50*cccba290SMatthias Ringwald #include "hci_dump_embedded_stdout.h" 5184e26812SMatthias Ringwald 5284e26812SMatthias Ringwald // BTstack HALs 5384e26812SMatthias Ringwald #include "hal_tick.h" 5484e26812SMatthias Ringwald #include "hal_stdin.h" 5584e26812SMatthias Ringwald 56c894dca1SMatthias Ringwald #include "btstack_port.h" 57c894dca1SMatthias Ringwald 58c894dca1SMatthias Ringwald #define CC256X_UART_ID 0 59c894dca1SMatthias Ringwald #define UART_RXFIFO_USABLE (MXC_UART_FIFO_DEPTH-3) 60c894dca1SMatthias Ringwald 61c894dca1SMatthias Ringwald static uint32_t baud_rate; 62c894dca1SMatthias Ringwald 63c894dca1SMatthias Ringwald // rx state 64c894dca1SMatthias Ringwald static int bytes_to_read = 0; 65c894dca1SMatthias Ringwald static uint8_t * rx_buffer_ptr = 0; 66c894dca1SMatthias Ringwald 67c894dca1SMatthias Ringwald // tx state 68c894dca1SMatthias Ringwald static int bytes_to_write = 0; 69c894dca1SMatthias Ringwald static uint8_t * tx_buffer_ptr = 0; 70c894dca1SMatthias Ringwald 71c894dca1SMatthias Ringwald const gpio_cfg_t PAN1326_SLOW_CLK = { PORT_1, PIN_7, GPIO_FUNC_GPIO, 72c894dca1SMatthias Ringwald GPIO_PAD_NORMAL }; 73c894dca1SMatthias Ringwald const gpio_cfg_t PAN1326_nSHUTD = { PORT_1, PIN_6, GPIO_FUNC_GPIO, 74c894dca1SMatthias Ringwald GPIO_PAD_NORMAL }; 75c894dca1SMatthias Ringwald const gpio_cfg_t PAN1326_HCIRTS = { PORT_0, PIN_3, GPIO_FUNC_GPIO, 768959f869SMatthias Ringwald GPIO_PAD_INPUT_PULLUP }; 778959f869SMatthias Ringwald const gpio_cfg_t PAN1326_HCICTS = { PORT_0, PIN_2, GPIO_FUNC_GPIO, 78c894dca1SMatthias Ringwald GPIO_PAD_NORMAL }; 79c894dca1SMatthias Ringwald 80c894dca1SMatthias Ringwald static void dummy_handler(void) {}; 81c894dca1SMatthias Ringwald static void (*rx_done_handler)(void) = dummy_handler; 82c894dca1SMatthias Ringwald static void (*tx_done_handler)(void) = dummy_handler; 83c894dca1SMatthias Ringwald 8424efff18SMatthias Ringwald 8524efff18SMatthias Ringwald 86c894dca1SMatthias Ringwald void hal_cpu_disable_irqs(void) 87c894dca1SMatthias Ringwald { 88c894dca1SMatthias Ringwald __disable_irq(); 89c894dca1SMatthias Ringwald } 90c894dca1SMatthias Ringwald 91c894dca1SMatthias Ringwald void hal_cpu_enable_irqs(void) 92c894dca1SMatthias Ringwald { 93c894dca1SMatthias Ringwald __enable_irq(); 94c894dca1SMatthias Ringwald } 95c894dca1SMatthias Ringwald void hal_cpu_enable_irqs_and_sleep(void) 96c894dca1SMatthias Ringwald { 9784e26812SMatthias Ringwald __enable_irq(); 9884e26812SMatthias Ringwald /* TODO: Add sleep mode */ 99c894dca1SMatthias Ringwald } 100c894dca1SMatthias Ringwald 101c894dca1SMatthias Ringwald void hal_uart_dma_send_block(const uint8_t *buffer, uint16_t len) 102c894dca1SMatthias Ringwald { 103c894dca1SMatthias Ringwald tx_buffer_ptr = (uint8_t *)buffer; 104c894dca1SMatthias Ringwald bytes_to_write = len; 105c894dca1SMatthias Ringwald } 106c894dca1SMatthias Ringwald 107c894dca1SMatthias Ringwald void hal_uart_dma_receive_block(uint8_t *buffer, uint16_t len) 108c894dca1SMatthias Ringwald { 109c894dca1SMatthias Ringwald rx_buffer_ptr = buffer; 110c894dca1SMatthias Ringwald bytes_to_read = len; 111c894dca1SMatthias Ringwald } 112c894dca1SMatthias Ringwald 113c894dca1SMatthias Ringwald void hal_btstack_run_loop_execute_once(void) 114c894dca1SMatthias Ringwald { 115c894dca1SMatthias Ringwald int rx_avail; 116c894dca1SMatthias Ringwald int num_rx_bytes; 117c894dca1SMatthias Ringwald int tx_avail; 118c894dca1SMatthias Ringwald int rx_bytes; 119c894dca1SMatthias Ringwald int tx_bytes; 120c894dca1SMatthias Ringwald int ret; 121c894dca1SMatthias Ringwald 122c894dca1SMatthias Ringwald while (bytes_to_read) { 123c894dca1SMatthias Ringwald rx_avail = UART_NumReadAvail(MXC_UART_GET_UART(CC256X_UART_ID)); 124c894dca1SMatthias Ringwald if (!rx_avail) 125c894dca1SMatthias Ringwald break; 126c894dca1SMatthias Ringwald 127c894dca1SMatthias Ringwald if (bytes_to_read > rx_avail) 128c894dca1SMatthias Ringwald num_rx_bytes = rx_avail; 129c894dca1SMatthias Ringwald else 130c894dca1SMatthias Ringwald num_rx_bytes = bytes_to_read; 131c894dca1SMatthias Ringwald 132c894dca1SMatthias Ringwald ret = UART_Read(MXC_UART_GET_UART(CC256X_UART_ID), rx_buffer_ptr, num_rx_bytes, &rx_bytes); 133c894dca1SMatthias Ringwald if (ret < 0) 134c894dca1SMatthias Ringwald break; 135c894dca1SMatthias Ringwald 136c894dca1SMatthias Ringwald rx_buffer_ptr += rx_bytes; 137c894dca1SMatthias Ringwald bytes_to_read -= rx_bytes; 138c894dca1SMatthias Ringwald 139c894dca1SMatthias Ringwald if (bytes_to_read < 0) { 140c894dca1SMatthias Ringwald bytes_to_read = 0; 141c894dca1SMatthias Ringwald } 142c894dca1SMatthias Ringwald 143c894dca1SMatthias Ringwald if (bytes_to_read == 0){ 144c894dca1SMatthias Ringwald (*rx_done_handler)(); 145c894dca1SMatthias Ringwald } 146c894dca1SMatthias Ringwald } 147c894dca1SMatthias Ringwald 148c894dca1SMatthias Ringwald while (bytes_to_write) { 149c894dca1SMatthias Ringwald tx_avail = UART_NumWriteAvail(MXC_UART_GET_UART(CC256X_UART_ID)); 150c894dca1SMatthias Ringwald if (!tx_avail) 151c894dca1SMatthias Ringwald break; 152c894dca1SMatthias Ringwald 153c894dca1SMatthias Ringwald if (bytes_to_write > tx_avail) 154c894dca1SMatthias Ringwald tx_bytes = tx_avail; 155c894dca1SMatthias Ringwald else 156c894dca1SMatthias Ringwald tx_bytes = bytes_to_write; 157c894dca1SMatthias Ringwald 158c894dca1SMatthias Ringwald ret = UART_Write(MXC_UART_GET_UART(CC256X_UART_ID), tx_buffer_ptr, tx_bytes); 159c894dca1SMatthias Ringwald if (ret < 0) 160c894dca1SMatthias Ringwald break; 161c894dca1SMatthias Ringwald bytes_to_write -= tx_bytes; 162c894dca1SMatthias Ringwald tx_buffer_ptr += tx_bytes; 163c894dca1SMatthias Ringwald if (bytes_to_write < 0) { 164c894dca1SMatthias Ringwald bytes_to_write = 0; 165c894dca1SMatthias Ringwald } 166c894dca1SMatthias Ringwald 167c894dca1SMatthias Ringwald if (bytes_to_write == 0){ 168c894dca1SMatthias Ringwald (*tx_done_handler)(); 169c894dca1SMatthias Ringwald } 170c894dca1SMatthias Ringwald } 171c894dca1SMatthias Ringwald 172c894dca1SMatthias Ringwald btstack_run_loop_embedded_execute_once(); 173c894dca1SMatthias Ringwald } 174c894dca1SMatthias Ringwald 175c894dca1SMatthias Ringwald void hal_uart_init(void) 176c894dca1SMatthias Ringwald { 177c894dca1SMatthias Ringwald int error = 0; 178c894dca1SMatthias Ringwald uart_cfg_t cfg; 179c894dca1SMatthias Ringwald 180c894dca1SMatthias Ringwald cfg.parity = UART_PARITY_DISABLE; 181c894dca1SMatthias Ringwald cfg.size = UART_DATA_SIZE_8_BITS; 182c894dca1SMatthias Ringwald cfg.extra_stop = 0; 183c894dca1SMatthias Ringwald cfg.cts = 1; 184c894dca1SMatthias Ringwald cfg.rts = 1; 185c894dca1SMatthias Ringwald 186c894dca1SMatthias Ringwald cfg.baud = baud_rate; 187c894dca1SMatthias Ringwald 188c894dca1SMatthias Ringwald sys_cfg_uart_t sys_cfg; 189c894dca1SMatthias Ringwald sys_cfg.clk_scale = CLKMAN_SCALE_AUTO; 190c894dca1SMatthias Ringwald 191c894dca1SMatthias Ringwald sys_cfg.io_cfg = (ioman_cfg_t )IOMAN_UART(0, 192c894dca1SMatthias Ringwald IOMAN_MAP_B, // io_map 193c894dca1SMatthias Ringwald IOMAN_MAP_B, // cts_map 194c894dca1SMatthias Ringwald IOMAN_MAP_B, // rts_map 195c894dca1SMatthias Ringwald 1, // io_en 196c894dca1SMatthias Ringwald 1, // cts_en 197c894dca1SMatthias Ringwald 1); //rts_en 198c894dca1SMatthias Ringwald 199c894dca1SMatthias Ringwald if ((error = UART_Init(MXC_UART_GET_UART(CC256X_UART_ID), &cfg, &sys_cfg)) != E_NO_ERROR) { 200c894dca1SMatthias Ringwald printf("Error initializing UART %d\n", error); 201c894dca1SMatthias Ringwald while (1); 202c894dca1SMatthias Ringwald } else { 203c894dca1SMatthias Ringwald printf("BTSTACK UART Initialized\n"); 204c894dca1SMatthias Ringwald } 205c894dca1SMatthias Ringwald 206c894dca1SMatthias Ringwald MXC_UART_GET_UART(CC256X_UART_ID)->ctrl |= MXC_F_UART_CTRL_CTS_POLARITY | MXC_F_UART_CTRL_RTS_POLARITY; 207c894dca1SMatthias Ringwald MXC_UART_GET_UART(CC256X_UART_ID)->ctrl &= ~((MXC_UART_FIFO_DEPTH - 4) << (MXC_F_UART_CTRL_RTS_LEVEL_POS)); 208c894dca1SMatthias Ringwald MXC_UART_GET_UART(CC256X_UART_ID)->ctrl |= ((UART_RXFIFO_USABLE) << MXC_F_UART_CTRL_RTS_LEVEL_POS); 209c894dca1SMatthias Ringwald } 210c894dca1SMatthias Ringwald 211c894dca1SMatthias Ringwald int hal_uart_dma_set_baud(uint32_t baud){ 212c894dca1SMatthias Ringwald baud_rate = baud; 213c894dca1SMatthias Ringwald printf("BAUD RATE IS = %d \n", baud); 214c894dca1SMatthias Ringwald hal_uart_init(); 215c894dca1SMatthias Ringwald return baud_rate; 216c894dca1SMatthias Ringwald } 217c894dca1SMatthias Ringwald 218c894dca1SMatthias Ringwald void hal_uart_dma_init(void){ 219c894dca1SMatthias Ringwald bytes_to_write = 0; 220c894dca1SMatthias Ringwald bytes_to_read = 0; 221c894dca1SMatthias Ringwald hal_uart_dma_set_baud(115200); 222c894dca1SMatthias Ringwald } 223c894dca1SMatthias Ringwald 224c894dca1SMatthias Ringwald void hal_uart_dma_set_block_received( void (*block_handler)(void)){ 225c894dca1SMatthias Ringwald rx_done_handler = block_handler; 226c894dca1SMatthias Ringwald } 227c894dca1SMatthias Ringwald 228c894dca1SMatthias Ringwald void hal_uart_dma_set_block_sent( void (*block_handler)(void)){ 229c894dca1SMatthias Ringwald 230c894dca1SMatthias Ringwald tx_done_handler = block_handler; 231c894dca1SMatthias Ringwald } 232c894dca1SMatthias Ringwald 233c894dca1SMatthias Ringwald void hal_uart_dma_set_csr_irq_handler( void (*csr_irq_handler)(void)){ 234c894dca1SMatthias Ringwald 235c894dca1SMatthias Ringwald } 236c894dca1SMatthias Ringwald 237c894dca1SMatthias Ringwald void hal_uart_dma_set_sleep(uint8_t sleep){ 238c894dca1SMatthias Ringwald 239c894dca1SMatthias Ringwald } 240c894dca1SMatthias Ringwald 241c894dca1SMatthias Ringwald void init_slow_clock(void) 242c894dca1SMatthias Ringwald { 243c894dca1SMatthias Ringwald MXC_PWRSEQ->reg0 &= ~(MXC_F_PWRSEQ_REG0_PWR_RTCEN_RUN | MXC_F_PWRSEQ_REG0_PWR_RTCEN_SLP); 244c894dca1SMatthias Ringwald MXC_PWRSEQ->reg4 &= ~MXC_F_PWRSEQ_REG4_PWR_PSEQ_32K_EN; 245c894dca1SMatthias Ringwald MXC_PWRSEQ->reg0 |= MXC_F_PWRSEQ_REG0_PWR_RTCEN_RUN | MXC_F_PWRSEQ_REG0_PWR_RTCEN_SLP; // Enable RTC 246c894dca1SMatthias Ringwald hal_delay_us(1); 247c894dca1SMatthias Ringwald MXC_PWRSEQ->reg4 |= MXC_F_PWRSEQ_REG4_PWR_PSEQ_32K_EN; // Enable the RTC out of P1.7 248c894dca1SMatthias Ringwald } 249c894dca1SMatthias Ringwald 250c894dca1SMatthias Ringwald int bt_comm_init() { 251c894dca1SMatthias Ringwald int error = 0; 252c894dca1SMatthias Ringwald int cnt = 0; 253c894dca1SMatthias Ringwald 254c894dca1SMatthias Ringwald hal_tick_init(); 255c894dca1SMatthias Ringwald hal_delay_us(1); 2568959f869SMatthias Ringwald 2578959f869SMatthias Ringwald /* HCI module RTS as input with 25k pullup */ 258c894dca1SMatthias Ringwald if ((error = GPIO_Config(&PAN1326_HCIRTS)) != E_NO_ERROR) { 259c894dca1SMatthias Ringwald printf("Error setting PAN1326_HCIRTS %d\n", error); 260c894dca1SMatthias Ringwald } 261c894dca1SMatthias Ringwald GPIO_OutSet(&PAN1326_HCIRTS); 2628959f869SMatthias Ringwald 263c894dca1SMatthias Ringwald init_slow_clock(); 264c894dca1SMatthias Ringwald /* 265c894dca1SMatthias Ringwald * when enabling the P1.7 RTC output, P1.6 will be hardcoded to an input with 25k pullup enabled. 266c894dca1SMatthias Ringwald * There is an internal pullup, so when it is set as an input, it will float high. 267c894dca1SMatthias Ringwald * The PAN1326B data sheet says the NSHUTD pin is pulled down, but the input impedance is stated at 1Meg Ohm, 268c894dca1SMatthias Ringwald * The so the 25k pullup should be enough to reach the minimum 1.42V to enable the device. 269c894dca1SMatthias Ringwald * */ 2708959f869SMatthias Ringwald 2718959f869SMatthias Ringwald /* Force PAN1326 shutdown to be output and take it out of reset */ 2728959f869SMatthias Ringwald if ((error = GPIO_Config(&PAN1326_nSHUTD)) != E_NO_ERROR) { 2738959f869SMatthias Ringwald printf("Error setting PAN1326_nSHUTD %d\n", error); 2748959f869SMatthias Ringwald } 2758959f869SMatthias Ringwald GPIO_OutSet(&PAN1326_nSHUTD); 2768959f869SMatthias Ringwald 2778959f869SMatthias Ringwald /*Check the module is ready to receive data */ 278c894dca1SMatthias Ringwald while (GPIO_InGet(&PAN1326_HCIRTS)) { 279c894dca1SMatthias Ringwald cnt++; 280c894dca1SMatthias Ringwald } 281c894dca1SMatthias Ringwald 282c894dca1SMatthias Ringwald printf("%s CC256X init completed. cnt: %d \n", __func__, cnt); 283c894dca1SMatthias Ringwald return 0; 284c894dca1SMatthias Ringwald } 285c894dca1SMatthias Ringwald 286c894dca1SMatthias Ringwald static hci_transport_config_uart_t config = { 287c894dca1SMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 288c894dca1SMatthias Ringwald 115200, 289c894dca1SMatthias Ringwald 4000000, 290c894dca1SMatthias Ringwald 1, // flow control 291c894dca1SMatthias Ringwald "max32630fthr", 292c894dca1SMatthias Ringwald }; 293c894dca1SMatthias Ringwald 294183c110aSMatthias Ringwald // hal_led.h implementation 295183c110aSMatthias Ringwald #include "hal_led.h" 296183c110aSMatthias Ringwald void hal_led_off(void){ 29724efff18SMatthias Ringwald LED_Off(LED_BLUE); 298183c110aSMatthias Ringwald } 29924efff18SMatthias Ringwald 300183c110aSMatthias Ringwald void hal_led_on(void){ 30124efff18SMatthias Ringwald LED_On(LED_BLUE); 302183c110aSMatthias Ringwald } 30324efff18SMatthias Ringwald 304183c110aSMatthias Ringwald void hal_led_toggle(void){ 30524efff18SMatthias Ringwald LED_Toggle(LED_BLUE); 306183c110aSMatthias Ringwald } 307183c110aSMatthias Ringwald 30884e26812SMatthias Ringwald // hal_stdin.h 30984e26812SMatthias Ringwald static uint8_t stdin_buffer[1]; 31084e26812SMatthias Ringwald static void (*stdin_handler)(char c); 31184e26812SMatthias Ringwald 31284e26812SMatthias Ringwald static uart_req_t uart_byte_request; 31384e26812SMatthias Ringwald 31484e26812SMatthias Ringwald static void uart_rx_handler(uart_req_t *request, int error) 31584e26812SMatthias Ringwald { 31684e26812SMatthias Ringwald if (stdin_handler){ 31784e26812SMatthias Ringwald (*stdin_handler)(stdin_buffer[0]); 31884e26812SMatthias Ringwald } 31984e26812SMatthias Ringwald UART_ReadAsync(MXC_UART_GET_UART(CONSOLE_UART), &uart_byte_request); 32084e26812SMatthias Ringwald } 32184e26812SMatthias Ringwald 32284e26812SMatthias Ringwald void hal_stdin_setup(void (*handler)(char c)){ 32384e26812SMatthias Ringwald // set handler 32484e26812SMatthias Ringwald stdin_handler = handler; 32584e26812SMatthias Ringwald 32684e26812SMatthias Ringwald /* set input handler */ 32784e26812SMatthias Ringwald uart_byte_request.callback = uart_rx_handler; 32884e26812SMatthias Ringwald uart_byte_request.data = stdin_buffer; 32984e26812SMatthias Ringwald uart_byte_request.len = sizeof(uint8_t); 33084e26812SMatthias Ringwald UART_ReadAsync(MXC_UART_GET_UART(CONSOLE_UART), &uart_byte_request); 33184e26812SMatthias Ringwald } 33284e26812SMatthias Ringwald 33384e26812SMatthias Ringwald #if 0 33484e26812SMatthias Ringwald 33584e26812SMatthias Ringwald #include "btstack_stdin.h" 33684e26812SMatthias Ringwald 33784e26812SMatthias Ringwald static btstack_data_source_t stdin_data_source; 33884e26812SMatthias Ringwald static void (*stdin_handler)(char c); 33984e26812SMatthias Ringwald 34084e26812SMatthias Ringwald static uart_req_t uart_byte_request; 34184e26812SMatthias Ringwald static volatile int stdin_character_received; 34284e26812SMatthias Ringwald static uint8_t stdin_buffer[1]; 34384e26812SMatthias Ringwald 34484e26812SMatthias Ringwald static void stdin_rx_complete(void) { 34584e26812SMatthias Ringwald stdin_character_received = 1; 34684e26812SMatthias Ringwald } 34784e26812SMatthias Ringwald 34884e26812SMatthias Ringwald static void uart_rx_handler(uart_req_t *request, int error) 34984e26812SMatthias Ringwald { 35084e26812SMatthias Ringwald stdin_rx_complete(); 35184e26812SMatthias Ringwald } 35284e26812SMatthias Ringwald 35384e26812SMatthias Ringwald static void stdin_process(struct btstack_data_source *ds, btstack_data_source_callback_type_t callback_type){ 35484e26812SMatthias Ringwald if (!stdin_character_received) return; 35584e26812SMatthias Ringwald if (stdin_handler){ 35684e26812SMatthias Ringwald (*stdin_handler)(stdin_buffer[0]); 35784e26812SMatthias Ringwald } 35884e26812SMatthias Ringwald stdin_character_received = 0; 35984e26812SMatthias Ringwald UART_ReadAsync(MXC_UART_GET_UART(CONSOLE_UART), &uart_byte_request); 36084e26812SMatthias Ringwald } 36184e26812SMatthias Ringwald 36284e26812SMatthias Ringwald static void btstack_stdin_handler(char c){ 36384e26812SMatthias Ringwald stdin_character_received = 1; 36484e26812SMatthias Ringwald btstack_run_loop_embedded_trigger(); 36584e26812SMatthias Ringwald printf("Received: %c\n", c); 36684e26812SMatthias Ringwald } 36784e26812SMatthias Ringwald 36884e26812SMatthias Ringwald void btstack_stdin_setup(void (*handler)(char c)){ 36984e26812SMatthias Ringwald // set handler 37084e26812SMatthias Ringwald stdin_handler = handler; 37184e26812SMatthias Ringwald 37284e26812SMatthias Ringwald // set up polling data_source 37384e26812SMatthias Ringwald btstack_run_loop_set_data_source_handler(&stdin_data_source, &stdin_process); 37484e26812SMatthias Ringwald btstack_run_loop_enable_data_source_callbacks(&stdin_data_source, DATA_SOURCE_CALLBACK_POLL); 37584e26812SMatthias Ringwald btstack_run_loop_add_data_source(&stdin_data_source); 37684e26812SMatthias Ringwald 37784e26812SMatthias Ringwald /* set input handler */ 37884e26812SMatthias Ringwald uart_byte_request.callback = uart_rx_handler; 37984e26812SMatthias Ringwald uart_byte_request.data = stdin_buffer; 38084e26812SMatthias Ringwald uart_byte_request.len = sizeof(uint8_t); 38184e26812SMatthias Ringwald UART_ReadAsync(MXC_UART_GET_UART(CONSOLE_UART), &uart_byte_request); 38284e26812SMatthias Ringwald } 38384e26812SMatthias Ringwald #endif 38484e26812SMatthias Ringwald 38517ae5bc4SMatthias Ringwald #include "hal_flash_bank_mxc.h" 3865efdcd7dSMatthias Ringwald #include "btstack_tlv.h" 38717ae5bc4SMatthias Ringwald #include "btstack_tlv_flash_bank.h" 3885efdcd7dSMatthias Ringwald #include "btstack_link_key_db_tlv.h" 3895efdcd7dSMatthias Ringwald #include "le_device_db_tlv.h" 3905efdcd7dSMatthias Ringwald 39117ae5bc4SMatthias Ringwald #define HAL_FLASH_BANK_SIZE 0x2000 39217ae5bc4SMatthias Ringwald #define HAL_FLASH_BANK_0_ADDR 0x1FC000 39317ae5bc4SMatthias Ringwald #define HAL_FLASH_BANK_1_ADDR 0x1FE000 3945efdcd7dSMatthias Ringwald 39517ae5bc4SMatthias Ringwald static hal_flash_bank_mxc_t hal_flash_bank_context; 39617ae5bc4SMatthias Ringwald static btstack_tlv_flash_bank_t btstack_tlv_flash_bank_context; 3975efdcd7dSMatthias Ringwald 39884e26812SMatthias Ringwald 39984e26812SMatthias Ringwald /******************************************************************************/ 400c894dca1SMatthias Ringwald int bluetooth_main(void) 401c894dca1SMatthias Ringwald { 40224efff18SMatthias Ringwald LED_Off(LED_GREEN); 40324efff18SMatthias Ringwald LED_On(LED_RED); 40424efff18SMatthias Ringwald LED_Off(LED_BLUE); 40524efff18SMatthias Ringwald 406c894dca1SMatthias Ringwald bt_comm_init(); 407c894dca1SMatthias Ringwald /* BT Stack Initialization */ 408c894dca1SMatthias Ringwald btstack_memory_init(); 409c894dca1SMatthias Ringwald btstack_run_loop_init(btstack_run_loop_embedded_get_instance()); 410c894dca1SMatthias Ringwald 41184e26812SMatthias Ringwald // enable packet logger 412*cccba290SMatthias Ringwald // hci_dump_init(hci_dump_embedded_stdout_get_instance()); 41384e26812SMatthias Ringwald 414c894dca1SMatthias Ringwald /* Init HCI */ 415c894dca1SMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance(btstack_uart_block_embedded_instance()); 416c894dca1SMatthias Ringwald hci_init(transport, &config); 417c894dca1SMatthias Ringwald hci_set_chipset(btstack_chipset_cc256x_instance()); 4185efdcd7dSMatthias Ringwald 41917ae5bc4SMatthias Ringwald // setup TLV Flash Bank implementation 42017ae5bc4SMatthias Ringwald const hal_flash_bank_t * hal_flash_bank_impl = hal_flash_bank_mxc_init_instance( 42117ae5bc4SMatthias Ringwald &hal_flash_bank_context, 42217ae5bc4SMatthias Ringwald HAL_FLASH_BANK_SIZE, 42317ae5bc4SMatthias Ringwald HAL_FLASH_BANK_0_ADDR, 42417ae5bc4SMatthias Ringwald HAL_FLASH_BANK_1_ADDR); 42517ae5bc4SMatthias Ringwald const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_flash_bank_init_instance( 42617ae5bc4SMatthias Ringwald &btstack_tlv_flash_bank_context, 42717ae5bc4SMatthias Ringwald hal_flash_bank_impl, 42817ae5bc4SMatthias Ringwald &hal_flash_bank_context); 4295efdcd7dSMatthias Ringwald 4305efdcd7dSMatthias Ringwald // setup Link Key DB using TLV 43117ae5bc4SMatthias Ringwald const btstack_link_key_db_t * btstack_link_key_db = btstack_link_key_db_tlv_get_instance(btstack_tlv_impl, &btstack_tlv_flash_bank_context); 4325efdcd7dSMatthias Ringwald hci_set_link_key_db(btstack_link_key_db); 4335efdcd7dSMatthias Ringwald 4345efdcd7dSMatthias Ringwald // setup LE Device DB using TLV 43517ae5bc4SMatthias Ringwald le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_bank_context); 4365efdcd7dSMatthias Ringwald 4375efdcd7dSMatthias Ringwald // go 438c894dca1SMatthias Ringwald btstack_main(0, (void *)NULL); 439c894dca1SMatthias Ringwald return 0; 440c894dca1SMatthias Ringwald } 441