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

📄 library.c

📁 s3c6410基于USB OTG下载内核至NORFLASH的源代码
💻 C
字号:
/**************************************************************************************
* 
*	Project Name : S3C6410 Validation
*
*	Copyright 2006 by Samsung Electronics, Inc.
*	All rights reserved.
*
*	Project Description :
*		This software is only for validating functions of the S3C6410.
*		Anybody can use this software without our permission.
*  
*--------------------------------------------------------------------------------------
* 
*	File Name : library.c
*  
*	File Description : This file implements library functions.
*
*	Author : Haksoo,Kim
*	Dept. : AP Development Team
*	Created Date : 2006/11/08
*	Version : 0.1 
* 
*	History
*	- Created(Haksoo,Kim 2006/11/08)
*  
**************************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "option.h"
#include "library.h"
#include "sfr6410.h"
#include "system.h"
#include "uart.h"
#include "gpio.h"



//////////
// Function Name : InitUartPort
// Function Description : This function initializes gpio for debugging uart ch.
// Input :  ch, uart ch number
//			flowControl, whether flow control or not
// Output : NONE
// Version :
void InitUartPort(u8 ch, bool flowControl)
{
	switch (ch)
	{
		default:
		case 0:
			if(flowControl == TRUE)
			{
				GPIO_SetFunctionEach(eGPIO_A, eGPIO_0, 2);
				GPIO_SetFunctionEach(eGPIO_A, eGPIO_1, 2);
				GPIO_SetFunctionEach(eGPIO_A, eGPIO_2, 2);
				GPIO_SetFunctionEach(eGPIO_A, eGPIO_3, 2);
			}
			else
			{
				GPIO_SetFunctionEach(eGPIO_A, eGPIO_0, 2);
				GPIO_SetFunctionEach(eGPIO_A, eGPIO_1, 2);
			}
			break;
			
		case 1:
			if(flowControl == TRUE)
			{
				GPIO_SetFunctionEach(eGPIO_A, eGPIO_4, 2);
				GPIO_SetFunctionEach(eGPIO_A, eGPIO_5, 2);
				GPIO_SetFunctionEach(eGPIO_A, eGPIO_6, 2);
				GPIO_SetFunctionEach(eGPIO_A, eGPIO_7, 2);
			}
			else
			{
				GPIO_SetFunctionEach(eGPIO_A, eGPIO_4, 2);
				GPIO_SetFunctionEach(eGPIO_A, eGPIO_5, 2);
			}
			break;
			
		case 2:
			GPIO_SetFunctionEach(eGPIO_B, eGPIO_0, 2);
			GPIO_SetFunctionEach(eGPIO_B, eGPIO_1, 2);
			break;
			
		case 3:
			GPIO_SetFunctionEach(eGPIO_B, eGPIO_2, 2);
			GPIO_SetFunctionEach(eGPIO_B, eGPIO_3, 2);
			break;		
	}
	
	return;	
}

//////////
// Function Name : InitLED
// Function Desctiption : This function initializes gpio for debugging LED
// Input : NONE
// Output : NONE
// Version :
void InitLED(void)
{
	GPIO_SetFunctionEach(eGPIO_N, eGPIO_12, 1);
	GPIO_SetFunctionEach(eGPIO_N, eGPIO_13, 1);
	GPIO_SetFunctionEach(eGPIO_N, eGPIO_14, 1);
	GPIO_SetFunctionEach(eGPIO_N, eGPIO_15, 1);	
	return;
}

//////////
// Function Name : DisplayLED
// Function Desctiption : This function controls debugging LED
// Input : data, LED value 
// Output : NONE
// Version :
void DisplayLED(u8 data)
{
	u32 temp;

	temp = GPIO_GetDataAll(eGPIO_N);
	temp = (temp & ~(0xf000))|((data&0xf)<<12);
	GPIO_SetDataAll(eGPIO_N, temp);

	return;
}


//////////
// Function Name : OpenConsole
// Function Description : This function opens uart and LED for debugging
// Input : NONE
// Output : NONE
// Version : 
void OpenConsole( void)
{	
	u8 ch=0;	//uart channel for debugging
	
	InitUartPort(ch, FALSE);
	UART_InitDebugCh(ch, 115200);
	InitLED();

	return;
}

//////////
// Function Name : GetIntNum
// Function Description : This function gets the number which a user enters
// Input : NONE
// Output : number, number which a user enters
// Version : 
s32 GetIntNum( void)
{
    char str[30];
    char *string = str;
    int base     = 10;
    int minus    = 0;
    int result   = 0;
    int lastIndex;    
    int i,j;
    
    gets(string);


	i=0; j=0;
	do {
		if (string[j]==0x8) {
			if (i>0)	i--; 
		} else 
			string[i++]=string[j];
	} while(string[j++]!=0);


    if(string[0]=='-') {
        minus = 1;
        string++;
    }
    
    if(string[0]=='0' && (string[1]=='x' || string[1]=='X')) {
        base    = 16;
        string += 2;
    }
    
    lastIndex = strlen(string) - 1;
    
    if(lastIndex<0)
        return -1;
    
    if(string[lastIndex]=='h' || string[lastIndex]=='H' ) {
        base = 16;
        string[lastIndex] = 0;
        lastIndex--;
    }

    if(base==10) {
        result = atoi(string);
        result = minus ? (-1*result):result;
    }
    else {
        for(i=0;i<=lastIndex;i++) {
            if(isalpha(string[i])) {
                if(isupper(string[i]))
                    result = (result<<4) + string[i] - 'A' + 10;
                else
                    result = (result<<4) + string[i] - 'a' + 10;
            }
            else
                result = (result<<4) + string[i] - '0';
        }
        result = minus ? (-1*result):result;
    }
    return result;
}

//////////
// Function Name : DownloadImageThruUart
// Function Description : This function downloads a certain image through uart
// Input : DownloadAddress, address to download the image 
// Output : FileSize, size of downloaded image
// Version : 
u32 DownloadImageThruUart(u8 *DownloadAddress)
{
	char buf[4];
	int i;
	u32	FileSize;
	u16	CheckSum=0,dnCS;

	printf("\nDownloadAddress : 0x%08x\n",DownloadAddress);
	printf("STATUS : ");

	//	To get the file size.
	for(i=0;i<4;i++)
		buf[i]=UART_Getc();

	FileSize=(buf[0])+(buf[1]<<8)+(buf[2]<<16)+(buf[3]<<24);
	FileSize-=4;
	
	for(i=0;i<FileSize;i++) {
		*(DownloadAddress+i)=UART_Getc();
		if((i&0x3ff)==0)
			putchar('#');
	}

	for(i=0;i<(FileSize-2);i++)
		CheckSum+=*((u8 *)(DownloadAddress+i));

	dnCS=*((u8 *)(DownloadAddress+FileSize-2))+
		(*( (u8 *)(DownloadAddress+FileSize-1) )<<8);

	if(CheckSum!=dnCS) {
		printf("\nChecksum Error!!! MEM : %x  DN : %x\n",CheckSum,dnCS);
		FileSize=0;
	} else {
		FileSize-=2;
		printf("\n%d bytes downloaded OK.\n",FileSize);
	}
    	
	return FileSize;
}

//////////
// Function Name : Delay
// Function Description : 
// Input : usec, time in 100us unit
// Output : NONE
// Version : 
u32 delayLoopCount;
void Delay(u32 usec)
{
	u32 i=0;
    
    for(;usec>0;usec--)
    {
    	for(i=0;i<delayLoopCount;i++);
    }
}

//////////
// Function Name : Pow
// Function Description : Calculates x raise to the power of y
// Input : x, y
// Output : uResult - result value
// Version : 
u32 Pow(u32 x, u32 y)
{
    u32 i, uResult = 1;
    
    for(i=0 ; i<y ; i++)
    {
    	uResult = uResult*x;
    }
    
    return uResult;
}  

//////////
// Function Name : Copy
// Function Description : Copy from src address to dst address by words count
// Input : sa, da, words
// Output : void
// Version : 
void Copy(u32 sa, u32 da, u32 words)
{
	u32 i;	
	for (i=0; i<words; i++)
		*(u32 *)(da+i*4) = *(u32 *)(sa+i*4);
}

//////////
// Function Name : Copy8
// Function Description : Copy from src address to dst address by bytes count
// Input : sa, da, bytes
// Output : void
// Version : 
void Copy8(u32 sa, u32 da, u32 bytes)
{
	u32 i;
	for (i=0; i<bytes; i++)
		*(u8 *)(da+i) = *(u8 *)(sa+i);
}

//////////
// Function Name : Compare
// Function Description : compare data
// Input : a0, a1, words
// Output : ret
// Version : 
bool Compare( u32 a0,  u32 a1,  u32 words)
{
	volatile u32 d0,d1;
	volatile u32 *pD0, *pD1;
	bool ret = true;
	u32  ecnt = 0;
	u32 i;
	
	pD0 = (volatile u32 *)(a0);
	pD1 = (volatile u32 *)(a1);
	
	printf("\n");	

	for (i=0; i<words; i++)
	{
		
		d0=*pD0;
		d1=*pD1;

		if (d0!= d1) 
		{
			ret = false;
			printf(" %08x=%08x <-> %08x=%08x\n", pD0, d0, pD1, d1);
			ecnt++;
		}
		pD0++;
		pD1++;
	}
/*
	if (ret == false)
	{
		Assert(0);		
		printf("\n");
	}
*/
	return ret;
}

