微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > ARM-Linux驱动--RTC(实时时钟)驱动分析

ARM-Linux驱动--RTC(实时时钟)驱动分析

时间:11-20 来源:互联网 点击:

  1. rtc_tm->tm_min=bcd2bin(rtc_tm->rtc_tm->tm_min=bcd2bin(rtc_tm->tm_min);
  2. rtc_tm->tm_hour=bcd2bin(rtc_tm->tm_hour);
  3. rtc_tm->tm_mday=bcd2bin(rtc_tm->tm_mday);
  4. rtc_tm->tm_mon=bcd2bin(rtc_tm->tm_mon);
  5. rtc_tm->tm_year=bcd2bin(rtc_tm->tm_year);
  6. rtc_tm->tm_year+=100;
  7. rtc_tm->tm_mon-=1;
  8. return0;
  9. }
  10. staticints3c_rtc_settime(structdevice*dev,structrtc_time*tm)
  11. {
  12. void__iomem*base=s3c_rtc_base;
  13. intyear=tm->tm_year-100;
  14. pr_debug("settime%02d.%02d.%02d%02d/%02d/%02d\n",
  15. tm->tm_year,tm->tm_mon,tm->tm_mday,
  16. tm->tm_hour,tm->tm_min,tm->tm_sec);
  17. /*wegetaroundy2kbysimplynotsupportingit*/
  18. if(year<0||year>=100){
  19. dev_err(dev,"rtconlysupports100years\n");
  20. return-EINVAL;
  21. }
  22. writeb(bin2bcd(tm->tm_sec),base+S3C2410_RTCSEC);
  23. writeb(bin2bcd(tm->tm_min),base+S3C2410_RTCMIN);
  24. writeb(bin2bcd(tm->tm_hour),base+S3C2410_RTCHOUR);
  25. writeb(bin2bcd(tm->tm_mday),base+S3C2410_RTCDATE);
  26. writeb(bin2bcd(tm->tm_mon+1),base+S3C2410_RTCMON);
  27. writeb(bin2bcd(year),base+S3C2410_RTCYEAR);
  28. return0;
  29. }

到这里RTC驱动的计时功能实现,报警功能还没有完成。下面是这个驱动源代码

  1. #include
  2. #include
  3. #include
  4. #include
  5. #include
  6. #includeinterrupt.h>
  7. #include
  8. #include
  9. #include
  10. #include
  11. #include
  12. #include
  13. #include
  14. #include
  15. #include
  16. staticstructresource*s3c_rtc_mem;
  17. staticvoid__iomem*s3c_rtc_base;
  18. staticints3c_rtc_tickno=NO_IRQ;
  19. staticDEFINE_SPINLOCK(s3c_rtc_pie_lock);
  20. staticirqreturn_ts3c_rtc_tickirq(intirq,void*id)
  21. {
  22. structrtc_device*rdev=id;
  23. rtc_update_irq(rdev,1,RTC_PF|RTC_IRQF);
  24. returnIRQ_HANDLED;
  25. }
  26. /*Updatecontrolregisters*/
  27. staticvoids3c_rtc_setaie(intto)
  28. {
  29. unsignedinttmp;
  30. pr_debug("%s:aie=%d\n",__func__,to);
  31. tmp=readb(s3c_rtc_base+S3C2410_RTCALM)&~S3C2410_RTCALM_ALMEN;
  32. if(to)
  33. tmp|=S3C2410_RTCALM_ALMEN;
  34. writeb(tmp,s3c_rtc_base+S3C2410_RTCALM);
  35. }
  36. staticints3c_rtc_setpie(structdevice*dev,intenabled)
  37. {
  38. unsignedinttmp;
  39. pr_debug("%s:pie=%d\n",__func__,enabled);
  40. spin_lock_irq(&s3c_rtc_pie_lock);
  41. tmp=readb(s3c_rtc_base+S3C2410_TICNT)&~S3C2410_TICNT_ENABLE;
  42. if(enabled)
  43. tmp|=S3C2410_TICNT_ENABLE;
  44. writeb(tmp,s3c_rtc_base+S3C2410_TICNT);
  45. spin_unlock_irq(&s3c_rtc_pie_lock);
  46. return0;
  47. }
  48. staticints3c_rtc_setfreq(structdevice*dev,intfreq)
  49. {
  50. unsignedinttmp;
  51. spin_lock_irq(&s3c_rtc_pie_lock);
  52. tmp=readb(s3c_rtc_base+S3C2410_TICNT)&S3C2410_TICNT_ENABLE;
  53. tmp|=(128/freq)-1;
  54. writeb(tmp,s3c_rtc_base+S3C2410_TICNT);
  55. spin_unlock_irq(&s3c_rtc_pie_lock);
  56. return0;
  57. }
  58. /*Timeread/write*/
  59. staticints3c_rtc_gettime(structdevice*dev,structrtc_time*rtc_tm)
  60. {
  61. unsignedinthave_retried=0;
  62. void__iomem*base=s3c_rtc_base;
  63. retry_get_time:
  64. rtc_tm->tm_min=readb(base+S3C2410_RTCMIN);
  65. rtc_tm->tm_hour=readb(base+S3C2410_RTCHOUR);
  66. rtc_tm->tm_mday=readb(base+S3C2410_RTCDATE);
  67. rtc_tm->tm_mon=readb(base+S3C2410_RTCMON);
  68. rtc_tm->tm_year=readb(base+S3C2410_RTCYEAR);
  69. rtc_tm->tm_sec=readb(base+S3C2410_RTCSEC);
  70. /*theonlywaytoworkoutwetherthesystemwasmid-update
  71. *whenwereaditistocheckthesecondcounter,andifit
  72. *iszero,thenwere-trytheentireread
  73. */
  74. if(rtc_tm->tm_sec==0&&!have_retried){
  75. have_retried=1;
  76. gotoretry_get_time;
  77. }
  78. pr_debug("readtime%02x.%02x.%02x%02x/%02x/%02x\n",
  79. rtc_tm->tm_year,rtc_tm->tm_mon,rtc_tm->tm_mday,
  80. rtc_tm->tm_hour,rtc_tm->tm_min,rtc_tm->tm_sec);
  81. rtc_tm->tm_sec=bcd2bin(rtc_tm->tm_sec);
  82. rtc_tm->tm_min=bcd2bin(rtc_tm->tm_min);
  83. rtc_tm->tm_hour=bcd2bin(rtc_tm->tm_hour);
  84. rtc_tm->tm_mday=bcd2bin(rtc_tm->tm_mday);
  85. rtc_tm->tm_mon=bcd2bin(rtc_tm->tm_mon);
  86. rtc_tm->tm_year=bcd2bin(rtc_tm->tm_year);
  87. rtc_tm->tm_year+=100;
  88. rtc_tm->tm_mon-=1;
  89. return0;
  90. }
  91. staticints3c_rtc_settime(structdevice*dev,structrtc_time*tm)
  92. {
  93. void__iomem*base=s3c_rtc_base;
  94. intyear=tm->tm_year-100;
  95. pr_debug("settime%02d.%02d.%02d%02d/%02d/%02d\n",
  96. tm->tm_year,tm->tm_mon,tm->tm_mday,
  97. tm->tm_hour,tm->tm_min,tm->tm_sec);
  98. /*wegetaroundy2kbysimplynotsupportingit*/
  99. if(year<0||year>=100){
  100. dev_err(dev,"rtconlysupports100years\n");
  101. return-EINVAL;
  102. }
  103. writeb(bin2bcd(tm->writeb(bin2bcd(tm->tm_sec)

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top