serial.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef __RT_HW_SERIAL_H__
  2. #define __RT_HW_SERIAL_H__
  3. #include <rthw.h>
  4. #include <rtthread.h>
  5. /* STM32F10x library definitions */
  6. #include <stm32f10x_lib.h>
  7. #define UART_DMA_RX_DESCRIPTOR 2
  8. #define UART_DMA_RX_BUFFER_SIZE 16
  9. #define UART_RX_BUFFER_SIZE 64
  10. #define UART_TX_BUFFER_SIZE 64
  11. struct stm32_serial_dma_rx
  12. {
  13. DMA_Channel_TypeDef* dma_channel;
  14. rt_uint8_t rx_buffer[UART_DMA_RX_DESCRIPTOR][UART_RX_BUFFER_SIZE];
  15. rt_uint32_t save_descriptor;
  16. rt_uint32_t read_index, read_descriptor;
  17. rt_bool_t is_full;
  18. };
  19. /* data node for Tx Mode */
  20. struct stm32_serial_data_node
  21. {
  22. rt_uint8_t *data_ptr;
  23. rt_size_t data_size;
  24. struct stm32_serial_data_node *next, *prev;
  25. };
  26. struct stm32_serial_dma_tx
  27. {
  28. DMA_Channel_TypeDef* dma_channel;
  29. struct stm32_serial_data_node *list_head, *list_tail;
  30. };
  31. struct stm32_serial_int_rx
  32. {
  33. rt_uint8_t rx_buffer[UART_RX_BUFFER_SIZE];
  34. rt_uint32_t read_index, save_index;
  35. };
  36. struct stm32_serial_int_tx
  37. {
  38. rt_uint8_t tx_buffer[UART_TX_BUFFER_SIZE];
  39. rt_uint32_t write_index, save_index;
  40. };
  41. struct stm32_serial_device
  42. {
  43. USART_TypeDef* uart_device;
  44. /* rx structure */
  45. struct stm32_serial_int_rx* int_rx;
  46. struct stm32_serial_dma_rx* dma_rx;
  47. /* tx structure */
  48. struct stm32_serial_int_tx* int_tx;
  49. struct stm32_serial_dma_tx* dma_tx;
  50. };
  51. rt_err_t rt_hw_serial_register(rt_device_t device, const char* name, rt_uint32_t flag, struct stm32_serial_device *serial);
  52. void rt_hw_serial_isr(rt_device_t device);
  53. void rt_hw_serial_dma_rx_isr(rt_device_t device);
  54. void rt_hw_serial_dma_tx_isr(rt_device_t device);
  55. #endif