SlideShare a Scribd company logo
1 of 18
TIMER DEVICE DRIVER 
(SYSTEM TIMER, RTC, WATCHDOG) 
1
系統宣告(arch/arm/mach-ixp4xx) 
MACHINE_START(IXDP425, "Intel IXDP425 Development Platform") 
/* Maintainer: MontaVista Software, Inc. */ 
.phys_io = IXP4XX_PERIPHERAL_BASE_PHYS, 
.io_pg_offst = ((IXP4XX_PERIPHERAL_BASE_VIRT) >> 18) & 
0xfffc, 
.map_io = ixp4xx_map_io, 
.init_irq = ixp4xx_init_irq, 
.timer = &ixp4xx_timer, 
.boot_params = 0x0100, 
.init_machine = ixdp425_init, 
MACHINE_END 
2
struct sys_timer armcpu_timer __initdata = 
{ 
.init = armcpu_timer_init, 
.offset = armcpu_gettimeoffset, 
.resume = armcpu_timer_setup, 
}; 
3 
Timer 資料結構宣告
static void armcpu_timer_setup(void) 
{ 
armcpu_timer_set_reload(USED_TIMER, APB_CLK/HZ); 
armcpu_timer_set_counter(USED_TIMER, APB_CLK/HZ); 
……………… 
} 
void __init armcpu_timer_init(void) 
{ 
armcpu_timer_setup(); 
setup_irq(IRQ_TIMER1, &armcpu_timer_irq); 
} 
4 
Timer Initiailzation
static irqreturn_t 
armcpu_timer_interrupt(int irq, void *dev_id) 
{ 
write_seqlock(&xtime_lock); 
timer_tick(); 
write_sequnlock(&xtime_lock); 
return IRQ_HANDLED; 
} 
5 
Timer interrupt
unsigned long armcpu_gettimeoffset (void) 
{ 
unsigned long volatile offsetticks; 
offsetticks = SET_COUNTER-armcpu_timer_get_counter(USED_TIMER); 
if ( *(volatile unsigned int *)(CPE_TIMER1_VA_BASE+TIMER_INTR_STATE) ) { 
offsetticks = SET_COUNTER-armcpu_timer_get_counter(USED_TIMER); 
offsetticks += SET_COUNTER; 
} 
offsetticks = offsetticks / (APB_CLK / 1000000); // tansfer ticks to usec 
return offsetticks; 
} 
6 
gettimeoffset callback 
#include <sys/time.h> 
int gettimeofday(struct timeval *tv, struct timezone *tz); 
struct timeval { 
time_t tv_sec; /* seconds */ 
suseconds_t tv_usec; /* microseconds */ 
};
RTC DRIVER 
7
簡單來看它就是一個時鐘,通常它只能計 
時至秒。 
大都規格是在一年內誤差為10幾秒。 
它需要電池供電,上電一輩子需要有一次 
啓動的動作,斷電後則時間停止。 
以PC為例系統時間(用date指令,或者任何 
API取得的時間)在開機時會讀入RTC時間並 
設定時間,關機時會將系統時間設回RTC。 
其應用層設定的程式為hwclock,使用的 
device node 為/dev/rtc (10, 135)。 
8 
RTC (Real Time Clock)
Usage: hwclock [-r|--show] [-s|--hctosys] [-w|--systohc] [-l|-- 
localtime] [-u|--utc] [-f FILE] 
Query and set hardware clock (RTC) 
Options: 
-r Show hardware clock time 
-s Set system time from hardware clock 
-w Set hardware clock to system time 
-u Hardware clock is in UTC 
-l Hardware clock is in local time 
-f FILE Use specified device (e.g. /dev/rtc2) 
9 
hwclock usage
RTC Device Driver Example 
可架構在misc之下 
主要是支援兩個ioctl command 
1) RTC_RD_TIME 讀取時間 
2) RTC_SET_TIME 設定時間 
struct rtc_time { 
int tm_sec; 
int tm_min; 
int tm_hour; 
int tm_mday; 
int tm_mon; 
int tm_year; 
int tm_wday; 
int tm_yday; 
int tm_isdst; 
}; 
10
Module Initialize Example 
static struct file_operations rtc_fops = { 
owner:THIS_MODULE, 
ioctl:rtc_ioctl, 
open:rtc_open, 
release:rtc_release, 
}; 
static struct miscdevice rtc_dev = { 
RTC_MINOR, 
"rtc", 
&rtc_fops 
}; 
static int __init rtc_init(void) 
{ 
misc_register(&rtc_dev); 
return 0; 
}
Ioctl Example 
static int 
rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) 
{ 
struct rtc_time rtc_tm; 
switch (cmd) { 
case RTC_RD_TIME: /* Read the time/date from RTC */ 
get_rtc_time(&rtc_tm); 
return copy_to_user((void *) arg, &rtc_tm, sizeof(rtc_tm)) ? -EFAULT : 0; 
case RTC_SET_TIME: /* Set the RTC */ 
if (copy_from_user(&rtc_tm, (struct rtc_time *) arg, sizeof(struct rtc_time))) 
return -EFAULT; 
// set to hardware RTC with the time 
return 0; 
default: 
return -EINVAL; 
} 
}
新RTC Driver 
Device node is put on /dev/misc directory. 
Up to 16 RTC node, from rtc0 
The major number is 254. 
The minor number is from 0. 
These are the same IOCTL command with the 
old type RTC driver. 
More features.
RTC Class Structure 
struct rtc_class_ops { 
int (*open)(struct device *); 
void (*release)(struct device *); 
int (*ioctl)(struct device *, unsigned int, unsigned long); 
int (*read_time)(struct device *, struct rtc_time *); 
int (*set_time)(struct device *, struct rtc_time *); 
int (*read_alarm)(struct device *, struct rtc_wkalrm *); 
int (*set_alarm)(struct device *, struct rtc_wkalrm *); 
int (*proc)(struct device *, struct seq_file *); 
int (*set_mmss)(struct device *, unsigned long secs); 
int (*irq_set_state)(struct device *, int enabled); 
int (*irq_set_freq)(struct device *, int freq); 
int (*read_callback)(struct device *, int data); 
int (*alarm_irq_enable)(struct device *, unsigned int enabled); 
int (*update_irq_enable)(struct device *, unsigned int enabled); 
};
RTC Time structure 
struct rtc_time { 
int tm_sec; 
int tm_min; 
int tm_hour; 
int tm_mday; 
int tm_mon; 
int tm_year; 
int tm_wday; 
int tm_yday; 
int tm_isdst; 
};
New RTC Driver Example 
static int rtc_read_time(struct device *dev, struct rtc_time *tm) 
{ 
……………. 
} 
static int rtc_set_time(struct device *dev, struct rtc_time *tm) 
{ 
………….. 
} 
static struct rtc_class_ops rtc_ops = { 
.read_time = rtc_read_time, 
.set_time = rtc_set_time, 
}; 
static struct rtc_device *rtc; 
static int rtc_module_init(void) 
{ 
………….. 
rtc = rtc_device_register(“name”, NULL, &rtc_ops, THIS_MODULE); 
if ( IS_ERR(rtc) { 
…….. 
} 
return 0; 
} 
static void rtc_module_exit(void) 
{ 
rtc_device_unregister(rtc); 
….. 
}
WATCHDOG DRIVER 
17
什麼是Watchdog 
1) 它是一種可以reset CPU的機制, 在一定的時間 
內需要觸發, 若沒有將會引起CPU reset 
它有什麼注意事項 
1) 小心檔案系統crashed, 因為它是直接CPU reset, 
並沒有做很好的shutdown動作 
2) 可能因為Flash在write state,使得reset之後無 
法boot from flash 
3) 硬體需配合reset CPU週邊元件, 否則系統還是 
無法reset 
View source code newwdt 
18 
Watchdog面面觀

