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

📄 main.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           : main.c
//* Object              : Test the Flash and Ram presence
//*                       This project can be run on all EBxx Targes
//*
//* 1.0 01/11/00 JPP    : Creation
//*----------------------------------------------------------------------------

//* --------------------------- include file ----------------------------------
#include <stdio.h>

//* Periph description
#include "periph/special_function/lib_sf.h"     //* for chip id

//* Driver description
#include "drivers/com/com.h"                    //* Communication driver
#include "drivers/time_rtc/time_rtc.h"          //* rtc description
#include "usart_menu.h"                         //* Communication driver
#include "get_usart.h"                          //* Usart descriptor
#include "get_ebi.h"                            //* Ebi definition
#include "..\xmodem\xmodem.h"                   //* Communication driver

//* --------------------------- Constant definition ---------------------------

#define BAUD_RATE       38400

//* ------------------------------  extern function  --------------------------

extern void get_chip_id(const char *message);
extern void monitor_menu(void);
extern void flash_menu(void);


//* ------------------------------  extern global data  -----------------------

//* ------------------------------  Local variable ----------------------------


static const char atmel_header[]=
{
"\n\r  *** ATMEL Flash TEST ***\n\r"
"Copyright (C) 2000 ATMEL Corporations Version: 1.0\n\r"
};

static const char main_menu[]=
{
"\n\r  COMMAND MENU: Base\n\r"
"  0)  Info\n\r"
"  1)  Get global value\n\r"
"  2)  Flash monitor\n\r"
"  3)  Set MEC EBI \n\r"
"  6)  transmit file (Xmodem)\n\r"
"  7)  receive file (Xmodem)\n\r"
"  8)  Usart Monitor\n\r"
"  9)  Memory Monitor\n\r"
"Enter command to execute: "
};
static const u_int mec_ebi[]=
{
    0x03002529,         //* csr 2  Flash AT49BV1604 2 M byte
    0x04002429,         //* csr 3  Flash AT49BV8011 1 M byte
    0x05002021,         //* csr 4  RAM 1 M byte
    0x05102021          //* csr 4  RAM 1 M byte
};

//* Global ebi definition
ebi_def ebi_map_conf[NB_CHIP_SELEC] ;

int             mcki;
char            message[512];
ComDesc         COM;
ComDesc         COM1;
ComDesc 		*Xmodem_COM;

int             csr,csr_val;
int             base_address;
FlashAt49BVDef  *flash;
u_char          * dump_address= (u_char*)0x2000000;
u_int           dump_size   = 0x40000;
int             read_get_val;
//*----------------------------------------------------------------------------
//* Function Name       : ram_write
//* Object              : write a Xmodem frame in RAM
//* Input Parameters    : <val> value to update
//* Output Parameters   : None
//*----------------------------------------------------------------------------
int ram_write(u_char *data , int size)
{
    u_int count;
    for ( count=0 ; count < size ; count++)
    {
         *dump_address++= * data++;
    }
    return TRUE;
}

