#ifndef board_h__
#define board_h__

#include "stdint.h"
#include "rtthread.h"
#include <rthw.h>
#include "string.h"


struct dev_struct{
  const char *name;
  void *dev;
};



typedef struct __uart_def{
  const char *name;
  int (*init)(struct __uart_def *u,int bsp);
  int (*deinit)(struct __uart_def *u);
  int (*set_irq)(struct __uart_def *u,void (*irq)(void *t,uint8_t d),void *t);
  int (*set_end_irq)(struct __uart_def *u,uint8_t *rx_buff,int rx_buff_size,
    void (*irq)(void *t,uint32_t len),void *t);
  int (*read)(struct __uart_def *u,uint8_t *b,int len);
  int (*write)(struct __uart_def *u,const uint8_t *b,int len);
  void *private_data;
}uart_def;



typedef struct __pwm_def{
  const char *name;
  int (*init)(struct __pwm_def *u);
  int (*deinit)(struct __pwm_def *u);
  int (*start)(struct __pwm_def *u,int step_count);
  int (*stop)(struct __pwm_def *u);
  int (*set_fre)(struct __pwm_def *u,int fre);
  int (*set_irq_fun)(struct __pwm_def *p,void (*fun)(void *t),void *t);
  void *private_data;
}pwm_def;


typedef struct __gpioin_def{
  const char *name;
  int (*init)(struct __gpioin_def *u);
  int (*deinit)(struct __gpioin_def *u);
  int (*state)(struct __gpioin_def *u);
  void *private_data;
}gpioin_def;

typedef struct __gpioout_def{
  const char *name;
  int (*init)(struct __gpioout_def *u);
  int (*deinit)(struct __gpioout_def *u);
  int (*set)(struct __gpioout_def *u,int state);
  void *private_data;
}gpioout_def;



typedef struct __timer_def{
  const char *name;
  int (*init)(struct __timer_def *u);
  int (*deinit)(struct __timer_def *u);
  uint32_t (*read)(struct __timer_def *u);
  int (*write)(struct __timer_def *u,const uint32_t value);
  void *private_data;
}timer_def;





#define CHECK_DO(s,to) if(s) {to(s);}

#define LENGTH(s) (sizeof(s)/sizeof(s[0]))




#define uart_init_export(name_,init_,deinit_,set_irq_,set_end_irq_,read_,write_,priv_) \
  const static char __dev_##name_##_name[] SECTION(".rodata.devstr") = #name_; \
  RT_USED static uart_def _uart_##name_={\
    .name=__dev_##name_##_name,\
    .init=init_,\
    .deinit=deinit_,\
    .set_irq=set_irq_,\
    .set_end_irq=set_end_irq_,\
    .read=read_,\
    .write=write_,\
    .private_data=priv_,\
  };\
  RT_USED static const struct dev_struct __dev_##name_ SECTION("devstruct")= \
  {                         \
    __dev_##name_##_name,   \
    &_uart_##name_,                   \
  };
  

  
#define pwm_init_export(name_,init_,deinit_,start_,stop_,set_fre_,set_end_irq_,priv_) \
  const static char __dev_##name_##_name[] SECTION(".rodata.devstr") = #name_; \
  RT_USED static pwm_def _pwm_##name_={\
    .name=__dev_##name_##_name,\
    .init=init_,\
    .deinit=deinit_,\
    .start=start_,\
    .stop=stop_,\
    .set_fre=set_fre_,\
    .set_irq_fun=set_end_irq_,\
    .private_data=priv_,\
  };\
  RT_USED static const struct dev_struct __dev_##name_ SECTION("devstruct")= \
  {                         \
    __dev_##name_##_name,   \
    &_pwm_##name_,\
  };
  
#define gpioin_init_export(name_,init_,deinit_,state_,priv_) \
  const static char __dev_##name_##_name[] SECTION(".rodata.devstr") = #name_; \
  RT_USED static gpioin_def _gpioin_##name_={\
    .name=__dev_##name_##_name,\
    .init=init_,\
    .deinit=deinit_,\
    .state=state_,\
    .private_data=priv_,\
  };\
  RT_USED static const struct dev_struct __dev_##name_ SECTION("devstruct")= \
  {                         \
    __dev_##name_##_name,   \
    &_gpioin_##name_,\
  };
  

