serial.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file serial.h
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #ifndef __SERIAL_H__
  35. #define __SERIAL_H__
  36. #include <rtthread.h>
  37. #include "completion.h"
  38. #include "dataqueue.h"
  39. #define BAUD_RATE_2400 2400
  40. #define BAUD_RATE_4800 4800
  41. #define BAUD_RATE_9600 9600
  42. #define BAUD_RATE_19200 19200
  43. #define BAUD_RATE_38400 38400
  44. #define BAUD_RATE_57600 57600
  45. #define BAUD_RATE_115200 115200
  46. #define BAUD_RATE_230400 230400
  47. #define BAUD_RATE_460800 460800
  48. #define BAUD_RATE_921600 921600
  49. #define BAUD_RATE_2000000 2000000
  50. #define BAUD_RATE_3000000 3000000
  51. #define DATA_BITS_5 5
  52. #define DATA_BITS_6 6
  53. #define DATA_BITS_7 7
  54. #define DATA_BITS_8 8
  55. #define DATA_BITS_9 9
  56. #define STOP_BITS_1 0
  57. #define STOP_BITS_0_5 1
  58. #define STOP_BITS_2 2
  59. #define STOP_BITS_1_5 3
  60. #ifdef _WIN32
  61. #include <windows.h>
  62. #else
  63. #define PARITY_NONE 0
  64. #define PARITY_ODD 1
  65. #define PARITY_EVEN 2
  66. #endif
  67. #define HFC_CONTROL_NONE 0
  68. #define HFC_CONTROL_RTS 1
  69. #define HFC_CONTROL_CTS 2
  70. #define HFC_CONTROL_RTS_CTS 3
  71. #define TX_MODE 0
  72. #define RX_MODE 1
  73. #define TX_RX_MODE 2
  74. #ifndef RT_SERIAL_RB_BUFSZ
  75. #define RT_SERIAL_RB_BUFSZ 64
  76. #endif
  77. #define RT_SERIAL_EVENT_RX_IND 0x01 /* Rx indication */
  78. #define RT_SERIAL_EVENT_TX_DONE 0x02 /* Tx complete */
  79. #define RT_SERIAL_EVENT_RX_DMADONE 0x03 /* Rx DMA transfer done */
  80. #define RT_SERIAL_EVENT_TX_DMADONE 0x04 /* Tx DMA transfer done */
  81. #define RT_SERIAL_EVENT_RX_TIMEOUT 0x05 /* Rx timeout */
  82. #define RT_SERIAL_DMA_RX 0x01
  83. #define RT_SERIAL_DMA_TX 0x02
  84. #define RT_SERIAL_RX_INT 0x01
  85. #define RT_SERIAL_TX_INT 0x02
  86. #define RT_SERIAL_ERR_OVERRUN 0x01
  87. #define RT_SERIAL_ERR_FRAMING 0x02
  88. #define RT_SERIAL_ERR_PARITY 0x03
  89. #define RT_SERIAL_TX_DATAQUEUE_SIZE 2048
  90. #define RT_SERIAL_TX_DATAQUEUE_LWM 30
  91. /* Default config for serial_configure structure */
  92. #define RT_SERIAL_CONFIG_DEFAULT \
  93. { \
  94. BAUD_RATE_115200, /* 115200 bits/s */ \
  95. DATA_BITS_8, /* 8 databits */ \
  96. STOP_BITS_1, /* 1 stopbit */ \
  97. PARITY_NONE, /* No parity */ \
  98. HFC_CONTROL_NONE, /* No Hardwareflow control */ \
  99. TX_RX_MODE, /* Tx_Rx mode */ \
  100. RT_SERIAL_RB_BUFSZ, /* Buffer size */ \
  101. 0 \
  102. }
  103. struct serial_configure
  104. {
  105. rt_uint32_t baud_rate;
  106. rt_uint32_t data_bits :4;
  107. rt_uint32_t stop_bits :2;
  108. rt_uint32_t parity :2;
  109. rt_uint32_t hardwareflow_control :2;
  110. rt_uint32_t mode :2;
  111. rt_uint32_t bufsz :16;
  112. rt_uint32_t reserved :6;
  113. };
  114. /*
  115. * Serial FIFO mode
  116. */
  117. struct rt_serial_rx_fifo
  118. {
  119. /* software fifo */
  120. rt_uint8_t *buffer;
  121. rt_uint16_t put_index, get_index;
  122. rt_bool_t is_full;
  123. };
  124. struct rt_serial_tx_fifo
  125. {
  126. struct rt_completion completion;
  127. };
  128. /*
  129. * Serial DMA mode
  130. */
  131. struct rt_serial_rx_dma
  132. {
  133. rt_bool_t activated;
  134. };
  135. struct rt_serial_tx_dma
  136. {
  137. rt_bool_t activated;
  138. struct rt_data_queue data_queue;
  139. };
  140. struct rt_serial_device
  141. {
  142. struct rt_device parent;
  143. const struct rt_uart_ops *ops;
  144. struct serial_configure config;
  145. void *serial_rx;
  146. void *serial_tx;
  147. };
  148. typedef struct rt_serial_device rt_serial_t;
  149. /**
  150. * uart operators
  151. */
  152. struct rt_uart_ops
  153. {
  154. rt_err_t (*configure)(struct rt_serial_device *serial, struct serial_configure *cfg);
  155. rt_err_t (*control)(struct rt_serial_device *serial, int cmd, void *arg);
  156. int (*putc)(struct rt_serial_device *serial, char c);
  157. int (*getc)(struct rt_serial_device *serial);
  158. rt_size_t (*dma_transmit)(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction);
  159. };
  160. void rt_hw_serial_isr(struct rt_serial_device *serial, int event);
  161. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  162. const char *name,
  163. rt_uint32_t flag,
  164. void *data);
  165. #endif