ZDBMS/code_bootloader/Main.c

648 lines
12 KiB
C
Raw Normal View History

2025-02-06 07:35:32 +00:00
/********************************************************************************
Copyright (C), Sinowealth Electronic. Ltd.
Author: Sino
Version: V0.0
Date: 2020/04/26
History:
V2.0 2020/04/26 Preliminary
********************************************************************************/
#include "MCURegister.h"
#include "C51_TYPE.H"
#include "Flash.h"
#include "IapIsp.h"
#include "INTRINS.H"
/*************************************************************************************************
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: InitGPIO
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: <EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ: <EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: ֻ<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD>͹<EFBFBD><EFBFBD><EFBFBD>ϵͳ<EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>IO<EFBFBD>˿<EFBFBD>
*************************************************************************************************/
void InitGPIO(void)
{
P0 = 0xF8;
P0CR = 0x8F;
P0PCR = 0xC8;
P1 = 0x80;
P1CR = 0x7F;
P1PCR = 0x80;
P2 = 0xE0;
P2CR = 0x3F;
P2PCR = 0xC0;
P3 = 0x19;
P3CR = 0xE6;
P3PCR = 0x19;
#if (UART_DEFINE == 1)
P0 |= 0xC0;
P0PCR |= 0x80;
#endif
#if (UART_DEFINE == 2)
P0 |= 0x40;
P2 |= 0x10;
P2PCR |= 0x10;
#endif
#if (UART_DEFINE == 3)
P0 |= 0x40;
P2 |= 0x20;
P2PCR |= 0x20;
#endif
#if (UART_DEFINE == 4)
P0 |= 0x41;
P0PCR |= 0x01;
#endif
#if (UART_DEFINE == 5)
P0 |= 0x42;
P0PCR |= 0x02;
#endif
#if (UART_DEFINE == 6)
P0 |= 0xC0;
P0PCR |= 0x40;
#endif
#if (UART_DEFINE == 7)
P0 |= 0x80;
P2 |= 0x10;
P2PCR |= 0x10;
#endif
#if (UART_DEFINE == 8)
P0 |= 0x80;
P2 |= 0x20;
P2PCR |= 0x20;
#endif
#if (UART_DEFINE == 9)
P0 |= 0x81;
P0PCR |= 0x01;
#endif
#if (UART_DEFINE == 10)
P0 |= 0x82;
P0PCR |= 0x02;
#endif
#if (UART_DEFINE == 11)
P0 |= 0x40;
P2 |= 0x10;
P0PCR |= 0x40;
#endif
#if (UART_DEFINE == 12)
P0 |= 0x80;
P2 |= 0x10;
P0PCR |= 0x80;
#endif
#if (UART_DEFINE == 13)
P2 |= 0x30;
P2PCR |= 0x20;
#endif
#if (UART_DEFINE == 14)
P0 |= 0x01;
P2 |= 0x10;
P0PCR |= 0x01;
#endif
#if (UART_DEFINE == 15)
P0 |= 0x02;
P2 |= 0x10;
P0PCR |= 0x02;
#endif
#if (UART_DEFINE == 16)
P0 |= 0x40;
P2 |= 0x20;
P0PCR |= 0x40;
#endif
#if (UART_DEFINE == 17)
P0 |= 0x80;
P2 |= 0x20;
P0PCR |= 0x80;
#endif
#if (UART_DEFINE == 18)
P2 |= 0x30;
P2PCR |= 0x10;
#endif
#if (UART_DEFINE == 19)
P0 |= 0x01;
P2 |= 0x20;
P0PCR |= 0x01;
#endif
#if (UART_DEFINE == 20)
P0 |= 0x02;
P2 |= 0x20;
P0PCR |= 0x02;
#endif
#if (UART_DEFINE == 21)
P0 |= 0x81;
P0PCR |= 0x80;
#endif
#if (UART_DEFINE == 22)
P0 |= 0x01;
P2 |= 0x10;
P2PCR |= 0x10;
#endif
#if (UART_DEFINE == 23)
P0 |= 0x01;
P2 |= 0x20;
P2PCR |= 0x20;
#endif
#if (UART_DEFINE == 24)
P0 |= 0x41;
P0PCR |= 0x40;
#endif
#if (UART_DEFINE == 25)
P0 |= 0x03;
P0PCR |= 0x02;
#endif
#if (UART_DEFINE == 26)
P0 |= 0x82;
P0PCR |= 0x80;
#endif
#if (UART_DEFINE == 27)
P0 |= 0x02;
P2 |= 0x10;
P2PCR |= 0x10;
#endif
#if (UART_DEFINE == 28)
P0 |= 0x02;
P2 |= 0x20;
P2PCR |= 0x20;
#endif
#if (UART_DEFINE == 29)
P0 |= 0x42;
P0PCR |= 0x40;
#endif
#if (UART_DEFINE == 30)
P0 |= 0x03;
P0PCR |= 0x01;
#endif
//UART1<54><31><EFBFBD><EFBFBD>IO<49><4F><EFBFBD><EFBFBD>
#if (UART_DEFINE == 31)
P1 |= 0x03;
P1PCR |= 0x02;
#endif
#if (UART_DEFINE == 32)
P1 |= 0x01;
P3 |= 0x08;
P3PCR |= 0x08;
#endif
#if (UART_DEFINE == 33)
P1 |= 0x01;
P3 |= 0x10;
P3PCR |= 0x10;
#endif
#if (UART_DEFINE == 34)
P1 |= 0x01;
P2 |= 0x40;
P2PCR |= 0x40;
#endif
#if (UART_DEFINE == 35)
P1 |= 0x01;
P2 |= 0x80;
P2PCR |= 0x80;
#endif
#if (UART_DEFINE == 36)
P1 |= 0x03;
P1PCR |= 0x01;
#endif
#if (UART_DEFINE == 37)
P1 |= 0x02;
P3 |= 0x08;
P3PCR |= 0x08;
#endif
#if (UART_DEFINE == 38)
P1 |= 0x02;
P3 |= 0x10;
P3PCR |= 0x10;
#endif
#if (UART_DEFINE == 39)
P1 |= 0x02;
P2 |= 0x40;
P2PCR |= 0x40;
#endif
#if (UART_DEFINE == 40)
P1 |= 0x02;
P2 |= 0x80;
P2PCR |= 0x80;
#endif
#if (UART_DEFINE == 41)
P1 |= 0x01;
P3 |= 0x08;
P1PCR |= 0x01;
#endif
#if (UART_DEFINE == 42)
P1 |= 0x02;
P3 |= 0x08;
P1PCR |= 0x01;
#endif
#if (UART_DEFINE == 43)
P3 |= 0x18;
P3PCR |= 0x10;
#endif
#if (UART_DEFINE == 44)
P3 |= 0x08;
P2 |= 0x40;
P2PCR |= 0x40;
#endif
#if (UART_DEFINE == 45)
P3 |= 0x08;
P2 |= 0x80;
P2PCR |= 0x80;
#endif
#if (UART_DEFINE == 46)
P1 |= 0x01;
P3 |= 0x10;
P1PCR |= 0x01;
#endif
#if (UART_DEFINE == 47)
P1 |= 0x02;
P3 |= 0x10;
P1PCR |= 0x02;
#endif
#if (UART_DEFINE == 48)
P3 |= 0x18;
P3PCR |= 0x08;
#endif
#if (UART_DEFINE == 49)
P3 |= 0x10;
P2 |= 0x40;
P2PCR |= 0x40;
#endif
#if (UART_DEFINE == 50)
P3 |= 0x10;
P2 |= 0x80;
P2PCR |= 0x80;
#endif
#if (UART_DEFINE == 51)
P1 |= 0x01;
P2 |= 0x40;
P1PCR |= 0x01;
#endif
#if (UART_DEFINE == 52)
P1 |= 0x02;
P2 |= 0x40;
P1PCR |= 0x02;
#endif
#if (UART_DEFINE == 53)
P2 |= 0x40;
P3 |= 0x08;
P3PCR |= 0x08;
#endif
#if (UART_DEFINE == 54)
P3 |= 0x10;
P2 |= 0x40;
P3PCR |= 0x10;
#endif
#if (UART_DEFINE == 55)
P2 |= 0xC0;
P2PCR |= 0x80;
#endif
#if (UART_DEFINE == 56)
P1 |= 0x01;
P2 |= 0x80;
P1PCR |= 0x01;
#endif
#if (UART_DEFINE == 57)
P1 |= 0x02;
P2 |= 0x80;
P1PCR |= 0x02;
#endif
#if (UART_DEFINE == 58)
P2 |= 0x80;
P3 |= 0x08;
P3PCR |= 0x08;
#endif
#if (UART_DEFINE == 59)
P3 |= 0x10;
P2 |= 0x80;
P3PCR |= 0x10;
#endif
#if (UART_DEFINE == 60)
P2 |= 0xC0;
P2PCR |= 0x40;
#endif
//UART2<54><32><EFBFBD><EFBFBD>IO<49><4F><EFBFBD>ã<EFBFBD><C3A3><EFBFBD>ǰdemo<6D><6F><EFBFBD>˴<EFBFBD><CBB4><EFBFBD><EFBFBD><EFBFBD>LED<45><44><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD>֧<EFBFBD>ִ<EFBFBD><D6B4><EFBFBD>ͨѶ<CDA8><D1B6><EFBFBD><EFBFBD>demo<6D><6F><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ʹ<EFBFBD><CAB9>uart2<74><32><EFBFBD>ڹ<EFBFBD><DAB9><EFBFBD>
#if (UART_DEFINE == 61)
P2 |= 0x06;
P2PCR |= 0x02;
#endif
}
/*************************************************************************************************
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: InitIRQ
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: <EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ: <EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: <EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϼ<EFBFBD><EFBFBD><EFBFBD>
*************************************************************************************************/
void InitIRQ(void)
{
IEN0 = 0x00;
IEN1 = 0x00;
IEN2 = 0x00;
}
#if ((UART_DEFINE >= 1)&&(UART_DEFINE<= 30))
/*************************************************************************************************
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: UARTInit
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: Bps: UARTͨѶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ: <EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>UARTģ<EFBFBD><EFBFBD>
*************************************************************************************************/
void UARTInit(void)
{
#if UART_DEFINE == 1
UART0CR = 0x32;
#elif UART_DEFINE == 2
UART0CR = 0x42;
#elif UART_DEFINE == 3
UART0CR = 0x52;
#elif UART_DEFINE == 4
UART0CR = 0x02;
#elif UART_DEFINE == 5
UART0CR = 0x12;
#elif UART_DEFINE == 6
UART0CR = 0x23;
#elif UART_DEFINE == 7
UART0CR = 0x43;
#elif UART_DEFINE == 8
UART0CR = 0x53;
#elif UART_DEFINE == 9
UART0CR = 0x03;
#elif UART_DEFINE == 10
UART0CR = 0x13;
#elif UART_DEFINE == 11
UART0CR = 0x24;
#elif UART_DEFINE == 12
UART0CR = 0x34;
#elif UART_DEFINE == 13
UART0CR = 0x54;
#elif UART_DEFINE == 14
UART0CR = 0x04;
#elif UART_DEFINE == 15
UART0CR = 0x14;
#elif UART_DEFINE == 16
UART0CR = 0x25;
#elif UART_DEFINE == 17
UART0CR = 0x35;
#elif UART_DEFINE == 18
UART0CR = 0x45;
#elif UART_DEFINE == 19
UART0CR = 0x05;
#elif UART_DEFINE == 20
UART0CR = 0x15;
#elif UART_DEFINE == 21
UART0CR = 0x30;
#elif UART_DEFINE == 22
UART0CR = 0x40;
#elif UART_DEFINE == 23
UART0CR = 0x50;
#elif UART_DEFINE == 24
UART0CR = 0x20;
#elif UART_DEFINE == 25
UART0CR = 0x10;
#elif UART_DEFINE == 26
UART0CR = 0x31;
#elif UART_DEFINE == 27
UART0CR = 0x41;
#elif UART_DEFINE == 28
UART0CR = 0x51;
#elif UART_DEFINE == 29
UART0CR = 0x21;
#elif UART_DEFINE == 30
UART0CR = 0x01;
#endif
PCON = 0x00;
SCON = 0x50;
SBRTH = UART_BPS_H;
SBRTL = UART_BPS_L;
SFINE = UART_BPS_F;
SBRTH |= 0x80;
SADDR = 0x0A;
SADEN = 0xFF;
}
#elif ((UART_DEFINE >= 31)&&(UART_DEFINE<= 60))
void UARTInit(void)
{
INSCON = 0x00;
#if UART_DEFINE == 31
UART1CR = 0x10;
#elif UART_DEFINE == 32
UART1CR = 0x40;
#elif UART_DEFINE == 33
UART1CR = 0x50;
#elif UART_DEFINE == 34
UART1CR = 0x20;
#elif UART_DEFINE == 35
UART1CR = 0x30;
#elif UART_DEFINE == 36
UART1CR = 0x01;
#elif UART_DEFINE == 37
UART1CR = 0x41;
#elif UART_DEFINE == 38
UART1CR = 0x51;
#elif UART_DEFINE == 39
UART1CR = 0x21;
#elif UART_DEFINE == 40
UART1CR = 0x31;
#elif UART_DEFINE == 41
UART1CR = 0x04;
#elif UART_DEFINE == 42
UART1CR = 0x14;
#elif UART_DEFINE == 43
UART1CR = 0x54;
#elif UART_DEFINE == 44
UART1CR = 0x24;
#elif UART_DEFINE == 45
UART1CR = 0x34;
#elif UART_DEFINE == 46
UART1CR = 0x05;
#elif UART_DEFINE == 47
UART1CR = 0x15;
#elif UART_DEFINE == 48
UART1CR = 0x45;
#elif UART_DEFINE == 49
UART1CR = 0x25;
#elif UART_DEFINE == 50
UART1CR = 0x35;
#elif UART_DEFINE == 51
UART1CR = 0x02;
#elif UART_DEFINE == 52
UART1CR = 0x12;
#elif UART_DEFINE == 53
UART1CR = 0x42;
#elif UART_DEFINE == 54
UART1CR = 0x52;
#elif UART_DEFINE == 55
UART1CR = 0x32;
#elif UART_DEFINE == 56
UART1CR = 0x03;
#elif UART_DEFINE == 57
UART1CR = 0x13;
#elif UART_DEFINE == 58
UART1CR = 0x43;
#elif UART_DEFINE == 59
UART1CR = 0x53;
#elif UART_DEFINE == 60
UART1CR = 0x23;
#endif
INSCON = 0x40;
PCON1 = 0x00;
SCON1 = 0x50; //<2F><><EFBFBD><EFBFBD>Uart<72><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ1
SBRTH1 = UART_BPS_H;
SBRTL1 = UART_BPS_L;
SFINE1 = UART_BPS_F;
SBRTH1 |= 0x80; //ʹ<>ܲ<EFBFBD><DCB2><EFBFBD><EFBFBD>ʷ<EFBFBD><CAB7><EFBFBD><EFBFBD><EFBFBD>
SADDR1 = 0x0A; //UART<52><54>ַ<EFBFBD><D6B7>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>Ϊ0x0A
SADEN1 = 0xFF;
INSCON = 0x00;
}
#elif (UART_DEFINE == 61)
void UARTInit(void)
{
INSCON = 0x40;
PCON2 = 0x00;
SCON2 = 0x50; //<2F><><EFBFBD><EFBFBD>Uart<72><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ1
SBRTH2 = UART_BPS_H; //115200bps
SBRTL2 = UART_BPS_L;
SFINE2 = UART_BPS_F;
SBRTH2 |= 0x80; //ʹ<>ܲ<EFBFBD><DCB2><EFBFBD><EFBFBD>ʷ<EFBFBD><CAB7><EFBFBD><EFBFBD><EFBFBD>
SADDR2 = 0x0A; //UART<52><54>ַ<EFBFBD><D6B7>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>Ϊ0x0A
SADEN2 = 0xFF;
INSCON = 0x00;
}
#else
#Error: "UART_DEFINE ERROR"
#endif
/*************************************************************************************************
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: Initial
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: <EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ: <EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ϵͳ
*************************************************************************************************/
void Initial(void)
{
InitIRQ();
InitGPIO();
bHandsheakOkFlg = 0;
}
/*************************************************************************************************
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: main
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: <EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ: <EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: Boot<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*************************************************************************************************/
void main(void)
{
BootMcuWdtClear();
Initial(); //Ӳ<><D3B2><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4>ʼ<EFBFBD><CABC>
//1.<2E><>ȡԶ<C8A1><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־λ״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IAP<41><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>г<EFBFBD><D0B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(McuFlashRdOneByte(BOOT_FLG_IAP_ADDR, MCU_TYPE_CODE) == IAP_FLG)
{
bIapIspFlg = IAP_MODE;
ucMcuFlashWrValid = 0x55; //<2F><><EFBFBD><EFBFBD>MCU Code<64><65><EFBFBD>ɲ<EFBFBD><C9B2><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9>д
if(BootUpgradeCode()) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
ucMcuFlashWrValid = 0;
((void(code*)(void))IAP_CODE_START_ADDR)(); //<2F><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ӧ<EFBFBD>ó<EFBFBD><C3B3><EFBFBD><EFBFBD>׵<EFBFBD>ַ
}
else if(BootUpgradeCode()) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
ucMcuFlashWrValid = 0;
((void(code*)(void))IAP_CODE_START_ADDR)(); //<2F><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ӧ<EFBFBD>ó<EFBFBD><C3B3><EFBFBD><EFBFBD>׵<EFBFBD>ַ
}
else
{
while(1)
{
ucMcuFlashWrValid = 0;
SUSLO = 0x55;
PCON |= 0x02;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
//2.<2E>ж<EFBFBD><D0B6>Ƿ<EFBFBD>ISP<53>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD>־<EFBFBD><D6BE><EFBFBD>ڱ<EFBFBD>ʾ֮ǰ<D6AE><C7B0>ISPʧ<50>ܣ<EFBFBD><DCA3><EFBFBD>Ҫ<EFBFBD><D2AA>BOOT<4F><54><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ISP
else if(McuFlashRdOneByte(BOOT_FLG_ISP_ADDR, MCU_TYPE_CODE) == ISP_FLG)
{
bIapIspFlg = ISP_MODE;
BootIapIsp();
}
//3.<2E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><><D6B1><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ӧ<EFBFBD>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD>
else
{
((void(code*)(void))IAP_CODE_START_ADDR)();
}
}