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

📄 common.c

📁 STR711 IAP底层驱动程序
💻 C
字号:
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name          : common.c
* Author             : MCD Application Team
* Date First Issued  : 10/25/2004
* Description        : This file provides all the common functions.
********************************************************************************
* History:
*  02/01/2006 : IAP Version 2.0
*  11/24/2004 : IAP Version 1.0
********************************************************************************
THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH
CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A
RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH
SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN
IN CONNECTION WITH THEIR PRODUCTS.
********************************************************************************/

#include "common.h"

s32 gimagesize = 0;
typedef void (*pointer)(void );
pointer jump_function;
/*******************************************************************************
* Function Name  : Int2Str
* Description    : Convert an Integer to a srting
* Input 1        : The string
* Input 2        : The intger to be converted
* Return         : None
*******************************************************************************/
void Int2Str(char* str,s32 intnum)
{
  u32 i,Div=1000000000,j=0,Status=0;
  for(i=0;i<10;i++)
  {
    str[j++]=(intnum/Div) + 48;
    intnum= intnum%Div;
    Div/=10;
    if ((str[j-1]=='0')&(Status==0))
      j=0;
    else
      Status++;
  }
}
/*******************************************************************************
* Function Name  : Str2Int
* Description    : Convert a srting to an integer
* Input 1        : The string to be converted
* Input 2        : The intger value
* Return         : 1 -- Correct
                 : 0 -- Error
*******************************************************************************/
u32 Str2Int(char *inputstr,s32 *intnum)
{
	u32 i,res;
	u32 val = 0;

	if (inputstr[0] == '0' && (inputstr[1] == 'x'||inputstr[1] == 'X'))
	{	
		if (inputstr[2] == '\0')
		{
			return 0;
		}
		for (i=2;i<11;i++)
		{
			if (inputstr[i] == '\0')
			{
				*intnum = val;
				res = 1; /* return 1; */
				break;
			}
			if (ISVALIDHEX(inputstr[i]))
			{
				val = (val << 4) + CONVERTHEX(inputstr[i]);
			}	
			else
			{
				/* return 0; Invalid input */
				res = 0;
				break;
			}	
		}	
		
		if (i>=11) res = 0;	/* over 8 digit hex --invalid */
	}
	else /* max 10-digit decimal input */
	{
		for (i=0;i<11;i++)
		{
			if (inputstr[i] == '\0')
			{
				*intnum = val;
				/* return 1; */
				res =1;
				break;
			}
			else if ((inputstr[i] == 'k' ||inputstr[i] == 'K') && (i>0))
			{
				val = val << 10;
				*intnum = val;
				res = 1;
				break;
			}
			else if ((inputstr[i] == 'm' ||inputstr[i] == 'M') && (i>0))
			{	
				
				val = val << 20;
				*intnum = val;
				res = 1;
				break;
			}
			else if (ISVALIDDEC(inputstr[i]))
				val = val*10 + CONVERTDEC(inputstr[i]);
			else
			{
				/* return 0; Invalid input */
				res = 0;
				break;
			}	
		}
		if (i>=11) res = 0; /* Over 10 digit decimal --invalid */
	}
	
	return res;
}
/*******************************************************************************
* Function Name  : GetIntegerInput
* Description    : Get an integer from the HyperTerminal
* Input          : The inetger
* Return         : 1 -- Correct
                 : 0 -- Error
*******************************************************************************/
u32 GetIntegerInput(s32 * num)
{
	char inputstr[16];

	while(1)
	{
		GetInputString(inputstr);
		if (inputstr[0] == '\0') continue;
		
		if ((inputstr[0] == 'a'||inputstr[0] == 'A')&& inputstr[1] == '\0')		
		{	
			SerialPutString("User Cancelled \r\n");
			return 0;
		}	
		
		if (Str2Int(inputstr,num) ==0)	
			SerialPutString("Error, Input again: \r\n");
		else return 1;	
	}
}

/*******************************************************************************
* Function Name  : SerialKeyPressed
* Description    : Test to see if a key has been pressed on the HyperTerminal
* Input          : The key pressed
* Return         : 1 -- Correct
                 : 0 -- Error
*******************************************************************************/
u32 SerialKeyPressed(char *key)
{
   	if (UART0->SR & UART_RxBufFull)
   	{
   		*key = (char)UART0->RxBUFR;
   		return 1;
   	}
   	else
   		return 0;
}
/*******************************************************************************
* Function Name  : GetKey
* Description    : Get a key from the HyperTerminal
* Input          : None
* Return         : The Key Pressed
*******************************************************************************/
u8 GetKey(void)
{
  u8 key;
  /* First clear Rx buffer */
  UART_FifoReset(UART0,UART_RxFIFO);
	
  /* Then, waiting for user input */
  while (1)
  {
    if (SerialKeyPressed((char*)&key)) break;
  }
  return key;
}

