Jetson tk1 PWM stepper motor control with grinch kernal

Hi all,

I have just installed the grinch 19.3.8 kernal on my Jetson board and got PWM to work. I can modify the period and duty cycle of the PWM pin just fine but I am interested in figuring out how many pulses have actually been outputted so that I can tell the position of a stepper motor with some decent accuracy. Knowing the number of pulses that have occurred is important because one pulse may be equal to 1 degree of rotation for the stepper motor as an example. Does anyone have any idea as to a function or library that would allow me to do this? I have considered keeping time with a clock function but this does not seem like the best way of doing things. Any help is appreciated.


When we use pwm, the output is continuous with frequency of 1/period and pulse time is duty cycle * period_time/100. Are you referring to one period PWM signal? Suppose we have 1ms pwm period, we will have 1KHz PWM signal at output. As this is continuous, there is no mechanism to know how any pulses going by accurately.

I have never found the PWM counter register in NVIDIA TRM, so I take the interrupt routine of GPIO_PU5 to count how many pulse was generated. You can take a try to loop-back PWM0(PIN49 of J3A2) to GPIO_PU5(PIN55 of J3A2) then apply the two patch on 21.2 ( This is not very accurate, because of the GPIO interrupt latency. Maybe the accuracy is sufficient to your control system. Do you want to take the pulse count to be the feed back signal in close loop control system? What is the sampling rate in your system?

Here is an example in 100 pulse per second.
sudo su
echo 163 >/sys/class/gpio/export
echo out >/sys/class/gpio/gpio163/direction
echo 1 >/sys/class/gpio/gpio163/value
echo 0 >/sys/class/gpio/gpio163/value
echo 163 >/sys/class/gpio/unexport
cd /sys/class/pwm/pwmchip0
echo 0 >export
echo 10000000 >pwm0/period
echo 5000000 >pwm0/duty_cycle
echo 1 >pwm0/enable

[ 69.587437] [pwm_count_isr::16]~pwm_g_count 1600~~
[ 70.583949] [pwm_count_isr::16]~pwm_g_count 1700~~
[ 71.583703] [pwm_count_isr::16]~pwm_g_count 1800~~
[ 72.582387] [pwm_count_isr::16]~pwm_g_count 1900~~
[ 73.582643] [pwm_count_isr::16]~pwm_g_count 2000~~
[ 74.581475] [pwm_count_isr::16]~pwm_g_count 2100~~
[ 75.581728] [pwm_count_isr::16]~pwm_g_count 2200~~
[ 76.580770] [pwm_count_isr::16]~pwm_g_count 2300~~
[ 77.579881] [pwm_count_isr::16]~pwm_g_count 2400~~
[ 78.580012] [pwm_count_isr::16]~pwm_g_count 2500~~
[ 79.578618] [pwm_count_isr::16]~pwm_g_count 2600~~
[ 80.578097] [pwm_count_isr::16]~pwm_g_count 2700~~
[ 81.577524] [pwm_count_isr::16]~pwm_g_count 2800~~
[ 82.577224] [pwm_count_isr::16]~pwm_g_count 2900~~
[ 83.576463] [pwm_count_isr::16]~pwm_g_count 3000~~
[ 84.576416] [pwm_count_isr::16]~pwm_g_count 3100~~
[ 85.576442] [pwm_count_isr::16]~pwm_g_count 3200~~
[ 86.575742] [pwm_count_isr::16]~pwm_g_count 3300~~
[ 87.575187] [pwm_count_isr::16]~pwm_g_count 3400~~
[ 88.574714] [pwm_count_isr::16]~pwm_g_count 3500~~
[ 89.574193] [pwm_count_isr::16]~pwm_g_count 3600~~
[ 90.573132] [pwm_count_isr::16]~pwm_g_count 3700~~
[ 91.573268] [pwm_count_isr::16]~pwm_g_count 3800~~
[ 92.572699] [pwm_count_isr::16]~pwm_g_count 3900~~
[ 93.572956] [pwm_count_isr::16]~pwm_g_count 4000~~
[ 94.571693] [pwm_count_isr::16]~pwm_g_count 4100~~

When I try this, it fails with the message:
Unbalanced enable for IRQ 342

I am using Grinch 21.3.4

Is anybody else using the same interrupt?


I am new to Tegra board bring up; by all trial and error, I am able to bring up the board with Kernel L4T 21.4

We have enabled the SPI by going through “

I tried to to enable the pwm modules by looking at the examples and removing the GPIO pins as well us adding the pinmux pwm0, pwm2 and pwm3 as told in “

I also tried to run “cat /sys/kernel/debug/pwm”

platfor/tegra-pwm, 4 PWM devices
pwm-0 (sysfs ): requested enabled
pwm-1 (pwm-backlight ): requested
pwm-2 ((null) ):
pwm-3 ((null) ):

however when I connect scope on pin 49 in J3A2; I do not see any wave form

Please help me to figure this out.

Thanks in Advance


Thank You BingTang,

Hi All,

Found it

The out direction need to be set through the gpio

for pin 49 (pwm0)

sudo su
echo 163 >/sys/class/gpio/export
echo out >/sys/class/gpio/gpio163/direction
echo 1 >/sys/class/gpio/gpio163/value
echo 0 >/sys/class/gpio/gpio163/value
echo 163 >/sys/class/gpio/unexport
cd /sys/class/pwm/pwmchip0
echo 0 >export
echo 10000000 >pwm0/period
echo 5000000 >pwm0/duty_cycle
echo 1 >pwm0/enable

for pin 55 (pwm2)
replace 163 for gpio with 165 and 0 for pwm as 2 and pwm2 directory

for pin 58 (pwm2)
replace 166 for gpio with 166 and 0 for pwm as 3 and pwm3 directory