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

📄 at91loader.c

📁 ARM入门的好帮手.包含了从简单到相对较复杂的程序.
💻 C
📖 第 1 页 / 共 2 页
字号:
//* --------------------------------------------------------------------------
//*         ATMEL Microcontroller Software Support  -  ROUSSET  -
//* --------------------------------------------------------------------------
//* File Name            : main.c
//* Librarian            : Not applicable
//* Treatments           : DLL test for Windows
//* Translator           : VISUAL C++ compiler v6
//*
//* 1.0 03/03/98 SDC     : Creation
//* --------------------------------------------------------------------------
// init
/*----- Files to be included Definition -----*/
#include <windows.h>
#include <stdio.h>
#include "resource.h"
#include "global_def.h"
#include "ComUsart.h"
#include "ReadFile.h"
#include "flash_49BV.h"
#include "send.h"
#include "scheduler.h"

/*----- Types and Constants Definition -----*/
#define LOADER_TIME_OUT     1000    // Time event x/1000 seconde
// COM port
ComLoader ComUsed;
HINSTANCE  hInst;                // Application instance
HWND       Hwnd;                 // window handle
COMMCONFIG  commConfig;         // COM user parameters (dwSize = 0 when none)
Appli_GET_WIN   int_mes;

Application_State State;
send_state send_mes;
FileName workFile;
scheduler_data schedule;
FlashDef FlashTable;

extern void init_all(HWND hwnd);
//* Windows variable
unsigned char currentDirectory[256];
char text_message[SCREEN_SIZE];

extern void getinitfile(void);
//* --------------------------------------------------------------------------
//* Function Name        : displayError
//* Object               : Display a message box describing a Windows error
//* Input Parameters     : none
//* Output Parameters    : none
//* Functions called     : none
//* --------------------------------------------------------------------------
void displayError(void)
{
    LPVOID lpMsgBuf;
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
        NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,0,NULL);
    MessageBox( NULL,lpMsgBuf,"Error", MB_OK|MB_ICONINFORMATION );
    LocalFree( lpMsgBuf );
}
void showReturMessage(int value,char * message)
{
    if (value == TRUE)
    {
        MessageBox( NULL,message,"OK", MB_OK );
    }else if (send_mes.error.current_error == ACK_REPLY)
    {
        MessageBox( NULL,message,"ACK", MB_OK );
    }else if (send_mes.error.current_error == NACK_REPLY)
    {
        MessageBox( NULL,message,"NAK", MB_OK );
    }else if (send_mes.error.current_error == ERROR_REPLY)
    {
        MessageBox( NULL,message,"ERROR REPLY", MB_OK );
    }else if (send_mes.error.current_error == TIME_OUT_REPLY)
    {
        MessageBox( NULL,message,"TIME OUT", MB_OK );
    }else if (send_mes.error.current_error == UNKNOWN_REPLY)
    {
        MessageBox( NULL,message,"UNKNOWN RECEIVE", MB_OK );
    }else
    {
        MessageBox( NULL,message,"UNKNOWN RETURN", MB_OK );
    }

}

