|
|
#include "usart1.h" |
|
|
#include <stdio.h> |
|
|
#include "iap.h" |
|
|
#include "cmsis_armcc.h" |
|
|
|
|
|
Cmd_States Uart1_Cmd_State = kCmd_Head1; |
|
|
|
|
|
extern uint32_t g_tim3_count; // |
|
|
uint32_t uart1_recv_time = 0; |
|
|
|
|
|
/** |
|
|
* @brief Configure nested vector interrupt controller NVIC. |
|
|
*/ |
|
|
void USART1_NVIC_Configuration(void) |
|
|
{ |
|
|
NVIC_InitType NVIC_InitStructure; |
|
|
|
|
|
/* Configure USART as interrupt source */ |
|
|
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; |
|
|
/*Set the priority*/ |
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; |
|
|
/*Set the sub priority */ |
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; |
|
|
/*Enable interrupt */ |
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; |
|
|
/* Initialize configuration NVIC */ |
|
|
NVIC_Init(&NVIC_InitStructure); |
|
|
|
|
|
USART_ClrIntPendingBit(USARTx, USART_INT_RXDNE); |
|
|
} |
|
|
|
|
|
void Usart1_Init(void) |
|
|
{ |
|
|
GPIO_InitType GPIO_InitStructure; |
|
|
USART_InitType USART_InitStructure; |
|
|
|
|
|
/* Enable GPIO clock */ |
|
|
GPIO_APBxClkCmd(USARTx_GPIO_CLK | RCC_APB2_PERIPH_AFIO, ENABLE); |
|
|
/* Enable USARTy and USARTz Clock */ |
|
|
USART_APBxClkCmd(USARTx_CLK, ENABLE); |
|
|
|
|
|
/* Configure USARTx Tx as alternate function push-pull */ |
|
|
GPIO_InitStructure.Pin = USARTx_TxPin; |
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; |
|
|
GPIO_InitPeripheral(USARTx_GPIO, &GPIO_InitStructure); |
|
|
|
|
|
/* Configure USARTx Rx as input floating */ |
|
|
GPIO_InitStructure.Pin = USARTx_RxPin; |
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; |
|
|
GPIO_InitPeripheral(USARTx_GPIO, &GPIO_InitStructure); |
|
|
|
|
|
/* USARTy and USARTz configuration ------------------------------------------------------*/ |
|
|
USART_InitStructure.BaudRate = 115200; |
|
|
USART_InitStructure.WordLength = USART_WL_8B; |
|
|
USART_InitStructure.StopBits = USART_STPB_1; |
|
|
USART_InitStructure.Parity = USART_PE_NO; |
|
|
USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE; |
|
|
USART_InitStructure.Mode = USART_MODE_RX | USART_MODE_TX; |
|
|
/* Configure USARTx */ |
|
|
USART_Init(USARTx, &USART_InitStructure); |
|
|
|
|
|
// Configuration interrupt priority of the usart port |
|
|
USART1_NVIC_Configuration(); |
|
|
|
|
|
// Enable usart port receive interrupt |
|
|
USART_ConfigInt(USARTx, USART_INT_RXDNE, ENABLE); |
|
|
/* Enable the USARTx */ |
|
|
USART_Enable(USARTx, ENABLE); |
|
|
} |
|
|
|
|
|
/***************** Send one byte data **********************/ |
|
|
static void Usart_SendByte(USART_Module* pUSARTx, uint8_t ch) |
|
|
{ |
|
|
/* Send one byte data to usart */ |
|
|
USART_SendData(pUSARTx, ch); |
|
|
|
|
|
/* Waiting to send data register is empty */ |
|
|
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXDE) == RESET) |
|
|
; |
|
|
} |
|
|
|
|
|
/****************** Send an array of 8 bit************************/ |
|
|
void Usart_SendArray(USART_Module* pUSARTx, uint8_t* array, uint16_t num) |
|
|
{ |
|
|
uint8_t i; |
|
|
|
|
|
for (i = 0; i < num; i++) |
|
|
{ |
|
|
/* Send one byte data to usart */ |
|
|
Usart_SendByte(pUSARTx, array[i]); |
|
|
} |
|
|
/* Wait to send finished */ |
|
|
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXC) == RESET) |
|
|
; |
|
|
} |
|
|
|
|
|
#pragma import(__use_no_semihosting) |
|
|
struct __FILE { |
|
|
int handle; |
|
|
}; |
|
|
|
|
|
FILE __stdout; |
|
|
void _sys_exit(int x) |
|
|
{ |
|
|
x = x; |
|
|
} |
|
|
/* retarget the C library printf function to the USART */ |
|
|
int fputc(int ch, FILE* f) |
|
|
{ |
|
|
USART_SendData(USARTx, (uint8_t)ch); |
|
|
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXDE) == RESET) |
|
|
; |
|
|
|
|
|
return (ch); |
|
|
} |
|
|
|
|
|
static uint8_t buf66[] = {0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66}; |
|
|
|
|
|
void Cmd_Parse(uint8_t buf) |
|
|
{ |
|
|
switch(Uart1_Cmd_State) { |
|
|
case kCmd_Head1 : // <EFBFBD><EFBFBD>ʼ״̬<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD>һ<EFBFBD><EFBFBD>ͷ |
|
|
if(buf == 0x55) { Uart1_Cmd_State = kCmd_Head2; } // <EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD> |
|
|
break; |
|
|
case kCmd_Head2 : // <EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ͷ<EFBFBD><EFBFBD><EFBFBD>ҵڶ<EFBFBD><EFBFBD><EFBFBD>ͷ |
|
|
if(buf == 0x55) { Uart1_Cmd_State = kCmd_Len; } // <EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD> |
|
|
else { Uart1_Cmd_State = kCmd_Head1; } // <EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD>ʼ̬ |
|
|
break; |
|
|
case kCmd_Len : // <EFBFBD>ҵ<EFBFBD><EFBFBD>ڶ<EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><EFBFBD><EFBFBD>ҳ<EFBFBD><EFBFBD>ȣ<EFBFBD>Ŀǰ<EFBFBD><EFBFBD><EFBFBD>ȹ̶<EFBFBD>Ϊ0x01<EFBFBD><EFBFBD> |
|
|
if(buf == 0x01) { Uart1_Cmd_State = kCmd_Id; } // <EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD> |
|
|
else { Uart1_Cmd_State = kCmd_Head1; } // <EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD>ʼ̬ |
|
|
break; |
|
|
case kCmd_Id : // <EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD>Ŀǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>IDֻ<EFBFBD><EFBFBD>0x01<EFBFBD><EFBFBD>IAP<EFBFBD><EFBFBD><EFBFBD>£<EFBFBD> |
|
|
if(buf == 0x01) { Uart1_Cmd_State = kCmd_data; } // <EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD> |
|
|
else { Uart1_Cmd_State = kCmd_Head1; } // <EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD>ʼ̬ |
|
|
break; |
|
|
case kCmd_data : |
|
|
if(buf == 0xAA) { |
|
|
// ִ<EFBFBD>в<EFBFBD><EFBFBD><EFBFBD> |
|
|
__disable_irq(); /* <EFBFBD><EFBFBD>ֹȫ<EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>*/ |
|
|
Usart_SendArray(USARTx, buf66, sizeof(buf66)); |
|
|
Set_IAP_UpdateFlag(); // дFlash<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ø<EFBFBD><EFBFBD>±<EFBFBD>־ |
|
|
__set_FAULTMASK(1); // <EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD> |
|
|
NVIC_SystemReset(); // ִ<EFBFBD>и<EFBFBD>λ |
|
|
} |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
void USART1_IRQHandler(void) |
|
|
{ |
|
|
uint8_t buf_temp; |
|
|
// |
|
|
if(USART_GetFlagStatus(USARTx, USART_INT_RXDNE) != RESET) { |
|
|
USART_ClrIntPendingBit(USARTx, USART_INT_RXDNE); |
|
|
buf_temp = USART_ReceiveData(USARTx); |
|
|
|
|
|
uart1_recv_time = g_tim3_count; |
|
|
Cmd_Parse(buf_temp); // Buf<EFBFBD><EFBFBD><EFBFBD><EFBFBD> |
|
|
} |
|
|
} |
|
|
|
|
|
|