iwdg_main.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/cdev.h>
  5. #include <linux/fs.h>
  6. #include <linux/errno.h>
  7. #include <asm/current.h>
  8. #include <linux/sched.h>
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <asm/uaccess.h>
  12. #include <linux/delay.h>
  13. #include <linux/uaccess.h>
  14. #include <asm-generic/ioctl.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irqreturn.h>
  17. #include <linux/wait.h>
  18. #include "stm32f4xx.h"
  19. #include "stm32f4xx_hal_gpio.h"
  20. #include "stm32_hal_legacy.h"
  21. #include "stm32f4xx_hal_iwdg.h"
  22. #include "driver.h"
  23. #define DEVNAME "watchdog"
  24. dev_t devno;
  25. struct cdev cdev;
  26. IWDG_HandleTypeDef IwdgHandle;
  27. static int iwdg_open(struct inode *inode, struct file *filep);
  28. static int iwdg_close(struct inode *inode, struct file *filep);
  29. static ssize_t iwdg_read(struct file *filep, char __user *buf, size_t size, loff_t *offset);
  30. static ssize_t iwdg_write(struct file *filep, const char __user *buf, size_t size, loff_t *offset);
  31. static int iwdg_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg);
  32. static struct file_operations iwdg_ops =
  33. {
  34. .owner = THIS_MODULE,
  35. .open = iwdg_open,
  36. .release = iwdg_close,
  37. .ioctl = iwdg_ioctl,
  38. .read = iwdg_read,
  39. .write = iwdg_write,
  40. };
  41. static int iwdg_open(struct inode *inode, struct file *filep)
  42. {
  43. return 0;
  44. }
  45. static int iwdg_close(struct inode *inode, struct file *filep)
  46. {
  47. return 0;
  48. }
  49. void iwdg_hw_init(void)
  50. {
  51. IwdgHandle.Instance = IWDG;
  52. IwdgHandle.Init.Prescaler = IWDG_PRESCALER_128; //4ms
  53. IwdgHandle.Init.Reload = 2500; //10s //最大4095
  54. if (HAL_IWDG_Init(&IwdgHandle) != HAL_OK)
  55. {
  56. /* Initialization Error */
  57. printk("Init iwdg error!\n");
  58. }
  59. }
  60. static int iwdg_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg)
  61. {
  62. int retval = 0;
  63. switch(cmd)
  64. {
  65. case FEED_WATCHDOG:
  66. if (HAL_IWDG_Refresh(&IwdgHandle) != HAL_OK)
  67. {
  68. printk("Feed IWDG error!\n");
  69. return -1;
  70. }
  71. break;
  72. case START_IWATCHDOG:
  73. iwdg_hw_init();
  74. printk("Independent Watch dog start...\n");
  75. break;
  76. case STOP_IWATCHDOG:
  77. break;
  78. default:
  79. printk("---> Invalid action\n");
  80. return -1;
  81. break;
  82. }
  83. return retval;
  84. }
  85. static ssize_t iwdg_read(struct file *filep, char __user *buf, size_t size, loff_t *offset)
  86. {
  87. return 0;
  88. }
  89. static ssize_t iwdg_write(struct file *filep, const char __user *buf, size_t size, loff_t *offset)
  90. {
  91. return 0;
  92. }
  93. static int __init iwdg_init(void)
  94. {
  95. printk("iwdg module start...\n");
  96. devno = MKDEV(MAJOR_IWDG, 0);
  97. register_chrdev_region(devno, 1, DEVNAME);
  98. cdev_add(&cdev, devno, 1);
  99. cdev_init(&cdev, &iwdg_ops);
  100. //iwdg_hw_init();
  101. return 0;
  102. }
  103. static void __exit iwdg_exit(void)
  104. {
  105. unregister_chrdev_region(devno, 1);
  106. cdev_del(&cdev);
  107. }
  108. module_init(iwdg_init);
  109. module_exit(iwdg_exit);
  110. MODULE_LICENSE("GPL");
  111. MODULE_AUTHOR("Jimbo");
  112. MODULE_DESCRIPTION("this is watchdog module");