rtthread_driver_serial.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <stdio.h>
  2. #include <rthw.h>
  3. #include <rtthread.h>
  4. #include <rtdevice.h>
  5. #include <board.h>
  6. #include "rtthread_driver_serial.h"
  7. #include "drivers/hci_driver.h"
  8. #include "drivers/hci_h4.h"
  9. #include "logging/bt_log_impl.h"
  10. #include "common/bt_buf.h"
  11. struct hci_trans_h4_uart_config {
  12. const char *device_name; /* Uart device name, i.e. "uart1" */
  13. int parity; /* Parity, i.e. PARITY_NONE */
  14. int stopbit; /* Stop bit, i.e. STOP_BITS_1 */
  15. int databit; /* Data bit, i.e. DATA_BITS_8 */
  16. uint32_t baudrate; /* Bautdate, i.e. BAUD_RATE_115200 */
  17. int flowcontrol; /* Flow Control, i.e. 1 */
  18. };
  19. struct h4_uart_config{
  20. const char *name;
  21. int flowcontrol; // RT-Thread not support CTS/RTS flowcontrol now, default is true.
  22. struct serial_configure rt_config;
  23. };
  24. static struct h4_uart_config uart_config = {
  25. .rt_config = RT_SERIAL_CONFIG_DEFAULT,
  26. };
  27. static rt_device_t h4_uart;
  28. static int hci_driver_h4_open(void)
  29. {
  30. RT_ASSERT(uart_config.name);
  31. printk("hci_driver_h4_open, uart_config.name: %s\n", uart_config.name);
  32. h4_uart = rt_device_find(uart_config.name);
  33. // printk("hci_driver_h4_open, h4_uart: 0x%x\n", h4_uart);
  34. RT_ASSERT(h4_uart);
  35. rt_err_t err;
  36. if ((err = rt_device_open(h4_uart, RT_DEVICE_FLAG_INT_RX))) {
  37. printk("Open h4_uart error\n");
  38. return -1;
  39. }
  40. if ((err = rt_device_control(h4_uart, RT_DEVICE_CTRL_CONFIG, &uart_config.rt_config))) {
  41. printk("Control h4_uart error\n");
  42. return -1;
  43. }
  44. return 0;
  45. }
  46. static int hci_driver_h4_send(uint8_t *buf, uint16_t len)
  47. {
  48. return rt_device_write(h4_uart, 0, buf, len);
  49. }
  50. static int hci_driver_h4_recv(uint8_t *buf, uint16_t len)
  51. {
  52. return rt_device_read(h4_uart, 0, buf, len);
  53. }
  54. static const struct bt_hci_h4_driver h4_drv = {
  55. .open = hci_driver_h4_open,
  56. .send = hci_driver_h4_send,
  57. .recv = hci_driver_h4_recv,
  58. };
  59. static void hci_driver_h4_init(void)
  60. {
  61. hci_h4_init(&h4_drv);
  62. }
  63. static char device_name[10];
  64. int uart_init_process(int idx, int rate, int databits, int stopbits, int parity, bool flowcontrol)
  65. {
  66. printk("uart_init_process idx: %d, rate: %d, databits: %d, stopbits: %d, parity: %d, flowcontrol: %d\n",
  67. idx, rate, databits, stopbits, parity, flowcontrol);
  68. rt_sprintf(device_name, "uart%d", idx);
  69. uart_config.name = device_name;
  70. uart_config.flowcontrol = flowcontrol;
  71. uart_config.rt_config.baud_rate = rate;
  72. uart_config.rt_config.data_bits = databits;
  73. uart_config.rt_config.stop_bits = stopbits;
  74. uart_config.rt_config.parity = parity;
  75. #if defined(RTTHREAD_VERSION) && RTTHREAD_VERSION > 40003 //< rtthread version larger than v4.0.3
  76. uart_config.rt_config.flowcontrol = flowcontrol;
  77. #endif
  78. return 0;
  79. }
  80. int bt_hci_init_serial_device(int idx, int rate, int databits, int stopbits, int parity, bool flowcontrol)
  81. {
  82. int ret = uart_init_process(idx, rate, databits, stopbits, parity, flowcontrol);
  83. if (ret < 0)
  84. {
  85. return ret;
  86. }
  87. hci_driver_h4_init();
  88. return (0);
  89. }