main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <stdarg.h>
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <rtthread.h>
  5. #include <rtdevice.h>
  6. #include <board.h>
  7. #include "rtthread_driver_serial.h"
  8. #include "chipset_interface.h"
  9. #include "platform_interface.h"
  10. #include "base/types.h"
  11. #include "utils/spool.h"
  12. #include <logging/bt_log_impl.h>
  13. #include <drivers/hci_driver.h>
  14. #include "host/hci_core.h"
  15. #include <bluetooth/bluetooth.h>
  16. #include <bluetooth/hci.h>
  17. extern void bt_ready(int err);
  18. extern void app_polling_work(void);
  19. int bt_init_hci_driver(void)
  20. {
  21. bt_uart_interface_t *p_interface = NULL;
  22. uint8_t com_num;
  23. #ifdef I
  24. // Enable AP6212 Bluetooth Power(BT_RST_EN)
  25. #define BT_AP6212_PIN GET_PIN(I, 11)
  26. rt_pin_mode(BT_AP6212_PIN, PIN_MODE_OUTPUT);
  27. rt_pin_write(BT_AP6212_PIN, PIN_LOW);
  28. rt_thread_mdelay(1000);
  29. rt_pin_write(BT_AP6212_PIN, PIN_HIGH);
  30. rt_thread_mdelay(1000);
  31. // Enable AP6212 Wake(BT_WAKE)
  32. // #define BT_AP6212_WAKE_PIN GET_PIN(I, 10)
  33. // rt_pin_mode(BT_AP6212_WAKE_PIN, PIN_MODE_OUTPUT);
  34. //
  35. // rt_pin_write(BT_AP6212_WAKE_PIN, PIN_HIGH);
  36. // rt_thread_mdelay(100);
  37. #endif
  38. p_interface = (bt_uart_interface_t *)bt_chipset_get_uart_interface();
  39. bt_uart_interface_t tmp = {0, 0, 0, 0, 0};
  40. tmp.rate = PKG_ZEPHYR_POLLING_HCI_UART_BAUDRATE;
  41. tmp.databits = p_interface->databits;
  42. tmp.stopbits = p_interface->stopbits;
  43. tmp.parity = p_interface->parity;
  44. #ifdef PKG_ZEPHYR_POLLING_HCI_UART_BAUDRATE_FLOWCONTROL
  45. tmp.flowcontrol = 1;
  46. #endif
  47. com_num = PKG_ZEPHYR_POLLING_HCI_UART_INDEX;
  48. if (bt_hci_init_serial_device(com_num, tmp.rate, tmp.databits, tmp.stopbits,
  49. tmp.parity, tmp.flowcontrol) < 0)
  50. {
  51. printk("Error, uart open failed.");
  52. return -1;
  53. }
  54. return 0;
  55. }
  56. void zephyr_polling_main(void* parameter)
  57. {
  58. int err = 0;
  59. bt_log_impl_register(bt_log_impl_local_instance());
  60. if (bt_init_hci_driver() < 0)
  61. {
  62. return;
  63. }
  64. bt_hci_chipset_driver_register(bt_hci_chipset_impl_local_instance());
  65. bt_storage_kv_register(bt_storage_kv_impl_local_instance());
  66. bt_timer_impl_local_init();
  67. /* Initialize the Bluetooth Subsystem */
  68. err = bt_enable(bt_ready);
  69. if(err)
  70. {
  71. printk("bt_enable(), err: %d\n", err);
  72. }
  73. #if defined(CONFIG_BT_MONITOR_SLEEP)
  74. bt_init_monitor_sleep();
  75. #endif
  76. while (1)
  77. {
  78. #if defined(CONFIG_BT_MONITOR_SLEEP)
  79. if (!bt_check_is_in_sleep())
  80. {
  81. bt_polling_work();
  82. if (bt_is_ready() && bt_check_allow_sleep())
  83. {
  84. bt_sleep_prepare_work();
  85. }
  86. }
  87. #else
  88. bt_polling_work();
  89. #endif
  90. app_polling_work();
  91. extern void bt_hci_h4_polling(void);
  92. bt_hci_h4_polling();
  93. // rt_thread_yield();
  94. rt_thread_delay(1);
  95. }
  96. }
  97. int zephyr(void)
  98. {
  99. static rt_thread_t tid = RT_NULL;
  100. tid = rt_thread_create("zephyr_polling_main",
  101. zephyr_polling_main, RT_NULL,
  102. 4096,
  103. 5, 5);
  104. if (tid != RT_NULL)
  105. {
  106. rt_thread_startup(tid);
  107. }
  108. return 0;
  109. }
  110. // INIT_APP_EXPORT(zephyr_polling_init);
  111. MSH_CMD_EXPORT(zephyr, "zephyr_polling start");