📄 gpio-toggle.c
字号:
/*################# GPIO test programm #################*/
#include <linux/delay.h>#include <linux/timer.h>#include <linux/module.h>#include <linux/mm.h>#include <linux/init.h>#include <linux/fs.h>#include <linux/blkpg.h>#include <linux/hdreg.h>#include <linux/major.h>#include <asm/uaccess.h>#include <asm/io.h>#include <linux/blk.h>MODULE_AUTHOR("Dr. Morg");MODULE_DESCRIPTION("Pseudo driver to toggle GPIOs");MODULE_SUPPORTED_DEVICE("DWL-G700AP");MODULE_LICENSE("GPL");//#define RTL_GPIO_PABDIR 0xbd010040
//#define RTL_GPIO_PABDATA 0xbd010044#define RTL_GPIO_PABDIR 0xbd010124#define RTL_GPIO_PABDATA 0xbd010120#define GPIO_DIR_INPUT 0#define GPIO_DIR_OUTPUT 1
#define RTL_R32(addr) (*(volatile unsigned long *)(addr))
#define RTL_W32(addr, l) ((*(volatile unsigned long*)(addr)) = (l))
typedef unsigned int uint32;
typedef unsigned long u32_t;
typedef unsigned short u16_t;
typedef unsigned char u8_t;#define DEBUG 1/*
* *******************************************************************
*
* Begin GPIO hardware access functions.
*
* *******************************************************************
*
*/
void gpio_setdir(int num, int dir)
{
if (dir == 1) {
RTL_W32(RTL_GPIO_PABDIR, (RTL_R32(RTL_GPIO_PABDIR) | (1 << num)));
} else {
RTL_W32(RTL_GPIO_PABDIR, (RTL_R32(RTL_GPIO_PABDIR) & (~(1 << num))));
}
}
int gpio_getdir(int num)
{
return ((RTL_R32(RTL_GPIO_PABDIR) & (1 << num)) ? 1 : 0);
}
static int gpio_read(int num)
{
int what;
what=((RTL_R32(RTL_GPIO_PABDATA) & (1 << num)) ? 1 : 0);
return (what);
}
static int gpio_write(int num, int val)
{
int check;
if (val == 1) {
RTL_W32(RTL_GPIO_PABDATA, (RTL_R32(RTL_GPIO_PABDATA) | (1 << num)));
} else {
RTL_W32(RTL_GPIO_PABDATA, (RTL_R32(RTL_GPIO_PABDATA) & (~(1 << num))));
}
#ifdef DEBUG
check=gpio_read(num);
if (check != val)
{
printk ("Error while write to %d: found %d after writing %d\n",num, check, val);
return (1);
}
else return(0);
#endif
}void toggle_port(int port){// int i;// printk("Printing gpio info: \n");// for (i = 0; i < 27; i++) {// printk("Pin %u: Alternate=%u, Direction=%u\n", i, gpio_getalt(g, i), gpio_getdir(g, i));// }// printk("gpio open OK\n");// gpio_setalt(g, port, 0);// printk ("gpio setalt OK\n"); gpio_setdir(port, GPIO_DIR_OUTPUT);// printk("gpio setdir OK\n");// printk("Printing gpio info: \n");// for (i = 0; i < 27; i++) {// printk("Pin %u: Alternate=%u, Direction=%u\n", i, gpio_getalt(g, i), gpio_getdir(g, i));// } if (gpio_read(port) == 1) { printk("GPIO %d going from 1 to 0 \n",port); gpio_write(port, 0); } else { printk("GPIO %d going from 0 to 1 \n",port); gpio_write(port, 1); }}static int __init gpiotoggle_driver_init(void){ printk("U32 : %u\n", sizeof(u32_t)); toggle_port(4); toggle_port(6); toggle_port(7); toggle_port(8); toggle_port(9); /*printk("Set GPIO 4 to OUTPUT\n"); gpio_setdir(4, 1); printk("Direction of GPIO 4: %d\n",gpio_getdir(4)); RTL_W32(RTL_GPIO_PABDATA, gpio_write((1 << 4),0)); printk("GPIO 4: %d\n",gpio_read(1 << 4));*/}static void __exit gpiotoggle_driver_exit(void){}module_init(gpiotoggle_driver_init);module_exit(gpiotoggle_driver_exit);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -