dlt645_port.c 3.0 KB

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