//* --------------------------------------------------------------------------
//* Function Name        : timer
//* Object               : Display in main windopws
//* Input Parameters     : hwnd_param - Windows Handle
//* Output Parameters    : none
//* Functions called     : Windows : BeginPaint,GetClientRect,SetBkMode
//*                                  SetTextColor,DrawText,EndPaint
//*                        C : memset
//* --------------------------------------------------------------------------
void Write_message(HWND hwnd_param)
{
    PAINTSTRUCT lpPaints;
    HDC hDC;
    RECT lpClient;
    int mode;
    int len=0;
    COLORREF TextColor;
    char Blanck[SCREEN_SIZE];
    if ( int_mes.timer_state != schedule.state)
    {
        InvalidateRect(hwnd_param,NULL,FALSE); //
        int_mes.timer_state = schedule.state;
        //* Erase screen
        memset(Blanck,' ',sizeof(Blanck));
        memset(text_message,' ',sizeof(text_message));

        hDC = BeginPaint(hwnd_param,&lpPaints);
        // client area
        GetClientRect(hwnd_param,&lpClient);
        mode = SetBkMode(hDC,OPAQUE);
        TextColor = SetTextColor(hDC,GetSysColor(COLOR_WINDOWTEXT));
        // erase the dialogue windows
        DrawText(hDC,Blanck,128,&lpClient, DT_CENTER|DT_VCENTER|DT_SINGLELINE);

        printSheduleState ( &text_message[len],&len);
        printSheduleInfo( &text_message[0],&len);

        // write text in dialogue windows
        DrawText(hDC,text_message,-1,&lpClient,  DT_CENTER|DT_VCENTER|DT_SINGLELINE);
        //* RTestore the Windows Flag
        SetTextColor(hDC,TextColor);
        SetBkMode(hDC,mode);
        EndPaint(hwnd_param,&lpPaints);
    }

}
//* --------------------------------------------------------------------------
//* Function Name        : getFileValue
//* Object               : Get file value and write if OK
//* Input Parameters     : Dialogue box IDD_DIALOG1 ,ComUsed
//* Output Parameters    : None
//* --------------------------------------------------------------------------
BOOL CALLBACK getFileValue(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    char chfilePaddingVal[20];
    char chfilePaddinglen[20];
    char filePaddingVal=0x00;
    long filePaddinglen=0;
    BOOL fileIncrase=FALSE;
   // Messages
    switch(message)
        {
        case WM_INITDIALOG:
            fileIncrase = FALSE;
            sprintf(chfilePaddingVal,"%X",filePaddingVal);
            SendDlgItemMessage(hwndDlg, IDC_EDIT1, WM_SETTEXT, 0,(LPARAM)chfilePaddingVal);
            sprintf(chfilePaddinglen,"%lX",filePaddinglen);
            SendDlgItemMessage(hwndDlg, IDC_EDIT2, WM_SETTEXT, 0,(LPARAM)chfilePaddinglen);
            break;
        case WM_CLOSE:
            EndDialog(hwndDlg,FALSE);
            break;

        case WM_COMMAND:
            if (wParam == IDOK)
            {
                SendDlgItemMessage(hwndDlg, IDC_EDIT1, WM_GETTEXT, 4,(LPARAM)chfilePaddingVal);
                sscanf(chfilePaddingVal,"%d",&filePaddingVal);
                SendDlgItemMessage(hwndDlg, IDC_EDIT2, WM_GETTEXT, 11,(LPARAM)chfilePaddinglen);
                sscanf(chfilePaddinglen,"%lX",&filePaddinglen);
                //* write binary file
                writeBinaryFile(filePaddingVal,filePaddinglen,fileIncrase);
                EndDialog(hwndDlg,TRUE);
            }
            if (wParam == IDC_RADIO1)
            {
                if(fileIncrase == FALSE) fileIncrase = TRUE;
                else                     fileIncrase = FALSE;
            }
            if (wParam == IDCANCEL)
            {
                EndDialog(hwndDlg,FALSE);
            }
            break;

        default:
            return FALSE;
        }
    return 0;
}

