[Linux驱动之路] 驱动设计的思想_面向对象_分层_分离—程序扩展

韦东山老师的Linux驱动设计基础课程的p128 5_101
驱动设计的思想_面向对象_分层_分离
这课在开发板上实作练习,可控制imx6ull_pro板子的LED开关。

基于韦老师代码的基础上,更改如下代码:

led_resource.h

#ifndef _LED_RESOURCE_H
#define _LED_RESOURCE_H/* GPIO3_0 */
/* bit[31:16] = group */
/* bit[15:0]  = which pin */
#define GROUP(x) (x>>16)
#define PIN(x)   (x&0xFFFF)
#define GROUP_PIN(g,p) ((g<<16) | (p))#define GPIO5_IO3 1/* GPIO3_0 */
// BIT[31:16] == group
// bit[15:0]  == pin number
struct led_resource {int pin;
};struct led_resource *get_led_resource(void);#endif

board_A_led.c

#include "led_resource.h"static struct led_resource board_A_led = {.pin = GROUP_PIN(5,3), //GPIO5_IO3
};struct led_resource *get_led_resource(void)
{return &board_A_led;
}

chip_demo_gpio.c

#include #include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "led_opr.h"
#include "led_resource.h"static struct led_resource *led_rsc;#ifdef GPIO5_IO3
static volatile unsigned int *CCM_CCGR1 = NULL;
static volatile unsigned int *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = NULL;
static volatile unsigned int *GPIO5_GDIR = NULL;
static volatile unsigned int *GPIO5_DR = NULL;
#endif/*******内部函数************/
static int _board_demo_led_init (int which) /* 初始化LED, which-哪个LED */	   
{unsigned int value;printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);if(which == 0){if(!CCM_CCGR1){CCM_CCGR1 								= ioremap(0x20C406C, 4);IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = ioremap(0x2290014, 4);GPIO5_GDIR								= ioremap(0x020AC000 + 0x4 , 4);GPIO5_DR 								= ioremap(0x020AC000, 4);}/* GPIO5_IO03 */ /* a. 使能 GPIO5 * set CCM to enable GPIO5 * CCM_CCGR1[CG15] 0x20C406C * bit[31:30] = 0b11 */*CCM_CCGR1 |= (0x3 << 30); /* b. 设置 GPIO5_IO03 用于 GPIO * set IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 * to configure GPIO5_IO03 as GPIO * IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 0x2290014 * bit[3:0] = 0b0101 alt5 */value = *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3;value &= ~0xF; value |= 0x5; *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = value;/* c. 设置 GPIO5_IO03 作为 output 引脚 * set GPIO5_GDIR to configure GPIO5_IO03 as output * GPIO5_GDIR 0x020AC000 + 0x4 * bit[3] = 0b1 */ *GPIO5_GDIR |= (1 << 3);}return 0;
}static int _board_demo_led_ctl (int which, char status) /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
{printk("%s %s line %d, led %d, %s\n", __FILE__, __FUNCTION__, __LINE__, which, status ? "on" : "off");if(which == 0){if(status) //on{/* d. 设置 GPIO5_DR 输出低电平 * set GPIO5_DR to configure GPIO5_IO03 output 0 * GPIO5_DR 0x020AC000 + 0 * bit[3] = 0b0 */ *GPIO5_DR &= ~(1 << 3); }else{/* e. 设置 GPIO5_IO3 输出高电平 * set GPIO5_DR to configure GPIO5_IO03 output 1 * GPIO5_DR 0x020AC000 + 0 * bit[3] = 0b1 */*GPIO5_DR |= (1 << 3); }}return 0;
}/************外部函数******************/static int board_demo_led_init (int which) /* 初始化LED, which-哪个LED */	   
{unsigned int value;if (!led_rsc){led_rsc = get_led_resource();}printk("init gpio: group %d, pin %d\n", GROUP(led_rsc->pin), PIN(led_rsc->pin));switch(GROUP(led_rsc->pin)){case 0:{printk("init pin of group 0 ...\n");break;}case 1:{printk("init pin of group 1 ...\n");break;}case 2:{printk("init pin of group 2 ...\n");break;}case 3:{printk("init pin of group 3 ...\n");break;}case 4:{printk("init pin of group 4 ...\n");break;}case 5:{printk("init pin of group 5 ...\n");_board_demo_led_init(which);break;			}return 0;
}
}static int board_demo_led_ctl (int which, char status) /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
{printk("set led %s: group %d, pin %d\n", status ? "on" : "off", GROUP(led_rsc->pin), PIN(led_rsc->pin));switch(GROUP(led_rsc->pin)){case 0:{printk("set pin of group 0 ...\n");_board_demo_led_ctl(PIN(led_rsc->pin), status);break;}case 1:{printk("set pin of group 1 ...\n");break;}case 2:{printk("set pin of group 2 ...\n");break;}case 3:{printk("set pin of group 3 ...\n");break;}case 4:{printk("init pin of group 4 ...\n");break;}case 5:{printk("init pin of group 5 ...\n");_board_demo_led_ctl(which, status);break;			}}return 0;
}static struct led_operations board_demo_led_opr = {.num = 1,.init = board_demo_led_init,.ctl  = board_demo_led_ctl,
};struct led_operations *get_board_led_opr(void)
{return &board_demo_led_opr;
} 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部