//*----------------------------------------------------------------------------
//* Function Name       : get_csr
//* Object              : get and print the CSR description
//* Input Parameters    : <csr_id>
//* Output Parameters   : none
//*----------------------------------------------------------------------------
void get_csr (unsigned int csr_id)
{
    get_ebi_csr(csr_id,(char*)message);
    at91_print(&COM,message);
    get_ebi_mem_type(csr_id, (char*)message, &ebi_map_conf[csr_id]);
    at91_print(&COM,message);
    at91_print_crlf(&COM);
}
//*----------------------------------------------------------------------------
//* Function Name       : get_val
//* Object              : get a integer or no update the value
//* Input Parameters    : <val> value to update
//* Output Parameters   : value updated
//*----------------------------------------------------------------------------
int  get_val( int val ,char * type )
{

    char buff[20];
    //* echo
    read_get_val = sprintf(&buff[0],type,val);
    sprintf(&buff[read_get_val],"\n\r");

    at91_print(&COM,buff);
    //* scan new value
    read_get_val = 0xFF;
    at91_scanf(&COM,type,&read_get_val);
    //* update if change
    if (   read_get_val != 0xFF ) val = read_get_val;
    at91_print_crlf(&COM);
    return val;
}
//*----------------------------------------------------------------------------
//* Function Name       : Get_Command
//* Object              : Get command val
//* Input Parameters    : None
//* Output Parameters   : int : Command num
//*----------------------------------------------------------------------------
int Get_Command(const char * menu)
{
  int command;
  at91_print(&COM,(char *)menu);
  at91_scanf(&COM,"%d", &command);
  at91_print_crlf(&COM);
  return command;
}
//*----------------------------------------------------------------------------
//* Function Name       : menu
//* Object              : check general menu
//* Input Parameters    : None
//* Output Parameters   : None
//*----------------------------------------------------------------------------
void menu(void)
{
int     command = 0;
int     count;
//init global data
    csr=6 ;
    csr_val= 0x60000000;
    EBI_BASE->EBI_CSR[csr]=csr_val;
    dump_address= (u_char*) 0x5000000;
    dump_size=0x100000;
for(;;)
  {
    command = Get_Command(main_menu);
    switch(command)
    {
     case 0: //* print Info
            //* print chip ID
            get_chip_id((char *)&message);
            at91_print(&COM,message);
            at91_print_crlf(&COM);
            //* print Chip Select info
            for (count=0 ;count<NB_CHIP_SELEC;count++)
            {
                get_csr(count);
            }
            for (count=0 ;count<NB_CHIP_SELEC;count++)
            {
                sprintf(message,"CSR %d Type %d Address:0x%08X size:0x%08X (%5d K b) %s\n\r",
                count,ebi_map_conf[count].type,ebi_map_conf[count].start_address,ebi_map_conf[count].size,(int)(ebi_map_conf[count].size /1024),
                ebi_map_conf[count].name);
                at91_print(&COM,message);
            }
            sprintf(message,"Echo Xmoden start: 0x%08X,len (send file): 0x%08X %d Kb\n\r",(u_int)dump_address,dump_size,dump_size/1024);
            at91_print(&COM,message);

          break;

     case 1: /* Get global value */
            //* Chip select for Flash
            at91_print(&COM,"Get the CSR num (0 to 7 ):");
            csr = get_val(csr,"%1d");
            //* New Chip select value
            at91_print(&COM,"Get the CSR value :");
            csr_val = get_val(csr_val,"%08X");
            //* work Base address
            //* Get load/store adress and sise
            at91_print(&COM,"size  \n\r");
            at91_print(&COM,"Xmodem Start work address:");
            dump_address = (u_char *) get_val((int )dump_address,"%08X");
            at91_print(&COM,"send len:");
            dump_size = get_val(dump_size,"%08X");
            //* Input data echo
            sprintf(message,"Echo CSR %d value: 0x%08X, \n\r", csr,csr_val);
            at91_print(&COM,message);
            //* set the new Chip select
            EBI_BASE->EBI_CSR[csr]=csr_val;
            //* Get CSR value
            get_csr(csr);
            sprintf(message,"Echo Xmoden start: 0x%08X,len (send file): 0x%08X %d Kb\n\r",(u_int)dump_address,dump_size,dump_size/1024);
            at91_print(&COM,message);
            break;

     case 2: //* Flash Erase
            flash_menu();
     case 3: //* Set EBI mec
            for (count=0; count < 4 ;count++)
            {
                EBI_BASE->EBI_CSR[count+2]=mec_ebi[count];
                sprintf(message,"EBI CSR: %d value %08X\n\r",count+2,mec_ebi[count]);
                at91_print(&COM,message);
            }
            break;


     case 6: //* Menu file transfert
            at91_print(&COM,"Xmodem Wait Syncrh 'C'(and send frame)\n\r");
             if ( xmodem_transmit(dump_address,dump_size))
            	at91_print(&COM,"Transmission ended OK\n\r");
            else
            	at91_print(&COM,"Transmission ended ERROR\n\r");
          break;
     case 7: //* Menu file transfert
            at91_print(&COM,"Xmodem wait frame (Send Syncrh 'C')\n\r");
            count =  get_ebi_memory_csr((char*)dump_address, &ebi_map_conf[0]);
            switch (ebi_map_conf[count].type)
            {
                case EBI_MEMORY_TYPE_RAM:
                    at91_print(&COM,"write in RAM\n\r");
                xmodem_receive( &ram_write);
                break;
                case EBI_MEMORY_TYPE_FLASH_AT49:
                    sprintf(message,"erase and write in Flash %s \n\r",ebi_map_conf[count].name);
                    at91_print(&COM,message);
                    flash_at49_init_write( (flash_word *)ebi_map_conf[count].start_address,
                                           (flash_word *)dump_address,
                                           ebi_map_conf[count].flash_type);
                    if (!xmodem_receive( &flash_at49_erase_write_block))
                        at91_print(&COM,"\n\rError \n\r");
                    else
                    	at91_print(&COM," \n\r OK\n\r");
                break;

                case EBI_MEMORY_TYPE_FLASH_AT29:
                    sprintf(message,"erase and write in Flash %s \n\r",ebi_map_conf[count].name);
                    at91_print(&COM,message);
                    flash_at29_init_write( (flash_word *)ebi_map_conf[count].start_address,
                                           (flash_word *)dump_address,
                                           ebi_map_conf[count].flash_type);
                    if (! xmodem_receive( &flash_at29_write_block))
                        at91_print(&COM,"\n\rError \n\r");
                    else if (! flash_at29_purge_write_block())
                        at91_print(&COM,"\n\rError on last block\n\r");
                    else at91_print(&COM," \n\r OK\n\r");
                    break;
                default:
                    at91_print(&COM,"Unknown memory type\n\r");
            }
            at91_print(&COM,"Reception ended \n\r");
          break;
     case 8://* dump memory
          usart_menu(mcki);
          break;
     case 9://* dump memory
          monitor_menu();
          break;

    default:         /*  invalid command  */
        at91_print(&COM,"Invalid command entered. Please enter again!\n\r");
    }
  }
}

