shark_uart.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #pragma once
  2. #include "s600.h"
  3. #include "s600_can.h"
  4. #include "byte_queue.h"
  5. #define S600_UART_USE_DMA 1
  6. #define S600_UART_USE_CACHE 1
  7. #define S600_UART_CACHE_TIMES 200
  8. #define CH_START 0xF5
  9. #define CH_END 0xF6
  10. #define CH_ESC 0xF7
  11. #define CH_ESC_START 0x05
  12. #define CH_ESC_END 0x06
  13. #define CH_ESC_ESC 0x07
  14. #define S600_UART_FRAME_SIZE 2048
  15. #ifdef CONFIG_BOARD_S600
  16. #define S600_UART_TX_MEM_SIZE 512
  17. #define S600_UART_RX_MEM_SIZE 512
  18. #else
  19. #define S600_UART_TX_MEM_SIZE 1024
  20. #define S600_UART_RX_MEM_SIZE 1024
  21. #endif
  22. #define S600_UART_MAIN_TX_MEM_SIZE 2048
  23. #define S600_UART_MAIN_RX_MEM_SIZE 2048
  24. typedef enum {
  25. CMD_CAN = 0,
  26. CMD_PING,
  27. CMD_VERSION,
  28. CMD_PRINT_ADD,
  29. CMD_PRINT_END,
  30. CMD_PWM_SET_ENABLE, // 5
  31. CMD_PWM_SET_VALUE,
  32. CMD_UART0,
  33. CMD_UART1,
  34. CMD_UART2,
  35. CMD_UART3, // 10
  36. CMD_UART4,
  37. CMD_UART_CONFIG,
  38. CMD_GPIO_INIT,
  39. CMD_GPIO_INPUT_BIT_GET,
  40. CMD_GPIO_OUTPUT_BIT_SET, // 15
  41. CMD_GPIO_OUTPUT_BIT_GET,
  42. CMD_GPIO_INPUT_PORT_GET,
  43. CMD_GPIO_OUTPUT_PORT_SET,
  44. CMD_GPIO_OUTPUT_PORT_GET,
  45. CMD_ADC, // 20
  46. CMD_DAC0,
  47. CMD_DAC1,
  48. CMD_DAC_SET_ENABLE,
  49. CMD_DAC_SET_VOLUME,
  50. CMD_DAC_SET_RATE, // 25
  51. CMD_SINGLE_COMM_CONFIG,
  52. CMD_SINGLE_COMM_DATA,
  53. CMD_SPI_LED_INIT,
  54. CMD_SPI_LED_COMMIT,
  55. CMD_SPI_LED_CLEAR, // 30
  56. CMD_SPI_LED_SET_COLOR,
  57. CMD_REG_RW,
  58. CMD_MEM_RW,
  59. CMD_I2C_INIT,
  60. CMD_I2C_RW, // 35
  61. CMD_EXTI_INIT,
  62. CMD_EXTI_NOTIFY,
  63. } short_command_t;
  64. typedef enum {
  65. S600_UART0,
  66. S600_UART1,
  67. S600_UART2,
  68. S600_UART3,
  69. S600_UART4,
  70. S600_UART_COUNT
  71. } s600_uart_t;
  72. typedef struct s600_uart_device {
  73. u32 com;
  74. u32 tx_dma;
  75. u32 rx_dma;
  76. dma_channel_enum tx_dma_ch;
  77. dma_channel_enum rx_dma_ch;
  78. #if S600_UART_USE_CACHE
  79. u8 cache[64];
  80. u8 cache_len;
  81. u8 cache_times;
  82. #endif
  83. u8 *tx_cache;
  84. u8 *rx_cache;
  85. u16 tx_cache_size;
  86. u16 rx_cache_size;
  87. u16 tx_length;
  88. u16 rx_index;
  89. byte_queue_t tx_queue;
  90. byte_queue_t rx_queue;
  91. void (*do_cache_data)(struct s600_uart_device *uart, u8 *buff, u16 size);
  92. void (*on_data_received)(u8 *buff, u16 size);
  93. void (*tx_poll)(struct s600_uart_device *uart);
  94. void (*rx_poll)(struct s600_uart_device *uart);
  95. } s600_uart_device_t;
  96. typedef struct {
  97. u8 *buff;
  98. u16 size;
  99. } s600_uart_message_t;
  100. extern s600_uart_device_t s600_uarts[];
  101. extern void s600_uart_received(s600_uart_device_t *uart, u8 *buff, u16 length);
  102. extern void s600_uart0_received(u8 *buff, u16 length);
  103. extern void s600_uart1_received(u8 *buff, u16 length);
  104. extern void s600_uart2_received(u8 *buff, u16 length);
  105. extern void s600_uart3_received(u8 *buff, u16 length);
  106. extern void s600_uart4_received(u8 *buff, u16 length);
  107. void s600_uart_init(s600_uart_device_t *uart);
  108. void s600_uart_dma_tx(s600_uart_device_t *uart);
  109. void s600_uart_dma_rx(s600_uart_device_t *uart);
  110. void s600_uart_write(s600_uart_device_t *uart, const u8 *buff, u16 size);
  111. u16 s600_uart_read(s600_uart_device_t *uart, u8 *buff, u16 size);
  112. void s600_uart_write_byte(s600_uart_device_t *uart, u8 value);
  113. u16 s600_uart_write_available(s600_uart_device_t *uart);
  114. bool s600_uart_write_full(s600_uart_device_t *uart, const u8 *buff, u16 size);
  115. void s600_uart_message_init(s600_uart_message_t *msg, const void *buff, u16 size);
  116. void s600_uart_write_messages(s600_uart_device_t *uart, const s600_uart_message_t *msgs, u8 count);
  117. void s600_uart_rcu_config(void);
  118. void s600_uart_poll(void);
  119. void s600_uart_send_command(u8 cmd, const void *buff, u16 size);
  120. void s600_uart_send_command_u8(u8 cmd, u8 value);
  121. void s600_uart_send_command_bool(u8 cmd, bool value);
  122. void s600_uart_send_can_frame(const s600_can_frame_t *frame);
  123. void s600_uart_send_can_pack(const s600_can_pack_t *pack);
  124. __STATIC_INLINE s600_uart_device_t *s600_uart_get_main(void)
  125. {
  126. return s600_uarts + CONFIG_UART_MAIN;
  127. }