dlt645_port.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. if( RESET != USART_GetFlagStatus(DLT645_USART, USART_FLAG_IDLE))
  58. {
  59. uint8_t temp;
  60. temp=DLT645_USART->SR; //先读sr再读DR才能清除idle中断
  61. temp=DLT645_USART->DR;
  62. }
  63. OSIntExit();
  64. }
  65. /**
  66. * Name: dlt645_hw_read
  67. * Brief: dlt645 硬件层接收数据
  68. * Input:
  69. * @ctx: 645运行环境
  70. * @msg: 接收数据存放地址
  71. * @len: 数据最大接收长度
  72. * Output: 读取数据的长度
  73. */
  74. static int dlt645_hw_read(dlt645_t *ctx, uint8_t *msg, uint16_t len)
  75. {
  76. int dataLength = 0;
  77. int startTime = gettick();
  78. while (1)
  79. {
  80. if (gettick() - startTime > dlt645_port.timeout )
  81. return 0;
  82. if (dlt645_port.done == 1)
  83. {
  84. dataLength = dlt645_port.index;
  85. memcpy(msg, &(dlt645_port.rxBuf[4]), len-4);
  86. dataLength = dlt645_port.index-4;
  87. return dataLength;
  88. }
  89. }
  90. }
  91. /**
  92. * Name: dlt645_hw_write
  93. * Brief: dlt645 硬件层发送数据
  94. * Input:
  95. * @ctx: 645运行环境
  96. * @buf: 待发送数据
  97. * @len: 发送长度
  98. * Output: 实际发送的字节数,错误返回-1
  99. */
  100. static int dlt645_hw_write(dlt645_t *ctx, uint8_t *buf, uint16_t len)
  101. {
  102. memset(dlt645_port.rxBuf, 0, DLT_RXSIZE);
  103. delay_ms(10);
  104. OSIntEnter();
  105. GPIO_WriteBit(DLT645_CTRL_GPIO,DLT645_CTRL_PIN,1);
  106. for (uint16_t i = 0; i <=len; i++)
  107. {
  108. USART_SendData(USART3,buf[i]);
  109. while (USART_GetFlagStatus(DLT645_USART, USART_FLAG_TXE) == RESET);
  110. }
  111. OSIntExit();
  112. GPIO_WriteBit(DLT645_CTRL_GPIO,DLT645_CTRL_PIN,0);
  113. dlt645_port.index = 0;
  114. dlt645_port.done = 0;
  115. return len;
  116. }
  117. void dlt645_init(uint32_t timeout)
  118. {
  119. GPIO_WriteBit(DLT645_CTRL_GPIO,DLT645_CTRL_PIN,1);
  120. dlt645_port.timeout = timeout;
  121. dlt645_port.dlt645_Tx = 0;
  122. dlt645_port.index = 0;
  123. }
  124. // 645结构体注册
  125. static dlt645_t dlt645 = {
  126. {0},
  127. 0,
  128. dlt645_hw_write,
  129. dlt645_hw_read,
  130. (void *)&dlt645_port};