ZNKT-ST-M/BSP/Src/Usart.c

211 lines
5.2 KiB
C
Raw Normal View History

#include "Usart.h"
#include "Include.h"
#include "string.h"
#include "user_queue.h"
#include "Timer.h"
uint16_t Usart1_ucRx_length = 0;//<2F><><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD>ճ<EFBFBD><D5B3><EFBFBD>
uint8_t usart1_rx_done = 0;//<2F><><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɱ<EFBFBD>־
uint8_t Usart1_Rx_Buf[USART1_RX_BUFFER_SIZE]={0};//<2F><><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
device_state_t device[MAX_SLAVE_NUMBER];//8<><38><EFBFBD><EFBFBD><E8B1B8>״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD>ƽ<C6BD><E1B9B9>
extern uint16_t usRec_Length; //<2F><><EFBFBD>ڽ<EFBFBD><DABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
extern uint8_t ucRec_Buffer[1024*10]; //<2F><><EFBFBD>ڽ<EFBFBD><DABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD>
uint8_t usart1_tx_done = 0;//<2F><><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɱ<EFBFBD>־
//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;//<2F><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD>
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //<2F>ٶ<EFBFBD> 50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //<2F><><EFBFBD><EFBFBD><ECB8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //<2F><><EFBFBD><EFBFBD>
GPIO_Init(BLE_USART_GPIO_PORT, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = baudrate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//<2F>ֳ<EFBFBD>Ϊ 8 λ<><CEBB><EFBFBD>ݸ<EFBFBD>ʽ
USART_InitStructure.USART_StopBits = USART_StopBits_1;//һ<><D2BB>ֹͣλ
USART_InitStructure.USART_Parity = USART_Parity_No;//<2F><><EFBFBD><EFBFBD>żУ<C5BC><D0A3>λ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //<2F>շ<EFBFBD>ģʽ
USART_Init(BLE_USART, &USART_InitStructure); //<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
NVIC_InitStructure.NVIC_IRQChannel = BLE_NVIC_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 6;//<2F><>ռ<EFBFBD><D5BC><EFBFBD>ȼ<EFBFBD> 6
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //<2F><>Ӧ<EFBFBD><D3A6><EFBFBD>ȼ<EFBFBD> 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ ͨ<><CDA8>ʹ<EFBFBD><CAB9>
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(BLE_USART, USART_IT_RXNE, ENABLE);//<2F><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
USART_ITConfig(BLE_USART, USART_IT_IDLE, ENABLE);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
USART_Cmd(BLE_USART, ENABLE); //ʹ<>ܴ<EFBFBD><DCB4><EFBFBD>
}
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<ucTx_length;i++)
// {
// USART_SendData(USART1,*(ucpTx_Data+i));
// while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
// }
//
//}
uint8_t ble_usart_send(uint8_t ucTx_length)
{
uint8_t i,temp;
uint8_t ucpTx_Data[ucTx_length];
OutQueue(&queue,&ucpTx_Data[0],ucTx_length);//<2F><><EFBFBD><EFBFBD>
for(i=0;i<ucTx_length;i++)
{
USART_SendData(USART1,ucpTx_Data[i]);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
}
return 2;
}
void net_received_data_analysis(void)
{
//ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD><DAB7><EFBFBD><EFBFBD><EFBFBD>52832
// ble_usart_send(ucRec_Buffer, usRec_Length);
if(Analysis_data())//<2F><><EFBFBD><EFBFBD>
{
if(EnterQueue(&queue,ucRec_Buffer,usRec_Length))//<2F><><EFBFBD><EFBFBD>
{
}
}
if(tim_flag)//<2F><>ʱ50ms֮<73><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
tim_flag = 0;
if(!IsEmptyQueue(&queue))
{
ble_usart_send(usRec_Length); //ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD><DAB7><EFBFBD><EFBFBD><EFBFBD>52832
}
}
}
void parameters_init(void)
{
uint8_t i, j;
for(i = 0; i < MAX_SLAVE_NUMBER; i++)
{
device[i].channel1.EMG_data_mode = RMS_Data;
device[i].channel1.mode = ChannelModeAcquisition;
device[i].channel1.rate = AcquisitionRate4K;
device[i].channel1.state = TurnOff;
device[i].channel2.EMG_data_mode = RMS_Data;
device[i].channel2.mode = ChannelModeAcquisition;
device[i].channel2.rate = AcquisitionRate4K;
device[i].channel2.state = TurnOff;
device[i].output_current_mA = 0;
device[i].preinstall_state = TurnOff;
device[i].stimulate_state = TurnOff;
device[i].slice_state = SliceConnect;
device[i].slice_detect_state = TurnOff;
device[i].formwave_state = IdleState;
for(j = 0; j < MAC_ADDRESS_LENGTH; j++)
{
device[i].mac_address[j] = 0x00;
}
device[i].connection_state = BLE_Disconnect;
device[i].adapter_state = BatterySupply;
device[i].electric_quantity = 100;
device[i].reset_flag = Reset;
device[i].frequency_Hz = 0;
device[i].width_us = 0;
device[i].climb_time_ms = 0;
device[i].keep_time_ms = 0;
device[i].down_time_ms = 0;
device[i].rest_time_ms = 0;
}
}
uint8_t Analysis_data()
{
uint8_t bcc=0,i=0,bcc1=0,j=0;
while(ucRec_Buffer[i] != 0xAA)
{
i++;
if(i >= usRec_Length)
{
return 0;
}
}
bcc = ucRec_Buffer[ucRec_Buffer[++i]+2];//У<><D0A3>λ <20><>ʱ<EFBFBD><CAB1><EFBFBD>׵<EFBFBD><D7B5><EFBFBD>֡<EFBFBD><D6A1><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
for(j=2;j<ucRec_Buffer[i]+2;j++)
{
bcc1+=ucRec_Buffer[j] ;
}
if(bcc != bcc1)//<2F>ж<EFBFBD>У<EFBFBD><D0A3>
{
return 0;
}
if(ucRec_Buffer[ucRec_Buffer[i]+3]!=0x55)//<2F>ж<EFBFBD>֡β
{
return 0;
}
return 1;
}