uart.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #pragma once
  2. #include "bsp/bsp.h"
  3. #include "os/os_task.h"
  4. #include "libs/byte_queue.h"
  5. #include "libs/circle_buffer.h"
  6. #define CH_START 0xF5
  7. #define CH_END 0xF6
  8. #define CH_ESC 0xF7
  9. #define CH_ESC_START 0x05
  10. #define CH_ESC_END 0x06
  11. #define CH_ESC_ESC 0x07
  12. #define SHARK_UART_TX_MEM_SIZE (512)
  13. #define SHARK_UART_RX_MEM_SIZE (256+128)
  14. #define RX_FRAME_MAX_LEN 260
  15. typedef enum {
  16. SHARK_UART0 = 0,
  17. SHARK_UART1,
  18. SHARK_UART2,
  19. SHARK_UART3,
  20. SHARK_UART4,
  21. SHARK_UART_COUNT
  22. } uart_enum_t;
  23. typedef struct {
  24. byte_queue_t tx_queue;
  25. u8 * rx_cache;
  26. dma_channel_type * rx_dma_ch;
  27. dma_channel_type * tx_dma_ch;
  28. uint16_t tx_length;
  29. uint16_t tx_crc16;
  30. usart_type *uart_com;//uart device
  31. uint8_t rx_frame[RX_FRAME_MAX_LEN];
  32. uint16_t rx_length;
  33. u16 rx_index;
  34. bool escape;
  35. bool start;
  36. bool uart_no_data;
  37. }shark_uart_t;
  38. void shark_uart_init(uart_enum_t uart_no);
  39. void shark_uart_deinit(uart_enum_t uart_no);
  40. void shark_uart_write_frame(uart_enum_t uart_no, uint8_t *bytes, int len);
  41. void shark_uart_frame_start(uart_enum_t uart_no, uint8_t *bytes, int len);
  42. void shark_uart_frame_continue(uart_enum_t uart_no, uint8_t *bytes, int len);
  43. void shark_uart_frame_end(uart_enum_t uart_no);
  44. void shark_uart_write_bytes(uart_enum_t uart_no, u8 *buff, u16 size);
  45. void shark_uart_flush(void);
  46. bool shark_uart_timeout(void);
  47. void shark_uart_log(void);
  48. int shark_uart_send_can_message(uint32_t efid, u8 *data, int len);
  49. static __INLINE void shark_uart_can_init(void){
  50. shark_uart_init(SHARK_UART0);
  51. }