More Related Content

What's hot

grsecurity and PaX
grsecurity and PaXgrsecurity and PaX
grsecurity and PaXKernel TLV
 
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Semtex.c [CVE-2013-2094] - A Linux Privelege EscalationSemtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Semtex.c [CVE-2013-2094] - A Linux Privelege EscalationKernel TLV
 
[嵌入式系統] MCS-51 實驗 - 使用 IAR (2)
[嵌入式系統] MCS-51 實驗 - 使用 IAR (2)[嵌入式系統] MCS-51 實驗 - 使用 IAR (2)
[嵌入式系統] MCS-51 實驗 - 使用 IAR (2)Simen Li
 
U-boot and Android Verified Boot 2.0
U-boot and Android Verified Boot 2.0U-boot and Android Verified Boot 2.0
U-boot and Android Verified Boot 2.0GlobalLogic Ukraine
 
Building Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCCBuilding Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCCKernel TLV
 
Zn task - defcon russia 20
Zn task  - defcon russia 20Zn task  - defcon russia 20
Zn task - defcon russia 20DefconRussia
 
“Automation Testing for Embedded Systems”
“Automation Testing for Embedded Systems” “Automation Testing for Embedded Systems”
“Automation Testing for Embedded Systems” GlobalLogic Ukraine
 
Debugging linux kernel tools and techniques
Debugging linux kernel tools and  techniquesDebugging linux kernel tools and  techniques
Debugging linux kernel tools and techniquesSatpal Parmar
 
