Skip to content

Commit

Permalink
Support Orange Pi Zero3 PWM
Browse files Browse the repository at this point in the history
  • Loading branch information
orangepi-xunlong committed Oct 11, 2023
1 parent 0c11e15 commit 6b9664b
Show file tree
Hide file tree
Showing 7 changed files with 842 additions and 119 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
*.so
lib*.so.*
*~
*.patch
debian-template/wiringPi
debian-template/wiringpi-*.deb
gpio/gpio
examples/speed
examples/blink
examples/pwm
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.51
2.52
154 changes: 119 additions & 35 deletions examples/pwm.c
Original file line number Diff line number Diff line change
@@ -1,44 +1,128 @@
/*
* pwm.c:
* This tests the hardware PWM channel.
*
* Copyright (c) 2012-2013 Gordon Henderson. <[email protected]>
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#include <wiringPi.h>

#include<wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define PWM_PIN 29

int main (void)
int main(int argc, char *argv [])
{
printf ("OrangePi Pi wiringPi PWM test program\n") ;
int i = 0;
int val = 0;
int pin= 0;

if (argc != 2) {
fprintf (stderr, "Usage: %s <pin>\n", argv [0]) ;
exit (1) ;
}

pin = (unsigned int)strtoul (argv [1], NULL, 10) ;

if (pin != 3 && pin != 4 && pin != 21 && pin != 22) {
fprintf (stderr, "pin 3 for PWM2 / pin 4 for PWM1 / pin 21 for PWM3 / pin 22 for PWM4\n") ;
exit (1) ;
}

// 初始化
printf("wiringPiSetup start\n");

wiringPiSetup();
pinMode(pin,PWM_OUTPUT);

printf("wiringPiSetup end\n");

// 开始测试
while(1)
{
pwmSetRange(pin,1024);
pwmSetClock(pin,1);
pwmWrite(pin,512);

//1. 调节PWM占空比
//1.1 通过设置ARR调节PWM占空比
printf("Modified ARR test start\n");

for (i = 0 ; i <= 8 ; i++) {
pwmSetRange(pin,1024+i*128);
delay(500);
}

delay(5000);

for (i = 7 ; i >= 0 ; i-- ) {
pwmSetRange(pin,1024+i*128);
delay(500);
}

delay(5000);

printf("Modified ARR test end\n");

pwmSetMode(pin,PWM_MODE_BAL);
delay(5000);
pwmSetMode(pin,PWM_MODE_MS);

//1.2 通过设置CRR调节PWM占空比
printf("Modified CRR test start\n");

for (i = 0 ; i <= 8 ; i++) {
pwmWrite(pin,512 + i*64);
delay(500);
}

delay(5000);

for (i = 7 ; i >= 0 ; i-- ) {
pwmWrite(pin,512 + i*64);
delay(500);
}

delay(5000);

printf("Modified active range test end\n");

pwmSetMode(pin,PWM_MODE_BAL);
delay(5000);
pwmSetMode(pin,PWM_MODE_MS);

//2.调节PWM频率
//2.1通过设置分频系数调节PWM频率

printf("Modified frequency division test start\n");

for (i = 1 ; i <= 10; i++) {
pwmSetClock(pin,i);
delay(500);
}

delay(5000);

for (i = 9 ; i >= 1 ; i--) {
pwmSetClock(pin,i);
delay(500);
}

delay(5000);

printf("Modified frequency division test end\n");

pwmSetMode(pin,PWM_MODE_BAL);
delay(5000);
pwmSetMode(pin,PWM_MODE_MS);

//2.2 直接设置PWM频率
printf("Modified PWM frequency test start\n");

for (i = 1 ; i <= 10; i++) {
pwmToneWrite(pin,2000*i);
delay(2000);
}

if (wiringPiSetup () == -1)
exit (1) ;
delay(5000);

pinMode (PWM_PIN, PWM_OUTPUT) ;
pwmWrite (PWM_PIN, 500) ;
printf("Modified PWM frequency test end\n");

return 0 ;
pwmSetMode(pin,PWM_MODE_BAL);
delay(5000);
pwmSetMode(pin,PWM_MODE_MS);
}
return 0;
}
82 changes: 48 additions & 34 deletions gpio/gpio.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ char *usage = "Usage: gpio -v\n"
// " gpio wfi <pin> <mode>\n"
// " gpio drive <group> <value>\n"
" gpio pwm-bal/pwm-ms \n"
// " gpio pwmr <range> \n"
// " gpio pwmc <divider> \n"
// " gpio pwmr <pin> <range> \n"
// " gpio pwmc <pin> <divider> \n"
// " gpio load spi/i2c\n"
// " gpio unload spi/i2c\n"
" gpio i2cd/i2cdetect port\n"
Expand Down Expand Up @@ -1289,7 +1289,7 @@ void doPwm (int argc, char *argv [])
pin = atoi (argv [2]) ;
val = atoi (argv [3]) ;

pinMode (pin, PWM_OUTPUT);
//pinMode (pin, PWM_OUTPUT);
pwmWrite (pin, val) ;
}

Expand All @@ -1300,55 +1300,69 @@ void doPwm (int argc, char *argv [])
*********************************************************************************
*/

