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" 5084e26812SMatthias Ringwald 5184e26812SMatthias Ringwald // BTstack HALs 5284e26812SMatthias Ringwald #include "hal_tick.h" 5384e26812SMatthias Ringwald #include "hal_stdin.h" 5484e26812SMatthias Ringwald 55c894dca1SMatthias Ringwald #include "btstack_port.h" 56c894dca1SMatthias Ringwald 57c894dca1SMatthias Ringwald #define CC256X_UART_ID 0 58c894dca1SMatthias Ringwald #define UART_RXFIFO_USABLE (MXC_UART_FIFO_DEPTH-3) 59c894dca1SMatthias Ringwald 60c894dca1SMatthias Ringwald static uint32_t baud_rate; 61c894dca1SMatthias Ringwald 62c894dca1SMatthias Ringwald // rx state 63c894dca1SMatthias Ringwald static int bytes_to_read = 0; 64c894dca1SMatthias Ringwald static uint8_t * rx_buffer_ptr = 0; 65c894dca1SMatthias Ringwald 66c894dca1SMatthias Ringwald // tx state 67c894dca1SMatthias Ringwald static int bytes_to_write = 0; 68c894dca1SMatthias Ringwald static uint8_t * tx_buffer_ptr = 0; 69c894dca1SMatthias Ringwald 70c894dca1SMatthias Ringwald const gpio_cfg_t PAN1326_SLOW_CLK = { PORT_1, PIN_7, GPIO_FUNC_GPIO, 71c894dca1SMatthias Ringwald GPIO_PAD_NORMAL }; 72c894dca1SMatthias Ringwald const gpio_cfg_t PAN1326_nSHUTD = { PORT_1, PIN_6, GPIO_FUNC_GPIO, 73c894dca1SMatthias Ringwald GPIO_PAD_NORMAL }; 74c894dca1SMatthias Ringwald const gpio_cfg_t PAN1326_HCIRTS = { PORT_0, PIN_3, GPIO_FUNC_GPIO, 75*8959f869SMatthias Ringwald GPIO_PAD_INPUT_PULLUP }; 76*8959f869SMatthias Ringwald const gpio_cfg_t PAN1326_HCICTS = { PORT_0, PIN_2, GPIO_FUNC_GPIO, 77c894dca1SMatthias Ringwald GPIO_PAD_NORMAL }; 78c894dca1SMatthias Ringwald 79c894dca1SMatthias Ringwald static void dummy_handler(void) {}; 80c894dca1SMatthias Ringwald static void (*rx_done_handler)(void) = dummy_handler; 81c894dca1SMatthias Ringwald static void (*tx_done_handler)(void) = dummy_handler; 82c894dca1SMatthias Ringwald 8324efff18SMatthias Ringwald 8424efff18SMatthias Ringwald 85c894dca1SMatthias Ringwald void hal_cpu_disable_irqs(void) 86c894dca1SMatthias Ringwald { 87c894dca1SMatthias Ringwald __disable_irq(); 88c894dca1SMatthias Ringwald } 89c894dca1SMatthias Ringwald 90c894dca1SMatthias Ringwald void hal_cpu_enable_irqs(void) 91c894dca1SMatthias Ringwald { 92c894dca1SMatthias Ringwald __enable_irq(); 93c894dca1SMatthias Ringwald } 94c894dca1SMatthias Ringwald void hal_cpu_enable_irqs_and_sleep(void) 95c894dca1SMatthias Ringwald { 9684e26812SMatthias Ringwald __enable_irq(); 9784e26812SMatthias Ringwald /* TODO: Add sleep mode */ 98c894dca1SMatthias Ringwald } 99c894dca1SMatthias Ringwald 100c894dca1SMatthias Ringwald void hal_uart_dma_send_block(const uint8_t *buffer, uint16_t len) 101c894dca1SMatthias Ringwald { 102c894dca1SMatthias Ringwald tx_buffer_ptr = (uint8_t *)buffer; 103c894dca1SMatthias Ringwald bytes_to_write = len; 104c894dca1SMatthias Ringwald } 105c894dca1SMatthias Ringwald 106c894dca1SMatthias Ringwald void hal_uart_dma_receive_block(uint8_t *buffer, uint16_t len) 107c894dca1SMatthias Ringwald { 108c894dca1SMatthias Ringwald rx_buffer_ptr = buffer; 109c894dca1SMatthias Ringwald bytes_to_read = len; 110c894dca1SMatthias Ringwald } 111c894dca1SMatthias Ringwald 112c894dca1SMatthias Ringwald void hal_btstack_run_loop_execute_once(void) 113c894dca1SMatthias Ringwald { 114c894dca1SMatthias Ringwald int rx_avail; 115c894dca1SMatthias Ringwald int num_rx_bytes; 116c894dca1SMatthias Ringwald int tx_avail; 117c894dca1SMatthias Ringwald int rx_bytes; 118c894dca1SMatthias Ringwald int tx_bytes; 119c894dca1SMatthias Ringwald int ret; 120c894dca1SMatthias Ringwald 121c894dca1SMatthias Ringwald while (bytes_to_read) { 122c894dca1SMatthias Ringwald rx_avail = UART_NumReadAvail(MXC_UART_GET_UART(CC256X_UART_ID)); 123c894dca1SMatthias Ringwald if (!rx_avail) 124c894dca1SMatthias Ringwald break; 125c894dca1SMatthias Ringwald 126c894dca1SMatthias Ringwald if (bytes_to_read > rx_avail) 127c894dca1SMatthias Ringwald num_rx_bytes = rx_avail; 128c894dca1SMatthias Ringwald else 129c894dca1SMatthias Ringwald num_rx_bytes = bytes_to_read; 130c894dca1SMatthias Ringwald 131c894dca1SMatthias Ringwald ret = UART_Read(MXC_UART_GET_UART(CC256X_UART_ID), rx_buffer_ptr, num_rx_bytes, &rx_bytes); 132c894dca1SMatthias Ringwald if (ret < 0) 133c894dca1SMatthias Ringwald break; 134c894dca1SMatthias Ringwald 135c894dca1SMatthias Ringwald rx_buffer_ptr += rx_bytes; 136c894dca1SMatthias Ringwald bytes_to_read -= rx_bytes; 137c894dca1SMatthias Ringwald 138c894dca1SMatthias Ringwald if (bytes_to_read < 0) { 139c894dca1SMatthias Ringwald bytes_to_read = 0; 140c894dca1SMatthias Ringwald } 141c894dca1SMatthias Ringwald 142c894dca1SMatthias Ringwald if (bytes_to_read == 0){ 143c894dca1SMatthias Ringwald (*rx_done_handler)(); 144c894dca1SMatthias Ringwald } 145c894dca1SMatthias Ringwald } 146c894dca1SMatthias Ringwald 147c894dca1SMatthias Ringwald while (bytes_to_write) { 148c894dca1SMatthias Ringwald tx_avail = UART_NumWriteAvail(MXC_UART_GET_UART(CC256X_UART_ID)); 149c894dca1SMatthias Ringwald if (!tx_avail) 150c894dca1SMatthias Ringwald break; 151c894dca1SMatthias Ringwald 152c894dca1SMatthias Ringwald if (bytes_to_write > tx_avail) 153c894dca1SMatthias Ringwald tx_bytes = tx_avail; 154c894dca1SMatthias Ringwald else 155c894dca1SMatthias Ringwald tx_bytes = bytes_to_write; 156c894dca1SMatthias Ringwald 157c894dca1SMatthias Ringwald ret = UART_Write(MXC_UART_GET_UART(CC256X_UART_ID), tx_buffer_ptr, tx_bytes); 158c894dca1SMatthias Ringwald if (ret < 0) 159c894dca1SMatthias Ringwald break; 160c894dca1SMatthias Ringwald bytes_to_write -= tx_bytes; 161c894dca1SMatthias Ringwald tx_buffer_ptr += tx_bytes; 162c894dca1SMatthias Ringwald if (bytes_to_write < 0) { 163c894dca1SMatthias Ringwald bytes_to_write = 0; 164c894dca1SMatthias Ringwald } 165c894dca1SMatthias Ringwald 166c894dca1SMatthias Ringwald if (bytes_to_write == 0){ 167c894dca1SMatthias Ringwald (*tx_done_handler)(); 168c894dca1SMatthias Ringwald } 169c894dca1SMatthias Ringwald } 170c894dca1SMatthias Ringwald 171c894dca1SMatthias Ringwald btstack_run_loop_embedded_execute_once(); 172c894dca1SMatthias Ringwald } 173c894dca1SMatthias Ringwald 174c894dca1SMatthias Ringwald void hal_uart_init(void) 175c894dca1SMatthias Ringwald { 176c894dca1SMatthias Ringwald int error = 0; 177c894dca1SMatthias Ringwald uart_cfg_t cfg; 178c894dca1SMatthias Ringwald 179c894dca1SMatthias Ringwald cfg.parity = UART_PARITY_DISABLE; 180c894dca1SMatthias Ringwald cfg.size = UART_DATA_SIZE_8_BITS; 181c894dca1SMatthias Ringwald cfg.extra_stop = 0; 182c894dca1SMatthias Ringwald cfg.cts = 1; 183c894dca1SMatthias Ringwald cfg.rts = 1; 184c894dca1SMatthias Ringwald 185c894dca1SMatthias Ringwald cfg.baud = baud_rate; 186c894dca1SMatthias Ringwald 187c894dca1SMatthias Ringwald sys_cfg_uart_t sys_cfg; 188c894dca1SMatthias Ringwald sys_cfg.clk_scale = CLKMAN_SCALE_AUTO; 189c894dca1SMatthias Ringwald 190c894dca1SMatthias Ringwald sys_cfg.io_cfg = (ioman_cfg_t )IOMAN_UART(0, 191c894dca1SMatthias Ringwald IOMAN_MAP_B, // io_map 192c894dca1SMatthias Ringwald IOMAN_MAP_B, // cts_map 193c894dca1SMatthias Ringwald IOMAN_MAP_B, // rts_map 194c894dca1SMatthias Ringwald 1, // io_en 195c894dca1SMatthias Ringwald 1, // cts_en 196c894dca1SMatthias Ringwald 1); //rts_en 197c894dca1SMatthias Ringwald 198c894dca1SMatthias Ringwald if ((error = UART_Init(MXC_UART_GET_UART(CC256X_UART_ID), &cfg, &sys_cfg)) != E_NO_ERROR) { 199c894dca1SMatthias Ringwald printf("Error initializing UART %d\n", error); 200c894dca1SMatthias Ringwald while (1); 201c894dca1SMatthias Ringwald } else { 202c894dca1SMatthias Ringwald printf("BTSTACK UART Initialized\n"); 203c894dca1SMatthias Ringwald } 204c894dca1SMatthias Ringwald 205c894dca1SMatthias Ringwald MXC_UART_GET_UART(CC256X_UART_ID)->ctrl |= MXC_F_UART_CTRL_CTS_POLARITY | MXC_F_UART_CTRL_RTS_POLARITY; 206c894dca1SMatthias Ringwald MXC_UART_GET_UART(CC256X_UART_ID)->ctrl &= ~((MXC_UART_FIFO_DEPTH - 4) << (MXC_F_UART_CTRL_RTS_LEVEL_POS)); 207c894dca1SMatthias Ringwald MXC_UART_GET_UART(CC256X_UART_ID)->ctrl |= ((UART_RXFIFO_USABLE) << MXC_F_UART_CTRL_RTS_LEVEL_POS); 208c894dca1SMatthias Ringwald } 209c894dca1SMatthias Ringwald 210c894dca1SMatthias Ringwald int hal_uart_dma_set_baud(uint32_t baud){ 211c894dca1SMatthias Ringwald baud_rate = baud; 212c894dca1SMatthias Ringwald printf("BAUD RATE IS = %d \n", baud); 213c894dca1SMatthias Ringwald hal_uart_init(); 214c894dca1SMatthias Ringwald return baud_rate; 215c894dca1SMatthias Ringwald } 216c894dca1SMatthias Ringwald 217c894dca1SMatthias Ringwald void hal_uart_dma_init(void){ 218c894dca1SMatthias Ringwald bytes_to_write = 0; 219c894dca1SMatthias Ringwald bytes_to_read = 0; 220c894dca1SMatthias Ringwald hal_uart_dma_set_baud(115200); 221c894dca1SMatthias Ringwald } 222c894dca1SMatthias Ringwald 223c894dca1SMatthias Ringwald void hal_uart_dma_set_block_received( void (*block_handler)(void)){ 224c894dca1SMatthias Ringwald rx_done_handler = block_handler; 225c894dca1SMatthias Ringwald } 226c894dca1SMatthias Ringwald 227c894dca1SMatthias Ringwald void hal_uart_dma_set_block_sent( void (*block_handler)(void)){ 228c894dca1SMatthias Ringwald 229c894dca1SMatthias Ringwald tx_done_handler = block_handler; 230c894dca1SMatthias Ringwald } 231c894dca1SMatthias Ringwald 232c894dca1SMatthias Ringwald void hal_uart_dma_set_csr_irq_handler( void (*csr_irq_handler)(void)){ 233c894dca1SMatthias Ringwald 234c894dca1SMatthias Ringwald } 235c894dca1SMatthias Ringwald 236c894dca1SMatthias Ringwald void hal_uart_dma_set_sleep(uint8_t sleep){ 237c894dca1SMatthias Ringwald 238c894dca1SMatthias Ringwald } 239c894dca1SMatthias Ringwald 240c894dca1SMatthias Ringwald void init_slow_clock(void) 241c894dca1SMatthias Ringwald { 242c894dca1SMatthias Ringwald MXC_PWRSEQ->reg0 &= ~(MXC_F_PWRSEQ_REG0_PWR_RTCEN_RUN | MXC_F_PWRSEQ_REG0_PWR_RTCEN_SLP); 243c894dca1SMatthias Ringwald MXC_PWRSEQ->reg4 &= ~MXC_F_PWRSEQ_REG4_PWR_PSEQ_32K_EN; 244c894dca1SMatthias Ringwald MXC_PWRSEQ->reg0 |= MXC_F_PWRSEQ_REG0_PWR_RTCEN_RUN | MXC_F_PWRSEQ_REG0_PWR_RTCEN_SLP; // Enable RTC 245c894dca1SMatthias Ringwald hal_delay_us(1); 246c894dca1SMatthias Ringwald MXC_PWRSEQ->reg4 |= MXC_F_PWRSEQ_REG4_PWR_PSEQ_32K_EN; // Enable the RTC out of P1.7 247c894dca1SMatthias Ringwald } 248c894dca1SMatthias Ringwald 249c894dca1SMatthias Ringwald int bt_comm_init() { 250c894dca1SMatthias Ringwald int error = 0; 251c894dca1SMatthias Ringwald int cnt = 0; 252c894dca1SMatthias Ringwald 253c894dca1SMatthias Ringwald hal_tick_init(); 254c894dca1SMatthias Ringwald hal_delay_us(1); 255*8959f869SMatthias Ringwald 256*8959f869SMatthias Ringwald /* HCI module RTS as input with 25k pullup */ 257c894dca1SMatthias Ringwald if ((error = GPIO_Config(&PAN1326_HCIRTS)) != E_NO_ERROR) { 258c894dca1SMatthias Ringwald printf("Error setting PAN1326_HCIRTS %d\n", error); 259c894dca1SMatthias Ringwald } 260c894dca1SMatthias Ringwald GPIO_OutSet(&PAN1326_HCIRTS); 261*8959f869SMatthias Ringwald 262c894dca1SMatthias Ringwald init_slow_clock(); 263c894dca1SMatthias Ringwald /* 264c894dca1SMatthias Ringwald * when enabling the P1.7 RTC output, P1.6 will be hardcoded to an input with 25k pullup enabled. 265c894dca1SMatthias Ringwald * There is an internal pullup, so when it is set as an input, it will float high. 266c894dca1SMatthias Ringwald * The PAN1326B data sheet says the NSHUTD pin is pulled down, but the input impedance is stated at 1Meg Ohm, 267c894dca1SMatthias Ringwald * The so the 25k pullup should be enough to reach the minimum 1.42V to enable the device. 268c894dca1SMatthias Ringwald * */ 269*8959f869SMatthias Ringwald 270*8959f869SMatthias Ringwald /* Force PAN1326 shutdown to be output and take it out of reset */ 271*8959f869SMatthias Ringwald if ((error = GPIO_Config(&PAN1326_nSHUTD)) != E_NO_ERROR) { 272*8959f869SMatthias Ringwald printf("Error setting PAN1326_nSHUTD %d\n", error); 273*8959f869SMatthias Ringwald } 274*8959f869SMatthias Ringwald GPIO_OutSet(&PAN1326_nSHUTD); 275*8959f869SMatthias Ringwald 276*8959f869SMatthias Ringwald /*Check the module is ready to receive data */ 277c894dca1SMatthias Ringwald while (GPIO_InGet(&PAN1326_HCIRTS)) { 278c894dca1SMatthias Ringwald cnt++; 279c894dca1SMatthias Ringwald } 280c894dca1SMatthias Ringwald 281c894dca1SMatthias Ringwald printf("%s CC256X init completed. cnt: %d \n", __func__, cnt); 282c894dca1SMatthias Ringwald return 0; 283c894dca1SMatthias Ringwald } 284c894dca1SMatthias Ringwald 285c894dca1SMatthias Ringwald static hci_transport_config_uart_t config = { 286c894dca1SMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 287c894dca1SMatthias Ringwald 115200, 288c894dca1SMatthias Ringwald 4000000, 289c894dca1SMatthias Ringwald 1, // flow control 290c894dca1SMatthias Ringwald "max32630fthr", 291c894dca1SMatthias Ringwald }; 292c894dca1SMatthias Ringwald 293183c110aSMatthias Ringwald // hal_led.h implementation 294183c110aSMatthias Ringwald #include "hal_led.h" 295183c110aSMatthias Ringwald void hal_led_off(void){ 29624efff18SMatthias Ringwald LED_Off(LED_BLUE); 297183c110aSMatthias Ringwald } 29824efff18SMatthias Ringwald 299183c110aSMatthias Ringwald void hal_led_on(void){ 30024efff18SMatthias Ringwald LED_On(LED_BLUE); 301183c110aSMatthias Ringwald } 30224efff18SMatthias Ringwald 303183c110aSMatthias Ringwald void hal_led_toggle(void){ 30424efff18SMatthias Ringwald LED_Toggle(LED_BLUE); 305183c110aSMatthias Ringwald } 306183c110aSMatthias Ringwald 30784e26812SMatthias Ringwald // hal_stdin.h 30884e26812SMatthias Ringwald static uint8_t stdin_buffer[1]; 30984e26812SMatthias Ringwald static void (*stdin_handler)(char c); 31084e26812SMatthias Ringwald 31184e26812SMatthias Ringwald static uart_req_t uart_byte_request; 31284e26812SMatthias Ringwald 31384e26812SMatthias Ringwald static void uart_rx_handler(uart_req_t *request, int error) 31484e26812SMatthias Ringwald { 31584e26812SMatthias Ringwald if (stdin_handler){ 31684e26812SMatthias Ringwald (*stdin_handler)(stdin_buffer[0]); 31784e26812SMatthias Ringwald } 31884e26812SMatthias Ringwald UART_ReadAsync(MXC_UART_GET_UART(CONSOLE_UART), &uart_byte_request); 31984e26812SMatthias Ringwald } 32084e26812SMatthias Ringwald 32184e26812SMatthias Ringwald void hal_stdin_setup(void (*handler)(char c)){ 32284e26812SMatthias Ringwald // set handler 32384e26812SMatthias Ringwald stdin_handler = handler; 32484e26812SMatthias Ringwald 32584e26812SMatthias Ringwald /* set input handler */ 32684e26812SMatthias Ringwald uart_byte_request.callback = uart_rx_handler; 32784e26812SMatthias Ringwald uart_byte_request.data = stdin_buffer; 32884e26812SMatthias Ringwald uart_byte_request.len = sizeof(uint8_t); 32984e26812SMatthias Ringwald UART_ReadAsync(MXC_UART_GET_UART(CONSOLE_UART), &uart_byte_request); 33084e26812SMatthias Ringwald } 33184e26812SMatthias Ringwald 33284e26812SMatthias Ringwald #if 0 33384e26812SMatthias Ringwald 33484e26812SMatthias Ringwald #include "btstack_stdin.h" 33584e26812SMatthias Ringwald 33684e26812SMatthias Ringwald static btstack_data_source_t stdin_data_source; 33784e26812SMatthias Ringwald static void (*stdin_handler)(char c); 33884e26812SMatthias Ringwald 33984e26812SMatthias Ringwald static uart_req_t uart_byte_request; 34084e26812SMatthias Ringwald static volatile int stdin_character_received; 34184e26812SMatthias Ringwald static uint8_t stdin_buffer[1]; 34284e26812SMatthias Ringwald 34384e26812SMatthias Ringwald static void stdin_rx_complete(void) { 34484e26812SMatthias Ringwald stdin_character_received = 1; 34584e26812SMatthias Ringwald } 34684e26812SMatthias Ringwald 34784e26812SMatthias Ringwald static void uart_rx_handler(uart_req_t *request, int error) 34884e26812SMatthias Ringwald { 34984e26812SMatthias Ringwald stdin_rx_complete(); 35084e26812SMatthias Ringwald } 35184e26812SMatthias Ringwald 35284e26812SMatthias Ringwald static void stdin_process(struct btstack_data_source *ds, btstack_data_source_callback_type_t callback_type){ 35384e26812SMatthias Ringwald if (!stdin_character_received) return; 35484e26812SMatthias Ringwald if (stdin_handler){ 35584e26812SMatthias Ringwald (*stdin_handler)(stdin_buffer[0]); 35684e26812SMatthias Ringwald } 35784e26812SMatthias Ringwald stdin_character_received = 0; 35884e26812SMatthias Ringwald UART_ReadAsync(MXC_UART_GET_UART(CONSOLE_UART), &uart_byte_request); 35984e26812SMatthias Ringwald } 36084e26812SMatthias Ringwald 36184e26812SMatthias Ringwald static void btstack_stdin_handler(char c){ 36284e26812SMatthias Ringwald stdin_character_received = 1; 36384e26812SMatthias Ringwald btstack_run_loop_embedded_trigger(); 36484e26812SMatthias Ringwald printf("Received: %c\n", c); 36584e26812SMatthias Ringwald } 36684e26812SMatthias Ringwald 36784e26812SMatthias Ringwald void btstack_stdin_setup(void (*handler)(char c)){ 36884e26812SMatthias Ringwald // set handler 36984e26812SMatthias Ringwald stdin_handler = handler; 37084e26812SMatthias Ringwald 37184e26812SMatthias Ringwald // set up polling data_source 37284e26812SMatthias Ringwald btstack_run_loop_set_data_source_handler(&stdin_data_source, &stdin_process); 37384e26812SMatthias Ringwald btstack_run_loop_enable_data_source_callbacks(&stdin_data_source, DATA_SOURCE_CALLBACK_POLL); 37484e26812SMatthias Ringwald btstack_run_loop_add_data_source(&stdin_data_source); 37584e26812SMatthias Ringwald 37684e26812SMatthias Ringwald /* set input handler */ 37784e26812SMatthias Ringwald uart_byte_request.callback = uart_rx_handler; 37884e26812SMatthias Ringwald uart_byte_request.data = stdin_buffer; 37984e26812SMatthias Ringwald uart_byte_request.len = sizeof(uint8_t); 38084e26812SMatthias Ringwald UART_ReadAsync(MXC_UART_GET_UART(CONSOLE_UART), &uart_byte_request); 38184e26812SMatthias Ringwald } 38284e26812SMatthias Ringwald #endif 38384e26812SMatthias Ringwald 38417ae5bc4SMatthias Ringwald #include "hal_flash_bank_mxc.h" 3855efdcd7dSMatthias Ringwald #include "btstack_tlv.h" 38617ae5bc4SMatthias Ringwald #include "btstack_tlv_flash_bank.h" 3875efdcd7dSMatthias Ringwald #include "btstack_link_key_db_tlv.h" 3885efdcd7dSMatthias Ringwald #include "le_device_db_tlv.h" 3895efdcd7dSMatthias Ringwald 39017ae5bc4SMatthias Ringwald #define HAL_FLASH_BANK_SIZE 0x2000 39117ae5bc4SMatthias Ringwald #define HAL_FLASH_BANK_0_ADDR 0x1FC000 39217ae5bc4SMatthias Ringwald #define HAL_FLASH_BANK_1_ADDR 0x1FE000 3935efdcd7dSMatthias Ringwald 39417ae5bc4SMatthias Ringwald static hal_flash_bank_mxc_t hal_flash_bank_context; 39517ae5bc4SMatthias Ringwald static btstack_tlv_flash_bank_t btstack_tlv_flash_bank_context; 3965efdcd7dSMatthias Ringwald 39784e26812SMatthias Ringwald 39884e26812SMatthias Ringwald /******************************************************************************/ 399c894dca1SMatthias Ringwald int bluetooth_main(void) 400c894dca1SMatthias Ringwald { 40124efff18SMatthias Ringwald LED_Off(LED_GREEN); 40224efff18SMatthias Ringwald LED_On(LED_RED); 40324efff18SMatthias Ringwald LED_Off(LED_BLUE); 40424efff18SMatthias Ringwald 405c894dca1SMatthias Ringwald bt_comm_init(); 406c894dca1SMatthias Ringwald /* BT Stack Initialization */ 407c894dca1SMatthias Ringwald btstack_memory_init(); 408c894dca1SMatthias Ringwald btstack_run_loop_init(btstack_run_loop_embedded_get_instance()); 409c894dca1SMatthias Ringwald 41084e26812SMatthias Ringwald // enable packet logger 41184e26812SMatthias Ringwald //hci_dump_open(NULL, HCI_DUMP_STDOUT); 41284e26812SMatthias Ringwald 413c894dca1SMatthias Ringwald /* Init HCI */ 414c894dca1SMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance(btstack_uart_block_embedded_instance()); 415c894dca1SMatthias Ringwald hci_init(transport, &config); 416c894dca1SMatthias Ringwald hci_set_chipset(btstack_chipset_cc256x_instance()); 4175efdcd7dSMatthias Ringwald 41817ae5bc4SMatthias Ringwald // setup TLV Flash Bank implementation 41917ae5bc4SMatthias Ringwald const hal_flash_bank_t * hal_flash_bank_impl = hal_flash_bank_mxc_init_instance( 42017ae5bc4SMatthias Ringwald &hal_flash_bank_context, 42117ae5bc4SMatthias Ringwald HAL_FLASH_BANK_SIZE, 42217ae5bc4SMatthias Ringwald HAL_FLASH_BANK_0_ADDR, 42317ae5bc4SMatthias Ringwald HAL_FLASH_BANK_1_ADDR); 42417ae5bc4SMatthias Ringwald const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_flash_bank_init_instance( 42517ae5bc4SMatthias Ringwald &btstack_tlv_flash_bank_context, 42617ae5bc4SMatthias Ringwald hal_flash_bank_impl, 42717ae5bc4SMatthias Ringwald &hal_flash_bank_context); 4285efdcd7dSMatthias Ringwald 4295efdcd7dSMatthias Ringwald // setup Link Key DB using TLV 43017ae5bc4SMatthias 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); 4315efdcd7dSMatthias Ringwald hci_set_link_key_db(btstack_link_key_db); 4325efdcd7dSMatthias Ringwald 4335efdcd7dSMatthias Ringwald // setup LE Device DB using TLV 43417ae5bc4SMatthias Ringwald le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_bank_context); 4355efdcd7dSMatthias Ringwald 4365efdcd7dSMatthias Ringwald // go 437c894dca1SMatthias Ringwald btstack_main(0, (void *)NULL); 438c894dca1SMatthias Ringwald return 0; 439c894dca1SMatthias Ringwald } 440