Linux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver OverviewLinux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver OverviewRajKumar Rampelli
 
“Linux Kernel CPU Hotplug in the Multicore System”
“Linux Kernel CPU Hotplug in the Multicore System”“Linux Kernel CPU Hotplug in the Multicore System”
“Linux Kernel CPU Hotplug in the Multicore System”GlobalLogic Ukraine
 
[2007 CodeEngn Conference 01] dual5651 - Windows 커널단의 후킹
[2007 CodeEngn Conference 01] dual5651 - Windows 커널단의 후킹[2007 CodeEngn Conference 01] dual5651 - Windows 커널단의 후킹
[2007 CodeEngn Conference 01] dual5651 - Windows 커널단의 후킹GangSeok Lee
 
Linux kernel debugging(ODP format)
Linux kernel debugging(ODP format)Linux kernel debugging(ODP format)
Linux kernel debugging(ODP format)yang firo
 
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)Gavin Guo
 

What's hot (20)

Sysprog 16
Sysprog 16Sysprog 16
Sysprog 16
 
Character drivers
Character driversCharacter drivers
Character drivers
 
Staging driver sins
Staging driver sinsStaging driver sins
Staging driver sins
 
grsecurity and PaX
grsecurity and PaXgrsecurity and PaX
grsecurity and PaX
 
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Semtex.c [CVE-2013-2094] - A Linux Privelege EscalationSemtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
 
[嵌入式系統] MCS-51 實驗 - 使用 IAR (2)
[嵌入式系統] MCS-51 實驗 - 使用 IAR (2)[嵌入式系統] MCS-51 實驗 - 使用 IAR (2)
[嵌入式系統] MCS-51 實驗 - 使用 IAR (2)
 
U-boot and Android Verified Boot 2.0
U-boot and Android Verified Boot 2.0U-boot and Android Verified Boot 2.0
U-boot and Android Verified Boot 2.0
 
Building Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCCBuilding Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCC
 
Zn task - defcon russia 20
Zn task  - defcon russia 20Zn task  - defcon russia 20
Zn task - defcon russia 20
 
I2c drivers
I2c driversI2c drivers
I2c drivers
 
“Automation Testing for Embedded Systems”
“Automation Testing for Embedded Systems” “Automation Testing for Embedded Systems”
“Automation Testing for Embedded Systems”
 
Kernel crashdump
Kernel crashdumpKernel crashdump
Kernel crashdump
 
Debugging linux kernel tools and techniques
Debugging linux kernel tools and  techniquesDebugging linux kernel tools and  techniques
Debugging linux kernel tools and techniques
 
Linux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver OverviewLinux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver Overview
 
“Linux Kernel CPU Hotplug in the Multicore System”
“Linux Kernel CPU Hotplug in the Multicore System”“Linux Kernel CPU Hotplug in the Multicore System”
“Linux Kernel CPU Hotplug in the Multicore System”
 
Debugging linux
Debugging linuxDebugging linux
Debugging linux
 
[2007 CodeEngn Conference 01] dual5651 - Windows 커널단의 후킹
[2007 CodeEngn Conference 01] dual5651 - Windows 커널단의 후킹[2007 CodeEngn Conference 01] dual5651 - Windows 커널단의 후킹
[2007 CodeEngn Conference 01] dual5651 - Windows 커널단의 후킹
 
Linux kernel debugging(ODP format)
Linux kernel debugging(ODP format)Linux kernel debugging(ODP format)
Linux kernel debugging(ODP format)
 
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)
 
Linux interrupts
Linux interruptsLinux interrupts
Linux interrupts
 

Viewers also liked

Selection and Integration of Embedded Display Devices
Selection and Integration of Embedded Display DevicesSelection and Integration of Embedded Display Devices
Selection and Integration of Embedded Display DevicesVIA Embedded
 
RFID embedded - MAGIC-PCB containing MAGICSTRAP
RFID embedded - MAGIC-PCB containing MAGICSTRAPRFID embedded - MAGIC-PCB containing MAGICSTRAP
RFID embedded - MAGIC-PCB containing MAGICSTRAPAlexander M. Schmoldt
 
