環境:
主機:Fedora 12
目標板:MINI6410
目標板LINUX內核版本:2.6.38
實現功能:
延遲2S
方法1:利用系統全局變量jiffies
jiffies記錄系統節拍,每一次節拍,內核時鐘中斷函數會將jiffies加1.
HZ在ARM中為100,表示1S被分為100份,系統每個節拍為10ms.
修改上篇《Linux驅動編寫:LED驅動測試》(見 http://www.linuxidc.com/Linux/2012-01/51818.htm)中的ioctl函數,測試延時
[cpp]
- <span style="font-family:'Arial Black';font-size:18px;">//功能:ioctl操作函數
- //返回值:成功返回0
- static long led_driver_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
- {
- unsigned int temp = 0;
- unsigned long t = 0;
-
- temp = readl(S3C64XX_GPKDAT);
- if (cmd == 0)
- {
- temp &= ~(1 << (arg + 3));
- }
- else
- {
- temp |= 1 << (arg + 3);
- }
-
- //等待2S
- t = jiffies;
- while (time_after(jiffies,t + 2 * HZ) != 1);
- writel(temp,S3C64XX_GPKDAT);
-
- printk (DEVICE_NAME"\tjdh:led_driver cmd=%d arg=%d jiffies = %d\n",cmd,arg,jiffies);
-
- return 0;
- }</span>
這個方法會使內核忙等待,會影響系統效率.
2.利用宏ndelay(n),延時ns
udelay(n),延時us
mdelay(n),延時ms
這個簡單,不做介紹