/** ****************************************************************************** * @file RefreshMenu.c * @author Vincent * @version V1.0.0 * @date 23-12-2015 * @brief ****************************************************************************** ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "RefreshMenu.h" #include "protocol.h" #include "uart_app.h" #include "can_app.h" #include "flash_if.h" #include "hwsetup.h" #include "ti_msp_dl_config.h" /* Private variables ---------------------------------------------------------*/ download_t downLoad; updatecount_t update; uint8_t flag_FlashWR[APPPACKGECOUNT_MAX_ADDRESS]; /* Private variables ---------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/ //void Protocol_Ack(void); //typedef void (*iapfun)(void); //瀹氫箟涓�涓嚱鏁扮被鍨嬬殑鍙傛暟 //iapfun jumpFun; otafun jump_to_app; void JumpToApp(__IO uint32_t ApplicationAddress) { /* check app starting address whether 0x08xxxxxx */ if(((*(uint32_t*)(ApplicationAddress + 0)) & 0xFFFF0000) == 0x20200000) // return; /* check the address of stack */ // if(((*(uint32_t*)ApplicationAddress) - 0x20000000) < (SRAM_SIZE * 1024)) { //crm_reset(); __disable_irq(); /* disable periph clock */ // crm_periph_clock_enable(CRM_TMR3_PERIPH_CLOCK, FALSE); // crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, FALSE); // crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, FALSE); DL_GPIO_reset(GPIOA); DL_GPIO_reset(GPIOB); DL_GPIO_disablePower(GPIOA); DL_GPIO_disablePower(GPIOB); //DL_UART_Main_disablePower(UART_BAT_INST); DL_UART_Main_disablePower(UART_HMI_INST); DL_MCAN_disablePower(MCAN0_INST); #if 1 //UART2_ENABLE // crm_periph_clock_enable(CRM_USART2_PERIPH_CLOCK, FALSE); // crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, FALSE); // nvic_irq_disable(USART2_IRQn); // __NVIC_ClearPendingIRQ(USART2_IRQn); //__NVIC_DisableIRQ(UART_BAT_INST_INT_IRQN); //__NVIC_ClearPendingIRQ(UART_BAT_INST_INT_IRQN); #endif /* disable nvic irq and clear pending */ // nvic_irq_disable(USART1_IRQn); // nvic_irq_disable(UART1_INT_IRQn); __NVIC_DisableIRQ(UART_HMI_INST_INT_IRQN); // __NVIC_ClearPendingIRQ(USART1_IRQn); __NVIC_ClearPendingIRQ(UART_HMI_INST_INT_IRQN); __NVIC_DisableIRQ(MCAN0_INST_INT_IRQN); __NVIC_ClearPendingIRQ(MCAN0_INST_INT_IRQN); // nvic_irq_disable(SysTick_IRQn); __NVIC_DisableIRQ(SysTick_IRQn); __NVIC_ClearPendingIRQ(SysTick_IRQn); SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; SysTick->VAL = 0x00; /* Load the SysTick Counter Value */ // nvic_irq_disable(TMR3_GLOBAL_IRQn); // __NVIC_ClearPendingIRQ(TMR3_GLOBAL_IRQn); // SCB->VTOR =0x00006000;// nvic_vector_table_set(NVIC_VECTTAB_FLASH, 0x00006000); // delay_cycles(100000); // jump_to_app =(otafun)*(uint32_t*)(0x00006131); // (*(void(*)(void))(0x00006131))(); jump_to_app = (otafun)*(uint32_t*)(ApplicationAddress + 4); /* code second word is reset address */ __set_MSP(*(uint32_t*)ApplicationAddress); /* init app stack pointer(code first word is stack address) */ jump_to_app(); /* jump to user app */ } } /******************************************************************************* * Function: void User_Manu_Init(void) * Discrible: 鍒濆鍖� * Input: * Output: * Return: * Others: * date version author record * ----------------------------------------------- * 20180419 V1.0 VINCENT Create *********************************************************************************/ void User_Manu_Init(void) { //_.ackcmd = ACK_REQ; downLoad.step = 0; downLoad.packagecount = 0; downLoad.wrenable = DISABLE; downLoad.finished = DISABLE; downLoad.startUpSendCount = 0; update.TimeCounter_Startup = 1000; update.TimeCounter_Update = 0; } /******************************************************************************* * Function: void User_Main_Manu(void) * Discrible: 涓昏彍鍗� * Input: * Output: * Return: * Others: * date version author record * ----------------------------------------------- * 20180419 V1.0 VINCENT Create *********************************************************************************/ void User_Main_Manu(void) { while(1) { //UART_RxData_Process(&UART_RxBuff_Struct1,&UART_TxBuff_Struct1); UART_RxData_Process(&UART_RxBuff_Struct2,&UART_TxBuff_Struct2); CAN_RxData_Process(&CAN_RxBuf_Struct1); startUpAck(); if(CAN_RxBuf_Struct1.ucCANEnable == 1) { updateAck(&UART_TxBuff_Struct2); timeOutAck(&UART_TxBuff_Struct2); } /*else if(UART_RxBuff_Struct1.ucCOMEnable == 1) { updateAck(&UART_TxBuff_Struct1); timeOutAck(&UART_TxBuff_Struct1); }*/ else if(UART_RxBuff_Struct2.ucCOMEnable == 1) { updateAck(&UART_TxBuff_Struct2); timeOutAck(&UART_TxBuff_Struct2); } if(downLoad.finished == ENABLE) { delay_1ms(50); DL_UART_reset(UART_HMI_INST); DL_MCAN_reset(MCAN0_INST); DL_GPIO_clearPins(OUTPUT_PORT, OUTPUT_POWER_EN_PIN); #if UART2_ENABLE // usart_reset(USART2); // DL_UART_reset(UART_BAT_INST); #endif JumpToApp(APPLICATION_ADDRESS); } } } void timeOutAck(USART_Buf_TypeDef* ptUartTx) { if((downLoad.finished == DISABLE) && (downLoad.step != 0)) { if(update.TimeCounter_Update > 3000 ) //3000ms { update.TimeCounter_Update = 0; sendUpdateAck(CMD_ACK_ERR , downLoad.packagecount); downLoad.finished = ENABLE; } } } /*上电启动,或复位后*/ void startUpAck(void) { if(downLoad.step == 0 ) { if(downLoad.startUpSendCount < 4) { if(update.TimeCounter_Startup >= 50) //200ms { update.TimeCounter_Startup = 0; downLoad.startUpSendCount++; // DL_UART_Main_transmitData(UART_0_INST, 0x5a); // DL_UART_Main_transmitData(UART_HMI_INST, 0x5a); //uart_sendBootInfo(&UART_TxBuff_Struct1); uart_sendBootInfo(&UART_TxBuff_Struct2); CAN_sendBootInfo(); //sendBootInfo(); } } else { downLoad.finished = ENABLE; } } } uint32_t fisrt_data; void updateAck(USART_Buf_TypeDef* ptUartTx) { if( update.AckEnable == ENABLE ) { update.AckEnable = DISABLE; switch(downLoad.step) { case 1: sendUpdateAck(downLoad.userCmd , 0x0000); break; case 2: sendUpdateAck(downLoad.userCmd , downLoad.packagecount); break; case 3: sendUpdateAck(downLoad.userCmd , 0x0000); downLoad.finished = ENABLE; break; case 5: uart_sendAck(ptUartTx); downLoad.step = 0; downLoad.finished = DISABLE; downLoad.startUpSendCount = 0; break; default : break; } } if(downLoad.wrenable == ENABLE) { downLoad.wrenable = DISABLE; // write flash if((downLoad.packagecount <= APPPACKGECOUNT_MAX_ADDRESS) && ( downLoad.packagecount > 0 ) ) // { if(flag_FlashWR[downLoad.packagecount - 1] == 0 ) { fisrt_data=*(uint32_t *)downLoad.dataBuff_128B; if(FLASH_If_Write( (downLoad.packagecount - 1) * PACKET_128B_SIZE + APPLICATION_ADDRESS, (uint32_t *)downLoad.dataBuff_128B, 16) != FLASHIF_OK ) { while(1) { sendUpdateAck(CMD_ACK_ERR_FLASH , 0xee); // Iwdg_Refresh(); delay_1ms(1000); } } flag_FlashWR[downLoad.packagecount - 1] = 1; /* 写完FLASH后立即应答,清超时计数 */ update.AckEnable = ENABLE; /*清除超时标志*/ update.TimeCounter_Update = 0; } else { /*重复写入,可能是上位机没有接收到正确应答信号,重发了数据包, 应答一个正确信号,让上位机发送下一包数据*/ sendUpdateAck(CMD_ACK_OK, downLoad.packagecount); sendUpdateAck(CMD_ACK_ERR_FLASH , 0xe1); } } else { sendUpdateAck(CMD_ACK_ERR_FLASH , 0xe2); } } } void sendUpdateAck(uint16_t cmd, uint16_t packageNum) { if(CAN_RxBuf_Struct1.ucCANEnable == 1) { CAN_sendUpdateAck(cmd, packageNum); } /*else if(UART_RxBuff_Struct1.ucCOMEnable == 1) { uart_sendUpdateAck(&UART_TxBuff_Struct1, cmd, packageNum); }*/ else if(UART_RxBuff_Struct2.ucCOMEnable == 1) { uart_sendUpdateAck(&UART_TxBuff_Struct2, cmd, packageNum); } } void sendAck(void) { if(CAN_RxBuf_Struct1.ucCANEnable == 1) { CAN_sendAck(); } /*else if(UART_RxBuff_Struct1.ucCOMEnable == 1) { uart_sendAck(&UART_TxBuff_Struct1); }*/ else if(UART_RxBuff_Struct2.ucCOMEnable == 1) { uart_sendAck(&UART_TxBuff_Struct2); } } /******************************************************************************* * Function: void Period_Stick(void) * Discrible: 周期定时器 * Input: * Output: * Return: * Others: * date version author record * ----------------------------------------------- * 20160307 V1.0 VINCENT Create *********************************************************************************/ void Period_Stick(void) { update.TimeCounter_Startup ++; update.TimeCounter_Update++; } /************************ (C) END OF FILE *********************************/