⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comm.c

📁 MCU为LPC2220,FLASH为39FV1601(地址0x80000000),外部一个SRAM(地址0x81000000)
💻 C
字号:
/****************************************Copyright (c)**************************************************
**                               广州周立功单片机发展有限公司
**                                     研    究    所
**                                        产品一部 
**
**                                 http://www.zlgmcu.com
**
**--------------文件信息--------------------------------------------------------------------------------
**文   件   名: target.c
**创   建   人: 陈明计
**最后修改日期: 2004年2月2日
**描        述: lpc22xx(飞利浦的ARM)目标板特殊的代码,包括异常处理程序和目标板初始化程序
**              每个工程应当具有这个文件的拷贝,用户根据程序的需要修改本文件。
**--------------历史版本信息----------------------------------------------------------------------------
** 创建人: 陈明计
** 版  本: v1.0
** 日 期: 2004年2月2日
** 描 述: 原始版本
**
**------------------------------------------------------------------------------------------------------
** 修改人:
** 版  本:
** 日 期:
** 描 述:
**
**--------------当前版本修订------------------------------------------------------------------------------
** 修改人:
** 日 期:
** 描 述:
**
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/

#define IN_COMM
#include "config.h"
//.h文件已定义BPS 为38400
/*#ifndef bps
#define bps 19200
#endif*/

#define BUF_SIZE 2048
static uint8 Buf[BUF_SIZE];
uint32 Hard;
uint32 End;
uint8 sendbytebuf[24];
uint8 recivebuf[1032];
/********
**串口使用
*********/
#define heard_comm 0xaa
#define ack_comm 0x08
#define AllLeng 0x7ff
//命令代码
/*#define heard_comm 0xaa
#define relink_comm 0x01
#define bootver_comm 0x02
#define flashid_comm 0x03
#define downboot_comm 0x04
#define downlogo_comm 0x05
#define downcharlib_comm 0x06
#define downapp_comm 0x07
#define ack_comm 0x08
#define rtc_comm 0x09
#define rtc_up_comm 0x0a*/
/*#define heard_comm 0xaa
#define relink_comm 0x01
#define bootver_comm 0x02
#define flashid_comm 0x03
#define clear_comm 0x04
#define read_comm 0x05
#define write_comm 0x06
#define runapp_comm 0x07
#define ack_comm 0x08
#define rtc_comm 0x09
#define rtc_up_comm 0x0a*/
//FLASH中各部分的地址
/*#define Addr_boot_base 0x00000000//各部分起始地址
#define Addr_log_base  0x00008000
#define Addr_lib_base  0x00009000
#define Addr_app_base  0x0003f000 
#define Vector_boot_base 0//各部分起始扇号
#define Vector_logo_base  8
#define Vector_lib_base  9
#define Vector_app_base  63*/

/*********************************************************************************************************
** 函数名称: SendChar
** 功能描述: 发送一个字符
** 输 入: 字符
** 输 出: 无
** 全局变量: 无
** 调用模块: 无
**
** 作 者: 陈明计
** 日 期: 2004年2月26日
**-------------------------------------------------------------------------------------------------------
** 修改人: 
** 日 期: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
        void SendChar(uint8 data)
{
    while((U0LSR & 0x00000020) == 0);
    U0THR = data;
}
void replayack(uint8 comm)
{
    uint8 tmp;
    tmp=0;
    tmp=heard_comm^ack_comm^0x00^0x01^comm;
    SendChar(heard_comm);
    SendChar(ack_comm);
    SendChar(0);
    SendChar(1);
    SendChar(comm);
    SendChar(tmp);
}
/***************************************
**回复命令
****************************************/
void replycomm(uint8 *bytebuf,uint8 len)
{
    uint8 tmp;
    uint8 i;
    uint8 crc=0;
    for(i=1;i<len;i++)
    {   
        tmp = *bytebuf++;
        crc=crc^tmp;
        SendChar(tmp);
    }
    //发送异或和
    SendChar(crc);
}
/****************************************
**接收到命令,返回true,无命令则返回false,
**接收到命令但校验出错,返回flase,且串口回复出错回应
*****************************************/
        void __irq UART0_Exception(void)
{
    uint8 temp;
    
    temp = U0IIR;

    while ((U0LSR & 0x00000001) != 0)
    {
        Buf[End++] = U0RBR;
        if (End >= BUF_SIZE)
        {
            End = 0;
        }
    }
    VICVectAddr = 0;            // 通知中断控制器中断结束
}


