Cainiao for help !!!

Source: Internet
Author: User
Cainiao for help !!! -- Linux general technology-Linux technology and application information. For details, refer to the following section. First of all, this is the first time I have developed linux. Please do not spray it. Recently, I used the friendly arm mini2440 to change the device driver in the original Board (in fact, it is a simple two-bit operation, I just follow the original imitation) and call always goes wrong (call turn_dir in ioct to report bad address). I wrote the red letter font. Ask strong people, thank you first,

# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include

# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include

# Define DEVICE_NAME "pwm"

# Define PWM_IOCTL_SET_FREQ1
# Define PWM_IOCTL_STOP0
# Define PWM_IOCTL_TURN2

Static struct semaphore lock;

/* Freq: pclk/50/16/65536 ~ Pclk/50/16
* If pclk = 50 MHz, freq is 1Hz to 62500Hz
* Human ear: 20Hz ~ 20000Hz
*/
Static void PWM_TURN (unsigned long dir)
{

If (dir = 1)
{
S3c2410_gpio_cfgpin (S3C2410_GPF (2), S3C2410_GPIO_OUTPUT );
S3c2410_gpio_setpin (S3C2410_GPF (2), 1 );
S3c2410_gpio_cfgpin (S3C2410_GPF (3), S3C2410_GPIO_OUTPUT );
S3c2410_gpio_setpin (S3C2410_GPF (3), 1 );
}

If (dir = 2)
{
S3c2410_gpio_cfgpin (S3C2410_GPF (2), S3C2410_GPIO_OUTPUT );
S3c2410_gpio_setpin (S3C2410_GPF (2), 1 );
S3c2410_gpio_cfgpin (S3C2410_GPF (3), S3C2410_GPIO_OUTPUT );
S3c2410_gpio_setpin (S3C2410_GPF (3), 0 );
}
} Static void PWM_Set_Freq (unsigned long freq)
{
Unsigned long tcon;
Unsigned long tcnt;
Unsigned long tcfg1;
Unsigned long t00000;

Struct clk * clk_p;
Unsigned long pclk;

// Set GPB0 as tout0, pwm output
S3c2410_gpio_cfgpin (S3C2410_GPB (0), S3C2410_GPB0_TOUT0 );

Tcon = _ raw_readl (S3C2410_TCON );
Tcfg1 = _ raw_readl (S3C2410_TCFG1 );
T1_0 = _ raw_readl (s3c2410_t1_0 );

// Prescaler = 50
T00000 & = ~ S3c2410_t1__prescaler0_mask;
T00000 | = (50-1 );

// Mux = 1/16
Tcfg1 & = ~ S3c2410_t1_mux0_mask;
Tcfg1 | = s3c2410_t00000000mux0_div16;

_ Raw_writel (tcfg1, S3C2410_TCFG1 );
_ Raw_writel (t1_0, s3c2410_t1_0 );

Clk_p = clk_get (NULL, "pclk ");
Pclk = clk_get_rate (clk_p );

/*************************************** **************************
Fixed the pwm frequency to 1000Hz.
**************************************** *************************/
Tcnt = (pclk/50/16)/1000;
_ Raw_writel (tcnt, S3C2410_TCNTB (0 ));

/*************************************** **************************
The significance of freq is to change the duty cycle of the pwm wave. This function parameter is used to change the duty cycle.
**************************************** *************************/
_ Raw_writel (tcnt * 10/freq, S3C2410_TCMPB (0 ));

Tcon & = ~ 0x1f;
Tcon | = 0xb; // disable deadzone, auto-reload, inv-off, update TCNTB0 & TCMPB0, start timer 0
_ Raw_writel (tcon, S3C2410_TCON );

Tcon & = ~ 2; // clear manual update bit
_ Raw_writel (tcon, S3C2410_TCON );

}

Static void PWM_Stop (void)
{

S3c2410_gpio_cfgpin (S3C2410_GPB (0), S3C2410_GPIO_OUTPUT );
S3c2410_gpio_setpin (S3C2410_GPB (0), 0 );
}

Static int s3c24xx_pwm_open (struct inode * inode, struct file * file)
{
S3c2410_gpio_cfgpin (S3C2410_GPF (0), S3C2410_GPIO_OUTPUT );
S3c2410_gpio_setpin (S3C2410_GPF (0), 1 );
If (! Down_trylock (& lock ))
Return 0;
Else
Return-EBUSY;
}


Static int s3c24xx_pwm_close (struct inode * inode, struct file * file)
{
PWM_Stop ();
Up (& lock );
Return 0;
}

Static int s3c24xx_pwm_ioctl (struct inode * inode, struct file * file, unsigned int cmd, unsigned long arg)
{
// Printk ("ioctl pwm: % x % lx \ n", cmd, arg );
Switch (cmd ){
Case PWM_IOCTL_SET_FREQ:
If (arg = 0)
Return-EINVAL;
PWM_Set_Freq (arg );
Break;

Case PWM_IOCTL_STOP:
PWM_Stop ();
Break;

Case PWM_IOCTL_TURN:
PWM_TURN (arg );
Break;
} Return 0;
}


Static struct file_operations dev_fops = {
. Owner = THIS_MODULE,
. Open = s3c24xx_pwm_open,
. Release = s3c24xx_pwm_close,
. Ioctl = s3c24xx_pwm_ioctl,
};

Static struct miscdevice misc = {
. Minor = MISC_DYNAMIC_MINOR,
. Name = DEVICE_NAME,
. Fops = & dev_fops,
};

Static int _ init dev_init (void)
{
Int ret;

Init_MUTEX (& lock );
Ret = misc_register (& misc );

Printk (DEVICE_NAME "\ tinitialized \ n ");
Return ret;
}

Static void _ exit dev_exit (void)
{
Misc_deregister (& misc );
}

Module_init (dev_init );
Module_exit (dev_exit );
MODULE_LICENSE ("GPL ");
MODULE_AUTHOR ("FriendlyARM Inc .");
MODULE_DESCRIPTION ("S3C2410/S3C2440 PWM Driver ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.