/*******************************************************************************
* Function Name  : SerialPutChar
* Description    : Print a character on the HyperTerminal
* Input          : The character to be printed
* Return         : None
*******************************************************************************/
void SerialPutChar(char c)
{
  UART_ByteSend(UART0,(u8*)&c);
}

/*******************************************************************************
* Function Name  : SerialPutString
* Description    : Print a string on the HyperTerminal
* Input          : The string to be printed
* Return         : None
*******************************************************************************/
void SerialPutString(char *s)
{
  while (*s != '\0')
  {
  SerialPutChar(*s);
  s ++;
  }
}

/*******************************************************************************
* Function Name  : GetInputString	
* Description    : Get Input string from the HyperTerminal
* Input          : The input string
* Return         : None
*******************************************************************************/
void GetInputString (char * buffP)
{
  u32 bytes_read = 0;
  char c;
  do
   {
   c = GetKey();
   if (c == '\r')   	
     break;
   if (c == '\b')	/* Backspace */
     {
     if (bytes_read > 0)
       {
       SerialPutString("\b \b");
       bytes_read --;
       }
  continue;
   }
  if (bytes_read >= CMD_STRING_SIZE )
    {
    SerialPutString("Command string size overflow\r\n");
    bytes_read = 0;
    continue;
    }
  if (c >= 0x20 && c<= 0x7E)
    {
    buffP[bytes_read++] = c;
    SerialPutChar(c);
    }	
    	
  }
  while (1);
  SerialPutString("\n\r");
  buffP[bytes_read] = '\0';
}

/*******************************************************************************
* Function Name  : FLASH_SectorMask
* Description    : Calculate the sectors mask
* Input          : The image size
* Return         : The sectors mask
*******************************************************************************/

u32 FLASH_SectorMask(vu32 Size)
{
#if 1  /*been modified : erase from secoter 2 : 0x40004000 */
  if (Size <= 0x2000)  // place in sec#2 (8k) --> 0000,0100  --> 0x4
    return 0x4;
  if (Size <= 0x4000)  // place in sec#2,3 (16k)--> 0000,1100 --> 0x0c
    return 0xC;
  
  if (Size <= 0xC000)  // place in sec#2,3,4 (48k) --> 0001,1100 --> 0x1c
    return 0x1C;
  if (Size <= 0x1C000)  // place in sec#2,3,4,5 (112k)--> 0011,1100 --> 0x3c
    return 0x3C;
  if (Size <= 0x2C000)  // place in sec#2,3,4,5,6 (176k)--> 0111,1100  --> 0x7c
    return 0x7C;
  if (Size <= 0x3C000)  // place in sec@2,3,4,5,6,7 (240)--> 1111,1100 --> 0xfc
    return 0xFC;
  return 0;
#endif
#if 0  /*erase from secoter 1 : 0x40002000 */
      if (Size <= 0x2000)
        return 0x2;
    if (Size <= 0x4000)
        return 0x6;
    if (Size <= 0x6000)
        return 0xE;
    if (Size <= 0xE000)
        return 0x1E;
    if (Size <= 0x1E000)
        return 0x3E;
    if (Size <= 0x2E000)
        return 0x7E;
    if (Size <= 0x3E000)
        return 0xFE;
    return 0;
#endif 
}

/*******************************************************************************
* Function Name  : Main_Menu	
* Description    : Display the  Main Menu on to HyperTerminal
* Input          : None
* Return         : None
*******************************************************************************/
void Main_Menu(void )
{
  u8 key;

  while(1)
  {
    SerialPutString("\r\n================== Main Menu ==================\r\n\n");
    SerialPutString("  Download Image To Internal Flash ---------- 1\r\n\n");
    SerialPutString("  Execute The New Program ------------------- 2\r\n\n");
    SerialPutString("===============================================\r\n\n");

    if ((PCU->BOOTCR&0x03)==2)
      SerialPutString("IAP remap to  RAM \r\n\n");
    if ((PCU->BOOTCR&0x03)==0)
      SerialPutString("IAP remap to Flash \r\n\n");

    key = GetKey();
    if (key == 0x31)
    {
      SerialDownload();
    }
    else if (key == 0x32)
    {
      /* Restore the default RCCU configuration */
      RCCU_RCLKSourceConfig(RCCU_CLOCK2);
      /* Jump to the new program */

      jump_function =(pointer) 0x40004000 ;
      jump_function();
    }
   else if (key == 0x33) 
    {
      /* Restore the default RCCU configuration */
      RCCU_RCLKSourceConfig(RCCU_CLOCK2);
      /* Jump to the new program */

      jump_function =(pointer) 0x40006000 ;
      jump_function();
    }
    

    else
    {
      SerialPutString("Invalid Number ! ==> The number should be either 1 or 2\r\n");
    }
  }
}
/*******************(C)COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

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