dlt645_port.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "gd32f30x.h"
  14. #include "delay.h"
  15. #include "usart.h"
  16. #include "string.h"
  17. #define DLT_RXSIZE 200
  18. // DLT645采集使用的串口名
  19. #define DLT645_USART COM_485
  20. #define DLT645_CTRL_GPIO DE485_GPIO_PORT
  21. #define DLT645_CTRL_PIN DE485_PIN
  22. // DL/T 645硬件拓展结构体
  23. typedef struct
  24. {
  25. uint8_t dlt645_Tx; // 用于串口接收的状态
  26. uint32_t timeout; //
  27. uint8_t rxBuf[DLT_RXSIZE];
  28. uint8_t done;
  29. uint8_t index;
  30. } dlt645_port_t;
  31. static dlt645_port_t dlt645_port;
  32. // dlt645 环境结构体
  33. dlt645_t dlt645;
  34. void dlt_callback()
  35. {
  36. if(RESET!=usart_flag_get(DLT645_USART,USART_FLAG_RBNE))
  37. {
  38. if (dlt645_port.index < DLT_RXSIZE - 1)
  39. {
  40. dlt645_port.rxBuf[dlt645_port.index] = usart_data_receive(DLT645_USART);
  41. dlt645_port.index++;
  42. }
  43. else
  44. {
  45. usart_data_receive(DLT645_USART);
  46. }
  47. }
  48. if((dlt645_port.index > 0) && RESET != usart_flag_get(DLT645_USART, USART_FLAG_IDLE))
  49. {
  50. usart_interrupt_disable(DLT645_USART, USART_INT_IDLE);
  51. dlt645_port.done = 1;
  52. }
  53. }
  54. /**
  55. * Name: dlt645_hw_read
  56. * Brief: dlt645 硬件层接收数据
  57. * Input:
  58. * @ctx: 645运行环境
  59. * @msg: 接收数据存放地址
  60. * @len: 数据最大接收长度
  61. * Output: 读取数据的长度
  62. */
  63. static int dlt645_hw_read(dlt645_t *ctx, uint8_t *msg, uint16_t len)
  64. {
  65. int dataLength = 0;
  66. int startTime = gettick();
  67. while (1)
  68. {
  69. if (gettick() - startTime > dlt645_port.timeout )
  70. return 0;
  71. if (dlt645_port.done == 1)
  72. {
  73. dataLength = dlt645_port.index;
  74. memcpy(msg, &(dlt645_port.rxBuf[4]), len-4);
  75. dataLength = dlt645_port.index-4;
  76. return dataLength;
  77. }
  78. }
  79. }
  80. /**
  81. * Name: dlt645_hw_write
  82. * Brief: dlt645 硬件层发送数据
  83. * Input:
  84. * @ctx: 645运行环境
  85. * @buf: 待发送数据
  86. * @len: 发送长度
  87. * Output: 实际发送的字节数,错误返回-1
  88. */
  89. static int dlt645_hw_write(dlt645_t *ctx, uint8_t *buf, uint16_t len)
  90. {
  91. memset(dlt645_port.rxBuf, 0, DLT_RXSIZE);
  92. delay_ms(10);
  93. gpio_bit_write(DLT645_CTRL_GPIO,DLT645_CTRL_PIN,1);
  94. for (uint16_t i = 0; i <=len; i++)
  95. {
  96. usart_data_transmit(USART0,buf[i]);
  97. while (usart_flag_get(DLT645_USART, USART_FLAG_TBE) == RESET);
  98. }
  99. gpio_bit_write(DLT645_CTRL_GPIO,DLT645_CTRL_PIN,0);
  100. dlt645_port.index = 0;
  101. dlt645_port.done = 0;
  102. return len;
  103. }
  104. void dlt645_init(uint32_t timeout)
  105. {
  106. gpio_bit_write(DLT645_CTRL_GPIO,DLT645_CTRL_PIN,1);
  107. dlt645_port.timeout = timeout;
  108. dlt645_port.dlt645_Tx = 0;
  109. dlt645_port.index = 0;
  110. }
  111. // 645结构体注册
  112. static dlt645_t dlt645 = {
  113. {0},
  114. 0,
  115. dlt645_hw_write,
  116. dlt645_hw_read,
  117. (void *)&dlt645_port
  118. };