//////////
// Function Name : Dump32
// Function Description : dump data
// Input : addr, words
// Output : ret
// Version : 
void Dump32(u32 addr, u32 words)
{
	int i, j;
	u32 *pMem;

	pMem = (u32 *)(addr/4*4);

	Disp("\n");
	for(i=0; i<words; )
	{
		Disp(" %04x: ", i);

		for(j=0; j<8; j++)
			Disp("%08x ", *pMem++),
			i++;
		Disp("\n");
	}
}

//////////
// Function Name : Dump16
// Function Description : dump data
// Input : addr, hwords
// Output : ret
// Version : 
void Dump16(u32 addr, u32 hwords)
{
	int i, j;
	u16 *pMem;

	pMem = (u16 *)(addr/2*2);

	Disp("\n");
	for(i=0; i<hwords; )
	{
		Disp(" %04x: ", i);

		for(j=0; j<16; j++)
			Disp("%04x ", *pMem++),
			i++;
		Disp("\n");
	}
}


//////////
// Function Name : Dump8
// Function Description : dump data
// Input : addr, bytes
// Output : ret
// Version :
void Dump8(u32 addr, u32 bytes)
{
	int i, j;
	u8 *pMem;

	pMem = (u8 *)addr;

	Disp("\n");
	for(i=0; i<bytes; )
	{
		Disp(" %04x: ", i);

		for(j=0; j<16; j++)
			Disp("%02x ", *pMem++),
			i++;
		Disp("\n");
	}
}


//////////
// Function Name : GetBitPosition
// Function Description : This function return the Bit Position of uValue
// Input :  	uValue - Value
// Output : 	Bit position
// Version :
// added by rb1004
u32 GetBitPosition(u32 uValue)
{
	u8 bitshift;

	for(bitshift=0 ; bitshift<32 ; bitshift++)
	{
	  if(uValue & (1<<bitshift))
	  	return bitshift;
	}
	return 0;  
}

⌨️ 快捷键说明

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