Serial Communication & Embedded System Interface
Serial Communication & Embedded System InterfaceSerial Communication & Embedded System Interface
Serial Communication & Embedded System InterfaceKUET
 
Serial peripheral Interface - Embedded System Protocol
Serial peripheral Interface - Embedded System ProtocolSerial peripheral Interface - Embedded System Protocol
Serial peripheral Interface - Embedded System ProtocolAditya Porwal
 
FSK , FM DEMODULATOR & VOLTAGE REGULATOR ICS
FSK , FM DEMODULATOR & VOLTAGE REGULATOR ICS  FSK , FM DEMODULATOR & VOLTAGE REGULATOR ICS
FSK , FM DEMODULATOR & VOLTAGE REGULATOR ICS vishalgohel12195
 
Programmable Logic Devices Plds
Programmable Logic Devices PldsProgrammable Logic Devices Plds
Programmable Logic Devices PldsGaditek
 
Embedded system
Embedded systemEmbedded system
Embedded systemmangal das
 
Risc and cisc eugene clewlow
Risc and cisc   eugene clewlowRisc and cisc   eugene clewlow
Risc and cisc eugene clewlowManish Prajapati
 
Prerna sharma
Prerna sharmaPrerna sharma
Prerna sharmaRCET
 
SysTick, Timer & Watchdog
SysTick, Timer & WatchdogSysTick, Timer & Watchdog
SysTick, Timer & WatchdogGiovanni Panice
 
Chapter 19 - Real Time Systems
Chapter 19 - Real Time SystemsChapter 19 - Real Time Systems
Chapter 19 - Real Time SystemsWayne Jones Jnr
 
Microprocessor and Microcontroller lec1
Microprocessor and Microcontroller lec1Microprocessor and Microcontroller lec1
Microprocessor and Microcontroller lec1Ameen San
 
Embedded System
Embedded SystemEmbedded System
Embedded Systemsureskal
 
Embedded c program and programming structure for beginners
Embedded c program and programming structure for beginnersEmbedded c program and programming structure for beginners
Embedded c program and programming structure for beginnersKamesh Mtec
 
Introduction to embedded systems
Introduction to embedded systemsIntroduction to embedded systems
Introduction to embedded systemsApurva Zope
 

Viewers also liked (20)

S emb t7-arch_bus
S emb t7-arch_busS emb t7-arch_bus
S emb t7-arch_bus
 
Embedded systems
Embedded systemsEmbedded systems
Embedded systems
 
Selection and Integration of Embedded Display Devices
Selection and Integration of Embedded Display DevicesSelection and Integration of Embedded Display Devices
Selection and Integration of Embedded Display Devices
 
RFID embedded - MAGIC-PCB containing MAGICSTRAP
RFID embedded - MAGIC-PCB containing MAGICSTRAPRFID embedded - MAGIC-PCB containing MAGICSTRAP
RFID embedded - MAGIC-PCB containing MAGICSTRAP
 
Serial Communication & Embedded System Interface
Serial Communication & Embedded System InterfaceSerial Communication & Embedded System Interface
Serial Communication & Embedded System Interface
 
Embedded c
Embedded cEmbedded c
Embedded c
 
Embedded systems
Embedded systemsEmbedded systems
Embedded systems
 
Linux watchdog timer
Linux watchdog timerLinux watchdog timer
Linux watchdog timer
 
Serial peripheral Interface - Embedded System Protocol
Serial peripheral Interface - Embedded System ProtocolSerial peripheral Interface - Embedded System Protocol
Serial peripheral Interface - Embedded System Protocol
 
FSK , FM DEMODULATOR & VOLTAGE REGULATOR ICS
FSK , FM DEMODULATOR & VOLTAGE REGULATOR ICS  FSK , FM DEMODULATOR & VOLTAGE REGULATOR ICS
FSK , FM DEMODULATOR & VOLTAGE REGULATOR ICS
 
Programmable Logic Devices Plds
Programmable Logic Devices PldsProgrammable Logic Devices Plds
Programmable Logic Devices Plds
 
Embedded system
Embedded systemEmbedded system
Embedded system
 
Risc and cisc eugene clewlow
Risc and cisc   eugene clewlowRisc and cisc   eugene clewlow
Risc and cisc eugene clewlow
 
Prerna sharma
Prerna sharmaPrerna sharma
Prerna sharma
 
