#include "usart1.h" #include #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 : // ��ʼ״̬���ҵ�һ��ͷ if(buf == 0x55) { Uart1_Cmd_State = kCmd_Head2; } // ��һ�� break; case kCmd_Head2 : // �ҵ���һ��ͷ���ҵڶ���ͷ if(buf == 0x55) { Uart1_Cmd_State = kCmd_Len; } // ��һ�� else { Uart1_Cmd_State = kCmd_Head1; } // ���˵���ʼ̬ break; case kCmd_Len : // �ҵ��ڶ���ͷ���ҳ��ȣ�Ŀǰ���ȹ̶�Ϊ0x01�� if(buf == 0x01) { Uart1_Cmd_State = kCmd_Id; } // ��һ�� else { Uart1_Cmd_State = kCmd_Head1; } // ���˵���ʼ̬ break; case kCmd_Id : // �ҵ����Ⱥ�������ID��Ŀǰ����IDֻ��0x01��IAP���£� if(buf == 0x01) { Uart1_Cmd_State = kCmd_data; } // ��һ�� else { Uart1_Cmd_State = kCmd_Head1; } // ���˵���ʼ̬ break; case kCmd_data : if(buf == 0xAA) { // ִ�в��� __disable_irq(); /* ��ֹȫ���ж�*/ Usart_SendArray(USARTx, buf66, sizeof(buf66)); Set_IAP_UpdateFlag(); // дFlash�����ø��±�־ __set_FAULTMASK(1); // �ر������ж� NVIC_SystemReset(); // ִ�и�λ } 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���� } }