#include "Usart.h" #include "Include.h" #include "string.h" #include "user_queue.h" #include "Timer.h" uint16_t Usart1_ucRx_length = 0;//串口1接收长度 uint8_t usart1_rx_done = 0;//串口1接收完成标志 uint8_t Usart1_Rx_Buf[USART1_RX_BUFFER_SIZE]={0};//串口1接收数据 device_state_t device[MAX_SLAVE_NUMBER];//8个设备的状态、控制结构体 extern uint16_t usRec_Length; //网口接收数据长度 extern uint8_t ucRec_Buffer[1024*10]; //网口接收数据缓存 uint8_t usart1_tx_done = 0;//串口1发送完成标志 //BLE_USART_TX_e A9UsartTxState_e = A9_USART_TX_IDLE; void ble_usart_init(uint32_t baudrate) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB1PeriphClockCmd(BLE_USART_GPIO_CLK, ENABLE); RCC_APB2PeriphClockCmd(BLE_USART_CLK, ENABLE); GPIO_PinAFConfig(BLE_USART_GPIO_PORT, BLE_USART_TX_PinSource, BLE_GPIO_AF_USART); GPIO_PinAFConfig(BLE_USART_GPIO_PORT, BLE_USART_RX_PinSource, BLE_GPIO_AF_USART); GPIO_InitStructure.GPIO_Pin = BLE_USART_TX_GPIO_PIN | BLE_USART_RX_GPIO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度 50MHz GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉 GPIO_Init(BLE_USART_GPIO_PORT, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = baudrate; USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为 8 位数据格式 USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 USART_Init(BLE_USART, &USART_InitStructure); //初始化串口 NVIC_InitStructure.NVIC_IRQChannel = BLE_NVIC_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 6;//抢占优先级 6 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级 0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ 通道使能 NVIC_Init(&NVIC_InitStructure); USART_ITConfig(BLE_USART, USART_IT_RXNE, ENABLE);//开启中断 USART_ITConfig(BLE_USART, USART_IT_IDLE, ENABLE);//开启空闲中断 USART_Cmd(BLE_USART, ENABLE); //使能串口 } void USART1_IRQHandler(void) { uint8_t temp; if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { if(Usart1_ucRx_length >= USART1_RX_BUFFER_SIZE) { Usart1_ucRx_length = 0; } Usart1_Rx_Buf[Usart1_ucRx_length++] = USART_ReceiveData(USART1); USART_ClearFlag(USART1, USART_IT_RXNE); } if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET) { temp = USART1 ->SR; temp = USART1 ->DR; // ble_usart_send(Usart1_Rx_Buf, Usart1_ucRx_length); // Usart1_ucRx_length = 0; usart1_rx_done = 1; } } //void ble_usart_send(uint8_t *ucpTx_Data,uint8_t ucTx_length) //{ // uint8_t i; // for(i=0;i= usRec_Length) { return 0; } } bcc = ucRec_Buffer[ucRec_Buffer[++i]+2];//校验位 此时队首到了帧长的位置 for(j=2;j