bcm.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "hm_chipset.h"
  2. #include "hm_hci_transport_h4.h"
  3. #include "hm_config.h"
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <rtthread.h>
  7. #define BCM_PATH_NAME "/dev/bt_image"
  8. #define HCI_COMMAND_HEADER 3
  9. static uint8_t download_commands[] = {
  10. 0x2e, 0xfc, 0x00,
  11. };
  12. int chipset_bcm_init(void)
  13. {
  14. int err = HM_SUCCESS;
  15. int res = 0;
  16. uint8_t event_buf[20];
  17. chip_send_hci_reset_cmd_until_ack();
  18. int fd = open(BCM_PATH_NAME, O_RDONLY);
  19. if (fd < 0) {
  20. rt_kprintf("Open bcm file %d error\n", BCM_PATH_NAME);
  21. return HM_OPEN_FILE_ERROR;
  22. }
  23. rt_kprintf("bcm init start\n");
  24. chip_hci_cmd_send(download_commands, ARRAY_SIZE(download_commands));
  25. chip_hci_event_read(event_buf, ARRAY_SIZE(event_buf), RT_WAITING_FOREVER);
  26. if (event_buf[0] != 0x0E) {
  27. err = HM_CHIPSET_INIT_ERROR;
  28. goto err_download_cmd;
  29. }
  30. uint16_t next_read = 0;
  31. uint8_t *p = hci_trans_h4_send_alloc(HCI_TRANS_H4_TYPE_CMD);
  32. RT_ASSERT(p);
  33. rt_kprintf("bcm start download init script\n");
  34. do {
  35. res = read(fd, p, 3);
  36. if (res == 0) {
  37. rt_kprintf("bcm chipset init file read end\n");
  38. break;
  39. }
  40. if (res < 0) {
  41. rt_kprintf("bcm chipset init file read error (%d)\n", res);
  42. err = HM_CHIPSET_INIT_FILE_ERROR;
  43. goto err_download;
  44. }
  45. /* Unknown error, test find it, is file end. */
  46. if (p[0] == 0xFF && p[1] == 0xFF && p[2] == 0xFF) {
  47. break;
  48. }
  49. next_read = p[2];
  50. res = read(fd, p + 3, next_read);
  51. if (res < 0) {
  52. rt_kprintf("bcm chipset init file read error (%d)\n", res);
  53. err = HM_CHIPSET_INIT_FILE_ERROR;
  54. goto err_download;
  55. }
  56. hci_trans_h4_send(HCI_TRANS_H4_TYPE_CMD, p);
  57. chip_hci_event_read(event_buf, ARRAY_SIZE(event_buf), RT_WAITING_FOREVER);
  58. if (event_buf[0] != 0x0E) {
  59. err = HM_CHIPSET_INIT_ERROR;
  60. goto err_download;
  61. }
  62. } while(1);
  63. close(fd);
  64. hci_trans_h4_send_free(p);
  65. rt_kprintf("bcm init script download success\n");
  66. chip_send_hci_reset_cmd_until_ack();
  67. rt_kprintf("bcm init success\n");
  68. return HM_SUCCESS;
  69. err_download:
  70. hci_trans_h4_send_free(p);
  71. err_download_cmd:
  72. close(fd);
  73. return err;
  74. }
  75. static hm_chipset_t chipset_bcm = {
  76. .name = "bcm",
  77. .init = chipset_bcm_init,
  78. };
  79. hm_chipset_t* hm_chipset_get_instance(void)
  80. {
  81. return &chipset_bcm;
  82. }
  83. #ifdef HM_USING_BOARD_ART_PI
  84. #include <spi_flash.h>
  85. #include <drv_spi.h>
  86. #include <fal.h>
  87. #define BT_FIRMWARE_PARTITION_NAME "bt_image"
  88. static int bt_firmware_create(void)
  89. {
  90. rt_kprintf("bt firmware start create.\n");
  91. rt_device_t bt_firmware = RT_NULL;
  92. const struct fal_partition *bt_partition = RT_NULL;
  93. bt_partition = fal_partition_find(BT_FIRMWARE_PARTITION_NAME);
  94. if (bt_partition == NULL)
  95. {
  96. rt_kprintf("%s partition is not exist, please check your configuration!\n", BT_FIRMWARE_PARTITION_NAME);
  97. return -1;
  98. }
  99. //create bt device fs
  100. bt_firmware = fal_char_device_create(BT_FIRMWARE_PARTITION_NAME);
  101. if (bt_firmware == NULL)
  102. {
  103. rt_kprintf("bt firmware device create failed\n");
  104. return -1;
  105. }
  106. return 0;
  107. }
  108. static int rt_flash_init(void)
  109. {
  110. extern rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const char *spi_dev_name);
  111. rt_hw_spi_device_attach("spi1", "spi10", GPIOA, GPIO_PIN_4);
  112. /* initialize SPI Flash device */
  113. rt_sfud_flash_probe("norflash0", "spi10");
  114. fal_init();
  115. bt_firmware_create();
  116. return 0;
  117. }
  118. INIT_ENV_EXPORT(rt_flash_init);
  119. #endif