?? loader.c
字號:
/****************************************Copyright (c)**************************************************
** 廣州周立功單片機發展有限公司
** 研 究 所
** ARM開發組
**
** http://www.zlgmcu.com
**
**--------------文件信息--------------------------------------------------------------------------------
**文 件 名: loader.c
**創 建 人: 陳明計
**最后修改日期: 2004年2月26日
**描 述: 用于下載hex文件的一行
**
**--------------歷史版本信息----------------------------------------------------------------------------
** 創建人: 陳明計
** 版 本: 2004年2月26日
** 日 期: 1.0
** 描 述: 原始版本
**
**--------------當前版本修訂------------------------------------------------------------------------------
** 修改人:
** 日 期:
** 描 述:
**
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
#define IN_LOADER
#include "config.h"
static uint32 Segm = 0; /* 段地址 */
/*********************************************************************************************************
** 函數名稱: strupr
** 功能描述: 小寫轉換為大寫
** 輸 入: 字符串指針
** 輸 出: 同輸入
**
** 全局變量: 無
** 調用模塊: 無
**
** 作 者: 陳明計
** 日 期: 2004年2月25日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
char *strupr(char *Str)
{
char *cp;
cp = Str;
while (*cp != 0)
{
if (*cp >= 'a' && *cp <= 'z' )
{
*cp -= 'a' - 'A';
}
cp++;
}
return Str;
}
/*********************************************************************************************************
** 函數名稱: CheckString
** 功能描述: 檢查字符串是否合法
** 輸 入: 無
** 輸 出: TRUE:成功
** FALSE:失敗
** 全局變量: 無
** 調用模塊: 無
**
** 作 者: 陳明計
** 日 期: 2004年2月26日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
uint8 CheckString(char *s)
{
uint16 i;
uint8 Ch;
i = 0;
Ch = *s;
while (Ch != 0)
{
i++;
s++;
Ch = *s;
if (i > 520)
{
return FALSE;
}
if (Ch >= '0' && Ch <='9')
{
continue;
}
if (Ch >= 'A' && Ch <='F')
{
continue;
}
}
if (i < 10)
{
return FALSE;
}
return TRUE;
}
/*********************************************************************************************************
** 函數名稱: HexToBin
** 功能描述: 將一個字符(16進制了表示)轉換為二進制數
** 輸 入: 字符
** 輸 出: 結果
** 全局變量: 無
** 調用模塊: 無
**
** 作 者: 陳明計
** 日 期: 2004年2月26日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
uint8 HexToBin(char Ch)
{
if (Ch >= '0' && Ch <='9')
{
return (Ch - '0');
}
else
{
return (Ch - 'A' + 0x0a);
}
}
/*********************************************************************************************************
** 函數名稱: Load2Hex
** 功能描述: 將而個字符(16進制了表示)轉換為二進制數
** 輸 入: 字符
** 輸 出: 結果
** 全局變量: 無
** 調用模塊: 無
**
** 作 者: 陳明計
** 日 期: 2004年2月26日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
uint8 Load2Hex(char Ch1, char Ch2)
{
return ((HexToBin(Ch1) << 4) | HexToBin(Ch2));
}
/*********************************************************************************************************
** 函數名稱: DownloadLine
** 功能描述: 處理hex文件的一行
** 輸 入: 字符串
** 輸 出: 參考loader.h
** 全局變量: 無
** 調用模塊: 無
**
** 作 者: 陳明計
** 日 期: 2004年2月26日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
#define Read2Hex(a) \
{ \
if (s[0] == 0 || s[1] == 0) \
{ \
return 2; \
} \
a = Load2Hex(s[0], s[1]); \
CheckSum += a; \
s += 2; \
}
/*#define Read4Hex(a) \
{ \
temp1 = Load2Hex(s[0], s[1]); \
checkSum += temp1; \
s += 2; \
temp2 = temp1; \
temp1 = Load2Hex(s[0], s[1]); \
checkSum += temp1; \
s += 2; \
a = (temp2 << 8) | temp1; \
}
*/
#define Read4Hex(b) \
{ \
Read2Hex(temp1); \
temp2 = temp1; \
Read2Hex(temp1); \
b = (temp2 << 8) | temp1; \
}
uint8 DownloadLine(char *s)
{
uint8 Length,CheckSum,temp1;
uint16 temp2;
uint32 Addr,Address;
if (s == (char *)0)
{
return DOWNLOAD_LINE;
}
if (*s == ';' || *s == '\n')
{
return DOWNLOAD_LINE;
}
if (*s != ':')
{
return FILE_FOEMAT_ERR;
}
s++;
strupr(s);
if (CheckString(s) != TRUE)
{
return FILE_FOEMAT_ERR;
}
CheckSum = 0;
Read2Hex(Length);
Read4Hex(Addr);
Read2Hex(temp1);
switch (temp1)
{
case 0:
Address = Segm + Addr;
break;
case 1:
CheckSum += Load2Hex(s[0],s[1]);
if (CheckSum != 0)
{
return CHECK_ERR;
}
else
{
return DOWNLOAD_FINISH;
}
case 2:
Read4Hex(Segm);
Segm = Segm << 4;
Length -= 2;
break;
case 3: /* read start address */
break;
case 4:
Read4Hex(Segm);
Segm = Segm << 16;
Length -= 2;
break;
default:
return BAD_RECORD_TYPE;
}
while (Length != 0)
{
Read2Hex(temp1);
Read2Hex(temp2);
temp2 = (temp2 << 8) | temp1;
// WordProgram(Address, temp2);
if (WordProgram(Address, temp2) == FALSE)
{
return PROGRAM_ERR;
}
Address += 2;
Length -= 2;
}
CheckSum += Load2Hex(s[0],s[1]);
if (CheckSum != 0)
{
return CHECK_ERR;
}
return DOWNLOAD_LINE;
}
uint8 Check(char *s, uint32 *Addr1)
{
uint8 Length,CheckSum,temp1;
uint16 temp2;
uint32 Addr,Address;
if (s == (char *)0)
{
return DOWNLOAD_LINE;
}
if (*s == ';' || *s == '\n')
{
return DOWNLOAD_LINE;
}
if (*s != ':')
{
return FILE_FOEMAT_ERR;
}
s++;
strupr(s);
if (CheckString(s) != TRUE)
{
return FILE_FOEMAT_ERR;
}
CheckSum = 0;
Read2Hex(Length);
Read4Hex(Addr);
Read2Hex(temp1);
switch (temp1)
{
case 0:
Address = Segm + Addr;
break;
case 1:
CheckSum += Load2Hex(s[0],s[1]);
if (CheckSum != 0)
{
return CHECK_ERR;
}
else
{
return DOWNLOAD_FINISH;
}
case 2:
Read4Hex(Segm);
Segm = Segm << 4;
Length -= 2;
break;
case 3: /* read start address */
break;
case 4:
Read4Hex(Segm);
Segm = Segm << 16;
Length -= 2;
break;
default:
return BAD_RECORD_TYPE;
}
while (Length != 0)
{
Read2Hex(temp1);
Read2Hex(temp2);
temp2 = (temp2 << 8) | temp1;
if (((uint16 *)(Address))[0] != temp2)
{
*Addr1 = Address;
return PROGRAM_ERR;
}
Address += 2;
Length -= 2;
}
CheckSum += Load2Hex(s[0],s[1]);
if (CheckSum != 0)
{
return CHECK_ERR;
}
return DOWNLOAD_LINE;
}
/*********************************************************************************************************
** End Of File
********************************************************************************************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -