dlt645_port.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "timer.h"
  15. #include "usart.h"
  16. #include "string.h"
  17. #include "led.h"
  18. dlt645_port_t dlt645_port;
  19. //static dlt645_port_t dlt645_port;
  20. // dlt645 环境结构体
  21. dlt645_t dlt645;
  22. void dlt_callback(UART_HandleTypeDef *husart)
  23. {
  24. if(__HAL_UART_GET_FLAG(husart, UART_FLAG_RXNE) && __HAL_UART_GET_IT_SOURCE(husart, UART_IT_RXNE))
  25. {
  26. if (dlt645_port.index < DLT_RXSIZE - 1)
  27. {
  28. dlt645_port.rxBuf[dlt645_port.index] = husart->Instance->DR;
  29. dlt645_port.index++;
  30. }
  31. else
  32. {
  33. uint8_t data = husart->Instance->DR;
  34. }
  35. }
  36. if((dlt645_port.index > 0) && RESET != __HAL_USART_GET_FLAG(&USART_InitStruct_485, USART_FLAG_IDLE))
  37. {
  38. uint8_t temp;
  39. temp=husart->Instance->SR; //先读sr再读DR才能清除idle中断
  40. temp=husart->Instance->DR;
  41. dlt645_port.done = 1;
  42. }
  43. }
  44. /**
  45. * Name: dlt645_hw_read
  46. * Brief: dlt645 硬件层接收数据
  47. * Input:
  48. * @ctx: 645运行环境
  49. * @msg: 接收数据存放地址
  50. * @len: 数据最大接收长度
  51. * Output: 读取数据的长度
  52. */
  53. static int dlt645_hw_read(dlt645_t *ctx, uint8_t *msg, uint16_t len)
  54. {
  55. int dataLength = 0;
  56. int startTime = gettick();
  57. while (1)
  58. {
  59. if (gettick() - startTime > dlt645_port.timeout * 1000 )
  60. return 0;
  61. if (dlt645_port.done == 1)
  62. {
  63. dataLength = dlt645_port.index;
  64. memcpy(msg, &(dlt645_port.rxBuf[4]), len-4);
  65. dataLength = dlt645_port.index-4;
  66. return dataLength;
  67. }
  68. }
  69. }
  70. /**
  71. * Name: dlt645_hw_write
  72. * Brief: dlt645 硬件层发送数据
  73. * Input:
  74. * @ctx: 645运行环境
  75. * @buf: 待发送数据
  76. * @len: 发送长度
  77. * Output: 实际发送的字节数,错误返回-1
  78. */
  79. static int dlt645_hw_write(dlt645_t *ctx, uint8_t *buf, uint16_t len)
  80. {
  81. memset(dlt645_port.rxBuf, 0, DLT_RXSIZE);
  82. delay_ms(10);
  83. USART_485_Send(buf,len);
  84. dlt645_port.index = 0;
  85. dlt645_port.done = 0;
  86. return len;
  87. }
  88. void dlt645_init(uint32_t timeout)
  89. {
  90. HAL_GPIO_WritePin(DLT645_CTRL_GPIO,DLT645_CTRL_PIN,1);
  91. dlt645_port.timeout = timeout;
  92. dlt645_port.dlt645_Tx = 0;
  93. dlt645_port.index = 0;
  94. }
  95. // 645结构体注册
  96. static dlt645_t dlt645 = {
  97. {0},
  98. 0,
  99. dlt645_hw_write,
  100. dlt645_hw_read,
  101. (void *)&dlt645_port
  102. };