static void doPwmMode (int mode)
static void doPwmMode (int argc, char *argv [])
{
pwmSetMode (mode) ;
int pin ;
int mode ;

if (argc != 3) {
fprintf (stderr, "Usage: %s pwm-ms/pwm-bal <pin> \n", argv [0]) ;
exit (1) ;
}

if (strcasecmp (argv [1], "pwm-ms" ) == 0) {
mode = PWM_MODE_MS;
} else {
mode = PWM_MODE_BAL;
}

pin = (unsigned int)strtoul (argv [2], NULL, 10) ;
pwmSetMode (pin,mode) ;
}

static void doPwmRange (int argc, char *argv [])
{
unsigned int range ;
int pin ;
unsigned int range ;

if (argc != 3)
{
fprintf (stderr, "Usage: %s pwmr <range>\n", argv [0]) ;
exit (1) ;
}
if (argc != 4) {
fprintf (stderr, "Usage: %s pwmr <pin> <range>\n", argv [0]) ;
exit (1) ;
}

range = (unsigned int)strtoul (argv [2], NULL, 10) ;
pin = (unsigned int)strtoul (argv [2], NULL, 10) ;
range = (unsigned int)strtoul (argv [3], NULL, 10) ;

if (range == 0)
{
fprintf (stderr, "%s: range must be > 0\n", argv [0]) ;
exit (1) ;
}
if (range == 0) {
fprintf (stderr, "%s: range must be > 0\n", argv [0]) ;
exit (1) ;
}

pwmSetRange (range) ;
pwmSetRange (pin,range) ;
}

static void doPwmClock (int argc, char *argv [])
{
unsigned int clock ;
int pin ;
unsigned int clock ;

if (argc != 3)
{
fprintf (stderr, "Usage: %s pwmc <clock>\n", argv [0]) ;
exit (1) ;
}
if (argc != 4) {
fprintf (stderr, "Usage: %s pwmc <pin> <clock>\n", argv [0]) ;
exit (1) ;
}

clock = (unsigned int)strtoul (argv [2], NULL, 10) ;
pin = (unsigned int)strtoul (argv [2], NULL, 10) ;
clock = (unsigned int)strtoul (argv [3], NULL, 10) ;

if ((clock < 1) || (clock > 4095))
{
fprintf (stderr, "%s: clock must be between 0 and 4096\n", argv [0]) ;
exit (1) ;
}
if ((clock < 1) || (clock > 4095)) {
fprintf (stderr, "%s: clock must be between 0 and 4096\n", argv [0]) ;
exit (1) ;
}

pwmSetClock (clock) ;
pwmSetClock (pin,clock) ;
}



/*
* doVersion:
* Handle the ever more complicated version command and print out
Expand Down Expand Up @@ -1607,8 +1621,8 @@ int main (int argc, char *argv [])

// Pi Specifics

else if (strcasecmp (argv [1], "pwm-bal" ) == 0) doPwmMode (PWM_MODE_BAL) ;
else if (strcasecmp (argv [1], "pwm-ms" ) == 0) doPwmMode (PWM_MODE_MS) ;
else if (strcasecmp (argv [1], "pwm-bal" ) == 0) doPwmMode (argc, argv) ;
else if (strcasecmp (argv [1], "pwm-ms" ) == 0) doPwmMode (argc, argv) ;
else if (strcasecmp (argv [1], "pwmr" ) == 0) doPwmRange (argc, argv) ;
else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ;
else if (strcasecmp (argv [1], "pwmTone" ) == 0) doPwmTone (argc, argv) ;
Expand Down
8 changes: 4 additions & 4 deletions gpio/readall.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,8 @@ static int physToWpi_ZERO_2[64] =
12, 13, //21, 22
14, 15, //23, 24
-1, 16, //25, 26
17, -1, //27, 28
18, -1, //29, 30
17, 21, //27, 28
18, 22, //29, 30
19, -1, //31, 32
20, -1, //33, 34

Expand All @@ -584,8 +584,8 @@ static char * physNames_ZERO_2[64] =
" MISO.1", "PC7 ",
" SCLK.1", "CE.1 ",
" GND", "PC10 ",
" PC1", " ",
" PI16", " ",
" PC1", "PWM3 ",
" PI16", "PWM4 ",
" PI6", " ",
" PH10", " ",
};
Expand Down
Loading

0 comments on commit 6b9664b

Please sign in to comment.