dlt645_port.c 2.5 KB

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