需要用到的知識點:
A)linux內核延時函數,這裏參考文章https://www.cnblogs.com/Ph-one/p/4678361.html
#include <linux/delay.h>
1、void ndelay(unsigned long nsecs); 納秒級
2、void udelay(unsigned long usecs); 微秒級
3、void mdelay(unsigned long msecs); 毫秒級
udelay一般適用於一個比較小的delay,如果你填的數大於2000,系統會認爲你這個是一個錯誤的delay函數,因此如果需要2ms以上的delay需要使用mdelay函數。
這裏強調如果模擬pwm信號,不要去用init_timer定時器和hrtimer_init,即使是hrtimer_init可以到達納秒級別。hrtimer_init模擬的pwm信號,用示波器查看方波圖達不到很精準。
B)PWM信號
個人見解是一個方波圖,類似下圖,只不過pwm方波圖的高低電平是可以控制的。
C)控制舵機的pwm方波圖,這裏參考文章https://blog.csdn.net/qq_38960810/article/details/83017629
其實pwm信號的週期不是很重要,關鍵是pwm方波圖中高電平的時間(自己可以做實驗,用pwm信號發生器去控制舵機,100HZ和200HZ都能控制轉動的,只要控制它的高電平符合下面角度對應的毫秒即可)。
0.5ms--------------0度;
1.0ms------------45度;
1.5ms------------90度;
2.0ms-----------135度;
2.5ms-----------180度;
也就是說只要pwm方波圖中的高電平有0.5ms,舵機轉角度就爲0,2.5ms的話就是180度。和週期沒有直接關係。
調試中個人總結:
1:
控制舵機的pwm信號必須是一直持續的,比如你提供一個1ms的pwm信號給舵機,舵機能夠轉動到90度,
但是接着你不繼續提供1ms的pwm信號,這時舵機雖然不會有啥變化,但是你用手去轉動舵機是可以轉動的,
反之持續的提供1ms的pwm信號,舵機是會一直保持90度,你就是用手去掰也不會轉動的。千萬不要只發一個週期的pwm方波信號。
2:
使用舵機過程中最好配備pwm信號發生器(淘寶有賣),手持示波器。這樣方便調式找出問題。
下面是代碼,代碼是在JZ2440開發板基礎上,只需要修改對應的GPIO口就行,這裏要注意的是舵機轉動到相應的角度其實也是需要一定的時間,不可能瞬間到達的,所以我們必須要延時一會,tdata變量就是用來延時的
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/ioctl.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/hrtimer.h>
static struct timer_list buttons_timer;
static struct hrtimer timer;
ktime_t kt;
#define DEVICE_NAME "pwdtimer" /* 加載模式後,執行”cat /proc/devices”命令看到的設備名稱 */
#define PWD_MAJOR 231 /* 主設備號 */
static struct class *pwd_class;
static struct class_device *pwd_class_devs;
/* bit0<=>D10, 0:亮, 1:滅
* bit1<=>D11, 0:亮, 1:滅
* bit2<=>D12, 0:亮, 1:滅
*/
static char leds_status = 0x0;
static DECLARE_MUTEX(leds_lock); // 定義賦值
//static int minor;
static unsigned long gpio_va;
#define GPIO_OFT(x) ((x) - 0x56000000)
#define GPFCON (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000050)))
#define GPFDAT (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000054)))
#define GPACON (*(volatile unsigned long *)(gpio_va))
#define GPADAT (*(volatile unsigned long *)(gpio_va))
/* 應用程序對設備文件/dev/leds執行open(...)時,
* 就會調用s3c24xx_pwd_open函數
*/
static int s3c24xx_pwd_open(struct inode *inode, struct file *file)
{
// 配置2引腳爲輸出
//s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);
GPFCON &= ~(0x3<<4);
GPFCON |= (1<<4);
GPFCON &= ~(0x3<<8);
GPFCON |= (1<<8);
// 都輸出0
//s3c2410_gpio_setpin(S3C2410_GPF4, 0);
GPFDAT &= ~(1<<2);
GPFDAT &= ~(1<<4);
GPFDAT |= (1<<4);
GPFCON &= ~(0x3<<2);
GPFCON |= (1<<2);
GPFDAT &= ~(1<<1);
return 0;
}
static ssize_t s3c24xx_leds_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
char val;
copy_from_user(&val, buf, 1);
s3c2410_gpio_setpin(S3C2410_GPF4, (val & 0x1));
s3c2410_gpio_setpin(S3C2410_GPF5, (val & 0x1));
s3c2410_gpio_setpin(S3C2410_GPF6, (val & 0x1));
return 1;
}
static long s3c24xx_pwd_ioctl(struct file * file, unsigned int cmd, unsigned long data)
{
int m_sec = 0,m_msec = 0,m_secex = 0,m_msecex = 0;
m_sec = data/1000;
m_msec = data%1000;
int tdata = 0;
m_secex = (10000 - data)/1000;
m_msecex = (10000 - data)%1000;
printk("m_sec:%d,m_msec:%d,m_secex:%d,m_msecex:%d\n",m_sec,m_msec,m_secex,m_msecex);
switch(data) {
case 500 :
while(1) {
GPFDAT &= ~(1<<2);
GPFDAT |= (1<<2);
GPFDAT &= ~(1<<4);
GPFDAT |= (1<<4);
GPFDAT |= (1<<1);
//mdelay(m_sec);
udelay(500);
GPFDAT &= ~(1<<2);
GPFDAT &= ~(1<<4);
GPFDAT &= ~(1<<1);
mdelay(9);
udelay(500);
tdata++;
if (tdata > 1000)
break;
}
break;
case 1000 :
while(1) {
GPFDAT &= ~(1<<2);
GPFDAT |= (1<<2);
GPFDAT &= ~(1<<4);
GPFDAT |= (1<<4);
GPFDAT |= (1<<1);
mdelay(1);
//udelay(500);
GPFDAT &= ~(1<<2);
GPFDAT &= ~(1<<4);
GPFDAT &= ~(1<<1);
mdelay(9);
//udelay(500);
tdata++;
if (tdata > 1000)
break;
}
break;
case 1500 :
while(1) {
GPFDAT &= ~(1<<2);
GPFDAT |= (1<<2);
GPFDAT &= ~(1<<4);
GPFDAT |= (1<<4);
GPFDAT |= (1<<1);
mdelay(1);
udelay(500);
GPFDAT &= ~(1<<2);
GPFDAT &= ~(1<<4);
GPFDAT &= ~(1<<1);
mdelay(8);
udelay(500);
tdata++;
if (tdata > 1000)
break;
}
break;
case 2000 :
while(1) {
GPFDAT &= ~(1<<2);
GPFDAT |= (1<<2);
GPFDAT &= ~(1<<4);
GPFDAT |= (1<<4);
GPFDAT |= (1<<1);
mdelay(2);
//udelay(500);
GPFDAT &= ~(1<<2);
GPFDAT &= ~(1<<4);
GPFDAT &= ~(1<<1);
mdelay(8);
//udelay(500);
tdata++;
if (tdata > 1000)
break;
}
break;
case 2500 :
while(1) {
GPFDAT &= ~(1<<2);
GPFDAT |= (1<<2);
GPFDAT &= ~(1<<4);
GPFDAT |= (1<<4);
GPFDAT |= (1<<1);
mdelay(2);
udelay(500);
GPFDAT &= ~(1<<2);
GPFDAT &= ~(1<<4);
GPFDAT &= ~(1<<1);
mdelay(7);
udelay(500);
tdata++;
if (tdata > 1000)
break;
}
break;
default:
break;
}
}
/* 這個結構是字符設備驅動程序的核心
* 當應用程序操作設備文件時所調用的open、read、write等函數,
* 最終會調用這個結構中指定的對應函數
*/
static struct file_operations s3c24xx_pwd_fops = {
.owner = THIS_MODULE, /* 這是一個宏,推向編譯模塊時自動創建的__this_module變量 */
.open = s3c24xx_pwd_open,
.write = s3c24xx_leds_write,
.unlocked_ioctl = s3c24xx_pwd_ioctl,
};
/*
* 執行insmod命令時就會調用這個函數
*/
static int __init s3c24xx_pwd_init(void)
{
//設置GPF2引腳,根據定時器輸出0/1
int ret;
int minor = 0;
gpio_va = ioremap(0x56000000, 0x100000);
if (!gpio_va) {
return -EIO;
}
/* 註冊字符設備
* 參數爲主設備號、設備名字、file_operations結構;
* 這樣,主設備號就和具體的file_operations結構聯繫起來了,
* 操作主設備爲PWD_MAJOR的設備文件時,就會調用s3c24xx_pwd_fops中的相關成員函數
* PWD_MAJOR可以設爲0,表示由內核自動分配主設備號
*/
ret = register_chrdev(PWD_MAJOR, DEVICE_NAME, &s3c24xx_pwd_fops);
if (ret < 0) {
printk(DEVICE_NAME " can't register major number\n");
return ret;
}
pwd_class = class_create(THIS_MODULE, "pwdtimer");
if (IS_ERR(pwd_class))
return PTR_ERR(pwd_class);
pwd_class_devs = class_device_create(pwd_class, NULL, MKDEV(PWD_MAJOR, 0), NULL, "pws"); /* /dev/leds */
printk(DEVICE_NAME " initialized\n");
#if 0
long count = 1;
long ti = 10;
long data = 0;
while(1) {
GPFDAT &= ~(1<<2);
GPFDAT |= (1<<2);
GPFDAT &= ~(1<<4);
GPFDAT |= (1<<4);
GPFDAT |= (1<<1);
udelay(500);
GPFDAT &= ~(1<<2);
GPFDAT &= ~(1<<4);
GPFDAT &= ~(1<<1);
mdelay(9);
udelay(500);
count++;
if (count > 1000)
break;
}
printk("100\n");
count = 1;
while(1) {
GPFDAT &= ~(1<<2);
GPFDAT |= (1<<2);
GPFDAT &= ~(1<<4);
GPFDAT |= (1<<4);
GPFDAT |= (1<<1);
mdelay(2);
GPFDAT &= ~(1<<2);
GPFDAT &= ~(1<<4);
GPFDAT &= ~(1<<1);
mdelay(8);
//udelay(500);
count++;
if (count > 1000)
break;
}
#endif
return 0;
}
/*
* 執行rmmod命令時就會調用這個函數
*/
static void __exit s3c24xx_pwd_exit(void)
{
int minor;
/* 卸載驅動程序 */
unregister_chrdev(PWD_MAJOR, DEVICE_NAME);
class_device_unregister(pwd_class_devs);
class_destroy(pwd_class);
iounmap(gpio_va);
hrtimer_cancel(&timer);
}
module_init(s3c24xx_pwd_init);
module_exit(s3c24xx_pwd_exit);
MODULE_LICENSE("GPL");
測試代碼
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
/*
* ledtest <dev> <on|off>
*/
void print_usage(char *file)
{
printf("Usage:\n");
printf("%s <dev> <on|off>\n",file);
printf("eg. \n");
printf("%s /dev/leds on\n", file);
printf("%s /dev/leds off\n", file);
printf("%s /dev/led1 on\n", file);
printf("%s /dev/led1 off\n", file);
}
int main(int argc, char **argv)
{
int fd;
char filename[] = "/dev/pws";
char val;
fd = open(filename, O_RDWR);
if (fd < 0)
{
printf("error, can't open %s\n", filename);
return 0;
}
int i,j;
volatile int count = 2;
volatile int count1 = 9000;
int ret = 0;
while(1){
ret = ioctl(fd, 0, 500);
if (ret) {
perror("ioctl read:");
exit(-4);
}
sleep(2);
#if 0
ret = ioctl(fd, 0, 1000);
if (ret) {
perror("ioctl read:");
exit(-4);
}
sleep(3);
ret = ioctl(fd, 0, 1500);
if (ret) {
perror("ioctl read:");
exit(-4);
}
sleep(3);
ret = ioctl(fd, 0, 2000);
if (ret) {
perror("ioctl read:");
exit(-4);
}
sleep(3);
#endif
ret = ioctl(fd, 0, 2500);
if (ret) {
perror("ioctl read:");
exit(-4);
}
sleep(2);
}
return 0;
}