dc5v_uart.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*!
  2. * \file dc5v_uart.h
  3. * \brief DC5V_UART driver header
  4. * \details
  5. * \author
  6. * \date
  7. * \copyright Actions
  8. */
  9. #include <soc.h>
  10. #include <string.h>
  11. #include "os_common_api.h"
  12. #include "config.h"
  13. #include <sys/ring_buffer.h>
  14. #include <drivers/uart_dma.h>
  15. #include <drivers/uart.h>
  16. #define PMU_SYS_SET_REG (0x40004000 + 0x108)
  17. #define PMU_WKUP_CTL_REG (0x40004000 + 0x110)
  18. #define PMU_WKUP_PD_REG (0x40004000 + 0x114)
  19. #define GPIO_DC5V_CTL_REG (0x40068000 + 0x310)
  20. /* register bits start */
  21. #define DC5V_CTL_50KPU_EN BIT(11)
  22. #define DC5V_CTL_10KPU_EN BIT(8)
  23. #define DC5V_CTL_AD_SEL BIT(4)
  24. #define DC5V_CTL_MFP_SHIFT 0
  25. #define DC5V_CTL_MFP_MASK (0x0f << DC5V_CTL_MFP_SHIFT)
  26. #define DC5V_CTL_MFP_SET(x) ((x) << DC5V_CTL_MFP_SHIFT)
  27. #define DC5V_CTL_MFP_GPIO DC5V_CTL_MFP_SET(0)
  28. #define DC5V_CTL_MFP_UART1TX DC5V_CTL_MFP_SET(1)
  29. #define DC5V_CTL_MFP_UART1RX DC5V_CTL_MFP_SET(2)
  30. #define DC5V_CTL_MFP_UART2TX DC5V_CTL_MFP_SET(3)
  31. #define DC5V_CTL_MFP_UART2RX DC5V_CTL_MFP_SET(4)
  32. #define SYSSET_UART_PWR_SEL BIT(14)
  33. #define SYSSET_UART_SW_MODE BIT(13)
  34. #define SYSSET_UART_SW_SEL BIT(12)
  35. #define WKEN_CTL_DC5V_LHV_VOL_SHIFT 24
  36. #define WKEN_CTL_DC5V_LHV_VOL_MASK (0x07 << WKEN_CTL_DC5V_LHV_VOL_SHIFT)
  37. #define WKEN_CTL_DC5VLV_WKEN_SHIFT 10
  38. #define WKEN_CTL_DC5VLV_WKEN BIT(10)
  39. /* register bits end */
  40. #define UART_CTRL_RX_DMA_BUF_SIZE 200
  41. #define UART_CTRL_RX_DATA_BUF_SIZE 256
  42. #define UART_RX_DATADEAL_STACK 1024
  43. #define UART_RX_THREAD_PRIO 8
  44. #define DC5V_UART_DMA_IRQ_TC (0) /* DMA completion flag */
  45. #define DC5V_UART_DMA_IRQ_HF (1) /* DMA half-full flag */
  46. /* 定时一小段时间后将 DMA 接收的数据保存至 data_buf
  47. */
  48. #define UART_CTRL_RX_DMA_TIMER_US 300
  49. /* DMA 启动后再没有接收到数据后维持一段时间再停止
  50. */
  51. #define UART_CTRL_RX_DMA_KEEP_MS 20
  52. typedef enum
  53. {
  54. DC5V_UART_RX_MODE = 0,
  55. DC5V_UART_TX_MODE,
  56. }dc5v_uart_mode_e;
  57. typedef enum
  58. {
  59. UART_SET_BAUDRATE = 1,
  60. UART_INQUIRY_STATE,
  61. UART_CHECK_WRITE_FINISH,
  62. UART_IS_RX_FIFO_EMPTY = 10,
  63. UART_READ_BYTE_DATA,
  64. UART_RX_DMA_ACCESS_SWITCH,
  65. UART_RX_DRQ_SWITCH,
  66. UART_RX_IRQ_SWITCH,
  67. UART_CLEAR_RX_IRQ_PENDING,
  68. }uart_ioctl_cmd_e;
  69. typedef enum
  70. {
  71. DC5V_UART_INIT = 1,
  72. DC5V_UART_SUSPEND,
  73. DC5V_UART_RESUME,
  74. DC5V_UART_SET_RX_DATA_HANDLER,
  75. DC5V_UART_SET_RX_BUF_SIZE,
  76. DC5V_UART_READ,
  77. DC5V_UART_WRITE,
  78. DC5V_UART_IOCTL,
  79. DC5V_UART_CHECK_IO_TIME,
  80. DC5V_UART_SET_ENABLE,
  81. DC5V_UART_RUN_RXDEAL,
  82. DC5V_UART_STOP_RXDEAL,
  83. }DC5V_UART_OPS;
  84. typedef struct
  85. {
  86. u8_t dma_buf [UART_CTRL_RX_DMA_BUF_SIZE];
  87. u8_t data_buf[UART_CTRL_RX_DATA_BUF_SIZE];
  88. const struct device *rx_dev;
  89. struct ring_buf rx_rbuf;
  90. struct k_timer rx_timer;
  91. //u16_t wait_count;
  92. bool (*rx_data_handler)(u8_t byte);
  93. } uart_ctrl_rx_context_t;
  94. typedef struct
  95. {
  96. const struct device *tx_dev;
  97. u16_t tx_len;
  98. u8_t* tx_buf;
  99. bool tx_switch_finish;
  100. struct k_timer tx_switch_timer;
  101. struct k_timer tx_check_timer;
  102. } uart_ctrl_tx_context_t;
  103. typedef struct
  104. {
  105. CFG_Type_DC5V_UART_Comm_Settings cfg;
  106. u8_t dc5v_uart_init;
  107. u8_t trx_mode;
  108. u8_t redirect_console_print;
  109. u8_t suspended;
  110. u8_t enabled;
  111. u8_t rxdeal_need_quit;
  112. u8_t rxdeal_quited;
  113. u32_t last_io_time;
  114. u32_t baud_rate;
  115. u32_t bak_PMU_SYSTEM_SET_SVCC;
  116. u32_t bak_GPIO_DC5V_CTL;
  117. char* rxdeal_thread_stack;
  118. uart_ctrl_tx_context_t tx_context;
  119. uart_ctrl_rx_context_t rx_context;
  120. u32_t disable_delay_ms;
  121. struct k_timer disable_timer;
  122. } dc5v_uart_context_t;
  123. extern dc5v_uart_context_t dc5v_uart_context;
  124. extern int dc5v_uart_operate(u32_t cmd, void* param1, u32_t param2, u32_t param3);