Проверка в analogWrite(), добавление экземпляров для Wire, SPI, Serial

В функции analogWrite() добавлена проверка, включен ли уже канал - если включен, применяется новое значение только если оно отличается от текущего.
В модулях Wire, SPI, Serial теперь по два экземпляра в соответствии с периферией МК. Для каждой платы задается свой экземпляр, используемый по умолчанию
Co-authored-by: KLASSENTS <klassen@elron.tech>
Co-committed-by: KLASSENTS <klassen@elron.tech>
This commit was merged in pull request #21.
This commit is contained in:
klassents authored and klassents committed 2025-05-27 12:35:36 +03:00
1 parent f1e091bf77
commit 778608b576
29 files changed
+186 -130

No files matched your search

+5 -2
View File
@@ -8,10 +8,13 @@
#include "wiring_LL.h"
// HardwareSerial class objects for use in Arduino IDE
HardwareSerial Serial(0);
HardwareSerial Serial0(0);
#if SERIAL_PORT_QTY > 1
HardwareSerial Serial1(1);
#endif
// default interface
HardwareSerial& Serial = DEFAULT_SERIAL;
void serialEvent() __attribute__((weak));
bool Serial0_available() __attribute__((weak));
@@ -144,7 +147,7 @@ extern "C" void __attribute__((optimize("O3"))) serial_interrupt_handler(uint8_t
{
if (uartNumInt == 0)
{
Serial.rx_complete_irq();
Serial0.rx_complete_irq();
}
else if ((uartNumInt == 1) && (SERIAL_PORT_QTY > 1))
{
+3 -1
View File
@@ -103,10 +103,12 @@ class HardwareSerial : public Stream
inline void rx_complete_irq(void) __attribute__((always_inline, optimize("O3")));
};
extern HardwareSerial Serial;
extern HardwareSerial Serial0;
#if SERIAL_PORT_QTY > 1
extern HardwareSerial Serial1;
#endif
// default interface
extern HardwareSerial& Serial;
extern void serialEventRun(void) __attribute__((weak));
+23 -6
View File
@@ -104,9 +104,23 @@ It is recommended to enable the timer in the following order:
*/
void analogWrite(uint32_t PinNumber, uint32_t writeVal)
{
if (digitalPinHasPWM(PinNumber))
if (writeVal > WriteValMax) writeVal = WriteValMax;
if (digitalPinPwmIsOn(PinNumber) > 0) // pin has pwm and pwm is already on
{
if (writeVal > WriteValMax) 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 new ocr differs from current, set new ocr
timer->CHANNELS[pwmPinToTimerChannel(PinNumber)].OCR = newOCR;
}
}
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);
@@ -127,7 +141,7 @@ void analogWrite(uint32_t PinNumber, uint32_t writeVal)
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 = (uint32_t) (((uint64_t)pwmTopVal * writeVal) / WriteValMax);
htimer32_channel.OCR = OCRval;
htimer32_channel.Noise = TIMER32_CHANNEL_FILTER_OFF;
HAL_Timer32_Channel_Init(&htimer32_channel);
@@ -137,7 +151,7 @@ void analogWrite(uint32_t PinNumber, uint32_t writeVal)
HAL_Timer32_Start(&htimer32);
pwmIsInited++; // increase inited channels qty
}
else
else // pin doesn't have pwm
ErrorMsgHandler("analogWrite(): invalid pwm pin number");
}
@@ -168,14 +182,17 @@ It is recommended to turn off the timer in the following order:
- Write 0 to the INT_CLEAR register;
- Set TIM_EN to 0.
*/
// use only if digitalPinPwmIsOn(PinNumber) > 0
void analogWriteStop(uint32_t PinNumber)
{
if (digitalPinHasPWM(PinNumber) && (pwmIsInited > 0) && digitalPinPwmIsOn(PinNumber))
if ((pwmIsInited > 0))
{
// load the timer address and channel number corresponding to the specified pin
htimer32.Instance = pwmPinToTimer(PinNumber);
htimer32_channel.TimerInstance = htimer32.Instance;
htimer32_channel.ChannelIndex = pwmPinToTimerChannel(PinNumber);
htimer32.Instance->CHANNELS[htimer32_channel.ChannelIndex].OCR = 0;
htimer32_channel.TimerInstance = htimer32.Instance;
// deinit channel
HAL_Timer32_Channel_DeInit(&htimer32_channel);
pwmIsInited--; // decrease inited channels qty
+5 -5
View File
@@ -37,8 +37,8 @@ void pinMode(uint32_t PinNumber, uint32_t PinMode)
return;
}
if (digitalPinHasPWM(PinNumber))
// if the pin can use PWM, disable PWM
if (digitalPinPwmIsOn(PinNumber))
// if the pin use PWM, disable PWM
analogWriteStop(PinNumber);
// determine the port and the pin number in the port
@@ -104,7 +104,7 @@ __attribute__((noinline, section(".ram_text"))) void digitalWrite(uint32_t PinNu
}
if (digitalPinPwmIsOn(PinNumber))
// if the pin can use PWM, disable PWM
// if the pin use PWM, disable PWM
analogWriteStop(PinNumber);
if (Val == HIGH)
@@ -123,7 +123,7 @@ __attribute__((noinline, section(".ram_text"))) int digitalRead(uint32_t PinNumb
}
if (digitalPinPwmIsOn(PinNumber))
// if the pin can use PWM, disable PWM
// if the pin use PWM, disable PWM
analogWriteStop(PinNumber);
return GPIO_READ_PIN(digitalPinToPort(PinNumber), digitalPinToBitMask(PinNumber));
@@ -139,7 +139,7 @@ __attribute__((noinline, section(".ram_text"))) void digitalToggle(uint32_t PinN
}
if (digitalPinPwmIsOn(PinNumber))
// if the pin can use PWM, disable PWM
// if the pin use PWM, disable PWM
analogWriteStop(PinNumber);
GPIO_TOGGLE_PIN(digitalPinToPort(PinNumber), digitalPinToBitMask(PinNumber));
+1 -1
View File
@@ -13,7 +13,7 @@ unsigned long pulseIn(int pin, int state, unsigned long timeout)
ErrorMsgHandler("pulseIn(): pin number exceeds the total number of pins");
return -1;
}
if (digitalPinHasPWM(pin))
if (digitalPinPwmIsOn(pin) > 0)
// pwm off
analogWriteStop(pin);