You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
6.4 KiB

5 months ago
/*
*********************************************************************************************************
*
* ģ : CANʽ̼
* ļ : demo_canfd_update.c
* : V1.0
* ˵ : CANʽ̼
*
* ޸ļ¼ :
* ˵
* V1.0 2022-06-15 Eric2013 ʽ
*
* Copyright (C), 2022-2030, www.armfly.com
*
*********************************************************************************************************
*/
#include "main.h"
#include "demo_canfd_update.h"
#include "bsp_cpu_flash.h"
#include "bsp_msg.h"
#include "bsp_can.h"
/*
*********************************************************************************************************
*
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*
*********************************************************************************************************
*/
#define AppAddr 0x08020000 /* APP��ַ */
/*
*********************************************************************************************************
*
*********************************************************************************************************
*/
__IO uint32_t uwCRCValue;
__IO uint32_t uwExpectedCRCValue;
__IO uint32_t uwAppSize;
uint8_t buf[1024];
uint32_t RecCount = 0;
uint32_t RecCount0 = 0;
uint32_t RecSize = 0;
uint8_t RecCplt = 0;
uint32_t filesize = 0;
uint32_t Bus_Error=0;
uint32_t update_index = 0;
uint32_t update_index_true = 0;
uint32_t update_sum = 0;
uint32_t update_sum_true = 0;
/*
*********************************************************************************************************
* : DemoCANUpdate
* ˵: CAN¼
* :
* ֵ:
*********************************************************************************************************
*/
void DemoCANUpdate(void)
{
uint8_t cmd;
uint32_t SectorCount = 0;
uint32_t SectorRemain = 0;
uint32_t i;
uint32_t TotalSize = 0;
uint8_t ucState;
MSG_T msg;
can_Init(); /* ��ʼ��CAN */
while (1)
{
if (bsp_GetMsg(&msg))
{
switch (msg.MsgCode)
{
case MSG_CAN2_RX: /* ���յ�CAN�豸��Ӧ�� */
cmd = g_Can2RxData[0];
/* ��ʼ�����̼����� **************/
if(cmd == '$')
{
/* ���չ�224������ */
RecSize = g_Can2RxData[1];
update_index = g_Can2RxData[2] + (g_Can2RxData[3] << 8) + (g_Can2RxData[4] << 16) + (g_Can2RxData[5] << 24);
if(update_index==update_index_true && Bus_Error==0)
{
update_sum = g_Can2RxData[6] + (g_Can2RxData[7] << 8);
update_sum_true = 0;
for(uint8_t i=0;i<RecSize;i++)
{
update_sum_true = update_sum_true + g_Can2RxData[8+i];
}
if(update_sum==update_sum_true && Bus_Error==0)
{
/* �����ڲ�Flash, */
ucState = bsp_WriteCpuFlash((uint32_t)(AppAddr + TotalSize), (uint8_t *)&g_Can2RxData[8], RecSize);
TotalSize += RecSize;
/* �������ط�0����ʾ����ʧ�� */
if(ucState != 0)
{
Bus_Error++;/* ����0x60����ʾ����ʧ�� */
}
/* ����0x30����ʾ���̳ɹ� */
}
else
{
Bus_Error++;
}
}
else
{
Bus_Error++;
can2_SendPacket(g_Can2RxData, 4);
}
update_index_true++;
}
/* ������������ **************/
if(cmd == '#' && Bus_Error==0)
{
can2_SendPacket(g_Can2RxData, 3);
HAL_Delay(10);
JumpToApp();
}
/* ���չ̼���С���� */
if(cmd == '*')
{
filesize = g_Can2RxData[1] + (g_Can2RxData[2] << 8) + (g_Can2RxData[3] << 16) + (g_Can2RxData[4] << 24);
uwAppSize = filesize;
SectorCount = filesize/(128*1024);
SectorRemain = filesize%(128*1024);
Bus_Error=0;
update_index_true=0;
update_sum_true=0;
TotalSize=0;
for(i = 0; i < SectorCount; i++)
{
bsp_EraseCpuFlash((uint32_t)(AppAddr + i*128*1024));
}
if(SectorRemain)
{
bsp_EraseCpuFlash((uint32_t)(AppAddr + i*128*1024));
}
/* ����0x30����ʾ�����ɹ� */
}
break;
}
}
}
}
/*
*********************************************************************************************************
* : JumpToApp
* ˵: תӦJumpToApp
* :
* ֵ:
*********************************************************************************************************
*/
void JumpToApp(void)
{
uint32_t i=0;
void (*AppJump)(void); /* ����һ������ָ�� */
/* �ر�ȫ���ж� */
DISABLE_INT();
/* ��������ʱ�ӵ�Ĭ��״̬��ʹ��HSIʱ�� */
bsp_DeInitCan2();
__HAL_RCC_GPIOH_CLK_DISABLE();
__HAL_RCC_GPIOB_CLK_DISABLE();
HAL_RCC_DeInit();
/* �رյδ���ʱ������λ��Ĭ��ֵ */
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;
/* �ر������жϣ����������жϹ�����־ */
for (i = 0; i < 8; i++)
{
NVIC->ICER[i]=0xFFFFFFFF;
NVIC->ICPR[i]=0xFFFFFFFF;
}
/* ʹ��ȫ���ж� */
ENABLE_INT();
/* ��ת��Ӧ�ó������׵�ַ��MSP����ַ+4�Ǹ�λ�жϷ���������ַ */
AppJump = (void (*)(void)) (*((uint32_t *) (AppAddr + 4)));
/* ��������ջָ�� */
__set_MSP(*(uint32_t *)AppAddr);
/* ��RTOS���̣�������������Ҫ������Ϊ��Ȩ��ģʽ��ʹ��MSPָ�� */
__set_CONTROL(0);
/* ��ת��ϵͳBootLoader */
AppJump();
/* ��ת�ɹ��Ļ�������ִ�е�����û��������������Ӵ��� */
while (1)
{
}
}
/*
*********************************************************************************************************
* : can_Init
* ˵: CANӲ
* :
* ֵ:
*********************************************************************************************************
*/
void can_Init(void)
{
bsp_InitCan2();
}
/*
*********************************************************************************************************
* : can_DeInit
* ˵: ˳CANӲӲʣָCPUصGPIOΪȱʡرCANж
* :
* ֵ:
*********************************************************************************************************
*/
void can_DeInit(void)
{
bsp_DeInitCan2();
}
/***************************** ���������� www.armfly.com (END OF FILE) *********************************/