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

📄 send.c

📁 ARM入门的好帮手.包含了从简单到相对较复杂的程序.
💻 C
字号:
//*-----------------------------------------------------------------------------
//*      ATMEL Microcontroller Software Support  -  ROUSSET  -
//*-----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*-----------------------------------------------------------------------------
//* File Name               : send.c
//* Object                  : Read Binary file
//*
//* 1.0 02/05/99 JPP        : Creation
//* 1;1 04/07/00 JPP        : Clean
//*-----------------------------------------------------------------------------

/*----- Files to be included Definition -----*/

#include "ComUsart.h"
#include "send.h"

/*-------- Global variable definition -------*/
extern send_state send_mes;                 // Send information
extern ComLoader ComUsed;                   // COM description
/*------ Internal fonction definition ------*/

//* --------------------------------------------------------------------------
//* Function Name        : send_frame
//* Object               : Send one byte command for ACK,NACK,LEN_SYNC
//* Input Parameters     : byte value
//* Output Parameters    : TRUE, FALSE
//* --------------------------------------------------------------------------
static int send_frame(BYTE* buffer,int len)
{
    if (!writeCom( buffer,len))
    {
        return FALSE;
    }
    //* Get the reply

    //* Read <ACK>
    if (get_Byte_status() ==ACK_REPLY)
            return(TRUE);
    else
            return(FALSE);
}
//* --------------------------------------------------------------------------
//* Function Name        : checksum
//* Object               : copy from dedst to src and Calculate the checksum
//*                        and additing the checksum at the end
//* Input Parameters     : <dest> pointer to destination
//*                        <src> pointer to source
//*                        <size> size for checsum
//* Output Parameters    : none
//* --------------------------------------------------------------------------
static void Checksum(unsigned char* dest,unsigned char* src,int size)
{
    int cmpt;
    unsigned char checksum =0;
    for(cmpt=0 ; cmpt<size ; cmpt++)
    {
        dest[cmpt]=src[cmpt];
        checksum += src[cmpt];
    }
    dest[cmpt]=checksum;
}

//* --------------------------------------------------------------------------
//* Function Name        : verifyChecksum
//* Object               : calculate a checksum
//* Input Parameters     : <buffer> address to start checksum
//*                        <size> len of checksum
//* Output Parameters    : checksum value
//* --------------------------------------------------------------------------
unsigned int verifyChecksum(unsigned char* buffer,int size)
{
    int cmpt;
    int checksum ;
    for(cmpt=0 ,checksum =0; cmpt<size ; cmpt++)
    {
        checksum += buffer[cmpt];
    }
    return checksum;
}
//* --------------------------------------------------------------------------
//* Function Name        : send_Byte
//* Object               : Send one byte command for ACK,NACK,LEN_SYNC
//* Input Parameters     : <value>
//* Output Parameters    : TRUE or FALSE
//* --------------------------------------------------------------------------
int send_Byte ( BYTE value)
{
    //* send char
    if (writeCom(&value,1))
        return TRUE;
    else
        return FALSE;
}
//* --------------------------------------------------------------------------
//* Function Name        : get_Byte_status
//* Object               : Get one byte command and check
//* Input Parameters     : none
//* Output Parameters    : ACK_REPLY,NACK_REPLY,ERROR_REPLY,UNKNOWN_REPLY
//*                        write in Global send_mes
//* --------------------------------------------------------------------------
int get_Byte_status( void)
{
    BYTE bRead;
    int status;
    //* Read char
    status =readCom(&bRead,sizeof(bRead));
    if (status == READ_OK)
    {
        switch(bRead)
        {
            case ACK_CMD:
                status = ACK_REPLY;
            break;
            case NACK_CMD:
                send_mes.error.Nak_Reply++;
                status = NACK_REPLY;
            break;
            case ERROR_REPLY:
                readCom(&bRead,sizeof(bRead));
                status = ERROR_REPLY;
                send_mes.error.Error_Reply++;
            break;
            default:
               status = UNKNOWN_REPLY;
               send_mes.error.Unknown_Reply++;
            break;
        }//* end switch
    }
    //* Memorize last status
    send_mes.error.current_error=status;
    return status;
}

//* --------------------------------------------------------------------------
//* Function Name        : send_synchro
//* Object               : synchronization step
//* Input Parameters     : none
//* Output Parameters    : TRUE or FALSE
//* --------------------------------------------------------------------------
int send_synchro ( void )
{
    char buffer[MAX_MESS_LEN];
    send_ATS *message;
    int status;

    status =FALSE;

    //* send <SYNC>
    if (!send_Byte(SYNCRO_CMD))
    {
        return FALSE;
    }

    memset(buffer,0xFF,sizeof(buffer));

    //* Read <ATS>
    if (readCom( buffer,LEN_ATS)==READ_OK)
    {
        message=(send_ATS *)buffer;
        //* Read ATS

        if (message->command==ATS_CMD)
        {
            //* send ACK
            send_Byte(ACK_CMD);

            //* Read values
            send_mes.chip           = message->chip;
            send_mes.manufacturer   = message->manufacturer;
            send_mes.type           = message->type;
            send_mes.cd             = message->cd;
            send_mes.version        = message->version;
            send_mes.addr           = message->addr;

            //get <ACK> Repply
            get_Byte_status(); // only for the first synchro
            // set the global status
            status= TRUE;
        }///* end message type

    }// end readCom
    if (status ==FALSE ) send_mes.error.current_error=TIME_OUT_REPLY;
    return status ;
}

