dlt645_port.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*************************************************
  2. Copyright (c) 2019
  3. All rights reserved.
  4. File name: dlt645_port.c
  5. Description: DLT645 移植&使用例程文件
  6. History:
  7. 1. Version:
  8. Date: 2019-09-19
  9. Author: wangjunjie
  10. Modify:
  11. *************************************************/
  12. #include "dlt645.h"
  13. #include "gd32f10x_gpio.h"
  14. #include "systick.h"
  15. #define DLT_RXSIZE 200
  16. // DLT645采集使用的串口名
  17. #define DLT645_USART USART1
  18. #define DLT645_CTRL_GPIO GPIOA
  19. #define DLT645_CTRL_PIN GPIO_PIN_8
  20. // DL/T 645硬件拓展结构体
  21. typedef struct
  22. {
  23. uint8_t dlt645_Tx; // 用于串口接收的状态
  24. uint32_t timeout; //
  25. uint8_t rxBuf[DLT_RXSIZE];
  26. uint8_t done;
  27. uint8_t index;
  28. } dlt645_port_t;
  29. static dlt645_port_t dlt645_port;
  30. // dlt645 环境结构体
  31. dlt645_t dlt645;
  32. void dlt_callback()
  33. {
  34. if (RESET != usart_interrupt_flag_get(DLT645_USART, USART_INT_FLAG_RBNE))
  35. {
  36. if (dlt645_port.index < DLT_RXSIZE - 1)
  37. {
  38. dlt645_port.rxBuf[dlt645_port.index] = usart_data_receive(DLT645_USART);
  39. dlt645_port.index++;
  40. }
  41. else
  42. {
  43. usart_data_receive(DLT645_USART);
  44. }
  45. }
  46. if ((dlt645_port.index > 0) && RESET != usart_interrupt_flag_get(DLT645_USART, USART_INT_FLAG_IDLE))
  47. {
  48. usart_interrupt_flag_clear(DLT645_USART, USART_INT_FLAG_IDLE);
  49. usart_data_receive(DLT645_USART);
  50. dlt645_port.done = 1;
  51. return;
  52. }
  53. else
  54. {
  55. usart_interrupt_flag_clear(DLT645_USART, USART_INT_FLAG_RBNE);
  56. }
  57. }
  58. /**
  59. * Name: dlt645_hw_read
  60. * Brief: dlt645 硬件层接收数据
  61. * Input:
  62. * @ctx: 645运行环境
  63. * @msg: 接收数据存放地址
  64. * @len: 数据最大接收长度
  65. * Output: 读取数据的长度
  66. */
  67. static int dlt645_hw_read(dlt645_t *ctx, uint8_t *msg, uint16_t len)
  68. {
  69. int dataLength = 0;
  70. int startTime = gettick();
  71. while (1)
  72. {
  73. if (gettick() - startTime > dlt645_port.timeout * 1000)
  74. return 0;
  75. if (dlt645_port.done == 1)
  76. {
  77. dataLength = dlt645_port.index;
  78. memcpy(msg, &(dlt645_port.rxBuf[4]), len-4);
  79. dataLength = dlt645_port.index-4;
  80. return dataLength;
  81. }
  82. }
  83. }
  84. /**
  85. * Name: dlt645_hw_write
  86. * Brief: dlt645 硬件层发送数据
  87. * Input:
  88. * @ctx: 645运行环境
  89. * @buf: 待发送数据
  90. * @len: 发送长度
  91. * Output: 实际发送的字节数,错误返回-1
  92. */
  93. static int dlt645_hw_write(dlt645_t *ctx, uint8_t *buf, uint16_t len)
  94. {
  95. memset(dlt645_port.rxBuf, 0, DLT_RXSIZE);
  96. gpio_bit_set(DLT645_CTRL_GPIO, DLT645_CTRL_PIN);
  97. for (uint16_t i = 0; i < len; i++)
  98. {
  99. usart_data_transmit(DLT645_USART, buf[i]);
  100. while (RESET == usart_flag_get(DLT645_USART, USART_FLAG_TC))
  101. ;
  102. }
  103. gpio_bit_reset(DLT645_CTRL_GPIO, DLT645_CTRL_PIN);
  104. dlt645_port.index = 0;
  105. dlt645_port.done = 0;
  106. return len;
  107. }
  108. void dlt645_init(uint32_t timeout)
  109. {
  110. gpio_bit_set(DLT645_USART, DLT645_CTRL_PIN);
  111. dlt645_port.timeout = timeout;
  112. dlt645_port.dlt645_Tx = 0;
  113. dlt645_port.index = 0;
  114. }
  115. // 645结构体注册
  116. static dlt645_t dlt645 = {
  117. {0},
  118. 0,
  119. dlt645_hw_write,
  120. dlt645_hw_read,
  121. (void *)&dlt645_port};