I need to implement PWM-module for linux kernel, it should be high resolution ( will be cool more than 10kHz on my 400MHz ARM cpu) Can anyone give me an advice, what I can use for that purpose? Kernel version is 2.6.28.9. Device hasn't a harware PWM driver. Now I use a kernel timer the frequency of which is set by CONFIG_HZ. Default value of this config is 100HZ, but it too little for me, maximum value is 4kHZ, but this is still insufficient
Pulse-width modulation is very, very hard to do in software. In theory, all you have to do is to find a timer in your CPU which has a good enough resolution, attach an IRQ handler and kick it off.
If you look at the clocks, it feels insane that you can't do a 10 kHz PWM on a 400 MHz CPU - you would have 400000/10 = 40000 CPU cycles per pulse which is surely enough?
Maybe not. The linux kernel uses interrupts itself to do kernel calls. Then you have other interrupts (DMA, NMIs, other timers, ...). Depending on the priority of the timer interrupt, some of them can block it. Which means that a network operation or something else can delay your IRQ handler. If you're using Linux kernel APIs to handle the interrupt, then you have more problems: The kernel won't call your handler. Instead, the real IRQ handler will collect some information about the interrupt and append this as a task to a chain.
Eventually, the kernel will look at that chain and process the tasks in it.
Another problem is that the CPU won't respond to an IRQ right away as well. There are op codes which you can't interrupt.
This leads to a lot of jitter.
If you want to try it, then you should write your own IRQ handler in assembler which does all the processing - taking the IRQ and writing the new output values. Don't use the standard linux interrupt API. Then you will have to disable the interrupt API for this timer (or the kernel might remove your handler).
You must use a timer which has a very, very high priority in your CPU - anything that can delay it will ruin your day.
And even if you have 40K CPU cycles to spend: There is always other software which also wants a bite. So you may not be able to run anything else.