uint8 GetComm(void)
{
    static uint8 findheard=FALSE;
    static uint16 commlen=0;
    uint16 Endsave,buflen,i;//暂存,以免处理过程中发生变动
    uint8 tmpchar,crc,re;
    re=FALSE;
    Endsave=End;
    buflen=(Endsave-Hard)&AllLeng; 
    //依据缓冲区内是否又数据来进行
    if(buflen!=0)//有数据需要进行处理
    {   
        tmpchar=Buf[Hard];
        if(findheard==FALSE)//没找到头,则这次就认为是头
        {
            //看是不是头
            if(tmpchar==heard_comm)//是头
            { 
                findheard=TRUE;
                commlen=0;
            }
            else//不是则跳下一个    
                Hard=(Hard+1)&AllLeng;
        }
        else//已经找到头了,需要进行数据接收
        {//--
            if(commlen!=0)//长度已找好
            {
                if(buflen>(commlen+4))//已接收完一帧
                {
                    //判断CRC是否正确
                    crc=0;
                    for(i=0;i<(commlen+4);i++)
                    {
                        crc=crc^Buf[(Hard+i)&AllLeng];
                    }
                    if(crc!=Buf[(Hard+commlen+4)&AllLeng])//校验不正确
                    {  
                        findheard=FALSE;
                        Hard=Endsave;//舍掉前面接收的部分
                        commlen=0;
                        replayack(FALSE);
                        //replayack(0x05);
                    }
                    else//接收完整一包数据
                    {   
                        for(i=0;i<(commlen+5);i++)
                            recivebuf[i]=Buf[(Hard+i)&AllLeng];                        
                        findheard=FALSE;
                        Hard=Endsave;//舍掉前面接收的部分
                        commlen=0;
                        re=TRUE;                          
                    }
                }
            }
            else//长度还没找好
            {
                if(buflen>4)
                {
                    commlen=Buf[(Hard+2)&AllLeng];
                    commlen=(commlen<<8)&0xff00;
                    commlen=commlen+Buf[(Hard+3)&AllLeng];
                    //--需要进行了
                    if(buflen>(commlen+4))//已接收完一帧
                    {
                        //判断CRC是否正确
                        crc=0;
                        for(i=0;i<(commlen+4);i++)
                        {
                            crc=crc^Buf[(Hard+i)&AllLeng];
                        }
                        if(crc!=Buf[(Hard+commlen+4)&AllLeng])//校验不正确
                        {  
                            findheard=FALSE;
                            Hard=Endsave;//舍掉前面接收的部分
                            commlen=0;
                            replayack(FALSE);
                            //replayack(0x06);
                            
                        }
                        else//接收完整一包数据
                        {   
                            for(i=0;i<(commlen+5);i++)
                                recivebuf[i]=Buf[(Hard+i)&AllLeng];                       
                            findheard=FALSE;
                            Hard=Endsave;//舍掉前面接收的部分
                            commlen=0;
                            re=TRUE;                           
                        }
                    }
                }
            }            
        }        
    }
    return re;
}
/*********************************************************************************************************
** 函数名称: CommInit
** 功能描述: 初始化通讯模块
** 输 入: 无
** 输 出: 无
** 全局变量: 无
** 调用模块: 无
**
** 作 者: 陈明计
** 日 期: 2004年2月26日
**38400
**-------------------------------------------------------------------------------------------------------
** 修改人: 
** 日 期: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
        void CommInit(void)
{
    uint16 Fdiv;
    
    PINSEL0 = (PINSEL0 & 0xfffffff0) | 0x05;    /* 选择管脚为UART0 */

    U0LCR = 0x80;                               /* 允许访问分频因子寄存器 */
    Fdiv = (Fpclk / 16) / bps;                  /* 设置波特率 */
    U0DLM = Fdiv / 256;							
	U0DLL = Fdiv % 256;						
    U0LCR = 0x03;                               /* 禁止访问分频因子寄存器 */
                                                /* 且设置为8,1,n */
	U0IER = 0x00;                               /* 禁止中断 */
    U0FCR = 0x87;                               /* 初始化FIFO */
    Hard = 0;
    End = 0;

    VICIntEnClr = 0xffffffff;

    VICVectAddr14 = (uint32)UART0_Exception;
    VICVectCntl14 = (0x20 | 0x06);
    VICIntEnable |= 1 << 6;
	U0IER = 0x01;                               /* 使能接收中断 */
}

/*********************************************************************************************************
**                            End Of File
********************************************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -