读u-boot深入学C语言框架 [2]

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


// 功能 定义集合结构体
// 注:定义时对应位置要匹配
// 	   可更具需要自行添加所需成员
struct peripheralname_device{
	char name[20];
	int  (*socname_peripheralname_gongneng1)(void);
	int  (*socname_peripheralname_gongneng2)(int) ;
	void (*socname_peripheralname_gongneng3)(void);
	void (*socname_peripheralname_gongneng4)(int) ;

};

// 功能 声明和定义同类型的函数 (比如某个SOC有多个功能相同的外设 可以这样定义声明)
// 框架 DECLARE_SOC名字_外设名字_FUNCTION(用于标识同类型的外设的号码) 可以再加入你需要的 
#define DECLARE_SOCNAME_PERIPHERALNAME_FUNCTION(law_param)	 								 \
int  socname_peripheralname_##law_param##_goneng1(void) {printf("gongneng 1\n");return 0;}	 \
int  socname_peripheralname_##law_param##_goneng2(int a){printf("gongneng 2\n");return 0;}	 \
void socname_peripheralname_##law_param##_goneng3(void) {printf("gongneng 3\n");}			 \
void socname_peripheralname_##law_param##_goneng4(int a){printf("gongneng 4\n");}	

// 功能 初始化外设结构体成员
// 框架 INIT_外设名字_STRUCTURE(用于标识同类型的外设的号码, 一些你想加入的东西ETC)
#define INIT_PERIPHERALNAME_STRUCTURE(law_param, name){ \
	name, 											 	\
	socname_peripheralname_##law_param##_goneng1,	 	\
	socname_peripheralname_##law_param##_goneng2,		\
	socname_peripheralname_##law_param##_goneng3,	 	\
	socname_peripheralname_##law_param##_goneng4,     }

// 描述 使用上面的框架 
// 		1 定义和上面函数, 2 初始化结构体成员。
// 优点 大大缩短了代码量和提高可移植性
DECLARE_SOCNAME_PERIPHERALNAME_FUNCTION(1);
struct peripheralname_device peripheralname_1 = 
	INIT_PERIPHERALNAME_STRUCTURE(1,"SOC_PER_1");
DECLARE_SOCNAME_PERIPHERALNAME_FUNCTION(2);
struct peripheralname_device peripheralname_2 = 
	INIT_PERIPHERALNAME_STRUCTURE(2,"SOC_PER_2");
DECLARE_SOCNAME_PERIPHERALNAME_FUNCTION(3);
struct peripheralname_device peripheralname_3 = 
	INIT_PERIPHERALNAME_STRUCTURE(3,"SOC_PER_3");
DECLARE_SOCNAME_PERIPHERALNAME_FUNCTION(4);
struct peripheralname_device peripheralname_4 = 
	INIT_PERIPHERALNAME_STRUCTURE(4,"SOC_PER_4");


int main(void){
	
	// 调用结构体成员
	peripheralname_1.socname_peripheralname_gongneng1();
	peripheralname_2.socname_peripheralname_gongneng2(520);
	peripheralname_3.socname_peripheralname_gongneng3();
	peripheralname_4.socname_peripheralname_gongneng4(520);
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值