From 6992794f1da0ca025d798db21beabd9c98568d3b Mon Sep 17 00:00:00 2001 From: mstroh76 Date: Fri, 23 Aug 2024 16:30:08 +0200 Subject: [PATCH] #267 new output commands PWM_MODE_BAL & PWM_MODE_MS --- wiringPi/wiringPi.c | 23 +++++++++++++++-------- wiringPi/wiringPi.h | 10 ++++++---- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index f5e8d41..38d7a8f 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -1794,6 +1794,9 @@ void pinMode (int pin, int mode) } else { *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input } + if (PM_OFF==mode && !usingGpioMem && pwm && PI_MODEL_5!=RaspberryPiModel && gpioToPwmALT[pin]>0) { //PWM pin -> reset + pwmWrite(origPin, 0); + } } else if (mode == OUTPUT) { if (PI_MODEL_5 == RaspberryPiModel) { pads[1+pin] = (pin<=8) ? RP1_PAD_DEFAULT_0TO8 : RP1_PAD_DEFAULT_FROM9; @@ -1811,22 +1814,26 @@ void pinMode (int pin, int mode) pinMode (origPin, PWM_OUTPUT) ; // Call myself to enable PWM mode pwmSetMode (PWM_MODE_MS) ; } - else if (mode == PWM_OUTPUT) - { + else if (PWM_OUTPUT==mode || PWM_MS_OUTPUT==mode || PWM_BAL_OUTPUT==mode) { RETURN_ON_MODEL5 if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin return ; - usingGpioMemCheck ("pinMode PWM") ; - -// Set pin to PWM mode + usingGpioMemCheck("pinMode PWM") ; // exit on error! + // Set pin to PWM mode *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ; delayMicroseconds (110) ; // See comments in pwmSetClockWPi - pwmSetMode (PWM_MODE_BAL) ; // Pi default mode - pwmSetRange (1024) ; // Default range of 1024 - pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM + if (PWM_OUTPUT==mode || PWM_BAL_OUTPUT==mode) { + pwmSetMode(PWM_MODE_BAL); // Pi default mode + } else { + pwmSetMode(PWM_MODE_MS); + } + if (PWM_OUTPUT==mode) { // predefine + pwmSetRange (1024) ; // Default range of 1024 + pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM + } } else if (mode == GPIO_CLOCK) { diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 64cbb55..79a895a 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -58,10 +58,12 @@ // Pin modes -#define INPUT 0 -#define OUTPUT 1 -#define PWM_OUTPUT 2 -#define GPIO_CLOCK 3 +#define INPUT 0 +#define OUTPUT 1 +#define PWM_OUTPUT 2 +#define PWM_MS_OUTPUT 8 +#define PWM_BAL_OUTPUT 9 +#define GPIO_CLOCK 3 #define SOFT_PWM_OUTPUT 4 #define SOFT_TONE_OUTPUT 5 #define PWM_TONE_OUTPUT 6