Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/err.h>
- #include <linux/timer.h>
- //#include <asm/io.h>
- //#include <mach/platform.h>
- //#include <linux/device.h>
- //#define __io_address(n) IOMEM(IO_ADDRESS(n))
- //#define IOMEM(x) ((void __force __iomem *)(x))
- //#define IOMEM(x) (x)
- #define IO_ADDRESS(x) (((x) & 0x00ffffff) + (((x) >> 4) & 0x0f000000) + 0xf0000000)
- #define BCM2708_PERI_BASE 0x3F000000
- #define GPIO_OFFSET 0x200000
- #define GPIO_BASE (BCM2708_PERI_BASE + GPIO_OFFSET)
- MODULE_LICENSE("GPL");
- struct GpioRegisters
- {
- uint32_t GPFSEL[6];
- uint32_t Reserved1;
- uint32_t GPSET[2];
- uint32_t Reserved2;
- uint32_t GPCLR[2];
- };
- struct GpioRegisters *s_pGpioRegisters;
- static void SetGPIOFunction(int GPIO, int functionCode)
- {
- int registerIndex = GPIO / 10;
- int bit = (GPIO % 10) * 3;
- unsigned oldValue = s_pGpioRegisters->GPFSEL[registerIndex];
- unsigned mask = 0b111 << bit;
- printk("Changing function of GPIO%d from %x to %x\n", GPIO, (oldValue >> bit) & 0b111, functionCode);
- s_pGpioRegisters->GPFSEL[registerIndex] = (oldValue & ~mask) | ((functionCode << bit) & mask);
- }
- static void SetGPIOOutputValue(int GPIO, bool outputValue)
- {
- if (outputValue)
- s_pGpioRegisters->GPSET[GPIO / 32] = (1 << (GPIO % 32));
- else
- s_pGpioRegisters->GPCLR[GPIO / 32] = (1 << (GPIO % 32));
- }
- static struct timer_list s_BlinkTimer;
- static int s_BlinkPeriod = 1000;
- static const int LedGpioPin = 25;
- static void BlinkTimerHandler(unsigned long unused)
- {
- static bool on = false;
- on = !on;
- SetGPIOOutputValue(LedGpioPin, on);
- mod_timer(&s_BlinkTimer, jiffies + msecs_to_jiffies(s_BlinkPeriod));
- }
- static int __init LedBlinkModule_init(void)
- {
- int result;
- unsigned int address;
- //address = IO_ADDRESS(GPIO_BASE);
- address = 0xF3200000;
- printk("address: %d\n", address);
- s_pGpioRegisters = (struct GpioRegisters *)address;
- SetGPIOFunction(LedGpioPin, 0b001); //Configure the pin as output
- setup_timer(&s_BlinkTimer, BlinkTimerHandler, 0);
- result = mod_timer(&s_BlinkTimer, jiffies + msecs_to_jiffies(s_BlinkPeriod));
- BUG_ON(result < 0);
- return 0;
- }
- static void __exit LedBlinkModule_exit(void)
- {
- SetGPIOFunction(LedGpioPin, 0); //Configure the pin as input
- del_timer(&s_BlinkTimer);
- }
- module_init(LedBlinkModule_init);
- module_exit(LedBlinkModule_exit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement