/************************************************* 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 "gd32f30x.h" #include "delay.h" #include "usart.h" #include "string.h" #include "led.h" dlt645_port_t dlt645_port; //static dlt645_port_t dlt645_port; // dlt645 环境结构体 dlt645_t dlt645; void dlt_callback() { if(RESET!=usart_flag_get(DLT645_USART,USART_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); } } if((dlt645_port.index > 0) && RESET != usart_flag_get(DLT645_USART, USART_FLAG_IDLE)) { usart_interrupt_disable(DLT645_USART, USART_INT_IDLE); dlt645_port.done = 1; gd_eval_led_toggle(LED_485RX); } } /** * 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) { memset(dlt645_port.rxBuf, 0, DLT_RXSIZE); delay_ms(10); gpio_bit_write(DLT645_CTRL_GPIO,DLT645_CTRL_PIN,1); for (uint16_t i = 0; i <=len; i++) { usart_data_transmit(USART0,buf[i]); while (usart_flag_get(DLT645_USART, USART_FLAG_TBE) == RESET); } gpio_bit_write(DLT645_CTRL_GPIO,DLT645_CTRL_PIN,0); dlt645_port.index = 0; dlt645_port.done = 0; return len; } void dlt645_init(uint32_t timeout) { gpio_bit_write(DLT645_CTRL_GPIO,DLT645_CTRL_PIN,1); 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 };