#ifndef __SYS_H
#define __SYS_H	
#include "stm32f10x.h"

typedef unsigned int clock_t;    /* cpu time type */
typedef unsigned int time_t;     /* date/time in unix secs past 1-Jan-70 */																	    
	 
//位带操作,实现51类似的GPIO控制功能
//具体实现思想,参考<<CM3权威指南>>第五章(87页~92页).
//IO口操作宏定义
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2)) 
#define MEM_ADDR(addr)  *((volatile unsigned long  *)(addr)) 
#define BIT_ADDR(addr, bitnum)   MEM_ADDR(BITBAND(addr, bitnum)) 
//IO口地址映射
#define GPIOA_ODR_Addr    (GPIOA_BASE+12) //0x4001080C 
#define GPIOB_ODR_Addr    (GPIOB_BASE+12) //0x40010C0C 
#define GPIOC_ODR_Addr    (GPIOC_BASE+12) //0x4001100C 
#define GPIOD_ODR_Addr    (GPIOD_BASE+12) //0x4001140C 
#define GPIOE_ODR_Addr    (GPIOE_BASE+12) //0x4001180C 
#define GPIOF_ODR_Addr    (GPIOF_BASE+12) //0x40011A0C    
#define GPIOG_ODR_Addr    (GPIOG_BASE+12) //0x40011E0C    

#define GPIOA_IDR_Addr    (GPIOA_BASE+8) //0x40010808 
#define GPIOB_IDR_Addr    (GPIOB_BASE+8) //0x40010C08 
#define GPIOC_IDR_Addr    (GPIOC_BASE+8) //0x40011008 
#define GPIOD_IDR_Addr    (GPIOD_BASE+8) //0x40011408 
#define GPIOE_IDR_Addr    (GPIOE_BASE+8) //0x40011808 
#define GPIOF_IDR_Addr    (GPIOF_BASE+8) //0x40011A08 
#define GPIOG_IDR_Addr    (GPIOG_BASE+8) //0x40011E08 
 
//IO口操作,只对单一的IO口!
//确保n的值小于16!
#define PAout(n)   BIT_ADDR(GPIOA_ODR_Addr,n)  //输出 
#define PAin(n)    BIT_ADDR(GPIOA_IDR_Addr,n)  //输入 

#define PBout(n)   BIT_ADDR(GPIOB_ODR_Addr,n)  //输出 
#define PBin(n)    BIT_ADDR(GPIOB_IDR_Addr,n)  //输入 

#define PCout(n)   BIT_ADDR(GPIOC_ODR_Addr,n)  //输出 
#define PCin(n)    BIT_ADDR(GPIOC_IDR_Addr,n)  //输入 

#define PDout(n)   BIT_ADDR(GPIOD_ODR_Addr,n)  //输出 
#define PDin(n)    BIT_ADDR(GPIOD_IDR_Addr,n)  //输入 

#define PEout(n)   BIT_ADDR(GPIOE_ODR_Addr,n)  //输出 
#define PEin(n)    BIT_ADDR(GPIOE_IDR_Addr,n)  //输入

#define PFout(n)   BIT_ADDR(GPIOF_ODR_Addr,n)  //输出 
#define PFin(n)    BIT_ADDR(GPIOF_IDR_Addr,n)  //输入

#define PGout(n)   BIT_ADDR(GPIOG_ODR_Addr,n)  //输出 
#define PGin(n)    BIT_ADDR(GPIOG_IDR_Addr,n)  //输入

#define REV_OK		1	//接收完成标志
#define REV_WAIT	0	//接收未完成标志

#define USART_DEBUG		USART1		//调试打印所使用的串口组
#define UART_RECV_SIZE 1024   //接收缓存大小
#define RECV_SIZE UART_RECV_SIZE + 200   //有效数据缓存大小
/*---------------------------------------------------------------------------*/
/* Type Definition Macros                                                    */
/*---------------------------------------------------------------------------*/

typedef int socklen_t;

typedef long long int           int64;
typedef unsigned long long int  uint64;


typedef _Bool			uint1;
typedef unsigned char   uint8;
typedef char			int8;
typedef unsigned short  uint16;
typedef short			int16;
typedef unsigned int    uint32;
typedef int				int32;
typedef unsigned int	size_t;

typedef struct uart_recv_st
{
	char buf[UART_RECV_SIZE]; //缓存大小
	unsigned int len; //接收长度
	int recv_flag; //接收完成标志 1:接收完成, 0:接收未完成
	char* start_addr; //字符开始地址
} uart_recv_t; //串口接收缓存


typedef struct valid_data_st {
		char buf[RECV_SIZE]; //有效数据
		int  len; //数据长度
} valid_data_t;

void byteToHexStr(const unsigned char* source, char* dest, int sourceLen);

void inttohex(u16 value,u8 *hex);

void letterSwitch(char *str, int flag);

int my_atoi(char *str);

uint64_t my_atoui64(char *str);

int my_atoabs(char *str);

char* my_itoa(int num,char* str,int radix);

/**
* @title 分割字符串
* @Param src:要分割字符串,seperator:分隔符, dest:分割后字符串地址, size:分割的数量
*/
void split(char *src,const char *separator,char *dest[],int size);

 /**
* @Title split,分割字符串
* @Param 要分割字符串,seperator:分隔符, dest:分割后字符串地址,limit:分割限制数量
*/
int splitCharLimit(char *str, char seprator, char *dest[], int limit);
 
 /**
* @title 延时函数
* @Param ms:延时时间（单位ms）
*/
 void delay_ms(uint32_t ms);

void Usart_SendString(USART_TypeDef *USARTx, unsigned char *str, unsigned short len);

void UsartPrintf(USART_TypeDef *USARTx, char *fmt,...);



void uart_recv_clear(void); 

void valid_data_clear(void);

void uart_recv_wait(int timeout);

int uart_recv_match_wait(char *res,int timeout);

#endif
