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 "hal_tick.h" 36c894dca1SMatthias Ringwald #include <stdio.h> 37c894dca1SMatthias Ringwald #include <string.h> 38c894dca1SMatthias Ringwald #include "lp.h" 39c894dca1SMatthias Ringwald #include "uart.h" 40c894dca1SMatthias Ringwald #include "board.h" 41c894dca1SMatthias Ringwald #include "btstack_debug.h" 42c894dca1SMatthias Ringwald 43c894dca1SMatthias Ringwald #include "btstack.h" 44c894dca1SMatthias Ringwald #include "btstack_config.h" 45c894dca1SMatthias Ringwald #include "btstack_run_loop_embedded.h" 46c894dca1SMatthias Ringwald #include "btstack_chipset_cc256x.h" 47c894dca1SMatthias Ringwald #include "btstack_port.h" 48c894dca1SMatthias Ringwald 49c894dca1SMatthias Ringwald #define CC256X_UART_ID 0 50c894dca1SMatthias Ringwald #define UART_RXFIFO_USABLE (MXC_UART_FIFO_DEPTH-3) 51c894dca1SMatthias Ringwald 52c894dca1SMatthias Ringwald static uint32_t baud_rate; 53c894dca1SMatthias Ringwald 54c894dca1SMatthias Ringwald // rx state 55c894dca1SMatthias Ringwald static int bytes_to_read = 0; 56c894dca1SMatthias Ringwald static uint8_t * rx_buffer_ptr = 0; 57c894dca1SMatthias Ringwald 58c894dca1SMatthias Ringwald // tx state 59c894dca1SMatthias Ringwald static int bytes_to_write = 0; 60c894dca1SMatthias Ringwald static uint8_t * tx_buffer_ptr = 0; 61c894dca1SMatthias Ringwald 62c894dca1SMatthias Ringwald const gpio_cfg_t PAN1326_SLOW_CLK = { PORT_1, PIN_7, GPIO_FUNC_GPIO, 63c894dca1SMatthias Ringwald GPIO_PAD_NORMAL }; 64c894dca1SMatthias Ringwald const gpio_cfg_t PAN1326_nSHUTD = { PORT_1, PIN_6, GPIO_FUNC_GPIO, 65c894dca1SMatthias Ringwald GPIO_PAD_NORMAL }; 66c894dca1SMatthias Ringwald const gpio_cfg_t PAN1326_HCIRTS = { PORT_0, PIN_3, GPIO_FUNC_GPIO, 67c894dca1SMatthias Ringwald GPIO_PAD_NORMAL }; 68c894dca1SMatthias Ringwald 69c894dca1SMatthias Ringwald static void dummy_handler(void) {}; 70c894dca1SMatthias Ringwald static void (*rx_done_handler)(void) = dummy_handler; 71c894dca1SMatthias Ringwald static void (*tx_done_handler)(void) = dummy_handler; 72c894dca1SMatthias Ringwald 73c894dca1SMatthias Ringwald void hal_cpu_disable_irqs(void) 74c894dca1SMatthias Ringwald { 75c894dca1SMatthias Ringwald __disable_irq(); 76c894dca1SMatthias Ringwald } 77c894dca1SMatthias Ringwald 78c894dca1SMatthias Ringwald void hal_cpu_enable_irqs(void) 79c894dca1SMatthias Ringwald { 80c894dca1SMatthias Ringwald __enable_irq(); 81c894dca1SMatthias Ringwald } 82c894dca1SMatthias Ringwald void hal_cpu_enable_irqs_and_sleep(void) 83c894dca1SMatthias Ringwald { 84c894dca1SMatthias Ringwald 85c894dca1SMatthias Ringwald } 86c894dca1SMatthias Ringwald 87c894dca1SMatthias Ringwald void hal_uart_dma_send_block(const uint8_t *buffer, uint16_t len) 88c894dca1SMatthias Ringwald { 89c894dca1SMatthias Ringwald tx_buffer_ptr = (uint8_t *)buffer; 90c894dca1SMatthias Ringwald bytes_to_write = len; 91c894dca1SMatthias Ringwald } 92c894dca1SMatthias Ringwald 93c894dca1SMatthias Ringwald void hal_uart_dma_receive_block(uint8_t *buffer, uint16_t len) 94c894dca1SMatthias Ringwald { 95c894dca1SMatthias Ringwald rx_buffer_ptr = buffer; 96c894dca1SMatthias Ringwald bytes_to_read = len; 97c894dca1SMatthias Ringwald } 98c894dca1SMatthias Ringwald 99c894dca1SMatthias Ringwald void hal_btstack_run_loop_execute_once(void) 100c894dca1SMatthias Ringwald { 101c894dca1SMatthias Ringwald int rx_avail; 102c894dca1SMatthias Ringwald int num_rx_bytes; 103c894dca1SMatthias Ringwald int tx_avail; 104c894dca1SMatthias Ringwald int rx_bytes; 105c894dca1SMatthias Ringwald int tx_bytes; 106c894dca1SMatthias Ringwald int ret; 107c894dca1SMatthias Ringwald 108c894dca1SMatthias Ringwald while (bytes_to_read) { 109c894dca1SMatthias Ringwald rx_avail = UART_NumReadAvail(MXC_UART_GET_UART(CC256X_UART_ID)); 110c894dca1SMatthias Ringwald if (!rx_avail) 111c894dca1SMatthias Ringwald break; 112c894dca1SMatthias Ringwald 113c894dca1SMatthias Ringwald if (bytes_to_read > rx_avail) 114c894dca1SMatthias Ringwald num_rx_bytes = rx_avail; 115c894dca1SMatthias Ringwald else 116c894dca1SMatthias Ringwald num_rx_bytes = bytes_to_read; 117c894dca1SMatthias Ringwald 118c894dca1SMatthias Ringwald ret = UART_Read(MXC_UART_GET_UART(CC256X_UART_ID), rx_buffer_ptr, num_rx_bytes, &rx_bytes); 119c894dca1SMatthias Ringwald if (ret < 0) 120c894dca1SMatthias Ringwald break; 121c894dca1SMatthias Ringwald 122c894dca1SMatthias Ringwald rx_buffer_ptr += rx_bytes; 123c894dca1SMatthias Ringwald bytes_to_read -= rx_bytes; 124c894dca1SMatthias Ringwald 125c894dca1SMatthias Ringwald if (bytes_to_read < 0) { 126c894dca1SMatthias Ringwald bytes_to_read = 0; 127c894dca1SMatthias Ringwald } 128c894dca1SMatthias Ringwald 129c894dca1SMatthias Ringwald if (bytes_to_read == 0){ 130c894dca1SMatthias Ringwald (*rx_done_handler)(); 131c894dca1SMatthias Ringwald } 132c894dca1SMatthias Ringwald } 133c894dca1SMatthias Ringwald 134c894dca1SMatthias Ringwald while (bytes_to_write) { 135c894dca1SMatthias Ringwald tx_avail = UART_NumWriteAvail(MXC_UART_GET_UART(CC256X_UART_ID)); 136c894dca1SMatthias Ringwald if (!tx_avail) 137c894dca1SMatthias Ringwald break; 138c894dca1SMatthias Ringwald 139c894dca1SMatthias Ringwald if (bytes_to_write > tx_avail) 140c894dca1SMatthias Ringwald tx_bytes = tx_avail; 141c894dca1SMatthias Ringwald else 142c894dca1SMatthias Ringwald tx_bytes = bytes_to_write; 143c894dca1SMatthias Ringwald 144c894dca1SMatthias Ringwald ret = UART_Write(MXC_UART_GET_UART(CC256X_UART_ID), tx_buffer_ptr, tx_bytes); 145c894dca1SMatthias Ringwald if (ret < 0) 146c894dca1SMatthias Ringwald break; 147c894dca1SMatthias Ringwald bytes_to_write -= tx_bytes; 148c894dca1SMatthias Ringwald tx_buffer_ptr += tx_bytes; 149c894dca1SMatthias Ringwald if (bytes_to_write < 0) { 150c894dca1SMatthias Ringwald bytes_to_write = 0; 151c894dca1SMatthias Ringwald } 152c894dca1SMatthias Ringwald 153c894dca1SMatthias Ringwald if (bytes_to_write == 0){ 154c894dca1SMatthias Ringwald (*tx_done_handler)(); 155c894dca1SMatthias Ringwald } 156c894dca1SMatthias Ringwald } 157c894dca1SMatthias Ringwald 158c894dca1SMatthias Ringwald btstack_run_loop_embedded_execute_once(); 159c894dca1SMatthias Ringwald } 160c894dca1SMatthias Ringwald 161c894dca1SMatthias Ringwald void hal_uart_init(void) 162c894dca1SMatthias Ringwald { 163c894dca1SMatthias Ringwald int error = 0; 164c894dca1SMatthias Ringwald uart_cfg_t cfg; 165c894dca1SMatthias Ringwald 166c894dca1SMatthias Ringwald cfg.parity = UART_PARITY_DISABLE; 167c894dca1SMatthias Ringwald cfg.size = UART_DATA_SIZE_8_BITS; 168c894dca1SMatthias Ringwald cfg.extra_stop = 0; 169c894dca1SMatthias Ringwald cfg.cts = 1; 170c894dca1SMatthias Ringwald cfg.rts = 1; 171c894dca1SMatthias Ringwald 172c894dca1SMatthias Ringwald cfg.baud = baud_rate; 173c894dca1SMatthias Ringwald 174c894dca1SMatthias Ringwald sys_cfg_uart_t sys_cfg; 175c894dca1SMatthias Ringwald sys_cfg.clk_scale = CLKMAN_SCALE_AUTO; 176c894dca1SMatthias Ringwald 177c894dca1SMatthias Ringwald sys_cfg.io_cfg = (ioman_cfg_t )IOMAN_UART(0, 178c894dca1SMatthias Ringwald IOMAN_MAP_B, // io_map 179c894dca1SMatthias Ringwald IOMAN_MAP_B, // cts_map 180c894dca1SMatthias Ringwald IOMAN_MAP_B, // rts_map 181c894dca1SMatthias Ringwald 1, // io_en 182c894dca1SMatthias Ringwald 1, // cts_en 183c894dca1SMatthias Ringwald 1); //rts_en 184c894dca1SMatthias Ringwald 185c894dca1SMatthias Ringwald if ((error = UART_Init(MXC_UART_GET_UART(CC256X_UART_ID), &cfg, &sys_cfg)) != E_NO_ERROR) { 186c894dca1SMatthias Ringwald printf("Error initializing UART %d\n", error); 187c894dca1SMatthias Ringwald while (1); 188c894dca1SMatthias Ringwald } else { 189c894dca1SMatthias Ringwald printf("BTSTACK UART Initialized\n"); 190c894dca1SMatthias Ringwald } 191c894dca1SMatthias Ringwald 192c894dca1SMatthias Ringwald MXC_UART_GET_UART(CC256X_UART_ID)->ctrl |= MXC_F_UART_CTRL_CTS_POLARITY | MXC_F_UART_CTRL_RTS_POLARITY; 193c894dca1SMatthias Ringwald MXC_UART_GET_UART(CC256X_UART_ID)->ctrl &= ~((MXC_UART_FIFO_DEPTH - 4) << (MXC_F_UART_CTRL_RTS_LEVEL_POS)); 194c894dca1SMatthias Ringwald MXC_UART_GET_UART(CC256X_UART_ID)->ctrl |= ((UART_RXFIFO_USABLE) << MXC_F_UART_CTRL_RTS_LEVEL_POS); 195c894dca1SMatthias Ringwald } 196c894dca1SMatthias Ringwald 197c894dca1SMatthias Ringwald int hal_uart_dma_set_baud(uint32_t baud){ 198c894dca1SMatthias Ringwald baud_rate = baud; 199c894dca1SMatthias Ringwald printf("BAUD RATE IS = %d \n", baud); 200c894dca1SMatthias Ringwald hal_uart_init(); 201c894dca1SMatthias Ringwald return baud_rate; 202c894dca1SMatthias Ringwald } 203c894dca1SMatthias Ringwald 204c894dca1SMatthias Ringwald void hal_uart_dma_init(void){ 205c894dca1SMatthias Ringwald bytes_to_write = 0; 206c894dca1SMatthias Ringwald bytes_to_read = 0; 207c894dca1SMatthias Ringwald hal_uart_dma_set_baud(115200); 208c894dca1SMatthias Ringwald } 209c894dca1SMatthias Ringwald 210c894dca1SMatthias Ringwald void hal_uart_dma_set_block_received( void (*block_handler)(void)){ 211c894dca1SMatthias Ringwald rx_done_handler = block_handler; 212c894dca1SMatthias Ringwald } 213c894dca1SMatthias Ringwald 214c894dca1SMatthias Ringwald void hal_uart_dma_set_block_sent( void (*block_handler)(void)){ 215c894dca1SMatthias Ringwald 216c894dca1SMatthias Ringwald tx_done_handler = block_handler; 217c894dca1SMatthias Ringwald } 218c894dca1SMatthias Ringwald 219c894dca1SMatthias Ringwald void hal_uart_dma_set_csr_irq_handler( void (*csr_irq_handler)(void)){ 220c894dca1SMatthias Ringwald 221c894dca1SMatthias Ringwald } 222c894dca1SMatthias Ringwald 223c894dca1SMatthias Ringwald void hal_uart_dma_set_sleep(uint8_t sleep){ 224c894dca1SMatthias Ringwald 225c894dca1SMatthias Ringwald } 226c894dca1SMatthias Ringwald 227c894dca1SMatthias Ringwald void init_slow_clock(void) 228c894dca1SMatthias Ringwald { 229c894dca1SMatthias Ringwald MXC_PWRSEQ->reg0 &= ~(MXC_F_PWRSEQ_REG0_PWR_RTCEN_RUN | MXC_F_PWRSEQ_REG0_PWR_RTCEN_SLP); 230c894dca1SMatthias Ringwald MXC_PWRSEQ->reg4 &= ~MXC_F_PWRSEQ_REG4_PWR_PSEQ_32K_EN; 231c894dca1SMatthias Ringwald MXC_PWRSEQ->reg0 |= MXC_F_PWRSEQ_REG0_PWR_RTCEN_RUN | MXC_F_PWRSEQ_REG0_PWR_RTCEN_SLP; // Enable RTC 232c894dca1SMatthias Ringwald hal_delay_us(1); 233c894dca1SMatthias Ringwald MXC_PWRSEQ->reg4 |= MXC_F_PWRSEQ_REG4_PWR_PSEQ_32K_EN; // Enable the RTC out of P1.7 234c894dca1SMatthias Ringwald } 235c894dca1SMatthias Ringwald 236c894dca1SMatthias Ringwald int bt_comm_init() { 237c894dca1SMatthias Ringwald int error = 0; 238c894dca1SMatthias Ringwald int cnt = 0; 239c894dca1SMatthias Ringwald 240c894dca1SMatthias Ringwald hal_tick_init(); 241c894dca1SMatthias Ringwald hal_delay_us(1); 242c894dca1SMatthias Ringwald if ((error = GPIO_Config(&PAN1326_HCIRTS)) != E_NO_ERROR) { 243c894dca1SMatthias Ringwald printf("Error setting PAN1326_HCIRTS %d\n", error); 244c894dca1SMatthias Ringwald } 245c894dca1SMatthias Ringwald GPIO_OutSet(&PAN1326_HCIRTS); 246c894dca1SMatthias Ringwald init_slow_clock(); 247c894dca1SMatthias Ringwald /* 248c894dca1SMatthias Ringwald * when enabling the P1.7 RTC output, P1.6 will be hardcoded to an input with 25k pullup enabled. 249c894dca1SMatthias Ringwald * There is an internal pullup, so when it is set as an input, it will float high. 250c894dca1SMatthias Ringwald * The PAN1326B data sheet says the NSHUTD pin is pulled down, but the input impedance is stated at 1Meg Ohm, 251c894dca1SMatthias Ringwald * The so the 25k pullup should be enough to reach the minimum 1.42V to enable the device. 252c894dca1SMatthias Ringwald * */ 253c894dca1SMatthias Ringwald while (GPIO_InGet(&PAN1326_HCIRTS)) { 254c894dca1SMatthias Ringwald cnt++; 255c894dca1SMatthias Ringwald } 256c894dca1SMatthias Ringwald 257c894dca1SMatthias Ringwald printf("%s CC256X init completed. cnt: %d \n", __func__, cnt); 258c894dca1SMatthias Ringwald return 0; 259c894dca1SMatthias Ringwald } 260c894dca1SMatthias Ringwald 261c894dca1SMatthias Ringwald static hci_transport_config_uart_t config = { 262c894dca1SMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 263c894dca1SMatthias Ringwald 115200, 264c894dca1SMatthias Ringwald 4000000, 265c894dca1SMatthias Ringwald 1, // flow control 266c894dca1SMatthias Ringwald "max32630fthr", 267c894dca1SMatthias Ringwald }; 268c894dca1SMatthias Ringwald 269183c110aSMatthias Ringwald // hal_led.h implementation 270183c110aSMatthias Ringwald #include "hal_led.h" 271183c110aSMatthias Ringwald void hal_led_off(void){ 272183c110aSMatthias Ringwald } 273183c110aSMatthias Ringwald void hal_led_on(void){ 274183c110aSMatthias Ringwald } 275183c110aSMatthias Ringwald void hal_led_toggle(void){ 276183c110aSMatthias Ringwald } 277183c110aSMatthias Ringwald 278*17ae5bc4SMatthias Ringwald #include "hal_flash_bank_mxc.h" 2795efdcd7dSMatthias Ringwald #include "btstack_tlv.h" 280*17ae5bc4SMatthias Ringwald #include "btstack_tlv_flash_bank.h" 2815efdcd7dSMatthias Ringwald #include "btstack_link_key_db_tlv.h" 2825efdcd7dSMatthias Ringwald #include "le_device_db_tlv.h" 2835efdcd7dSMatthias Ringwald 284*17ae5bc4SMatthias Ringwald #define HAL_FLASH_BANK_SIZE 0x2000 285*17ae5bc4SMatthias Ringwald #define HAL_FLASH_BANK_0_ADDR 0x1FC000 286*17ae5bc4SMatthias Ringwald #define HAL_FLASH_BANK_1_ADDR 0x1FE000 2875efdcd7dSMatthias Ringwald 288*17ae5bc4SMatthias Ringwald static hal_flash_bank_mxc_t hal_flash_bank_context; 289*17ae5bc4SMatthias Ringwald static btstack_tlv_flash_bank_t btstack_tlv_flash_bank_context; 2905efdcd7dSMatthias Ringwald 291c894dca1SMatthias Ringwald int bluetooth_main(void) 292c894dca1SMatthias Ringwald { 293c894dca1SMatthias Ringwald bt_comm_init(); 294c894dca1SMatthias Ringwald /* BT Stack Initialization */ 295c894dca1SMatthias Ringwald btstack_memory_init(); 296c894dca1SMatthias Ringwald btstack_run_loop_init(btstack_run_loop_embedded_get_instance()); 297c894dca1SMatthias Ringwald 298c894dca1SMatthias Ringwald /* Init HCI */ 299c894dca1SMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance(btstack_uart_block_embedded_instance()); 300c894dca1SMatthias Ringwald hci_init(transport, &config); 301c894dca1SMatthias Ringwald hci_set_chipset(btstack_chipset_cc256x_instance()); 3025efdcd7dSMatthias Ringwald 303*17ae5bc4SMatthias Ringwald // setup TLV Flash Bank implementation 304*17ae5bc4SMatthias Ringwald const hal_flash_bank_t * hal_flash_bank_impl = hal_flash_bank_mxc_init_instance( 305*17ae5bc4SMatthias Ringwald &hal_flash_bank_context, 306*17ae5bc4SMatthias Ringwald HAL_FLASH_BANK_SIZE, 307*17ae5bc4SMatthias Ringwald HAL_FLASH_BANK_0_ADDR, 308*17ae5bc4SMatthias Ringwald HAL_FLASH_BANK_1_ADDR); 309*17ae5bc4SMatthias Ringwald const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_flash_bank_init_instance( 310*17ae5bc4SMatthias Ringwald &btstack_tlv_flash_bank_context, 311*17ae5bc4SMatthias Ringwald hal_flash_bank_impl, 312*17ae5bc4SMatthias Ringwald &hal_flash_bank_context); 3135efdcd7dSMatthias Ringwald 3145efdcd7dSMatthias Ringwald // setup Link Key DB using TLV 315*17ae5bc4SMatthias 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); 3165efdcd7dSMatthias Ringwald hci_set_link_key_db(btstack_link_key_db); 3175efdcd7dSMatthias Ringwald 3185efdcd7dSMatthias Ringwald // setup LE Device DB using TLV 319*17ae5bc4SMatthias Ringwald le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_bank_context); 3205efdcd7dSMatthias Ringwald 3215efdcd7dSMatthias Ringwald // go 322c894dca1SMatthias Ringwald btstack_main(0, (void *)NULL); 323c894dca1SMatthias Ringwald 324c894dca1SMatthias Ringwald return 0; 325c894dca1SMatthias Ringwald } 326