//* --------------------------------------------------------------------------
//* Function Name        : get_address
//* Object               : Get file value
//* Input Parameters     : Dialogue box IDD_DIALOG1 ,ComUsed
//* Output Parameters    : None
//* Functions called     : Windows : SendDlgItemMessage,EndDialog
//*
//*                        Application : displayError
//* --------------------------------------------------------------------------
BOOL CALLBACK get_address(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    char Val[40];

   // Messages
    switch(message)
        {
        case WM_INITDIALOG:
            sprintf(Val,"0x%X",int_mes.address_val);
            SendDlgItemMessage(hwndDlg, IDC_EDIT1, WM_SETTEXT, 0,(LPARAM)Val);
            SendDlgItemMessage(hwndDlg, IDC_CONFIRM, WM_SETTEXT, 0,(LPARAM)Val);
            break;
        case WM_CLOSE:
            EndDialog(hwndDlg,FALSE);
            break;
        case WM_COMMAND:
            if (wParam == IDOK)
            {
                SendDlgItemMessage(hwndDlg, IDC_EDIT1, WM_GETTEXT, 20,(LPARAM)Val);
                sscanf(Val,"%lx",&int_mes.address_val);
                EndDialog(hwndDlg,TRUE);
            break;
            }
            if (wParam == IDC_CHECK1)
            {
                SendDlgItemMessage(hwndDlg, IDC_EDIT1, WM_GETTEXT, 20,(LPARAM)Val);
                sscanf(Val,"%lx",&int_mes.address_val);
                sprintf(Val,"0x%X",int_mes.address_val);
                SendDlgItemMessage(hwndDlg, IDC_CONFIRM, WM_SETTEXT, 0,(LPARAM)Val);
            }
            if (wParam == IDCANCEL)
            {
                EndDialog(hwndDlg,FALSE);
            }
            break;
        default:
            return FALSE;
        }
    return 0;
}
//* --------------------------------------------------------------------------
//* Function Name        : get_address
//* Object               : Get file value
//* Input Parameters     : Dialogue box IDD_DIALOG1 ,ComUsed
//* Output Parameters    : None
//* Functions called     : Windows : SendDlgItemMessage,EndDialog
//*
//*                        Application : displayError
//* --------------------------------------------------------------------------
BOOL CALLBACK dialogue_state(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    int len;
   // Messages
    switch(message)
        {
        case WM_INITDIALOG:
            //IDC_FLASH
            len =0;
            printFlashInfo ( &text_message[0],&len,&FlashTable);
            SendDlgItemMessage(hwndDlg, IDC_FLASH, WM_SETTEXT, 0,(LPARAM)text_message);
            len =0;
            printComInfo ( &text_message[0],&len);
            SendDlgItemMessage(hwndDlg, IDC_COM, WM_SETTEXT, 0,(LPARAM)text_message);
            len =0;
            printFileInfo( &text_message[0],&len);
            SendDlgItemMessage(hwndDlg, IDC_FILE, WM_SETTEXT, 0,(LPARAM)text_message);
            len =0;
            printSheduleState ( &text_message[0],&len);
            if (State.debug) sprintf(&text_message[len]," Debug: enable");
            SendDlgItemMessage(hwndDlg, IDC_STATE, WM_SETTEXT, 0,(LPARAM)text_message);

            break;
        case WM_CLOSE:
            EndDialog(hwndDlg,FALSE);
            break;
        case WM_COMMAND:
            if (wParam == IDOK)
            {
                EndDialog(hwndDlg,TRUE);
            break;
            }
            if (wParam == IDCANCEL)
            {
                EndDialog(hwndDlg,FALSE);
            }
            break;
        default:
            return FALSE;
        }
    return 0;
}
//* --------------------------------------------------------------------------
//* Function Name        : get_address
//* Object               : Get file value
//* Input Parameters     : Dialogue box IDD_DIALOG1 ,ComUsed
//* Output Parameters    : None
//* Functions called     : Windows : SendDlgItemMessage,EndDialog
//*
//*                        Application : displayError
//* --------------------------------------------------------------------------
BOOL CALLBACK verify_DialogBox(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
   // Messages
    long checksum =0;
    BYTE    *bin;
    unsigned int   add;
    unsigned int   len;
    unsigned int   check;
    switch(message)
        {
        case WM_INITDIALOG:
            //IDC_FLASH
            sprintf (text_message,"0x%08lx",FlashTable.load_add);
            SendDlgItemMessage(hwndDlg, IDC_START_ADDRESS, WM_SETTEXT, 0,(LPARAM)text_message);
            sprintf (text_message,"0x%08lx",workFile.size);
            SendDlgItemMessage(hwndDlg, IDC_LEN, WM_SETTEXT, 0,(LPARAM)text_message);
            if ( openBinFile())
            {
                // Allocate a buffer (return if error)
                workFile.hBin=GlobalAlloc(GMEM_MOVEABLE,workFile.size);
                //* Fill the buffer (return NULL if error)
                bin=GlobalLock(workFile.hBin);
                if (readDataFile(bin,workFile.size)==workFile.size)
                {
                   checksum= verifyChecksum(bin,workFile.size);
                }
                GlobalUnlock(workFile.hBin);
                GlobalFree(workFile.hBin);
            }
            sprintf (text_message,"0x%08lx",checksum);
            SendDlgItemMessage(hwndDlg, IDC_CHECKSUM, WM_SETTEXT, 0,(LPARAM)text_message);
            break;
        case WM_CLOSE:
            EndDialog(hwndDlg,FALSE);
            break;
        case WM_COMMAND:
            if (wParam == IDOK)
            {
                SendDlgItemMessage(hwndDlg, IDC_START_ADDRESS, WM_GETTEXT, 11,(LPARAM)text_message);
                sscanf(text_message,"%lx",&add);
                SendDlgItemMessage(hwndDlg, IDC_LEN, WM_GETTEXT, 11,(LPARAM)text_message);
                sscanf(text_message,"%lX",&len);
                SendDlgItemMessage(hwndDlg, IDC_CHECKSUM, WM_GETTEXT, 11,(LPARAM)text_message);
                sscanf(text_message,"%lX",&check);
                //* send verify
                if (send_verify (add,check,len))
                {
                    MessageBox( NULL,"Verify","OK", MB_OK );

                }
                else
                {
                    MessageBox( NULL,"Verify","ERROR", MB_OK );
                }
            break;
            }
            if (wParam == IDCANCEL)
            {
                EndDialog(hwndDlg,FALSE);
            }
            break;
        default:
            return FALSE;
        }
    return 0;
}

