/** * init_module() - This function is called when the module is loaded * * Return: On succes the function retuns a 0 (SUCCES). Otherwise a * negative number is returned. */ int init_module(void) { Major = register_chrdev(0, DEVICE_NAME, &fops); if (Major < 0) { printk(KERN_ALERT "Registering char device failed with %d\n", Major); return Major; } printk(KERN_INFO "I was assigned major number %d. To talk to\n", Major); printk(KERN_INFO "the driver, create a device file with\n"); printk(KERN_INFO "'mknod /dev/%s c %d 0'.\n", DEVICE_NAME, Major); printk(KERN_INFO "Try various minor numbers. Try to echo to\n"); printk(KERN_INFO "the device file.\n"); printk(KERN_INFO "Remove the device file and module when done.\n"); gpio = (volatile unsigned int *)ioremap(GPIO_BASE, 1024); pwm = (volatile unsigned int *)ioremap(PWM_BASE, 1024); clk = (volatile unsigned int *)ioremap(CLOCK_BASE, 1024); // Set pin directions for the output INP_GPIO(GPIO_OUTP); OUT_GPIO(GPIO_OUTP); // PWM and clock settings SET_GPIO_ALT(GPIO_PWM1, GPIO_ALT); // stop clock and waiting for busy flag doesn't work, so kill clock *(clk + CLK_CTL) = CLK_PASSWD | CLK_CTL_KILL; udelay(10); // Set clock divider *(clk + CLK_DIV) = CLK_PASSWD | CLK_DIV_DIVI(96); // source=osc and enable clock *(clk + CLK_CTL) = CLK_PASSWD | CLK_CTL_ENAB | CLK_CTL_SRC(CLK_CTL_SRC_OSC); // disable PWM and start with a clean register *(pwm + PWM_CTL) = 0; // needs some time until the PWM module gets disabled, without the delay the PWM module crashes udelay(10); // Set the PWM range *(pwm + PWM_RNG2) = 4000; // Initialize with a 50% dutycycle setServo(50); // start PWM in M/S transmission mode *(pwm + PWM_CTL) = PWM_CTL_MSEN2 | PWM_CTL_PWEN2; interrupt_config(); return SUCCESS; }
void main() { unsigned char j; TRISD=0xFE; //RD0 is output HLVDCON=0x1C; //config HLVD module while(IRVST==0); //wait until internal reference is stable interrupt_config(); //configure HLVD interrupt RD0=0; //trip indicator while(1) { asm("SLEEP"); //go into sleep mode } }
void main() { TRISA=0x0F; //configure RA0-RA3 as input & RA4-RA5 as output TRISD=0xFC; //RD0-RD1 is output //configure RA0-RA3 as analog inputs PCFG0=1; PCFG1=1; PCFG2=0; PCFG3=1; //configure comparator CM0=1; CM1=0; CM2=0; interrupt_config(); //configure comparator interrupt RD1=0; while(1) { //toggle RD0 every 1s RD0=1; delay(1000); RD0=0; delay(1000); } }