Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/err.h>
  4. #include <linux/timer.h>
  5.  
  6. //#include <asm/io.h>
  7. //#include <mach/platform.h>
  8. //#include <linux/device.h>
  9.  
  10. //#define __io_address(n)     IOMEM(IO_ADDRESS(n))
  11. //#define IOMEM(x)  ((void __force __iomem *)(x))
  12. //#define IOMEM(x)  (x)
  13.  
  14. #define IO_ADDRESS(x)               (((x) & 0x00ffffff) + (((x) >> 4) & 0x0f000000) + 0xf0000000)
  15. #define BCM2708_PERI_BASE           0x3F000000
  16.  
  17. #define GPIO_OFFSET                 0x200000
  18. #define GPIO_BASE                   (BCM2708_PERI_BASE + GPIO_OFFSET)
  19.  
  20.  
  21. MODULE_LICENSE("GPL");
  22.  
  23. struct GpioRegisters
  24. {
  25.     uint32_t GPFSEL[6];
  26.     uint32_t Reserved1;
  27.     uint32_t GPSET[2];
  28.     uint32_t Reserved2;
  29.     uint32_t GPCLR[2];
  30. };
  31.  
  32. struct GpioRegisters *s_pGpioRegisters;
  33.  
  34. static void SetGPIOFunction(int GPIO, int functionCode)
  35. {
  36.     int registerIndex = GPIO / 10;
  37.     int bit = (GPIO % 10) * 3;
  38.  
  39.     unsigned oldValue = s_pGpioRegisters->GPFSEL[registerIndex];
  40.     unsigned mask = 0b111 << bit;
  41.     printk("Changing function of GPIO%d from %x to %x\n", GPIO, (oldValue >> bit) & 0b111, functionCode);
  42.     s_pGpioRegisters->GPFSEL[registerIndex] = (oldValue & ~mask) | ((functionCode << bit) & mask);
  43. }
  44.  
  45. static void SetGPIOOutputValue(int GPIO, bool outputValue)
  46. {
  47.     if (outputValue)
  48.         s_pGpioRegisters->GPSET[GPIO / 32] = (1 << (GPIO % 32));
  49.     else
  50.         s_pGpioRegisters->GPCLR[GPIO / 32] = (1 << (GPIO % 32));
  51. }
  52.  
  53. static struct timer_list s_BlinkTimer;
  54. static int s_BlinkPeriod = 1000;
  55. static const int LedGpioPin = 25;
  56.  
  57. static void BlinkTimerHandler(unsigned long unused)
  58. {
  59.     static bool on = false;
  60.     on = !on;
  61.     SetGPIOOutputValue(LedGpioPin, on);
  62.     mod_timer(&s_BlinkTimer, jiffies + msecs_to_jiffies(s_BlinkPeriod));
  63. }
  64.  
  65. static int __init LedBlinkModule_init(void)
  66. {
  67.     int result;
  68.     unsigned int address;
  69.     //address = IO_ADDRESS(GPIO_BASE);
  70.     address = 0xF3200000;
  71.     printk("address: %d\n", address);
  72.     s_pGpioRegisters = (struct GpioRegisters *)address;
  73.        
  74.     SetGPIOFunction(LedGpioPin, 0b001); //Configure the pin as output
  75.    
  76.     setup_timer(&s_BlinkTimer, BlinkTimerHandler, 0);
  77.     result = mod_timer(&s_BlinkTimer, jiffies + msecs_to_jiffies(s_BlinkPeriod));
  78.     BUG_ON(result < 0);
  79.  
  80.     return 0;
  81. }
  82.  
  83. static void __exit LedBlinkModule_exit(void)
  84. {
  85.     SetGPIOFunction(LedGpioPin, 0); //Configure the pin as input
  86.     del_timer(&s_BlinkTimer);
  87. }
  88.  
  89. module_init(LedBlinkModule_init);
  90. module_exit(LedBlinkModule_exit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement