controller_hci_uart_demo.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "nvs_flash.h"
  16. #include "bt.h"
  17. #include "driver/uart.h"
  18. #include "esp_log.h"
  19. static const char *tag = "CONTROLLER_UART_HCI";
  20. static void uart_gpio_reset(void)
  21. {
  22. #if CONFIG_BT_HCI_UART_NO == 1
  23. periph_module_enable(PERIPH_UART1_MODULE);
  24. #elif CONFIG_BT_HCI_UART_NO == 2
  25. periph_module_enable(PERIPH_UART2_MODULE);
  26. #endif
  27. periph_module_enable(PERIPH_UHCI0_MODULE);
  28. #ifdef CONFIG_BT_HCI_UART_NO
  29. ESP_LOGI(tag, "HCI UART%d Pin select: TX 5, RX, 18, CTS 23, RTS 19", CONFIG_BT_HCI_UART_NO);
  30. uart_set_pin(CONFIG_BT_HCI_UART_NO, 5, 18, 19, 23);
  31. #endif
  32. }
  33. void app_main()
  34. {
  35. esp_err_t ret;
  36. /* Initialize NVS — it is used to store PHY calibration data */
  37. ret = nvs_flash_init();
  38. if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
  39. ESP_ERROR_CHECK(nvs_flash_erase());
  40. ret = nvs_flash_init();
  41. }
  42. ESP_ERROR_CHECK( ret );
  43. /* As the UART1/2 pin conflict with flash pin, so do matrix of the signal and pin */
  44. uart_gpio_reset();
  45. esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  46. ret = esp_bt_controller_init(&bt_cfg);
  47. if (ret != ESP_OK) {
  48. ESP_LOGE(tag, "Bluetooth Controller initialize failed, ret %d", ret);
  49. return;
  50. }
  51. ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
  52. if (ret != ESP_OK) {
  53. ESP_LOGE(tag, "Bluetooth Controller initialize failed, ret %d", ret);
  54. return;
  55. }
  56. }