main.c 2.6 KB

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