dlt645_port.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #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()
  23. {
  24. if(RESET!=usart_flag_get(DLT645_USART,USART_FLAG_RBNE))
  25. {
  26. if (dlt645_port.index < DLT_RXSIZE - 1)
  27. {
  28. dlt645_port.rxBuf[dlt645_port.index] = usart_data_receive(DLT645_USART);
  29. dlt645_port.index++;
  30. }
  31. else
  32. {
  33. usart_data_receive(DLT645_USART);
  34. }
  35. }
  36. if((dlt645_port.index > 0) && RESET != usart_flag_get(DLT645_USART, USART_FLAG_IDLE))
  37. {
  38. usart_interrupt_disable(DLT645_USART, USART_INT_IDLE);
  39. dlt645_port.done = 1;
  40. gd_eval_led_toggle(LED_485RX);
  41. }
  42. }
  43. /**
  44. * Name: dlt645_hw_read
  45. * Brief: dlt645 硬件层接收数据
  46. * Input:
  47. * @ctx: 645运行环境
  48. * @msg: 接收数据存放地址
  49. * @len: 数据最大接收长度
  50. * Output: 读取数据的长度
  51. */
  52. static int dlt645_hw_read(dlt645_t *ctx, uint8_t *msg, uint16_t len)
  53. {
  54. int dataLength = 0;
  55. int startTime = gettick();
  56. while (1)
  57. {
  58. if (gettick() - startTime > dlt645_port.timeout * 1000 )
  59. return 0;
  60. if (dlt645_port.done == 1)
  61. {
  62. dataLength = dlt645_port.index;
  63. memcpy(msg, &(dlt645_port.rxBuf[4]), len-4);
  64. dataLength = dlt645_port.index-4;
  65. return dataLength;
  66. }
  67. }
  68. }
  69. /**
  70. * Name: dlt645_hw_write
  71. * Brief: dlt645 硬件层发送数据
  72. * Input:
  73. * @ctx: 645运行环境
  74. * @buf: 待发送数据
  75. * @len: 发送长度
  76. * Output: 实际发送的字节数,错误返回-1
  77. */
  78. static int dlt645_hw_write(dlt645_t *ctx, uint8_t *buf, uint16_t len)
  79. {
  80. memset(dlt645_port.rxBuf, 0, DLT_RXSIZE);
  81. delay_ms(10);
  82. gpio_bit_write(DLT645_CTRL_GPIO,DLT645_CTRL_PIN,1);
  83. for (uint16_t i = 0; i <=len; i++)
  84. {
  85. usart_data_transmit(USART0,buf[i]);
  86. while (usart_flag_get(DLT645_USART, USART_FLAG_TBE) == RESET);
  87. }
  88. gpio_bit_write(DLT645_CTRL_GPIO,DLT645_CTRL_PIN,0);
  89. dlt645_port.index = 0;
  90. dlt645_port.done = 0;
  91. return len;
  92. }
  93. void dlt645_init(uint32_t timeout)
  94. {
  95. gpio_bit_write(DLT645_CTRL_GPIO,DLT645_CTRL_PIN,1);
  96. dlt645_port.timeout = timeout;
  97. dlt645_port.dlt645_Tx = 0;
  98. dlt645_port.index = 0;
  99. }
  100. // 645结构体注册
  101. static dlt645_t dlt645 = {
  102. {0},
  103. 0,
  104. dlt645_hw_write,
  105. dlt645_hw_read,
  106. (void *)&dlt645_port
  107. };