val-off-little/hart/hart.c

76 lines
1.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "hart.h"
uint8_t hart_rxbit = 0;
uint8_t usart4_rxbit = 0;
uint8_t hart_rxbuffer[MAX_BUF_SIZE];
uint8_t hart_txbuffer[MAX_BUF_SIZE];
uint8_t array[3] = {11,22,33};
int hart_rxcnt = 0;
int usart4_rxcnt = 0;
int hart_timcnt = 0;
int hart_timerflag = 0;
int hart_rxsize = 0;
int usart4_rxsize = 0;
int rx_flag = 0;
int tx_flag = 0;
void hart_init(void)
{
HAL_GPIO_WritePin(HART_RST_GPIO_Port,HART_RST_Pin,GPIO_PIN_RESET);
HAL_Delay(100);
HAL_GPIO_WritePin(HART_RST_GPIO_Port,HART_RST_Pin,GPIO_PIN_SET);
HAL_UART_Receive_IT(&huart4, (uint8_t *)&usart4_rxbit, 1);
HAL_UART_Receive_IT(&huart2, (uint8_t *)&hart_rxbit, 1);
__HAL_TIM_CLEAR_FLAG(&htim10, TIM_FLAG_UPDATE); //手动添加
//HAL_TIM_Base_Start_IT(&htim10);
}
void usart2_rx_cb(void)
{
hart_rxbuffer[0] = hart_rxbit;
HAL_UART_Transmit_DMA(&huart4,hart_rxbuffer,1);
ch395_send_data(CH395Q_SOCKET_5, hart_rxbuffer, 1);
HAL_UART_Receive_IT(&huart2, (uint8_t *)&hart_rxbit, 1);
}
void usart4_rx_cb(void)
{
if(usart4_rxcnt >= MAX_BUF_SIZE-1) //接收数据量超限,错误
{
usart4_rxcnt = 0;
memset(hart_txbuffer, 0x00, sizeof(&hart_txbuffer));
HAL_UART_Transmit(&huart4, (uint8_t *)"数据溢出", 10, 0xFFFF);
}
else //接收正常
{
hart_txbuffer[usart4_rxcnt++] = usart4_rxbit; //接收数据存储到hart_txbuffer
HAL_TIM_Base_Stop_IT(&htim10);
__HAL_TIM_SET_COUNTER(&htim10, 0);
HAL_TIM_Base_Start_IT(&htim10); //将定时器10的计数值清零后重新计数
rx_flag = 0;
}
HAL_UART_Receive_IT(&huart4, &usart4_rxbit, 1);
}
void hart_tim_cb(void)
{
__HAL_TIM_CLEAR_FLAG(&htim10, TIM_FLAG_UPDATE); //产生中断证明超过2ms没有接收到数据了一帧接收完成
HAL_TIM_Base_Stop_IT(&htim10); //中断之后停止定时器,开启在下一次接收到数据开始
usart4_rxsize = usart4_rxcnt;
usart4_rxcnt = 0;
usart2_send(hart_txbuffer,usart4_rxsize);
if(usart4_rxsize > 5)
{
rx_flag = 1;
}
}