SysTick, Timer & Watchdog
SysTick, Timer & WatchdogSysTick, Timer & Watchdog
SysTick, Timer & Watchdog
 
Chapter 19 - Real Time Systems
Chapter 19 - Real Time SystemsChapter 19 - Real Time Systems
Chapter 19 - Real Time Systems
 
Microprocessor and Microcontroller lec1
Microprocessor and Microcontroller lec1Microprocessor and Microcontroller lec1
Microprocessor and Microcontroller lec1
 
Embedded System
Embedded SystemEmbedded System
Embedded System
 
Embedded c program and programming structure for beginners
Embedded c program and programming structure for beginnersEmbedded c program and programming structure for beginners
Embedded c program and programming structure for beginners
 
Introduction to embedded systems
Introduction to embedded systemsIntroduction to embedded systems
Introduction to embedded systems
 

Similar to Linux Timer device driver

Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debuggingJungMinSEO5
 
Embedded JavaScript
Embedded JavaScriptEmbedded JavaScript
Embedded JavaScriptJens Siebert
 
Lee 2020 what the clock !
Lee 2020  what the clock !Lee 2020  what the clock !
Lee 2020 what the clock !Neil Armstrong
 
Timers in Unix/Linux
Timers in Unix/LinuxTimers in Unix/Linux
Timers in Unix/Linuxgeeksrik
 
Attack your Trusted Core
Attack your Trusted CoreAttack your Trusted Core
Attack your Trusted CoreDi Shen
 
망고100 보드로 놀아보자 15
망고100 보드로 놀아보자 15망고100 보드로 놀아보자 15
망고100 보드로 놀아보자 15종인 전
 
Epoll - from the kernel side
Epoll -  from the kernel sideEpoll -  from the kernel side
Epoll - from the kernel sidellj098
 
RTOS implementation
RTOS implementationRTOS implementation
RTOS implementationRajan Kumar
 
