Skip to content

Commit

Permalink
Support Orange Pi 5/5b/5plus PWM
Browse files Browse the repository at this point in the history
  • Loading branch information
liu1313277 authored and orangepi-xunlong committed Oct 27, 2023
1 parent 63a2f4c commit 4af4456
Show file tree
Hide file tree
Showing 3 changed files with 626 additions and 81 deletions.
113 changes: 81 additions & 32 deletions examples/pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,104 @@
#include <stdlib.h>
#include <stdint.h>

extern int OrangePiModel;
extern void sunxi_pwm_set_enable(int en);

typedef struct {
unsigned int ccr;
unsigned int arr;
unsigned int div;
unsigned int ccr;
unsigned int div_stepping;
} pwm_info;

static pwm_info pwm_info_t;

static void set_pwm_info(int OrangePiModel)
static void set_pwm_info(int pin)
{
switch (OrangePiModel)
int model;

piBoardId (&model);

switch (model)
{
case PI_MODEL_ZERO_2:
case PI_MODEL_ZERO_2_W:

if (pin != 3 && pin != 4 && pin != 21 && pin != 22) {
fprintf (stderr, "the pin you choose doesn't support hardware PWM\n") ;
exit (1) ;
}

pwm_info_t.ccr = 512;
pwm_info_t.arr = 1024;
pwm_info_t.div = 1;
pwm_info_t.div_stepping = 1;
break;

case PI_MODEL_ZERO_2_W:

if (pin != 2 && pin != 9 && pin != 21 && pin != 22) {
fprintf (stderr, "the pin you choose doesn't support hardware PWM\n") ;
exit (1) ;
}

pwm_info_t.ccr = 512;
pwm_info_t.arr = 1024;
pwm_info_t.div = 1;
pwm_info_t.div_stepping = 1;
break;

case PI_MODEL_3_PLUS:

if (pin != 2 && pin != 16) {
fprintf (stderr, "the pin you choose doesn't support hardware PWM\n") ;
exit (1) ;
}

pwm_info_t.ccr = 500;
pwm_info_t.arr = 1000;
pwm_info_t.div = 120;
pwm_info_t.div_stepping = 1;
break;

case PI_MODEL_5:

if (pin != 0 && pin != 2 && pin != 5 && pin != 8 && pin != 9 && pin != 10 && pin != 14 && pin != 16) {
fprintf (stderr, "the pin you choose doesn't support hardware PWM\n") ;
exit (1) ;
}

pwm_info_t.ccr = 500;
pwm_info_t.arr = 1000;
pwm_info_t.div = 120;
pwm_info_t.div_stepping = 2;
break;
default:

case PI_MODEL_5B:

if (pin != 0 && pin != 2 && pin != 5 && pin != 8 && pin != 9 && pin != 10 && pin != 13 && pin != 15) {
fprintf (stderr, "the pin you choose doesn't support hardware PWM\n") ;
exit (1) ;
}

pwm_info_t.ccr = 500;
pwm_info_t.arr = 1000;
pwm_info_t.div = 120;
pwm_info_t.div_stepping = 2;
break;

case PI_MODEL_5_PLUS:

if (pin != 0 && pin != 1 && pin != 2 && pin != 6 && pin != 9 && pin != 10 && pin != 13 && pin != 21 && pin != 22) {
fprintf (stderr, "the pin you choose doesn't support hardware PWM\n") ;
exit (1) ;
}

pwm_info_t.ccr = 500;
pwm_info_t.arr = 1000;
pwm_info_t.div = 120;
pwm_info_t.div_stepping = 2;
break;

default:
printf("Oops - unable to determine board type...");
exit(1);
}
}

Expand All @@ -47,17 +117,12 @@ int main(int argc, char *argv [])

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

if (pin != 3 && pin != 4 && pin != 21 && pin != 22 && pin != 2 && pin != 9 && pin != 16) {
fprintf (stderr, "the pin you choose doesn't support hardware PWM\n") ;
exit (1) ;
}

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

wiringPiSetup();
set_pwm_info(pin);
pinMode(pin,PWM_OUTPUT);
set_pwm_info(OrangePiModel);

printf("wiringPiSetup end\n");

Expand Down Expand Up @@ -87,10 +152,6 @@ int main(int argc, char *argv [])

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

sunxi_pwm_set_enable(0);
delay(5000);
sunxi_pwm_set_enable(1);

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

Expand All @@ -110,23 +171,19 @@ int main(int argc, char *argv [])

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

sunxi_pwm_set_enable(0);
delay(5000);
sunxi_pwm_set_enable(1);

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

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

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

delay(5000);

for (i = 9 ; i >= 1 ; i--) {
for (i = 9 * pwm_info_t.div_stepping; i >= pwm_info_t.div_stepping ; i -= pwm_info_t.div_stepping) {
pwmSetClock(pin,i);
delay(500);
}
Expand All @@ -135,10 +192,6 @@ int main(int argc, char *argv [])

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

sunxi_pwm_set_enable(0);
delay(5000);
sunxi_pwm_set_enable(1);

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

Expand All @@ -150,9 +203,5 @@ int main(int argc, char *argv [])
delay(5000);

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

sunxi_pwm_set_enable(0);
delay(5000);
sunxi_pwm_set_enable(1);
}
}
Loading

0 comments on commit 4af4456

Please sign in to comment.