PWM in NanoPi M4

From wiki.ferrari.mo.it
Revision as of 16:59, 3 April 2020 by Admin (talk | contribs) (→‎PWM)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Preparing NanoPi M4 for PWM[edit]

Wiring[edit]

Review-NPIM4p31.jpg

Installation[edit]

Download the file

rk3399-eflasher-friendlycore-bionic-4.4-arm64-20191227.img

from the FriendlyElec repository.

Then, install it in a 16GB flash memory through balenaEtcher or equivalent software.

You need to connect USB Keyboard and Mouse, and an HDMI monitor. Select the OS and click on Install.

The OS will be installed in the eMMC card (it's not included in the board, so it's necessary to buy as option).

Test[edit]

Connect the USB2Serial to the 4pins between the USB ports. It's necessary to cut a slice because it doesn't fit properly. The black wire is the most external.

Configuration:

1500000 Baud
8 Data bits
1 Stop bit
Parity None
Flow Control XON/XOFF

Username root, password fa .

PWM[edit]

Stop and disable the pwm-fan service

service pwm-fan stop
systemctl disable pwm-fan

Then, start the PWM

echo 0 > /sys/class/pwm/pwmchip1/export
echo 100000 > /sys/class/pwm/pwmchip1/pwm0/period
echo normal > /sys/class/pwm/pwmchip1/pwm0/polarity
echo 5000 > /sys/class/pwm/pwmchip1/pwm0/duty_cycle
echo 1 > /sys/class/pwm/pwmchip1/pwm0/enable

It's possible to test it connecting the PINS 19 (Ground) and 23 (PWM1) of the 24-Pins secondary header to a simple handheld oscilliscope. It will show a PWM signal of about 2V.

Sowtware PWM[edit]

Create the followinf file:

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main() {
    int fd = open("/sys/class/gpio/gpio9/value", O_WRONLY);
    if (fd == -1) {
        perror("Unable to open /sys/class/gpio/gpio9/value");
        exit(1);
    }
    for (int i = 0; i < 10000; i++) {
        if (write(fd, "1", 1) != 1) {
            perror("Error writing to /sys/class/gpio/gpio9/value");
            exit(1);
        }
        usleep(400);
        if (write(fd, "0", 1) != 1) {
            perror("Error writing to /sys/class/gpio/gpio9/value");
            exit(1);
        }
        usleep(3600);
    }
    close(fd);
}

And compile it

gcc -o pwm9 pwm.c

Launch and test

./pwm9