123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- /*************************************************
- Copyright (c) 2019
- All rights reserved.
- File name: dlt645_port.c
- Description: DLT645 移植&使用例程文件
- History:
- 1. Version:
- Date: 2019-09-19
- Author: wangjunjie
- Modify:
- *************************************************/
- #include "dlt645.h"
- #include "gd32f10x_gpio.h"
- #include "systick.h"
- #define DLT_RXSIZE 200
- // DLT645采集使用的串口名
- #define DLT645_USART USART1
- #define DLT645_CTRL_GPIO GPIOA
- #define DLT645_CTRL_PIN GPIO_PIN_8
- // DL/T 645硬件拓展结构体
- typedef struct
- {
- uint8_t dlt645_Tx; // 用于串口接收的状态
- uint32_t timeout; //
- uint8_t rxBuf[DLT_RXSIZE];
- uint8_t done;
- uint8_t index;
- } dlt645_port_t;
- static dlt645_port_t dlt645_port;
- // dlt645 环境结构体
- dlt645_t dlt645;
- #if 1
- void dlt_callback()
- {
- if (RESET != usart_interrupt_flag_get(DLT645_USART, USART_INT_FLAG_RBNE))
- {
- if (dlt645_port.index < DLT_RXSIZE - 1)
- {
- dlt645_port.rxBuf[dlt645_port.index] = usart_data_receive(DLT645_USART);
- dlt645_port.index++;
- }
- else
- {
- usart_data_receive(DLT645_USART);
- }
- usart_interrupt_flag_clear(DLT645_USART, USART_INT_FLAG_RBNE);
- }
- if ((dlt645_port.index > 0) && RESET != usart_interrupt_flag_get(DLT645_USART, USART_INT_FLAG_IDLE))
- {
- usart_interrupt_flag_clear(DLT645_USART, USART_INT_FLAG_IDLE);
- usart_data_receive(DLT645_USART);
- if(dlt645_port.rxBuf[dlt645_port.index-1]!=0x16)//增加一步结束符判断解决部分电表数据不连贯问题
- {
- dlt645_port.done=0;
- }
- else
- {
- dlt645_port.done = 1;
- }
- return;
- }
- else
- {
- usart_interrupt_flag_clear(DLT645_USART, USART_INT_FLAG_RBNE);
- }
- }
- #else
- //针对部分老电表发数据包中间出现卡断数据包做的特殊处理
- static uint32_t data_time;
- static uint32_t data_time_sum[10];
- void dlt_callback()
- {
- if (RESET != usart_interrupt_flag_get(DLT645_USART, USART_INT_FLAG_RBNE))
- {
- data_time=gettick();//获取最后一次得到数据的时间
- data_time_sum[dlt645_port.index]=data_time;
- if (dlt645_port.index < DLT_RXSIZE - 1)
- {
- dlt645_port.rxBuf[dlt645_port.index] = usart_data_receive(DLT645_USART);
- dlt645_port.index++;
- }
- usart_interrupt_flag_clear(DLT645_USART, USART_INT_FLAG_RBNE);
- }
-
- if(gettick()-data_time>2000 && data_time!=0)
- {
- uint32_t nowtime=gettick();
- data_time=0;
- usart_interrupt_flag_clear(DLT645_USART, USART_INT_FLAG_IDLE);
- usart_data_receive(DLT645_USART);
- dlt645_port.done = 1;
- return;
- }
- else
- {
- usart_interrupt_flag_clear(DLT645_USART, USART_INT_FLAG_RBNE);
- }
- }
- #endif
- /**
- * Name: dlt645_hw_read
- * Brief: dlt645 硬件层接收数据
- * Input:
- * @ctx: 645运行环境
- * @msg: 接收数据存放地址
- * @len: 数据最大接收长度
- * Output: 读取数据的长度
- */
- static int dlt645_hw_read(dlt645_t *ctx, uint8_t *msg, uint16_t len)
- {
- int dataLength = 0;
- int startTime = gettick();
- while (1)
- {
- if (gettick() - startTime > dlt645_port.timeout * 1000)
- return 0;
- if (dlt645_port.done == 1)
- {
- dataLength = dlt645_port.index;
- memcpy(msg, &(dlt645_port.rxBuf[4]), len-4);
- dataLength = dlt645_port.index-4;
- return dataLength;
- }
- }
- }
- /**
- * Name: dlt645_hw_write
- * Brief: dlt645 硬件层发送数据
- * Input:
- * @ctx: 645运行环境
- * @buf: 待发送数据
- * @len: 发送长度
- * Output: 实际发送的字节数,错误返回-1
- */
- static int dlt645_hw_write(dlt645_t *ctx, uint8_t *buf, uint16_t len)
- {
- delay_1ms(1000);
- memset(dlt645_port.rxBuf, 0, DLT_RXSIZE);
- gpio_bit_set(DLT645_CTRL_GPIO, DLT645_CTRL_PIN);
- for (uint16_t i = 0; i < len; i++)
- {
- usart_data_transmit(DLT645_USART, buf[i]);
- while (RESET == usart_flag_get(DLT645_USART, USART_FLAG_TC))
- ;
- }
- gpio_bit_reset(DLT645_CTRL_GPIO, DLT645_CTRL_PIN);
- dlt645_port.index = 0;
- dlt645_port.done = 0;
- return len;
- }
- void dlt645_init(uint32_t timeout)
- {
- gpio_bit_set(DLT645_USART, DLT645_CTRL_PIN);
- dlt645_port.timeout = timeout;
- dlt645_port.dlt645_Tx = 0;
- dlt645_port.index = 0;
- }
- // 645结构体注册
- static dlt645_t dlt645 = {
- {0},
- 0,
- dlt645_hw_write,
- dlt645_hw_read,
- (void *)&dlt645_port};
|