[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack FirmwareSimen Li
 
ARM® Cortex™ M Bootup_CMSIS_Part_2_3
ARM® Cortex™ M Bootup_CMSIS_Part_2_3ARM® Cortex™ M Bootup_CMSIS_Part_2_3
ARM® Cortex™ M Bootup_CMSIS_Part_2_3Raahul Raghavan
 
Contiki introduction I.
Contiki introduction I.Contiki introduction I.
Contiki introduction I.Dingxin Xu
 
Kernel Recipes 2015 - Porting Linux to a new processor architecture
Kernel Recipes 2015 - Porting Linux to a new processor architectureKernel Recipes 2015 - Porting Linux to a new processor architecture
Kernel Recipes 2015 - Porting Linux to a new processor architectureAnne Nicolas
 
Taipei.py 2018 - Control device via ioctl from Python
Taipei.py 2018 - Control device via ioctl from Python Taipei.py 2018 - Control device via ioctl from Python
Taipei.py 2018 - Control device via ioctl from Python Hua Chu
 
DSP_Assign_2 (Autosaved)
DSP_Assign_2 (Autosaved)DSP_Assign_2 (Autosaved)
DSP_Assign_2 (Autosaved)Joseph Chandler
 

Similar to Linux Timer device driver (20)

Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debugging
 
Embedded JavaScript
Embedded JavaScriptEmbedded JavaScript
Embedded JavaScript
 
Microkernel Development
Microkernel DevelopmentMicrokernel Development
Microkernel Development
 
Sysprog 12
Sysprog 12Sysprog 12
Sysprog 12
 
Sysprog 12
Sysprog 12Sysprog 12
Sysprog 12
 
Lee 2020 what the clock !
Lee 2020  what the clock !Lee 2020  what the clock !
Lee 2020 what the clock !
 
Timers in Unix/Linux
Timers in Unix/LinuxTimers in Unix/Linux
Timers in Unix/Linux
 
Sysprog17
Sysprog17Sysprog17
Sysprog17
 
Attack your Trusted Core
Attack your Trusted CoreAttack your Trusted Core
Attack your Trusted Core
 
망고100 보드로 놀아보자 15
망고100 보드로 놀아보자 15망고100 보드로 놀아보자 15
망고100 보드로 놀아보자 15
 
Epoll - from the kernel side
Epoll -  from the kernel sideEpoll -  from the kernel side
Epoll - from the kernel side
 
RTOS implementation
RTOS implementationRTOS implementation
RTOS implementation
 
[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware
 
ARM® Cortex™ M Bootup_CMSIS_Part_2_3
ARM® Cortex™ M Bootup_CMSIS_Part_2_3ARM® Cortex™ M Bootup_CMSIS_Part_2_3
ARM® Cortex™ M Bootup_CMSIS_Part_2_3
 
Contiki introduction I.
Contiki introduction I.Contiki introduction I.
Contiki introduction I.
 
RxJava on Android
RxJava on AndroidRxJava on Android
RxJava on Android
 
Ieee 1149.1-2013-tutorial-ijtag
Ieee 1149.1-2013-tutorial-ijtagIeee 1149.1-2013-tutorial-ijtag
Ieee 1149.1-2013-tutorial-ijtag
 
Kernel Recipes 2015 - Porting Linux to a new processor architecture
Kernel Recipes 2015 - Porting Linux to a new processor architectureKernel Recipes 2015 - Porting Linux to a new processor architecture
Kernel Recipes 2015 - Porting Linux to a new processor architecture
 
Taipei.py 2018 - Control device via ioctl from Python
Taipei.py 2018 - Control device via ioctl from Python Taipei.py 2018 - Control device via ioctl from Python
Taipei.py 2018 - Control device via ioctl from Python
 
DSP_Assign_2 (Autosaved)
DSP_Assign_2 (Autosaved)DSP_Assign_2 (Autosaved)
DSP_Assign_2 (Autosaved)
 

More from 艾鍗科技

TinyML - 4 speech recognition
TinyML - 4 speech recognition TinyML - 4 speech recognition
TinyML - 4 speech recognition 艾鍗科技
 
Appendix 1 Goolge colab
Appendix 1 Goolge colabAppendix 1 Goolge colab
Appendix 1 Goolge colab艾鍗科技
 
Project-IOT於餐館系統的應用
Project-IOT於餐館系統的應用Project-IOT於餐館系統的應用
Project-IOT於餐館系統的應用艾鍗科技
 
02 IoT implementation
02 IoT implementation02 IoT implementation
02 IoT implementation艾鍗科技
 
Tiny ML for spark Fun Edge
Tiny ML for spark Fun EdgeTiny ML for spark Fun Edge
Tiny ML for spark Fun Edge艾鍗科技
 
2. 機器學習簡介
2. 機器學習簡介2. 機器學習簡介
2. 機器學習簡介艾鍗科技
 
5.MLP(Multi-Layer Perceptron)
5.MLP(Multi-Layer Perceptron) 5.MLP(Multi-Layer Perceptron)
5.MLP(Multi-Layer Perceptron) 艾鍗科技
 
心率血氧檢測與運動促進
心率血氧檢測與運動促進心率血氧檢測與運動促進
心率血氧檢測與運動促進艾鍗科技
 
利用音樂&情境燈幫助放鬆
利用音樂&情境燈幫助放鬆利用音樂&情境燈幫助放鬆
利用音樂&情境燈幫助放鬆艾鍗科技
 
IoT感測器驅動程式 在樹莓派上實作
IoT感測器驅動程式在樹莓派上實作IoT感測器驅動程式在樹莓派上實作
IoT感測器驅動程式 在樹莓派上實作艾鍗科技
 
無線聲控遙控車
無線聲控遙控車無線聲控遙控車
無線聲控遙控車艾鍗科技
 
最佳光源的研究和實作
最佳光源的研究和實作最佳光源的研究和實作
最佳光源的研究和實作 艾鍗科技
 
無線監控網路攝影機與控制自走車
無線監控網路攝影機與控制自走車無線監控網路攝影機與控制自走車
無線監控網路攝影機與控制自走車 艾鍗科技
 
Reinforcement Learning
Reinforcement LearningReinforcement Learning
Reinforcement Learning艾鍗科技
 
人臉辨識考勤系統
人臉辨識考勤系統人臉辨識考勤系統
人臉辨識考勤系統艾鍗科技
 
智慧家庭Smart Home
智慧家庭Smart Home智慧家庭Smart Home
智慧家庭Smart Home艾鍗科技
 

More from 艾鍗科技 (20)

TinyML - 4 speech recognition
TinyML - 4 speech recognition TinyML - 4 speech recognition
TinyML - 4 speech recognition
 
Appendix 1 Goolge colab
Appendix 1 Goolge colabAppendix 1 Goolge colab
Appendix 1 Goolge colab
 
Project-IOT於餐館系統的應用
Project-IOT於餐館系統的應用Project-IOT於餐館系統的應用
Project-IOT於餐館系統的應用
 
02 IoT implementation
02 IoT implementation02 IoT implementation
02 IoT implementation
 
Tiny ML for spark Fun Edge
Tiny ML for spark Fun EdgeTiny ML for spark Fun Edge
Tiny ML for spark Fun Edge
 
Openvino ncs2
Openvino ncs2Openvino ncs2
Openvino ncs2
 
Step motor
Step motorStep motor
Step motor
 
2. 機器學習簡介
2. 機器學習簡介2. 機器學習簡介
2. 機器學習簡介
 
5.MLP(Multi-Layer Perceptron)
5.MLP(Multi-Layer Perceptron) 5.MLP(Multi-Layer Perceptron)
5.MLP(Multi-Layer Perceptron)
 
3. data features
3. data features3. data features
3. data features
 
心率血氧檢測與運動促進
心率血氧檢測與運動促進心率血氧檢測與運動促進
心率血氧檢測與運動促進
 
利用音樂&情境燈幫助放鬆
利用音樂&情境燈幫助放鬆利用音樂&情境燈幫助放鬆
利用音樂&情境燈幫助放鬆
 
IoT感測器驅動程式 在樹莓派上實作
IoT感測器驅動程式在樹莓派上實作IoT感測器驅動程式在樹莓派上實作
IoT感測器驅動程式 在樹莓派上實作
 
無線聲控遙控車
無線聲控遙控車無線聲控遙控車
無線聲控遙控車
 
最佳光源的研究和實作
最佳光源的研究和實作最佳光源的研究和實作
最佳光源的研究和實作
 
無線監控網路攝影機與控制自走車
無線監控網路攝影機與控制自走車無線監控網路攝影機與控制自走車
無線監控網路攝影機與控制自走車
 
Reinforcement Learning
Reinforcement LearningReinforcement Learning
Reinforcement Learning
 
人臉辨識考勤系統
人臉辨識考勤系統人臉辨識考勤系統
人臉辨識考勤系統
 
智慧家庭Smart Home
智慧家庭Smart Home智慧家庭Smart Home
智慧家庭Smart Home
 
智能健身
智能健身智能健身
智能健身
 

Recently uploaded

%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456KiaraTiradoMicha
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 

Recently uploaded (20)

%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 

Linux Timer device driver

  • 1. TIMER DEVICE DRIVER (SYSTEM TIMER, RTC, WATCHDOG) 1
  • 2. 系統宣告(arch/arm/mach-ixp4xx) MACHINE_START(IXDP425, "Intel IXDP425 Development Platform") /* Maintainer: MontaVista Software, Inc. */ .phys_io = IXP4XX_PERIPHERAL_BASE_PHYS, .io_pg_offst = ((IXP4XX_PERIPHERAL_BASE_VIRT) >> 18) & 0xfffc, .map_io = ixp4xx_map_io, .init_irq = ixp4xx_init_irq, .timer = &ixp4xx_timer, .boot_params = 0x0100, .init_machine = ixdp425_init, MACHINE_END 2
  • 3. struct sys_timer armcpu_timer __initdata = { .init = armcpu_timer_init, .offset = armcpu_gettimeoffset, .resume = armcpu_timer_setup, }; 3 Timer 資料結構宣告
  • 4. static void armcpu_timer_setup(void) { armcpu_timer_set_reload(USED_TIMER, APB_CLK/HZ); armcpu_timer_set_counter(USED_TIMER, APB_CLK/HZ); ……………… } void __init armcpu_timer_init(void) { armcpu_timer_setup(); setup_irq(IRQ_TIMER1, &armcpu_timer_irq); } 4 Timer Initiailzation
  • 5. static irqreturn_t armcpu_timer_interrupt(int irq, void *dev_id) { write_seqlock(&xtime_lock); timer_tick(); write_sequnlock(&xtime_lock); return IRQ_HANDLED; } 5 Timer interrupt
  • 6. unsigned long armcpu_gettimeoffset (void) { unsigned long volatile offsetticks; offsetticks = SET_COUNTER-armcpu_timer_get_counter(USED_TIMER); if ( *(volatile unsigned int *)(CPE_TIMER1_VA_BASE+TIMER_INTR_STATE) ) { offsetticks = SET_COUNTER-armcpu_timer_get_counter(USED_TIMER); offsetticks += SET_COUNTER; } offsetticks = offsetticks / (APB_CLK / 1000000); // tansfer ticks to usec return offsetticks; } 6 gettimeoffset callback #include <sys/time.h> int gettimeofday(struct timeval *tv, struct timezone *tz); struct timeval { time_t tv_sec; /* seconds */ suseconds_t tv_usec; /* microseconds */ };
  • 8. 簡單來看它就是一個時鐘,通常它只能計 時至秒。 大都規格是在一年內誤差為10幾秒。 它需要電池供電,上電一輩子需要有一次 啓動的動作,斷電後則時間停止。 以PC為例系統時間(用date指令,或者任何 API取得的時間)在開機時會讀入RTC時間並 設定時間,關機時會將系統時間設回RTC。 其應用層設定的程式為hwclock,使用的 device node 為/dev/rtc (10, 135)。 8 RTC (Real Time Clock)
  • 9. Usage: hwclock [-r|--show] [-s|--hctosys] [-w|--systohc] [-l|-- localtime] [-u|--utc] [-f FILE] Query and set hardware clock (RTC) Options: -r Show hardware clock time -s Set system time from hardware clock -w Set hardware clock to system time -u Hardware clock is in UTC -l Hardware clock is in local time -f FILE Use specified device (e.g. /dev/rtc2) 9 hwclock usage
  • 10. RTC Device Driver Example 可架構在misc之下 主要是支援兩個ioctl command 1) RTC_RD_TIME 讀取時間 2) RTC_SET_TIME 設定時間 struct rtc_time { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; }; 10
  • 11. Module Initialize Example static struct file_operations rtc_fops = { owner:THIS_MODULE, ioctl:rtc_ioctl, open:rtc_open, release:rtc_release, }; static struct miscdevice rtc_dev = { RTC_MINOR, "rtc", &rtc_fops }; static int __init rtc_init(void) { misc_register(&rtc_dev); return 0; }
  • 12. Ioctl Example static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { struct rtc_time rtc_tm; switch (cmd) { case RTC_RD_TIME: /* Read the time/date from RTC */ get_rtc_time(&rtc_tm); return copy_to_user((void *) arg, &rtc_tm, sizeof(rtc_tm)) ? -EFAULT : 0; case RTC_SET_TIME: /* Set the RTC */ if (copy_from_user(&rtc_tm, (struct rtc_time *) arg, sizeof(struct rtc_time))) return -EFAULT; // set to hardware RTC with the time return 0; default: return -EINVAL; } }
  • 13. 新RTC Driver Device node is put on /dev/misc directory. Up to 16 RTC node, from rtc0 The major number is 254. The minor number is from 0. These are the same IOCTL command with the old type RTC driver. More features.
  • 14. RTC Class Structure struct rtc_class_ops { int (*open)(struct device *); void (*release)(struct device *); int (*ioctl)(struct device *, unsigned int, unsigned long); int (*read_time)(struct device *, struct rtc_time *); int (*set_time)(struct device *, struct rtc_time *); int (*read_alarm)(struct device *, struct rtc_wkalrm *); int (*set_alarm)(struct device *, struct rtc_wkalrm *); int (*proc)(struct device *, struct seq_file *); int (*set_mmss)(struct device *, unsigned long secs); int (*irq_set_state)(struct device *, int enabled); int (*irq_set_freq)(struct device *, int freq); int (*read_callback)(struct device *, int data); int (*alarm_irq_enable)(struct device *, unsigned int enabled); int (*update_irq_enable)(struct device *, unsigned int enabled); };
  • 15. RTC Time structure struct rtc_time { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; };
  • 16. New RTC Driver Example static int rtc_read_time(struct device *dev, struct rtc_time *tm) { ……………. } static int rtc_set_time(struct device *dev, struct rtc_time *tm) { ………….. } static struct rtc_class_ops rtc_ops = { .read_time = rtc_read_time, .set_time = rtc_set_time, }; static struct rtc_device *rtc; static int rtc_module_init(void) { ………….. rtc = rtc_device_register(“name”, NULL, &rtc_ops, THIS_MODULE); if ( IS_ERR(rtc) { …….. } return 0; } static void rtc_module_exit(void) { rtc_device_unregister(rtc); ….. }
  • 18. 什麼是Watchdog 1) 它是一種可以reset CPU的機制, 在一定的時間 內需要觸發, 若沒有將會引起CPU reset 它有什麼注意事項 1) 小心檔案系統crashed, 因為它是直接CPU reset, 並沒有做很好的shutdown動作 2) 可能因為Flash在write state,使得reset之後無 法boot from flash 3) 硬體需配合reset CPU週邊元件, 否則系統還是 無法reset View source code newwdt 18 Watchdog面面觀