123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- #include "dlt645.h"
- #include "gd32f10x_gpio.h"
- #include "systick.h"
- #define DLT_RXSIZE 200
- #define DLT645_USART USART1
- #define DLT645_CTRL_GPIO GPIOA
- #define DLT645_CTRL_PIN GPIO_PIN_8
- 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_t dlt645;
- 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);
- }
- }
- 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);
- dlt645_port.done = 1;
- return;
- }
- else
- {
- usart_interrupt_flag_clear(DLT645_USART, USART_INT_FLAG_RBNE);
- }
- }
- 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;
- }
- }
- }
- static int dlt645_hw_write(dlt645_t *ctx, uint8_t *buf, uint16_t len)
- {
- 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;
- }
- static dlt645_t dlt645 = {
- {0},
- 0,
- dlt645_hw_write,
- dlt645_hw_read,
- (void *)&dlt645_port};
|