From cd3e2268c68c72eb6c6e93052f49b30f9cb4f2e8 Mon Sep 17 00:00:00 2001 From: KLASSENTS Date: Fri, 1 Aug 2025 13:24:49 +0700 Subject: [PATCH] =?UTF-8?q?=D0=B5=D1=81=D0=BB=D0=B8=20=D0=B2=20=D0=A8?= =?UTF-8?q?=D0=98=D0=9C=20=D0=BD=D1=83=D0=B6=D0=B5=D0=BD=20=D0=BC=D0=B0?= =?UTF-8?q?=D0=BA=D1=81=D0=B8=D0=BC=D0=B0=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20?= =?UTF-8?q?=D1=83=D1=80=D0=BE=D0=B2=D0=B5=D0=BD=D1=8C,=20=D0=B8=D1=81?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D1=83=D0=B5=D1=82=D1=81=D1=8F=20?= =?UTF-8?q?digitalWrite(),=20=D1=87=D1=82=D0=BE=D0=B1=D1=8B=20=D0=BD=D0=B5?= =?UTF-8?q?=20=D0=B1=D1=8B=D0=BB=D0=BE=20=D0=BF=D1=80=D0=BE=D1=81=D0=B0?= =?UTF-8?q?=D0=B4=D0=BE=D0=BA=20=D0=B5=D0=BB=D0=B5=20=D0=B7=D0=B0=D0=BC?= =?UTF-8?q?=D0=B5=D1=82=D0=BD=D1=8B=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/wiring_analog.c | 99 +++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 45 deletions(-) diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c index c497b6b..6379d90 100644 --- a/cores/arduino/wiring_analog.c +++ b/cores/arduino/wiring_analog.c @@ -104,55 +104,64 @@ It is recommended to enable the timer in the following order: */ void analogWrite(uint32_t PinNumber, uint32_t writeVal) { - if (writeVal > WriteValMax) writeVal = WriteValMax; - - if (digitalPinPwmIsOn(PinNumber) > 0) // pin has pwm and pwm is already on + if (writeVal >= WriteValMax) { - // we can only change writeVal if it is differ from current value - TIMER32_TypeDef* timer = pwmPinToTimer(PinNumber); - uint32_t newOCR = (uint32_t) (((uint64_t)pwmTopVal * writeVal) / WriteValMax); - if (timer->CHANNELS[pwmPinToTimerChannel(PinNumber)].OCR != newOCR) + // if we need max value, use digitalWrite to supply constant level + digitalWrite(PinNumber, HIGH); + } + else + { + // if we need less then max, use pwm + int8_t pwmState = digitalPinPwmIsOn(PinNumber); + + if (pwmState > 0) // pin has pwm and pwm is already on { - // if new ocr differs from current, set new ocr - timer->CHANNELS[pwmPinToTimerChannel(PinNumber)].OCR = newOCR; + // we can only change writeVal if it is differ from current value + TIMER32_TypeDef* timer = pwmPinToTimer(PinNumber); + uint32_t newOCR = (uint32_t) (((uint64_t)pwmTopVal * writeVal) / WriteValMax); + if (timer->CHANNELS[pwmPinToTimerChannel(PinNumber)].OCR != newOCR) + { + // if new ocr differs from current, set new ocr + timer->CHANNELS[pwmPinToTimerChannel(PinNumber)].OCR = newOCR; + } } + else if (pwmState == 0) // pin has pwm and pwm is off + { + // init pin as pwm + uint32_t OCRval = (pwmTopVal * writeVal) / WriteValMax; + + // initialization of the required timer + htimer32.Instance = pwmPinToTimer(PinNumber); + htimer32.Top = pwmTopVal; + htimer32.State = TIMER32_STATE_DISABLE; + htimer32.Clock.Source = TIMER32_SOURCE_PRESCALER; + htimer32.Clock.Prescaler = 0; // Prescaler = 1 + htimer32.InterruptMask = 0; + htimer32.CountMode = TIMER32_COUNTMODE_FORWARD; + HAL_Timer32_Init(&htimer32); + + // gpio init as timer channel pin + HAL_GPIO_PinConfig(digitalPinToPort(PinNumber), digitalPinToBitMask(PinNumber), + HAL_GPIO_MODE_TIMER_SERIAL, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA); + + htimer32_channel.TimerInstance = htimer32.Instance; + htimer32_channel.ChannelIndex = pwmPinToTimerChannel(PinNumber); + htimer32_channel.PWM_Invert = TIMER32_CHANNEL_NON_INVERTED_PWM; + htimer32_channel.Mode = TIMER32_CHANNEL_MODE_PWM; + htimer32_channel.CaptureEdge = TIMER32_CHANNEL_CAPTUREEDGE_RISING; + htimer32_channel.OCR = OCRval; + htimer32_channel.Noise = TIMER32_CHANNEL_FILTER_OFF; + HAL_Timer32_Channel_Init(&htimer32_channel); + + // start timer with initialized channel + HAL_Timer32_Channel_Enable(&htimer32_channel); + HAL_Timer32_Value_Clear(&htimer32); + HAL_Timer32_Start(&htimer32); + pwmIsInited++; // increase inited channels qty + } + else // pin doesn't have pwm + ErrorMsgHandler("analogWrite(): invalid pwm pin number"); } - else if (digitalPinPwmIsOn(PinNumber) == 0) // pin has pwm and pwm is off - { - // init pin as pwm - uint32_t OCRval = (uint32_t) (((uint64_t)pwmTopVal * writeVal) / WriteValMax); - - // initialization of the required timer - htimer32.Instance = pwmPinToTimer(PinNumber); - htimer32.Top = pwmTopVal; - htimer32.State = TIMER32_STATE_DISABLE; - htimer32.Clock.Source = TIMER32_SOURCE_PRESCALER; - htimer32.Clock.Prescaler = 0; // Prescaler = 1 - htimer32.InterruptMask = 0; - htimer32.CountMode = TIMER32_COUNTMODE_FORWARD; - HAL_Timer32_Init(&htimer32); - - // gpio init as timer channel pin - HAL_GPIO_PinConfig(digitalPinToPort(PinNumber), digitalPinToBitMask(PinNumber), - HAL_GPIO_MODE_TIMER_SERIAL, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA); - - htimer32_channel.TimerInstance = htimer32.Instance; - htimer32_channel.ChannelIndex = pwmPinToTimerChannel(PinNumber); - htimer32_channel.PWM_Invert = TIMER32_CHANNEL_NON_INVERTED_PWM; - htimer32_channel.Mode = TIMER32_CHANNEL_MODE_PWM; - htimer32_channel.CaptureEdge = TIMER32_CHANNEL_CAPTUREEDGE_RISING; - htimer32_channel.OCR = OCRval; - htimer32_channel.Noise = TIMER32_CHANNEL_FILTER_OFF; - HAL_Timer32_Channel_Init(&htimer32_channel); - - // start timer with initialized channel - HAL_Timer32_Channel_Enable(&htimer32_channel); - HAL_Timer32_Value_Clear(&htimer32); - HAL_Timer32_Start(&htimer32); - pwmIsInited++; // increase inited channels qty - } - else // pin doesn't have pwm - ErrorMsgHandler("analogWrite(): invalid pwm pin number"); } // Set the resolution of analogWrite parameters