1 /******************************************************************************* 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. 3 * Author: Ismail H. Kose <[email protected]> 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included 13 * in all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES 19 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21 * OTHER DEALINGS IN THE SOFTWARE. 22 * 23 * Except as contained in this notice, the name of Maxim Integrated 24 * Products, Inc. shall not be used except as stated in the Maxim Integrated 25 * Products, Inc. Branding Policy. 26 * 27 * The mere transfer of this software does not imply any licenses 28 * of trade secrets, proprietary technology, copyrights, patents, 29 * trademarks, maskwork rights, or any other form of intellectual 30 * property whatsoever. Maxim Integrated Products, Inc. retains all 31 * ownership rights. 32 ******************************************************************************* 33 */ 34 35 #include <stdio.h> 36 #include <string.h> 37 38 // MXC 39 #include "lp.h" 40 #include "uart.h" 41 #include "board.h" 42 #include "led.h" 43 44 // BTstack Core 45 #include "btstack_debug.h" 46 #include "btstack.h" 47 #include "btstack_config.h" 48 #include "btstack_run_loop_embedded.h" 49 #include "btstack_chipset_cc256x.h" 50 51 // BTstack HALs 52 #include "hal_tick.h" 53 #include "hal_stdin.h" 54 55 #include "btstack_port.h" 56 57 #define CC256X_UART_ID 0 58 #define UART_RXFIFO_USABLE (MXC_UART_FIFO_DEPTH-3) 59 60 static uint32_t baud_rate; 61 62 // rx state 63 static int bytes_to_read = 0; 64 static uint8_t * rx_buffer_ptr = 0; 65 66 // tx state 67 static int bytes_to_write = 0; 68 static uint8_t * tx_buffer_ptr = 0; 69 70 const gpio_cfg_t PAN1326_SLOW_CLK = { PORT_1, PIN_7, GPIO_FUNC_GPIO, 71 GPIO_PAD_NORMAL }; 72 const gpio_cfg_t PAN1326_nSHUTD = { PORT_1, PIN_6, GPIO_FUNC_GPIO, 73 GPIO_PAD_NORMAL }; 74 const gpio_cfg_t PAN1326_HCIRTS = { PORT_0, PIN_3, GPIO_FUNC_GPIO, 75 GPIO_PAD_INPUT_PULLUP }; 76 const gpio_cfg_t PAN1326_HCICTS = { PORT_0, PIN_2, GPIO_FUNC_GPIO, 77 GPIO_PAD_NORMAL }; 78 79 static void dummy_handler(void) {}; 80 static void (*rx_done_handler)(void) = dummy_handler; 81 static void (*tx_done_handler)(void) = dummy_handler; 82 83 84 85 void hal_cpu_disable_irqs(void) 86 { 87 __disable_irq(); 88 } 89 90 void hal_cpu_enable_irqs(void) 91 { 92 __enable_irq(); 93 } 94 void hal_cpu_enable_irqs_and_sleep(void) 95 { 96 __enable_irq(); 97 /* TODO: Add sleep mode */ 98 } 99 100 void hal_uart_dma_send_block(const uint8_t *buffer, uint16_t len) 101 { 102 tx_buffer_ptr = (uint8_t *)buffer; 103 bytes_to_write = len; 104 } 105 106 void hal_uart_dma_receive_block(uint8_t *buffer, uint16_t len) 107 { 108 rx_buffer_ptr = buffer; 109 bytes_to_read = len; 110 } 111 112 void hal_btstack_run_loop_execute_once(void) 113 { 114 int rx_avail; 115 int num_rx_bytes; 116 int tx_avail; 117 int rx_bytes; 118 int tx_bytes; 119 int ret; 120 121 while (bytes_to_read) { 122 rx_avail = UART_NumReadAvail(MXC_UART_GET_UART(CC256X_UART_ID)); 123 if (!rx_avail) 124 break; 125 126 if (bytes_to_read > rx_avail) 127 num_rx_bytes = rx_avail; 128 else 129 num_rx_bytes = bytes_to_read; 130 131 ret = UART_Read(MXC_UART_GET_UART(CC256X_UART_ID), rx_buffer_ptr, num_rx_bytes, &rx_bytes); 132 if (ret < 0) 133 break; 134 135 rx_buffer_ptr += rx_bytes; 136 bytes_to_read -= rx_bytes; 137 138 if (bytes_to_read < 0) { 139 bytes_to_read = 0; 140 } 141 142 if (bytes_to_read == 0){ 143 (*rx_done_handler)(); 144 } 145 } 146 147 while (bytes_to_write) { 148 tx_avail = UART_NumWriteAvail(MXC_UART_GET_UART(CC256X_UART_ID)); 149 if (!tx_avail) 150 break; 151 152 if (bytes_to_write > tx_avail) 153 tx_bytes = tx_avail; 154 else 155 tx_bytes = bytes_to_write; 156 157 ret = UART_Write(MXC_UART_GET_UART(CC256X_UART_ID), tx_buffer_ptr, tx_bytes); 158 if (ret < 0) 159 break; 160 bytes_to_write -= tx_bytes; 161 tx_buffer_ptr += tx_bytes; 162 if (bytes_to_write < 0) { 163 bytes_to_write = 0; 164 } 165 166 if (bytes_to_write == 0){ 167 (*tx_done_handler)(); 168 } 169 } 170 171 btstack_run_loop_embedded_execute_once(); 172 } 173 174 void hal_uart_init(void) 175 { 176 int error = 0; 177 uart_cfg_t cfg; 178 179 cfg.parity = UART_PARITY_DISABLE; 180 cfg.size = UART_DATA_SIZE_8_BITS; 181 cfg.extra_stop = 0; 182 cfg.cts = 1; 183 cfg.rts = 1; 184 185 cfg.baud = baud_rate; 186 187 sys_cfg_uart_t sys_cfg; 188 sys_cfg.clk_scale = CLKMAN_SCALE_AUTO; 189 190 sys_cfg.io_cfg = (ioman_cfg_t )IOMAN_UART(0, 191 IOMAN_MAP_B, // io_map 192 IOMAN_MAP_B, // cts_map 193 IOMAN_MAP_B, // rts_map 194 1, // io_en 195 1, // cts_en 196 1); //rts_en 197 198 if ((error = UART_Init(MXC_UART_GET_UART(CC256X_UART_ID), &cfg, &sys_cfg)) != E_NO_ERROR) { 199 printf("Error initializing UART %d\n", error); 200 while (1); 201 } else { 202 printf("BTSTACK UART Initialized\n"); 203 } 204 205 MXC_UART_GET_UART(CC256X_UART_ID)->ctrl |= MXC_F_UART_CTRL_CTS_POLARITY | MXC_F_UART_CTRL_RTS_POLARITY; 206 MXC_UART_GET_UART(CC256X_UART_ID)->ctrl &= ~((MXC_UART_FIFO_DEPTH - 4) << (MXC_F_UART_CTRL_RTS_LEVEL_POS)); 207 MXC_UART_GET_UART(CC256X_UART_ID)->ctrl |= ((UART_RXFIFO_USABLE) << MXC_F_UART_CTRL_RTS_LEVEL_POS); 208 } 209 210 int hal_uart_dma_set_baud(uint32_t baud){ 211 baud_rate = baud; 212 printf("BAUD RATE IS = %d \n", baud); 213 hal_uart_init(); 214 return baud_rate; 215 } 216 217 void hal_uart_dma_init(void){ 218 bytes_to_write = 0; 219 bytes_to_read = 0; 220 hal_uart_dma_set_baud(115200); 221 } 222 223 void hal_uart_dma_set_block_received( void (*block_handler)(void)){ 224 rx_done_handler = block_handler; 225 } 226 227 void hal_uart_dma_set_block_sent( void (*block_handler)(void)){ 228 229 tx_done_handler = block_handler; 230 } 231 232 void hal_uart_dma_set_csr_irq_handler( void (*csr_irq_handler)(void)){ 233 234 } 235 236 void hal_uart_dma_set_sleep(uint8_t sleep){ 237 238 } 239 240 void init_slow_clock(void) 241 { 242 MXC_PWRSEQ->reg0 &= ~(MXC_F_PWRSEQ_REG0_PWR_RTCEN_RUN | MXC_F_PWRSEQ_REG0_PWR_RTCEN_SLP); 243 MXC_PWRSEQ->reg4 &= ~MXC_F_PWRSEQ_REG4_PWR_PSEQ_32K_EN; 244 MXC_PWRSEQ->reg0 |= MXC_F_PWRSEQ_REG0_PWR_RTCEN_RUN | MXC_F_PWRSEQ_REG0_PWR_RTCEN_SLP; // Enable RTC 245 hal_delay_us(1); 246 MXC_PWRSEQ->reg4 |= MXC_F_PWRSEQ_REG4_PWR_PSEQ_32K_EN; // Enable the RTC out of P1.7 247 } 248 249 int bt_comm_init() { 250 int error = 0; 251 int cnt = 0; 252 253 hal_tick_init(); 254 hal_delay_us(1); 255 256 /* HCI module RTS as input with 25k pullup */ 257 if ((error = GPIO_Config(&PAN1326_HCIRTS)) != E_NO_ERROR) { 258 printf("Error setting PAN1326_HCIRTS %d\n", error); 259 } 260 GPIO_OutSet(&PAN1326_HCIRTS); 261 262 init_slow_clock(); 263 /* 264 * when enabling the P1.7 RTC output, P1.6 will be hardcoded to an input with 25k pullup enabled. 265 * There is an internal pullup, so when it is set as an input, it will float high. 266 * The PAN1326B data sheet says the NSHUTD pin is pulled down, but the input impedance is stated at 1Meg Ohm, 267 * The so the 25k pullup should be enough to reach the minimum 1.42V to enable the device. 268 * */ 269 270 /* Force PAN1326 shutdown to be output and take it out of reset */ 271 if ((error = GPIO_Config(&PAN1326_nSHUTD)) != E_NO_ERROR) { 272 printf("Error setting PAN1326_nSHUTD %d\n", error); 273 } 274 GPIO_OutSet(&PAN1326_nSHUTD); 275 276 /*Check the module is ready to receive data */ 277 while (GPIO_InGet(&PAN1326_HCIRTS)) { 278 cnt++; 279 } 280 281 printf("%s CC256X init completed. cnt: %d \n", __func__, cnt); 282 return 0; 283 } 284 285 static hci_transport_config_uart_t config = { 286 HCI_TRANSPORT_CONFIG_UART, 287 115200, 288 4000000, 289 1, // flow control 290 "max32630fthr", 291 }; 292 293 // hal_led.h implementation 294 #include "hal_led.h" 295 void hal_led_off(void){ 296 LED_Off(LED_BLUE); 297 } 298 299 void hal_led_on(void){ 300 LED_On(LED_BLUE); 301 } 302 303 void hal_led_toggle(void){ 304 LED_Toggle(LED_BLUE); 305 } 306 307 // hal_stdin.h 308 static uint8_t stdin_buffer[1]; 309 static void (*stdin_handler)(char c); 310 311 static uart_req_t uart_byte_request; 312 313 static void uart_rx_handler(uart_req_t *request, int error) 314 { 315 if (stdin_handler){ 316 (*stdin_handler)(stdin_buffer[0]); 317 } 318 UART_ReadAsync(MXC_UART_GET_UART(CONSOLE_UART), &uart_byte_request); 319 } 320 321 void hal_stdin_setup(void (*handler)(char c)){ 322 // set handler 323 stdin_handler = handler; 324 325 /* set input handler */ 326 uart_byte_request.callback = uart_rx_handler; 327 uart_byte_request.data = stdin_buffer; 328 uart_byte_request.len = sizeof(uint8_t); 329 UART_ReadAsync(MXC_UART_GET_UART(CONSOLE_UART), &uart_byte_request); 330 } 331 332 #if 0 333 334 #include "btstack_stdin.h" 335 336 static btstack_data_source_t stdin_data_source; 337 static void (*stdin_handler)(char c); 338 339 static uart_req_t uart_byte_request; 340 static volatile int stdin_character_received; 341 static uint8_t stdin_buffer[1]; 342 343 static void stdin_rx_complete(void) { 344 stdin_character_received = 1; 345 } 346 347 static void uart_rx_handler(uart_req_t *request, int error) 348 { 349 stdin_rx_complete(); 350 } 351 352 static void stdin_process(struct btstack_data_source *ds, btstack_data_source_callback_type_t callback_type){ 353 if (!stdin_character_received) return; 354 if (stdin_handler){ 355 (*stdin_handler)(stdin_buffer[0]); 356 } 357 stdin_character_received = 0; 358 UART_ReadAsync(MXC_UART_GET_UART(CONSOLE_UART), &uart_byte_request); 359 } 360 361 static void btstack_stdin_handler(char c){ 362 stdin_character_received = 1; 363 btstack_run_loop_embedded_trigger(); 364 printf("Received: %c\n", c); 365 } 366 367 void btstack_stdin_setup(void (*handler)(char c)){ 368 // set handler 369 stdin_handler = handler; 370 371 // set up polling data_source 372 btstack_run_loop_set_data_source_handler(&stdin_data_source, &stdin_process); 373 btstack_run_loop_enable_data_source_callbacks(&stdin_data_source, DATA_SOURCE_CALLBACK_POLL); 374 btstack_run_loop_add_data_source(&stdin_data_source); 375 376 /* set input handler */ 377 uart_byte_request.callback = uart_rx_handler; 378 uart_byte_request.data = stdin_buffer; 379 uart_byte_request.len = sizeof(uint8_t); 380 UART_ReadAsync(MXC_UART_GET_UART(CONSOLE_UART), &uart_byte_request); 381 } 382 #endif 383 384 #include "hal_flash_bank_mxc.h" 385 #include "btstack_tlv.h" 386 #include "btstack_tlv_flash_bank.h" 387 #include "btstack_link_key_db_tlv.h" 388 #include "le_device_db_tlv.h" 389 390 #define HAL_FLASH_BANK_SIZE 0x2000 391 #define HAL_FLASH_BANK_0_ADDR 0x1FC000 392 #define HAL_FLASH_BANK_1_ADDR 0x1FE000 393 394 static hal_flash_bank_mxc_t hal_flash_bank_context; 395 static btstack_tlv_flash_bank_t btstack_tlv_flash_bank_context; 396 397 398 /******************************************************************************/ 399 int bluetooth_main(void) 400 { 401 LED_Off(LED_GREEN); 402 LED_On(LED_RED); 403 LED_Off(LED_BLUE); 404 405 bt_comm_init(); 406 /* BT Stack Initialization */ 407 btstack_memory_init(); 408 btstack_run_loop_init(btstack_run_loop_embedded_get_instance()); 409 410 // enable packet logger 411 //hci_dump_open(NULL, HCI_DUMP_STDOUT); 412 413 /* Init HCI */ 414 const hci_transport_t * transport = hci_transport_h4_instance(btstack_uart_block_embedded_instance()); 415 hci_init(transport, &config); 416 hci_set_chipset(btstack_chipset_cc256x_instance()); 417 418 // setup TLV Flash Bank implementation 419 const hal_flash_bank_t * hal_flash_bank_impl = hal_flash_bank_mxc_init_instance( 420 &hal_flash_bank_context, 421 HAL_FLASH_BANK_SIZE, 422 HAL_FLASH_BANK_0_ADDR, 423 HAL_FLASH_BANK_1_ADDR); 424 const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_flash_bank_init_instance( 425 &btstack_tlv_flash_bank_context, 426 hal_flash_bank_impl, 427 &hal_flash_bank_context); 428 429 // setup Link Key DB using TLV 430 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); 431 hci_set_link_key_db(btstack_link_key_db); 432 433 // setup LE Device DB using TLV 434 le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_bank_context); 435 436 // go 437 btstack_main(0, (void *)NULL); 438 return 0; 439 } 440