//*----------------------------------------------------------------------------
//* Function Name       : main
//* Object              : Main function of Polling USART 0
//* Input Parameters    : none
//* Output Parameters   : True
//*----------------------------------------------------------------------------
int main( void )
//* Begin

{
    u_short cd_baud,error;
    u_int  count;


    //* open terminal for console
    COM.usart= get_usart0_desc ( &mcki );
    COM1.usart= get_usart1_desc ();
	//* Set the Xmodem transaction on USART 0 COM
	Xmodem_COM = &COM;

    //* Check the Baud rate
    cd_baud = at91_baud_com(mcki, BAUD_RATE);
    error = at91_error_baud_com(mcki, BAUD_RATE);

    //* Open com
    at91_open_com(&COM,(COM_8_BIT|COM_PAR_NONE|COM_NBSTOP_1|COM_FLOW_CONTROL_NONE), cd_baud);
    at91_open_com(&COM1,(COM_8_BIT|COM_PAR_NONE|COM_NBSTOP_1|COM_FLOW_CONTROL_NONE), cd_baud);

    //* print header
    at91_print_frame(&COM,(char *)atmel_header,sizeof(atmel_header));

    //* Print com state Status
    sprintf(message," \n\r\n\rAT91 Com at %d, 8 bits, parity none, one stop, error = %02d %% \n\r",BAUD_RATE,error);
    at91_print(&COM,message);

    //* print chip ID
    get_chip_id((char *)&message);
    at91_print(&COM,message);
    at91_print_crlf(&COM);
    //* Get chip capabilities
    for (count=0 ;count<NB_CHIP_SELEC;count++)
    {
        get_csr(count);
    }
    // print Menu
    menu();

    return(TRUE);
//* End
}

⌨️ 快捷键说明

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