/********************************************************************                                                                  **
** 
**  File:		Uti.c
**  Author:
**  Target:
********************************************************************/
#include "uti.h"
#include "debug.h"
//#include "uart.h"
#include "BL210x_uart.h"

unsigned int TimingDelay;

static uint8_t  fac_us = 0;							//us延时倍乘数			   
static uint16_t fac_ms = 0;							//ms延时倍乘数,在ucos下,代表每个节拍的ms数
			   
void delay_init(void)
{
	fac_us = SystemCoreClock/1000000;	
	fac_ms = fac_us*1000;
}

//延时nus
//nus为要延时的us数.		    								   
void delay_us(uint32_t nus)
{		
	uint32_t temp;	
	
	SysTick->LOAD = nus*fac_us; 				//时间加载	  		 
	SysTick->VAL = 0x00;        				//清空计数器
	SysTick->CTRL |=SysTick_CTRL_ENABLE_Msk;  //开始倒数 	 
	do
	{
		temp = SysTick->CTRL;
	}while((temp&0x01)&&!(temp&(1<<16)));	//等待时间到达   
	SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;      	 			//关闭计数器
	SysTick->VAL = 0x00;       				//清空计数器	 
}
//注意nms的范围
//SysTick->LOAD为24位寄存器,所以,最大延时为:
//nms<=0xffffff*1000/SystemCoreClock
//SystemCoreClock单位为Hz,nms单位为ms
//对24M条件下,nms<=699
//对48M条件下,nms<=349
void delay_ms(uint16_t nms)
{	 		  	  
	uint32_t temp;
	
	SysTick->LOAD =(uint32_t)nms*fac_ms;			//时间加载(SysTick->LOAD为24bit)
	SysTick->VAL =0x00;           			//清空计数器
	SysTick->CTRL |=SysTick_CTRL_ENABLE_Msk;  //开始倒数 	 
	do
	{
		temp=SysTick->CTRL;
	}while((temp&0x01)&&!(temp&(1<<16)));	//等待时间到达   
	SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;   //关闭计数器
	SysTick->VAL =0x00;       				//清空计数器	  	    
} 

/********************************************************************************************************
**函数信息 ：delay_ms(__IO uint32_t nTime)                     
**功能描述 ：程序应用调用延时，使用systick
**输入参数 ：nTime：延时
**输出参数 ：无
********************************************************************************************************/
void delay_mms(unsigned int nTime)
{ 
	TimingDelay = nTime;
	
	while(TimingDelay != 0);
}



/**********************************************************
* Delay1ms
**********************************************************/
void Delay1ms(unsigned int n)
{
    unsigned char i,j;

    while(n--)
	{
        for(i=0; i<12; i++)
            for(j=0; j<220 ;j++);
	}
}

/**********************************************************
* Delay10ms
**********************************************************/

void Delay10ms(unsigned char n)
{
    unsigned char i,j;
    while(n--)
    {
        for(i=0;i<106;i++)
            for(j=0;j<250;j++);
    }
}

/**********************************************************
* Delay100us
**********************************************************/
void Delay100us(unsigned char n)
{
    unsigned char i;
    while(n--)
	{
        for(i=0;i<120;i++);
//		{
//#pragma asm
//	        nop
//#pragma endasm
//		}
	}
}

/**********************************************************
* Delay10us
**********************************************************/
void Delay10us(unsigned char n)
{
    unsigned char i;
    while(n--)
	{
        for(i=0;i<9;i++);
//			i=i;
//#pragma asm
//	        nop
//#pragma endasm
	
	}
}

//
// putchar (full version):  expands '\n' into CR LF and handles
//                          XON/XOFF (Ctrl+S/Ctrl+Q) protocol
//
/*
char putchar (char c)  {

  if (c == '\n')  {
    if (RI)  {
      if (SBUF == XOFF)  {
        do  {
          RI = 0;
          while (!RI);
        }
        while (SBUF != XON);
        RI = 0; 
      }
    }
    while (!TI);
    TI = 0;
    SBUF = 0x0d;                         // output CR
  }
  if (RI)  {
    if (SBUF == XOFF)  {
      do  {
        RI = 0;
        while (!RI);
      }
      while (SBUF != XON);
      RI = 0; 
    }
  }
  while (!TI);
  TI = 0;
  return (SBUF = c);
}
*/

// comment len < 255
void  LogBuf(const char *comment ,unsigned char *buf, unsigned char buf_len)
{ 
#if DEBUG_MODE
//	unsigned char slen=0;
//    Uart1_SendData(2,"\r\n",0);
//		UART_SendChars(Rec_Data,Rec_Num); 
//	if(buf_len)
//	{
//	    Uart1_SendData(1,"L",0);
//	    Uart1_SendData(1,&buf_len,1);
//	    Uart1_SendData(1,">",0);
//	}
//	while(comment[slen++]);
//	Uart1_SendData(slen-1,comment,0);
//	if(buf_len)
//	{
//        Uart1_SendData(buf_len,buf,1);
//	}	
	
/*    
    int i ;
	m_printf("\r\n(L->%d)%s",(unsigned int)buf_len,comment);
    for(i = 0; i < buf_len; i++)
    {
        m_printf("%02x ", (unsigned int)buf[i]);
    }	
*/
#endif	
}

//memset - Fill a region of memory with the given value
// @s: Pointer to the start of the area.
// @c: The byte to fill the area with
// @count: The size of the area.
// Do not use memset() to access IO space, use memset_io() instead.
void *rwmemset(void *s, int c, unsigned char count)
{
    char *xs = s;

    while (count--)
        *xs++ = c;
    return s;
}

//memcpy - Copy one area of memory to another
// @dest: Where to copy to
// @src: Where to copy from
// @count: The size of the area.
// You should not use this function to access IO space, use memcpy_toio()
// or memcpy_fromio() instead.
void *rwmemcpy(void *desc,const void * src,unsigned char size)
{
	unsigned char *desc1 = (unsigned char*)desc;
	unsigned char *src1 = (unsigned char*)src;

	while(size-- >0)
	{
	    *desc1 = *src1;
	    desc1++;
	    src1++;
	}
	return desc;
}

//memcmp - Compare two areas of memory
// @cs: One area of memory
// @ct: Another area of memory
// @count: The size of the area.
int rwmemcmp(const void *cs, const void *ct, unsigned char count)
{
    const unsigned char *su1, *su2;
    int res = 0;

    for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
        if ((res = *su1 - *su2) != 0)
            break;
    return res;
}




