dlt645_port.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "gateway_message.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. GATEWAY_PARAMS *get;
  82. get= get_gateway_config_params();
  83. memset(dlt645_port.rxBuf, 0, DLT_RXSIZE);
  84. delay_ms(10);
  85. if(get->comProtocol){ // get->protocol 1:232 0:485
  86. USART_232_Send(buf,len);
  87. }else{
  88. USART_485_Send(buf,len);
  89. }
  90. dlt645_port.index = 0;
  91. dlt645_port.done = 0;
  92. return len;
  93. }
  94. void dlt645_init(uint32_t timeout)
  95. {
  96. HAL_GPIO_WritePin(DLT645_CTRL_GPIO,DLT645_CTRL_PIN,GPIO_PIN_SET);
  97. dlt645_port.timeout = timeout;
  98. dlt645_port.dlt645_Tx = 0;
  99. dlt645_port.index = 0;
  100. }
  101. // 645结构体注册
  102. static dlt645_t dlt645 = {
  103. {0},
  104. 0,
  105. dlt645_hw_write,
  106. dlt645_hw_read,
  107. (void *)&dlt645_port
  108. };