//* --------------------------------------------------------------------------
//* Function Name        : help
//* Object               : show the Help dialogue box
//* Input Parameters     : Dialogue box IDHELP
//* Output Parameters    : None
//* Functions called     : Windows : EndDialog
//* --------------------------------------------------------------------------
BOOL CALLBACK help_loader(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{

    switch(message)
        {
        case WM_COMMAND:
        case WM_CLOSE:
            EndDialog(hwndDlg,FALSE);
        default:
            return FALSE;
        }
    return 0;
}

//* --------------------------------------------------------------------------
//* Function Name        : TargetInfo
//* Object               : show the target
//* Input Parameters     : Dialogue box
//* Output Parameters    : None
//* Functions called     : Windows : EndDialog
//* --------------------------------------------------------------------------
BOOL CALLBACK TargetInfo(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    int len ;
    switch(message)
        {
        case WM_INITDIALOG:
            if (send_mes.chip == 0)
            {
                sprintf (text_message,"Unknown");
            }
            else
            {
                len = sprintf (text_message,"Version:%04lX  manufacturer:0x%04lX, chip:0x%04lX\n",
                    send_mes.version ,send_mes.manufacturer,send_mes.chip);
                len += sprintf (&text_message[len],"type: 0x%04lX, Addresse: 0x%08lX, CD:%d\n",
                    send_mes.type  ,send_mes.addr, send_mes.cd);

            }
            SendDlgItemMessage(hwndDlg, IDC_VALUE, WM_SETTEXT, 0,(LPARAM)text_message);
        break;
        case WM_COMMAND:
        case WM_CLOSE:
            EndDialog(hwndDlg,FALSE);
        default:

⌨️ 快捷键说明

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