#define gpioout_init_export(name_,init_,deinit_,set_,priv_) \
  const static char __dev_##name_##_name[] SECTION(".rodata.devstr") = #name_; \
  RT_USED static gpioout_def _gpioout_##name_={\
    .name=__dev_##name_##_name,\
    .init=init_,\
    .deinit=deinit_,\
    .set=set_,\
    .private_data=priv_,\
  };\
  RT_USED static const struct dev_struct __dev_##name_ SECTION("devstruct")= \
  {                         \
    __dev_##name_##_name,   \
    &_gpioout_##name_,\
  };

  


#define timer_init_export(name_,init_,deinit_,read_,write_,priv_) \
  const static char __dev_##name_##_name[] SECTION(".rodata.devstr") = #name_; \
  RT_USED static timer_def _timer_##name_={\
    .name=__dev_##name_##_name,\
    .init=init_,\
    .deinit=deinit_,\
    .read=read_,\
    .write=write_,\
    .private_data=priv_,\
  };\
  RT_USED static const struct dev_struct __dev_##name_ SECTION("devstruct")= \
  {                         \
    __dev_##name_##_name,   \
    &_timer_##name_,                   \
  };



  
struct init_struct{
  int (*init_fun)(void);
};



#define app_init_export(fun_)\
  RT_USED static const struct init_struct __init_##fun_ SECTION("initstruct")= \
  {\
    .init_fun=fun_,\
  };
  
  
  
#define def_find_fun(type_,g_data_)\
static const type_ *find(const char *name,int *index)\
{\
  const type_ *r=0;\
  for(int i=0;i<LENGTH(g_data_);i++)\
  {\
    r=&g_data_[i];\
    if(strcmp(r->name,name)==0){\
    if(index) *index=i;\
    return r;\
    }\
  }\
  return r;\
}

void app_init(void);
void *app_variable(const char *name,void *p,void (*del)(void *t));
void app_valid_call(const char *name,void (*fun)(void *t),void *t);
  
void param_err_handle(const char *param,const char *file,const char *fun,int line);

void cpy4byte(uint32_t *dst,uint32_t *src,int num_4byte);


//#ifdef DEBUG
// 如果s==0,则打印
#define param_check(s)  \
  if((s)==0){\
    param_err_handle(#s,__FILE__,__func__,__LINE__);}
//#else

//#define param_check(s)

//#endif

#ifdef RT_THREAD    
    
#define irq_disable()\
  register rt_base_t __level;\
  __level = rt_hw_interrupt_disable()

#define irq_enable()\
  rt_hw_interrupt_enable(__level)

void *dev_get(const char *name);

    

#else

uint32_t __interrupt_disable(void);
void __interrupt_enable(uint32_t level);


#define irq_disable()\
  register uint32_t __level;\
  __level = __interrupt_disable()

#define irq_enable()\
  __interrupt_enable(__level)


#endif
    
    
    
    
    
    
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2)) 
#define MEM_ADDR(addr)  *((volatile uint32_t  *)(addr)) 
#define BIT_ADDR(addr, bitnum)   (MEM_ADDR(BITBAND(addr, bitnum)))

// 获取输出寄存器地址s=A~I
#define GPIOx_ODR_ADDR(s) (GPIO##s##_BASE+12)
// 获取输入寄存器地址s=A~I
#define GPIOx_IDR_ADDR(s) (GPIO##s##_BASE+8)

// gpio输出，s=A~I,n=0~15
#define PINOUT(s,n)   BIT_ADDR(GPIOx_ODR_ADDR(s),n)
#define PININ(s,n)    BIT_ADDR(GPIOx_IDR_ADDR(s),n)
    
    
    
    
    
    
    
    
    
    
    




#endif