//* --------------------------------------------------------------------------
//* Function Name        : send_speed
//* Object               : Speed step ComUsed.cd
//* Input Parameters     : CD target value for speed
//* Output Parameters    : TRUE or FALSE
//* --------------------------------------------------------------------------
int send_speed (WORD cd )
{
    char buffer[MAX_MESS_LEN];
    send_SPEED *message;
    int status;
    int cmpt;
    //* make the message
    message = (send_SPEED *) buffer;
    message->command= SPEED_CMD;
    message->cd = cd;
    if (!writeCom( buffer,LEN_SPEED))
    {
        return FALSE;
    }
    //* Change Speed
    if (ComUsed.name[COM_BAUD_CURRENT]!= ComUsed.baud[COM_BAUD_LOAD])
            openCom(&ComUsed.name[0],ComUsed.baud[1]);
    //* Read <ACK>or NAK
    for ( cmpt =0; cmpt < 10; cmpt++)
    {
        status = get_Byte_status();
        if (status == ACK_REPLY)
        {
            send_Byte (ACK_CMD);
            // purge PC ACK
            while ( get_Byte_status() != TIME_OUT_REPLY);
            return TRUE;
        }
    }
    return FALSE;
}

//* --------------------------------------------------------------------------
//* Function Name        : send_erase
//* Object               : Send ERASE command
//* Input Parameters     : Sector address to erase
//* Output Parameters    : TRUE or FALSE
//* --------------------------------------------------------------------------
int send_erase (unsigned int add )
{
    char buffer[MAX_MESS_LEN];
    send_ERASE *message;

    //* make the message
    message = (send_ERASE *) buffer;
    message->command= ERASE_CMD;
    message->add = add;
    //* Send message
   return send_frame(buffer,LEN_ERASE);
}

//* --------------------------------------------------------------------------
//* Function Name        : send_write
//* Object               : Send Write command and wait the ACK
//* Input Parameters     : <len> next data buffer
//*                        <add> first address for data frame
//* Output Parameters    : TRUE or FALSE by send_frame
//* --------------------------------------------------------------------------
int send_write ( BYTE len, unsigned int add )
{
    char buffer[MAX_MESS_LEN];
    send_WRITE *message;
    //* make the message
    message = (send_WRITE *) buffer;
    message->command= WRITE_CMD;
    message->len = len;
    message->add = add;
    //* Send message
    return(send_frame(buffer,LEN_WRITE));
}
//* --------------------------------------------------------------------------
//* Function Name        : send_read
//* Object               : Write step
//* Input Parameters     : <&buffer> pt of output data
//*                        <len> next data buffer
//*                        <add> first address for data frame
//* Output Parameters    : TRUE or FALSE
//* --------------------------------------------------------------------------
int send_read ( BYTE * buffer,BYTE len,unsigned int add )
{
    send_READ *message;

    //* make the message
    message = (send_READ *) buffer;
    message->command= READ_CMD;
    message->len = len;
    message->add = add;
    //* Send message
    writeCom(buffer,LEN_READ);
    if (readCom(buffer,len+2)== READ_OK)
        return(TRUE);
    else
        return(FALSE);
}

//* --------------------------------------------------------------------------
//* Function Name        : send_data
//* Object               : Data step
//* Input Parameters     : none
//* Output Parameters    : TRUE or FALSE
//* --------------------------------------------------------------------------
int send_data ( unsigned char *message ,int len)
{
    unsigned char buffer[MAX_MESS_LEN];
    // read data file
    buffer[0]=DATA_CMD;                 //* Command
    // calculate cheksum
    Checksum(&buffer[1],message,len);

    return send_frame(buffer,len+2);
}
//* --------------------------------------------------------------------------
//* Function Name        : send_verify
//* Object               : Data step
//* Input Parameters     : <add> Start target address for verify
//*                        <checsum> result checksum value
//*                        <len> size of checksum block
//* Output Parameters    : TRUE or FALSE
//* --------------------------------------------------------------------------
int send_verify ( unsigned int add, unsigned int checksum,unsigned int len )
{
    unsigned char buffer[MAX_MESS_LEN];
    send_VERIFY *message;
    //* make the message
    message = (send_VERIFY *) buffer;
    //* Set the header
    message->command= VERYFY_CMD;       //* Command
    message->add = add;                 //* Start address read
    message->len = len;                 //* checksum blocks size
    message->checksum = checksum ;      //* Checksum
    //* Send message
    return send_frame(buffer,LEN_VERIFY);
}
//* --------------------------------------------------------------------------
//* Function Name        : send_upload
//* Object               : Upload step
//* Input Parameters     : <len> next data buffer
//*                        <add> first address for data frame
//* Output Parameters    : TRUE or FALSE
//* --------------------------------------------------------------------------
int send_upload ( BYTE len,unsigned int add )
{
    char buffer[MAX_MESS_LEN];
    send_UPLOAD *message;
    //* make the message
    message = (send_UPLOAD *) buffer;
    //* Set the header
    message->command= UPLOAD_CMD;       //* Command
    message->len = len+2;               //* Next data size
    message->add = add;                 //* Start address upload
    //* Send message
    return (send_frame(buffer,LEN_UPLOAD));
}

//* End of file

⌨️ 快捷键说明

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